Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney committed Dec 3, 2024
1 parent ff93d0d commit 1f87766
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
});

Expand Down
22 changes: 11 additions & 11 deletions packages/web/src/db/adapters/LockedAsyncDatabaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class LockedAsyncDatabaseAdapter extends BaseObserver<LockedAsyncDatabase
private debugMode: boolean;
private _dbIdentifier: string;
private _isInitialized = false;
protected initPromise: Promise<void>;
private _db: AsyncDatabaseConnection | null = null;
protected _disposeTableChangeListener: (() => void) | null = null;

Expand Down Expand Up @@ -64,6 +65,7 @@ export class LockedAsyncDatabaseAdapter extends BaseObserver<LockedAsyncDatabase
this.dbGetHelpers = this.generateDBHelpers({
execute: (query, params) => this.acquireLock(() => this._execute(query, params))
});
this.initPromise = this._init();
}

protected get baseDB() {
Expand All @@ -87,7 +89,14 @@ export class LockedAsyncDatabaseAdapter extends BaseObserver<LockedAsyncDatabase
});
}

/**
* Init is automatic, this helps catch errors or explicitly await initialization
*/
async init() {
return this.initPromise;
}

protected async _init() {
this._db = await this.options.openConnection();
await this._db.init();
await this.registerOnChangeListener(this._db);
Expand All @@ -97,17 +106,8 @@ export class LockedAsyncDatabaseAdapter extends BaseObserver<LockedAsyncDatabase
}

protected async waitForInitialized() {
if (this._isInitialized) {
return;
}
return new Promise<void>((resolve) => {
const l = this.registerListener({
initialized: () => {
resolve();
l();
}
});
});
// Awaiting this will expose errors on function calls like .execute etc
await this.initPromise;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class WASqliteConnection extends BaseObserver<WASQLiteConnectionListener>
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];
}
Expand Down
8 changes: 2 additions & 6 deletions packages/web/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class WASQLiteOpenFactory extends AbstractWebSQLOpenFactory {

const workerDBOpener = Comlink.wrap<OpenAsyncDatabaseConnection<WASQLiteOpenOptions>>(messagePort);

const workerAdapter = new WorkerLockedAsyncDatabaseAdapter({
adapter = new WorkerLockedAsyncDatabaseAdapter({
messagePort,
openConnection: () =>
workerDBOpener({
Expand All @@ -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,
Expand All @@ -77,8 +75,6 @@ export class WASQLiteOpenFactory extends AbstractWebSQLOpenFactory {
debugMode: this.options.debugMode,
logger: this.logger
});
contextAdapter.init();
adapter = contextAdapter;
}

return adapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ class SharedSyncClientProvider extends AbstractSharedSyncClientProvider {
}

async getDBWorkerPort(): Promise<MessagePort> {
// 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]);
}

Expand Down

0 comments on commit 1f87766

Please sign in to comment.