Skip to content

Commit c08e664

Browse files
cleanup code flow
1 parent de7804f commit c08e664

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

packages/web/src/worker/sync/SharedSyncImplementation.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -341,34 +341,38 @@ export class SharedSyncImplementation extends BaseObserver<SharedSyncImplementat
341341
}
342342
});
343343

344-
const shouldReconnect = !!this.connectionManager.syncStreamImplementation && this.ports.length > 0;
345-
/**
346-
* If the current database adapter is the one that is being closed, we need to disconnect from the backend.
347-
* We can disconnect in the portMutex lock. This ensures the disconnect is not affected by potential other
348-
* connect operations coming from other tabs.
349-
*/
350-
if (this.dbAdapter && this.dbAdapter == trackedPort.db) {
351-
this.logger.debug(`Disconnecting due to closed database: should reconnect: ${shouldReconnect}`);
352-
this.dbAdapter = null;
353-
// Unconditionally close the connection because the database it's writing to has just been closed.
354-
// The connection has been closed previously, this might throw. We should be able to ignore it.
355-
await this.connectionManager
356-
.disconnect()
357-
.catch((ex) => this.logger.warn('Error while disconnecting. Will attempt to reconnect.', ex));
358-
}
344+
const shouldReconnect =
345+
!!this.connectionManager.syncStreamImplementation &&
346+
this.ports.length > 0 &&
347+
this.dbAdapter &&
348+
this.dbAdapter == trackedPort.db;
359349

350+
// Close the worker wrapped database connection, we can't accurately rely on this connection
360351
for (const closeListener of trackedPort.closeListeners) {
361352
await closeListener();
362353
}
363354

355+
// Close the LockedAsyncDatabaseAdapter for cleanup
364356
try {
365357
await trackedPort.db?.close();
366358
} catch (ex) {
367359
this.logger.warn('error closing database', ex);
368360
}
369361

370-
// Re-index subscriptions, the subscriptions of the removed port would no longer be considered.
362+
/**
363+
* If the current database adapter is the one that is being closed, we need to disconnect from the backend.
364+
* We can disconnect in the portMutex lock. This ensures the disconnect is not affected by potential other
365+
* connect operations coming from other tabs.
366+
* Re-index subscriptions, the subscriptions of the removed port would no longer be considered.
367+
*/
371368
if (shouldReconnect) {
369+
this.logger.debug(`Disconnecting due to closed database: should reconnect: ${shouldReconnect}`);
370+
this.dbAdapter = null;
371+
// Unconditionally close the connection because the database it's writing to has just been closed.
372+
// The connection has been closed previously, this might throw. We should be able to ignore it.
373+
await this.connectionManager
374+
.disconnect()
375+
.catch((ex) => this.logger.warn('Error while disconnecting. Will attempt to reconnect.', ex));
372376
/**
373377
* The internals of this needs a port mutex lock.
374378
* It should be safe to start this operation here, but we cannot and don't need to await it.

0 commit comments

Comments
 (0)