1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Create indexeddb worker when starting the store

Rather than when creating it, otherwise we could potentially end
up starting workers unnecessarily.
This commit is contained in:
David Baker
2018-03-09 16:53:27 +00:00
parent df70e16b4d
commit 8bd68e0f10

View File

@@ -31,20 +31,16 @@ import Promise from 'bluebird';
const RemoteIndexedDBStoreBackend = function RemoteIndexedDBStoreBackend(
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");
});
this._startPromise = null;
};
@@ -55,7 +51,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'));
},
/**
@@ -64,7 +60,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'));
},
/**
@@ -93,6 +89,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