Skip to content

Commit 3679fda

Browse files
committed
Scheduling writes to a ControlledExecutor.
1 parent cedd8a3 commit 3679fda

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

.changeset/rude-pears-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/common': minor
3+
---
4+
5+
Added ControlledExecutor utility to exports.

packages/common/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export * from './db/schema/TableV2.js';
3333

3434
export * from './utils/AbortOperation.js';
3535
export * from './utils/BaseObserver.js';
36+
export * from './utils/ControlledExecutor.js';
3637
export * from './utils/DataStream.js';
3738
export * from './utils/Logger.js';
3839
export * from './utils/parseQuery.js';

packages/dev-adapter/src/SQLJSAdapter.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
BaseListener,
33
BaseObserver,
44
BatchedUpdateNotification,
5+
ControlledExecutor,
56
DBAdapter,
67
DBAdapterListener,
78
DBLockOptions,
@@ -13,7 +14,7 @@ import {
1314
} from '@powersync/common';
1415
import { Mutex } from 'async-mutex';
1516
// This uses a pure JS version which avoids the need for WebAssembly, which is not supported in React Native.
16-
import SQLJs from 'sql.js/dist/sql-asm-debug.js';
17+
import SQLJs from 'sql.js/dist/sql-asm.js';
1718

1819
export interface SQLJSPersister {
1920
readFile: () => Promise<ArrayLike<number> | Buffer | null>;
@@ -51,6 +52,7 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
5152
protected _db: SQLJs.Database | null;
5253
protected tableUpdateCache: Set<string>;
5354
protected dbP: number | null;
55+
protected writeScheduler: ControlledExecutor<SQLJs.Database>;
5456

5557
static sharedObserver = new TableObserver();
5658
protected disposeListener: () => void;
@@ -81,6 +83,10 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
8183
this.tableUpdateCache.add(table);
8284
}
8385
});
86+
87+
this.writeScheduler = new ControlledExecutor(async (db: SQLJs.Database) => {
88+
await this.options.persister.writeFile(db.export());
89+
});
8490
}
8591

8692
protected async init(): Promise<SQLJs.Database> {
@@ -219,7 +225,7 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
219225
const db = await this.getDB();
220226
const result = await fn(this.generateLockContext());
221227

222-
await this.options.persister.writeFile(db.export());
228+
this.writeScheduler.schedule(db);
223229

224230
const notification: BatchedUpdateNotification = {
225231
rawUpdates: [],

0 commit comments

Comments
 (0)