File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -204,6 +204,8 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
204204 */
205205 protected pendingConnectionOptions : StoredConnectionOptions | null ;
206206
207+ protected connectionMutex : Mutex ;
208+
207209 constructor ( options : PowerSyncDatabaseOptionsWithDBAdapter ) ;
208210 constructor ( options : PowerSyncDatabaseOptionsWithOpenFactory ) ;
209211 constructor ( options : PowerSyncDatabaseOptionsWithSettings ) ;
@@ -237,6 +239,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
237239 this . connectingPromise = null ;
238240 this . syncStreamInitPromise = null ;
239241 this . pendingConnectionOptions = null ;
242+ this . connectionMutex = new Mutex ( ) ;
240243 // Start async init
241244 this . _isReadyPromise = this . initialize ( ) ;
242245 }
@@ -460,7 +463,9 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
460463 * Locking mechanism for exclusively running critical portions of connect/disconnect operations.
461464 * Locking here is mostly only important on web for multiple tab scenarios.
462465 */
463- protected abstract runExclusive < T > ( callback : ( ) => Promise < T > ) : Promise < T > ;
466+ protected runExclusive < T > ( callback : ( ) => Promise < T > ) : Promise < T > {
467+ return this . connectionMutex . runExclusive ( callback ) ;
468+ }
464469
465470 protected async connectInternal ( ) {
466471 let appliedOptions : PowerSyncConnectionOptions | null = null ;
Original file line number Diff line number Diff line change @@ -16,7 +16,6 @@ import {
1616import { NodeRemote } from '../sync/stream/NodeRemote.js' ;
1717import { NodeStreamingSyncImplementation } from '../sync/stream/NodeStreamingSyncImplementation.js' ;
1818
19- import Lock from 'async-lock' ;
2019import { Dispatcher } from 'undici' ;
2120import { BetterSQLite3DBAdapter } from './BetterSQLite3DBAdapter.js' ;
2221import { NodeSQLOpenOptions } from './options.js' ;
@@ -56,11 +55,8 @@ export type NodePowerSyncConnectionOptions = PowerSyncConnectionOptions & NodeAd
5655 * ```
5756 */
5857export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
59- protected lock : Lock ;
60-
6158 constructor ( options : NodePowerSyncDatabaseOptions ) {
6259 super ( options ) ;
63- this . lock = new Lock ( ) ;
6460 }
6561
6662 async _initialize ( ) : Promise < void > {
@@ -85,10 +81,6 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
8581 return super . connect ( connector , options ) ;
8682 }
8783
88- protected async runExclusive < T > ( callback : ( ) => Promise < T > ) : Promise < T > {
89- return await this . lock . acquire ( `lock-${ this . database . name } ` , callback ) ;
90- }
91-
9284 protected generateSyncStreamImplementation (
9385 connector : PowerSyncBackendConnector ,
9486 options : NodeAdditionalConnectionOptions
Original file line number Diff line number Diff line change 88 type RequiredAdditionalConnectionOptions ,
99 SqliteBucketStorage
1010} from '@powersync/common' ;
11- import Lock from 'async-lock' ;
1211import { ReactNativeRemote } from '../sync/stream/ReactNativeRemote' ;
1312import { ReactNativeStreamingSyncImplementation } from '../sync/stream/ReactNativeStreamingSyncImplementation' ;
1413import { ReactNativeQuickSqliteOpenFactory } from './adapters/react-native-quick-sqlite/ReactNativeQuickSQLiteOpenFactory' ;
@@ -28,8 +27,6 @@ import { ReactNativeQuickSqliteOpenFactory } from './adapters/react-native-quick
2827 * ```
2928 */
3029export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
31- protected lock = new Lock ( ) ;
32-
3330 async _initialize ( ) : Promise < void > { }
3431
3532 /**
@@ -45,10 +42,6 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
4542 return new SqliteBucketStorage ( this . database , AbstractPowerSyncDatabase . transactionMutex ) ;
4643 }
4744
48- protected async runExclusive < T > ( callback : ( ) => Promise < T > ) : Promise < T > {
49- return await this . lock . acquire ( `lock-${ this . database . name } ` , callback ) ;
50- }
51-
5245 protected generateSyncStreamImplementation (
5346 connector : PowerSyncBackendConnector ,
5447 options : RequiredAdditionalConnectionOptions
You can’t perform that action at this time.
0 commit comments