From 1f87766467b4be974f4b3a49b04e895c199cf450 Mon Sep 17 00:00:00 2001 From: stevensJourney Date: Tue, 3 Dec 2024 08:41:45 +0200 Subject: [PATCH] cleanup --- .../components/providers/SystemProvider.tsx | 6 ++--- .../db/adapters/LockedAsyncDatabaseAdapter.ts | 22 +++++++++---------- .../adapters/wa-sqlite/WASQLiteConnection.ts | 2 +- .../adapters/wa-sqlite/WASQLiteOpenFactory.ts | 8 ++----- .../SharedWebStreamingSyncImplementation.ts | 6 ++--- 5 files changed, 19 insertions(+), 25 deletions(-) diff --git a/demos/react-supabase-todolist/src/components/providers/SystemProvider.tsx b/demos/react-supabase-todolist/src/components/providers/SystemProvider.tsx index 85b244fb..a8648bfc 100644 --- a/demos/react-supabase-todolist/src/components/providers/SystemProvider.tsx +++ b/demos/react-supabase-todolist/src/components/providers/SystemProvider.tsx @@ -17,9 +17,9 @@ export const db = new PowerSyncDatabase({ // dbFilename: 's.sqlite' // } database: new WASQLiteOpenFactory({ - dbFilename: 'examplswse.db', - vfs: WASQLiteVFS.OPFSCoopSyncVFS - // vfs: WASQLiteVFS.OPFSCoopSyncVFS //Out of memory errors on iOS Safari + dbFilename: 'examplsw1se11.db', + // vfs: WASQLiteVFS.OPFSCoopSyncVFS + vfs: WASQLiteVFS.OPFSCoopSyncVFS //Out of memory errors on iOS Safari }) }); diff --git a/packages/web/src/db/adapters/LockedAsyncDatabaseAdapter.ts b/packages/web/src/db/adapters/LockedAsyncDatabaseAdapter.ts index c37dbe13..89880329 100644 --- a/packages/web/src/db/adapters/LockedAsyncDatabaseAdapter.ts +++ b/packages/web/src/db/adapters/LockedAsyncDatabaseAdapter.ts @@ -37,6 +37,7 @@ export class LockedAsyncDatabaseAdapter extends BaseObserver; private _db: AsyncDatabaseConnection | null = null; protected _disposeTableChangeListener: (() => void) | null = null; @@ -64,6 +65,7 @@ export class LockedAsyncDatabaseAdapter extends BaseObserver this.acquireLock(() => this._execute(query, params)) }); + this.initPromise = this._init(); } protected get baseDB() { @@ -87,7 +89,14 @@ export class LockedAsyncDatabaseAdapter extends BaseObserver((resolve) => { - const l = this.registerListener({ - initialized: () => { - resolve(); - l(); - } - }); - }); + // Awaiting this will expose errors on function calls like .execute etc + await this.initPromise; } /** diff --git a/packages/web/src/db/adapters/wa-sqlite/WASQLiteConnection.ts b/packages/web/src/db/adapters/wa-sqlite/WASQLiteConnection.ts index 1ca8e260..497ba7a6 100644 --- a/packages/web/src/db/adapters/wa-sqlite/WASQLiteConnection.ts +++ b/packages/web/src/db/adapters/wa-sqlite/WASQLiteConnection.ts @@ -123,7 +123,7 @@ export class WASqliteConnection extends BaseObserver this.updatedTables = new Set(); this.updateTimer = null; this.broadcastChannel = null; - this.connectionId = new Date().valueOf() + Math.random() * 1000; + this.connectionId = new Date().valueOf() + Math.random(); this.statementMutex = new Mutex(); this._moduleFactory = DEFAULT_MODULE_FACTORIES[this.options.vfs ?? WASQLiteVFS.IDBBatchAtomicVFS]; } diff --git a/packages/web/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts b/packages/web/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts index a5306bd7..858ae47e 100644 --- a/packages/web/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts +++ b/packages/web/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts @@ -48,7 +48,7 @@ export class WASQLiteOpenFactory extends AbstractWebSQLOpenFactory { const workerDBOpener = Comlink.wrap>(messagePort); - const workerAdapter = new WorkerLockedAsyncDatabaseAdapter({ + adapter = new WorkerLockedAsyncDatabaseAdapter({ messagePort, openConnection: () => workerDBOpener({ @@ -60,11 +60,9 @@ export class WASQLiteOpenFactory extends AbstractWebSQLOpenFactory { debugMode: this.options.debugMode, logger: this.logger }); - workerAdapter.init(); - adapter = workerAdapter; } else { // Don't use a web worker - const contextAdapter = new LockedAsyncDatabaseAdapter({ + adapter = new LockedAsyncDatabaseAdapter({ openConnection: async () => new WASqliteConnection({ dbFilename: this.options.dbFilename, @@ -77,8 +75,6 @@ export class WASQLiteOpenFactory extends AbstractWebSQLOpenFactory { debugMode: this.options.debugMode, logger: this.logger }); - contextAdapter.init(); - adapter = contextAdapter; } return adapter; diff --git a/packages/web/src/db/sync/SharedWebStreamingSyncImplementation.ts b/packages/web/src/db/sync/SharedWebStreamingSyncImplementation.ts index f480bded..bda4b32c 100644 --- a/packages/web/src/db/sync/SharedWebStreamingSyncImplementation.ts +++ b/packages/web/src/db/sync/SharedWebStreamingSyncImplementation.ts @@ -27,10 +27,8 @@ class SharedSyncClientProvider extends AbstractSharedSyncClientProvider { } async getDBWorkerPort(): Promise { - // FIXME type error - const port = (await this.dbWorkerPort) as MessagePort; - - // TODO this can only be done once. Throw an error if multiple attempts are made + // This is provided asynchronously for an easier initialization + const port = await this.dbWorkerPort; return Comlink.transfer(port, [port]); }