Skip to content

Commit fe2bba9

Browse files
stability improvements
1 parent a0dfaf6 commit fe2bba9

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

packages/common/src/client/ConnectionManager.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
101101
// If we do already have pending options, a disconnect has already been performed.
102102
// The connectInternal method also does a sanity disconnect to prevent straggler connections.
103103
// We should also disconnect if we have already completed a connection attempt.
104-
if (!hadPendingOptions || !this.connectingPromise) {
104+
if (!hadPendingOptions) {
105105
await this.disconnectInternal();
106106
}
107107

@@ -139,8 +139,6 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
139139
*/
140140
this.syncStreamInitPromise = new Promise(async (resolve, reject) => {
141141
try {
142-
await this.disconnectingPromise;
143-
144142
if (!this.pendingConnectionOptions) {
145143
this.logger.debug('No pending connection options found, not creating sync stream implementation');
146144
// A disconnect could have cleared this.
@@ -175,9 +173,9 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
175173
// and this point. Awaiting here allows the sync stream to be cleared if disconnected.
176174
await this.disconnectingPromise;
177175

178-
this.syncStreamImplementation?.triggerCrudUpload();
179176
this.logger.debug('Attempting to connect to PowerSync instance');
180177
await this.syncStreamImplementation?.connect(appliedOptions!);
178+
this.syncStreamImplementation?.triggerCrudUpload();
181179
}
182180

183181
/**
@@ -208,10 +206,16 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
208206
}
209207

210208
protected async performDisconnect() {
211-
await this.syncStreamImplementation?.disconnect();
212-
await this.syncStreamImplementation?.dispose();
213-
await this.syncDisposer?.();
214-
this.syncDisposer = null;
209+
// Keep reference to the sync stream implementation and disposer
210+
// The class members will be cleared before we trigger the disconnect
211+
// to prevent any further calls to the sync stream implementation.
212+
const sync = this.syncStreamImplementation;
215213
this.syncStreamImplementation = null;
214+
const disposer = this.syncDisposer;
215+
this.syncDisposer = null;
216+
217+
await sync?.disconnect();
218+
await sync?.dispose();
219+
await disposer?.();
216220
}
217221
}

0 commit comments

Comments
 (0)