Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

[Fix] Watched Queries Race Condition #23

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-moons-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@journeyapps/powersync-sdk-web': patch
---

Fixed watched queries not updating due to race condition when opening multiple WA-SQLite connections due to initiating multiple PowerSync instances simultaneously.
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,30 @@ import { DBWorkerInterface, _openDB } from './open-db';

const _self: SharedWorkerGlobalScope = self as any;

const DBMap = new Map<string, DBWorkerInterface>();
const DBMap = new Map<string, Promise<DBWorkerInterface>>();

const openDB = async (dbFileName: string): Promise<DBWorkerInterface> => {
if (!DBMap.has(dbFileName)) {
DBMap.set(dbFileName, await _openDB(dbFileName));
const openPromise = _openDB(dbFileName);
DBMap.set(dbFileName, openPromise);
openPromise.catch((error) => {
// Allow for retries if an error ocurred
console.error(error);
DBMap.delete(dbFileName);
});
}
return Comlink.proxy(DBMap.get(dbFileName)!);
return Comlink.proxy(await DBMap.get(dbFileName)!);
};

_self.onconnect = function (event: MessageEvent<string>) {
const port = event.ports[0];

console.debug('Exposing db on port', port);
Comlink.expose(openDB, port);
};

addEventListener('beforeunload', (event) => {
Array.from(DBMap.values()).forEach((db) => {
Array.from(DBMap.values()).forEach(async (dbPromise) => {
const db = await dbPromise;
db.close?.();
});
});
7 changes: 5 additions & 2 deletions packages/powersync-sdk-web/src/worker/db/open-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export type WASQLiteExecuteMethod = (sql: string, params?: any[]) => Promise<WAS
export type OnTableChangeCallback = (opType: number, tableName: string, rowId: number) => void;
export type OpenDB = (dbFileName: string) => DBWorkerInterface;

const listeners = new Map<string, OnTableChangeCallback>();

export async function _openDB(dbFileName: string): Promise<DBWorkerInterface> {
const { default: moduleFactory } = await import('@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs');
const module = await moduleFactory();
Expand All @@ -37,6 +35,11 @@ export async function _openDB(dbFileName: string): Promise<DBWorkerInterface> {

const db = await sqlite3.open_v2(dbFileName);

/**
* Listeners are exclusive to the DB connection.
*/
const listeners = new Map<string, OnTableChangeCallback>();

sqlite3.register_table_onchange_hook(db, (opType: number, tableName: string, rowId: number) => {
Array.from(listeners.values()).forEach((l) => l(opType, tableName, rowId));
});
Expand Down
Loading