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

Use webpack worker-loader instead of homegrown hack #1780

Merged
merged 2 commits into from
Jul 14, 2021
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
8 changes: 3 additions & 5 deletions src/store/indexeddb-remote-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ export class RemoteIndexedDBStoreBackend implements IIndexedDBBackend {
* Construct a new Indexed Database store backend. This requires a call to
* <code>connect()</code> before this store can be used.
* @constructor
* @param {string} workerScript URL to the worker script
* @param {Function} workerFactory Factory which produces a Worker
* @param {string=} dbName Optional database name. The same name must be used
* to open the same database.
* @param {Object} WorkerApi The web worker compatible interface object
*/
constructor(
private readonly workerScript: string,
private readonly workerFactory: () => Worker,
private readonly dbName: string,
private readonly WorkerApi: typeof Worker,
) {}

/**
Expand Down Expand Up @@ -137,7 +135,7 @@ export class RemoteIndexedDBStoreBackend implements IIndexedDBBackend {

private ensureStarted(): Promise<void> {
if (this.startPromise === null) {
this.worker = new this.WorkerApi(this.workerScript);
this.worker = this.workerFactory();
this.worker.onmessage = this.onWorkerMessage;

// tell the worker the db name.
Expand Down
15 changes: 3 additions & 12 deletions src/store/indexeddb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ const WRITE_DELAY_MS = 1000 * 60 * 5; // once every 5 minutes
interface IOpts extends IBaseOpts {
indexedDB: IDBFactory;
dbName?: string;
workerScript?: string;
workerApi?: typeof Worker;
workerFactory?: () => Worker;
}

export class IndexedDBStore extends MemoryStore {
Expand Down Expand Up @@ -111,16 +110,8 @@ export class IndexedDBStore extends MemoryStore {
throw new Error('Missing required option: indexedDB');
}

if (opts.workerScript) {
// try & find a webworker-compatible API
let workerApi = opts.workerApi;
if (!workerApi) {
// default to the global Worker object (which is where it in a browser)
workerApi = global.Worker;
}
this.backend = new RemoteIndexedDBStoreBackend(
opts.workerScript, opts.dbName, workerApi,
);
if (opts.workerFactory) {
this.backend = new RemoteIndexedDBStoreBackend(opts.workerFactory, opts.dbName);
} else {
this.backend = new LocalIndexedDBStoreBackend(opts.indexedDB, opts.dbName);
}
Expand Down