Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to terminate idb worker #3362

Merged
merged 5 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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();
}
Comment on lines +157 to +159
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't close be a better name? destroy sounds like a pretty violent thing to do to your database.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, can we add this to IStore, so that the application doesn't need to do instanceof checks?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also also, any reason not to do this automatically in stopClient?

Copy link
Member Author

@t3chguy t3chguy May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also also, any reason not to do this automatically in stopClient?

Symmetry: the startup method isn't called by startClient - and seemingly you might want to be able to start and stop your client independently from your stores, i.e stop your client when the network is down without tearing down your worker etc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re naming, it could be close terminate cleanup stop but we seem to use destroy generically here, at least in the react-sdk, where we even have an IDestroyable iface.

Happy to move it to IStore though


private onClose = (): void => {
this.emitter.emit("closed");
};
Expand Down