Skip to content

Commit

Permalink
Add methods to terminate idb worker (#3362)
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored May 16, 2023
1 parent 8d14d45 commit 789458e
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 0 deletions.
27 changes: 27 additions & 0 deletions spec/unit/stores/indexeddb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
5 changes: 5 additions & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,9 @@ export interface IStore {
* Removes a specific batch of to-device messages from the queue
*/
removeToDeviceBatch(id: number): Promise<void>;

/**
* Stop the store and perform any appropriate cleanup
*/
destroy(): Promise<void>;
}
1 change: 1 addition & 0 deletions src/store/indexeddb-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface IIndexedDBBackend {
saveToDeviceBatches(batches: ToDeviceBatchWithTxnId[]): Promise<void>;
getOldestToDeviceBatch(): Promise<IndexedToDeviceBatch | null>;
removeToDeviceBatch(id: number): Promise<void>;
destroy(): Promise<void>;
}

export type UserTuple = [userId: string, presenceEvent: Partial<IEvent>];
7 changes: 7 additions & 0 deletions src/store/indexeddb-local-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,11 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend {
store.delete(id);
await txnAsPromise(txn);
}

/*
* Close the database
*/
public async destroy(): Promise<void> {
this.db?.close();
}
}
7 changes: 7 additions & 0 deletions src/store/indexeddb-remote-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,11 @@ export class RemoteIndexedDBStoreBackend implements IIndexedDBBackend {
logger.warn("Unrecognised message from worker: ", msg);
}
};

/*
* Destroy the web worker
*/
public async destroy(): Promise<void> {
this.worker?.terminate();
}
}
7 changes: 7 additions & 0 deletions src/store/indexeddb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ export class IndexedDBStore extends MemoryStore {
});
}

/*
* Close the database and destroy any associated workers
*/
public destroy(): Promise<void> {
return this.backend.destroy();
}

private onClose = (): void => {
this.emitter.emit("closed");
};
Expand Down
4 changes: 4 additions & 0 deletions src/store/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
// Nothing to do
}
}
4 changes: 4 additions & 0 deletions src/store/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,8 @@ export class StubStore implements IStore {
public async removeToDeviceBatch(id: number): Promise<void> {
return Promise.resolve();
}

public async destroy(): Promise<void> {
// Nothing to do
}
}

0 comments on commit 789458e

Please sign in to comment.