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

Create indexeddb worker when starting the store #627

Merged
merged 4 commits into from
Mar 9, 2018
Merged
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
35 changes: 23 additions & 12 deletions src/store/indexeddb-remote-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,23 @@ import Promise from 'bluebird';
* @param {string} workerScript URL to the worker script
* @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
* @param {Object} workerApi The web worker compatible interface object
*/
const RemoteIndexedDBStoreBackend = function RemoteIndexedDBStoreBackend(
workerScript, dbName, WorkerApi,
workerScript, dbName, workerApi,
) {
this._workerScript = workerScript;
this._dbName = dbName;
this._worker = new WorkerApi(workerScript);
this._workerApi = workerApi;
this._worker = null;
this._nextSeq = 0;
// The currently in-flight requests to the actual backend
this._inFlight = {
// seq: promise,
};

this._worker.onmessage = this._onWorkerMessage.bind(this);

// tell the worker the db name.
this._startPromise = this._doCmd('_setupWorker', [this._dbName]).then(() => {
console.log("IndexedDB worker is ready");
});
// Once we start connecting, we keep the promise and re-use it
// if we try to connect again
this._startPromise = null;
};


Expand All @@ -55,7 +53,7 @@ RemoteIndexedDBStoreBackend.prototype = {
* @return {Promise} Resolves if successfully connected.
*/
connect: function() {
return this._startPromise.then(() => this._doCmd('connect'));
return this._ensureStarted().then(() => this._doCmd('connect'));
},

/**
Expand All @@ -64,7 +62,7 @@ RemoteIndexedDBStoreBackend.prototype = {
* @return {Promise} Resolved when the database is cleared.
*/
clearDatabase: function() {
return this._startPromise.then(() => this._doCmd('clearDatabase'));
return this._ensureStarted().then(() => this._doCmd('clearDatabase'));
},

/**
Expand Down Expand Up @@ -93,6 +91,19 @@ RemoteIndexedDBStoreBackend.prototype = {
return this._doCmd('getUserPresenceEvents');
},

_ensureStarted: function() {
if (this._startPromise === null) {
this._worker = new this._workerApi(this._workerScript);
this._worker.onmessage = this._onWorkerMessage.bind(this);

// tell the worker the db name.
this._startPromise = this._doCmd('_setupWorker', [this._dbName]).then(() => {
console.log("IndexedDB worker is ready");
});
}
return this._startPromise;
},

_doCmd: function(cmd, args) {
// wrap in a q so if the postMessage throws,
// the promise automatically gets rejected
Expand Down