From 7740d562c08a33e6552aab40a4709ee87220c0dc Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 15 May 2023 10:45:30 +0100 Subject: [PATCH 1/4] Add methods to terminate idb worker --- src/store/indexeddb-backend.ts | 1 + src/store/indexeddb-local-backend.ts | 4 ++++ src/store/indexeddb-remote-backend.ts | 4 ++++ src/store/indexeddb.ts | 4 ++++ 4 files changed, 13 insertions(+) diff --git a/src/store/indexeddb-backend.ts b/src/store/indexeddb-backend.ts index 008867dfc33..c93afb9e705 100644 --- a/src/store/indexeddb-backend.ts +++ b/src/store/indexeddb-backend.ts @@ -35,6 +35,7 @@ export interface IIndexedDBBackend { saveToDeviceBatches(batches: ToDeviceBatchWithTxnId[]): Promise; getOldestToDeviceBatch(): Promise; removeToDeviceBatch(id: number): Promise; + destroy(): Promise; } export type UserTuple = [userId: string, presenceEvent: Partial]; diff --git a/src/store/indexeddb-local-backend.ts b/src/store/indexeddb-local-backend.ts index a82de854e60..63ecd05afd3 100644 --- a/src/store/indexeddb-local-backend.ts +++ b/src/store/indexeddb-local-backend.ts @@ -594,4 +594,8 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend { store.delete(id); await txnAsPromise(txn); } + + public async destroy(): Promise { + this.db?.close(); + } } diff --git a/src/store/indexeddb-remote-backend.ts b/src/store/indexeddb-remote-backend.ts index 7e2aa0ccbe9..1d4628f8520 100644 --- a/src/store/indexeddb-remote-backend.ts +++ b/src/store/indexeddb-remote-backend.ts @@ -200,4 +200,8 @@ export class RemoteIndexedDBStoreBackend implements IIndexedDBBackend { logger.warn("Unrecognised message from worker: ", msg); } }; + + public async destroy(): Promise { + this.worker?.terminate(); + } } diff --git a/src/store/indexeddb.ts b/src/store/indexeddb.ts index cc77bf9c80f..07554d01f80 100644 --- a/src/store/indexeddb.ts +++ b/src/store/indexeddb.ts @@ -151,6 +151,10 @@ export class IndexedDBStore extends MemoryStore { }); } + public destroy(): Promise { + return this.backend.destroy(); + } + private onClose = (): void => { this.emitter.emit("closed"); }; From 3a22ba3691c0217bd91aa06c283eda0fe919473d Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 15 May 2023 13:16:18 +0100 Subject: [PATCH 2/4] Add docs --- src/store/indexeddb-local-backend.ts | 3 +++ src/store/indexeddb-remote-backend.ts | 3 +++ src/store/indexeddb.ts | 3 +++ 3 files changed, 9 insertions(+) diff --git a/src/store/indexeddb-local-backend.ts b/src/store/indexeddb-local-backend.ts index 63ecd05afd3..3bc5914066a 100644 --- a/src/store/indexeddb-local-backend.ts +++ b/src/store/indexeddb-local-backend.ts @@ -595,6 +595,9 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend { await txnAsPromise(txn); } + /* + * Close the database + */ public async destroy(): Promise { this.db?.close(); } diff --git a/src/store/indexeddb-remote-backend.ts b/src/store/indexeddb-remote-backend.ts index 1d4628f8520..32ff51efe71 100644 --- a/src/store/indexeddb-remote-backend.ts +++ b/src/store/indexeddb-remote-backend.ts @@ -201,6 +201,9 @@ export class RemoteIndexedDBStoreBackend implements IIndexedDBBackend { } }; + /* + * Destroy the web worker + */ public async destroy(): Promise { this.worker?.terminate(); } diff --git a/src/store/indexeddb.ts b/src/store/indexeddb.ts index 07554d01f80..3ca9ea2dca4 100644 --- a/src/store/indexeddb.ts +++ b/src/store/indexeddb.ts @@ -151,6 +151,9 @@ export class IndexedDBStore extends MemoryStore { }); } + /* + * Close the database and destroy any associated workers + */ public destroy(): Promise { return this.backend.destroy(); } From 3a4283008f23d9f03602dce2ae578635364cbd08 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 16 May 2023 08:44:39 +0100 Subject: [PATCH 3/4] Iterate --- src/store/index.ts | 5 +++++ src/store/memory.ts | 4 ++++ src/store/stub.ts | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/src/store/index.ts b/src/store/index.ts index 78741786ac2..ac0a344e852 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -245,4 +245,9 @@ export interface IStore { * Removes a specific batch of to-device messages from the queue */ removeToDeviceBatch(id: number): Promise; + + /** + * Stop the store and perform any appropriate cleanup + */ + destroy(): Promise; } diff --git a/src/store/memory.ts b/src/store/memory.ts index 091dcd4c8ca..8b560784622 100644 --- a/src/store/memory.ts +++ b/src/store/memory.ts @@ -435,4 +435,8 @@ export class MemoryStore implements IStore { this.pendingToDeviceBatches = this.pendingToDeviceBatches.filter((batch) => batch.id !== id); return Promise.resolve(); } + + public async destroy(): Promise { + // Nothing to do + } } diff --git a/src/store/stub.ts b/src/store/stub.ts index cc128540630..5ea91cf98cc 100644 --- a/src/store/stub.ts +++ b/src/store/stub.ts @@ -266,4 +266,8 @@ export class StubStore implements IStore { public async removeToDeviceBatch(id: number): Promise { return Promise.resolve(); } + + public async destroy(): Promise { + // Nothing to do + } } From 732b1968022329dca625c5919691a0dfeaab3da8 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 16 May 2023 09:42:10 +0100 Subject: [PATCH 4/4] Add test --- spec/unit/stores/indexeddb.spec.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/unit/stores/indexeddb.spec.ts b/spec/unit/stores/indexeddb.spec.ts index 544a3d75c5b..7f2bc25d458 100644 --- a/spec/unit/stores/indexeddb.spec.ts +++ b/spec/unit/stores/indexeddb.spec.ts @@ -254,4 +254,31 @@ describe("IndexedDBStore", () => { }); await expect(store.startup()).rejects.toThrow("Test"); }); + + it("remote worker should terminate upon destroy call", async () => { + const terminate = jest.fn(); + const worker = new (class MockWorker { + private onmessage!: (data: any) => void; + postMessage(data: any) { + this.onmessage({ + data: { + command: "cmd_success", + seq: data.seq, + result: [], + }, + }); + } + public terminate = terminate; + })() as unknown as Worker; + + const store = new IndexedDBStore({ + indexedDB: indexedDB, + dbName: "database", + localStorage, + workerFactory: () => worker, + }); + await store.startup(); + await expect(store.destroy()).resolves; + expect(terminate).toHaveBeenCalled(); + }); });