@@ -8,10 +8,11 @@ import {
88 DBAdapterListener ,
99 DBLockOptions ,
1010 LockContext ,
11+ mutexRunExclusive ,
1112 QueryResult ,
1213 Transaction
1314} from '@powersync/web' ;
14- import Lock from 'async-lock ' ;
15+ import { Mutex } from 'async-mutex ' ;
1516import { PowerSyncCore } from '../plugin/PowerSyncCore.js' ;
1617import { messageForErrorCode } from '../plugin/PowerSyncPlugin.js' ;
1718import { CapacitorSQLiteOpenFactoryOptions , DEFAULT_SQLITE_OPTIONS } from './CapacitorSQLiteOpenFactory.js' ;
@@ -39,13 +40,15 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
3940 protected _writeConnection : SQLiteDBConnection | null ;
4041 protected _readConnection : SQLiteDBConnection | null ;
4142 protected initializedPromise : Promise < void > ;
42- protected lock : Lock ;
43+ protected writeMutex : Mutex ;
44+ protected readMutex : Mutex ;
4345
4446 constructor ( protected options : CapacitorSQLiteOpenFactoryOptions ) {
4547 super ( ) ;
4648 this . _writeConnection = null ;
4749 this . _readConnection = null ;
48- this . lock = new Lock ( ) ;
50+ this . writeMutex = new Mutex ( ) ;
51+ this . readMutex = new Mutex ( ) ;
4952 this . initializedPromise = this . init ( ) ;
5053 }
5154
@@ -237,10 +240,14 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
237240 }
238241
239242 readLock < T > ( fn : ( tx : LockContext ) => Promise < T > , options ?: DBLockOptions ) : Promise < T > {
240- return this . lock . acquire ( 'read_lock' , async ( ) => {
241- await this . initializedPromise ;
242- return await fn ( this . generateLockContext ( this . readConnection ) ) ;
243- } ) ;
243+ return mutexRunExclusive (
244+ this . readMutex ,
245+ async ( ) => {
246+ await this . initializedPromise ;
247+ return await fn ( this . generateLockContext ( this . readConnection ) ) ;
248+ } ,
249+ options
250+ ) ;
244251 }
245252
246253 readTransaction < T > ( fn : ( tx : Transaction ) => Promise < T > , options ?: DBLockOptions ) : Promise < T > {
@@ -250,24 +257,28 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
250257 }
251258
252259 writeLock < T > ( fn : ( tx : LockContext ) => Promise < T > , options ?: DBLockOptions ) : Promise < T > {
253- return this . lock . acquire ( 'write_lock' , async ( ) => {
254- await this . initializedPromise ;
255- const result = await fn ( this . generateLockContext ( this . writeConnection ) ) ;
256-
257- // Fetch table updates
258- const updates = await this . writeConnection . query ( "SELECT powersync_update_hooks('get') AS table_name" ) ;
259- const jsonUpdates = updates . values ?. [ 0 ] ;
260- if ( ! jsonUpdates || ! jsonUpdates . table_name ) {
261- throw new Error ( 'Could not fetch table updates' ) ;
262- }
263- const notification : BatchedUpdateNotification = {
264- rawUpdates : [ ] ,
265- tables : JSON . parse ( jsonUpdates . table_name ) ,
266- groupedUpdates : { }
267- } ;
268- this . iterateListeners ( ( l ) => l . tablesUpdated ?.( notification ) ) ;
269- return result ;
270- } ) ;
260+ return mutexRunExclusive (
261+ this . writeMutex ,
262+ async ( ) => {
263+ await this . initializedPromise ;
264+ const result = await fn ( this . generateLockContext ( this . writeConnection ) ) ;
265+
266+ // Fetch table updates
267+ const updates = await this . writeConnection . query ( "SELECT powersync_update_hooks('get') AS table_name" ) ;
268+ const jsonUpdates = updates . values ?. [ 0 ] ;
269+ if ( ! jsonUpdates || ! jsonUpdates . table_name ) {
270+ throw new Error ( 'Could not fetch table updates' ) ;
271+ }
272+ const notification : BatchedUpdateNotification = {
273+ rawUpdates : [ ] ,
274+ tables : JSON . parse ( jsonUpdates . table_name ) ,
275+ groupedUpdates : { }
276+ } ;
277+ this . iterateListeners ( ( l ) => l . tablesUpdated ?.( notification ) ) ;
278+ return result ;
279+ } ,
280+ options
281+ ) ;
271282 }
272283
273284 writeTransaction < T > ( fn : ( tx : Transaction ) => Promise < T > , options ?: DBLockOptions ) : Promise < T > {
0 commit comments