@@ -6,16 +6,16 @@ import {
66 SyncStatusOptions
77} from '@powersync/common' ;
88import * as Comlink from 'comlink' ;
9+ import { getNavigatorLocks } from '../../shared/navigator' ;
910import { AbstractSharedSyncClientProvider } from '../../worker/sync/AbstractSharedSyncClientProvider' ;
1011import { ManualSharedSyncPayload , SharedSyncClientEvent } from '../../worker/sync/SharedSyncImplementation' ;
11- import { DEFAULT_CACHE_SIZE_KB , resolveWebSQLFlags , TemporaryStorageOption } from '../adapters/web-sql-flags ' ;
12+ import { WorkerClient } from '../../worker/sync/WorkerClient ' ;
1213import { WebDBAdapter } from '../adapters/WebDBAdapter' ;
14+ import { DEFAULT_CACHE_SIZE_KB , TemporaryStorageOption , resolveWebSQLFlags } from '../adapters/web-sql-flags' ;
1315import {
1416 WebStreamingSyncImplementation ,
1517 WebStreamingSyncImplementationOptions
1618} from './WebStreamingSyncImplementation' ;
17- import { WorkerClient } from '../../worker/sync/WorkerClient' ;
18- import { getNavigatorLocks } from '../../shared/navigator' ;
1919
2020/**
2121 * The shared worker will trigger methods on this side of the message port
@@ -160,6 +160,21 @@ export class SharedWebStreamingSyncImplementation extends WebStreamingSyncImplem
160160 const { crudUploadThrottleMs, identifier, retryDelayMs } = this . options ;
161161 const flags = { ...this . webOptions . flags , workers : undefined } ;
162162
163+ // Request a random lock until this client is disposed. The name of the lock is sent to the shared worker, which
164+ // will also attempt to acquire it. Since the lock is returned when the tab is closed, this allows the share worker
165+ // to free resources associated with this tab.
166+ // We take hold of this lock as soon-as-possible in order to cater for potentially closed tabs.
167+ getNavigatorLocks ( ) . request ( `tab-close-signal-${ crypto . randomUUID ( ) } ` , async ( lock ) => {
168+ if ( ! this . abortOnClose . signal . aborted ) {
169+ // Awaiting here ensures the worker is waiting for the lock
170+ await this . syncManager . addLockBasedCloseSignal ( lock ! . name ) ;
171+
172+ await new Promise < void > ( ( r ) => {
173+ this . abortOnClose . signal . onabort = ( ) => r ( ) ;
174+ } ) ;
175+ }
176+ } ) ;
177+
163178 this . isInitialized = this . syncManager . setParams (
164179 {
165180 dbParams : this . dbAdapter . getConfiguration ( ) ,
@@ -190,19 +205,6 @@ export class SharedWebStreamingSyncImplementation extends WebStreamingSyncImplem
190205 * This performs bi-directional method calling.
191206 */
192207 Comlink . expose ( this . clientProvider , this . messagePort ) ;
193-
194- // Request a random lock until this client is disposed. The name of the lock is sent to the shared worker, which
195- // will also attempt to acquire it. Since the lock is returned when the tab is closed, this allows the share worker
196- // to free resources associated with this tab.
197- getNavigatorLocks ( ) . request ( `tab-close-signal-${ crypto . randomUUID ( ) } ` , async ( lock ) => {
198- if ( ! this . abortOnClose . signal . aborted ) {
199- this . syncManager . addLockBasedCloseSignal ( lock ! . name ) ;
200-
201- await new Promise < void > ( ( r ) => {
202- this . abortOnClose . signal . onabort = ( ) => r ( ) ;
203- } ) ;
204- }
205- } ) ;
206208 }
207209
208210 /**
0 commit comments