diff --git a/spec/test-utils.js b/spec/test-utils.js index ab47a85e7..88168130d 100644 --- a/spec/test-utils.js +++ b/spec/test-utils.js @@ -13,9 +13,14 @@ const MatrixEvent = sdk.MatrixEvent; */ module.exports.syncPromise = function(client) { const def = q.defer(); - client.on('sync', (state) => { - if (state == 'SYNCING') def.resolve(); - }); + const cb = (state) => { + if (state == 'SYNCING') { + def.resolve(); + } else { + client.once('sync', cb); + } + }; + client.once('sync', cb); return def.promise; }; diff --git a/src/indexeddb-worker.js b/src/indexeddb-worker.js new file mode 100644 index 000000000..aa6312e2f --- /dev/null +++ b/src/indexeddb-worker.js @@ -0,0 +1,24 @@ +/* +Copyright 2017 Vector Creations Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +/** + * Separate exports file for the indexeddb web worker, which is designed + * to be used separately + */ + +/** The {@link module:indexeddb-store-worker~IndexedDBStoreWorker} class. */ +module.exports.IndexedDBStoreWorker = require("./store/indexeddb-store-worker.js"); + diff --git a/src/matrix.js b/src/matrix.js index 7ad894351..ea885b932 100644 --- a/src/matrix.js +++ b/src/matrix.js @@ -61,8 +61,6 @@ module.exports.Filter = require("./filter"); module.exports.TimelineWindow = require("./timeline-window").TimelineWindow; /** The {@link module:interactive-auth} class. */ module.exports.InteractiveAuth = require("./interactive-auth"); -/** The {@link module:indexeddb-remote-worker} class. */ -module.exports.IndexedDbStoreWorker = require("./store/indexeddb-remote-worker.js"); /** diff --git a/src/store/indexeddb-local-backend.js b/src/store/indexeddb-local-backend.js index d0094e89b..0f4010d3c 100644 --- a/src/store/indexeddb-local-backend.js +++ b/src/store/indexeddb-local-backend.js @@ -243,7 +243,7 @@ LocalIndexedDBStoreBackend.prototype = { /** * Persist a list of [user id, presence event] they are for. * Users with the same 'userId' will be replaced. - * Prfesence events should be the event in its raw form (not the Event + * Presence events should be the event in its raw form (not the Event * object) * @param {Object[]} tuples An array of [userid, event] tuples * @return {Promise} Resolves if the users were persisted. diff --git a/src/store/indexeddb-remote-backend.js b/src/store/indexeddb-remote-backend.js index 6b27b9882..4daa60814 100644 --- a/src/store/indexeddb-remote-backend.js +++ b/src/store/indexeddb-remote-backend.js @@ -30,6 +30,7 @@ import q from "q"; const RemoteIndexedDBStoreBackend = function RemoteIndexedDBStoreBackend( workerScript, dbName, ) { + this._dbName = dbName; this._worker = new Worker(workerScript); this._nextSeq = 0; // The currently in-flight requests to the actual backend @@ -38,10 +39,6 @@ const RemoteIndexedDBStoreBackend = function RemoteIndexedDBStoreBackend( }; this._worker.onmessage = this._onWorkerMessage.bind(this); - - this._doCmd('_setupWorker', [dbName]).done(() => { - console.log("IndexedDB worker is ready"); - }); }; @@ -52,7 +49,10 @@ RemoteIndexedDBStoreBackend.prototype = { * @return {Promise} Resolves if successfully connected. */ connect: function() { - return this._doCmd('connect'); + return this._doCmd('_setupWorker', [this._dbName]).then(() => { + console.log("IndexedDB worker is ready"); + return this._doCmd('connect'); + }); }, /** diff --git a/src/store/indexeddb-remote-worker.js b/src/store/indexeddb-store-worker.js similarity index 69% rename from src/store/indexeddb-remote-worker.js rename to src/store/indexeddb-store-worker.js index 0a857f0a9..725c6c91b 100644 --- a/src/store/indexeddb-remote-worker.js +++ b/src/store/indexeddb-store-worker.js @@ -18,10 +18,25 @@ import q from "q"; import LocalIndexedDBStoreBackend from "./indexeddb-local-backend.js"; /** - * This class lives in the webworker and drives a LocalIndexedDbStoreBackend + * This class lives in the webworker and drives a LocalIndexedDBStoreBackend * controlled by messages from the main process. + * + * It should be instantiated by a web worker script provided by the application + * in a script, for example: + * + * import {IndexedDBStoreWorker} from 'matrix-js-sdk/lib/indexeddb-worker.js'; + * const remoteWorker = new IndexedDBStoreWorker(postMessage); + * onmessage = remoteWorker.onMessage; + * + * Note that it is advisable to import this class by referencing the file directly to + * avoid a dependency on the whole js-sdk. + * */ -class IndexedDbStoreWorker { +class IndexedDBStoreWorker { + /** + * @param {function} postMessage The web worker postMessage function that + * should be used to communicate back to the main script. + */ constructor(postMessage) { this.backend = null; this.postMessage = postMessage; @@ -29,6 +44,12 @@ class IndexedDbStoreWorker { this.onMessage = this.onMessage.bind(this); } + /** + * Passes a message event from the main script into the class. This method + * can be directly assigned to the web worker `onmessage` variable. + * + * @param {Object} ev The message event + */ onMessage(ev) { const msg = ev.data; let prom; @@ -51,7 +72,7 @@ class IndexedDbStoreWorker { case 'clearDatabase': prom = this.backend.clearDatabase().then((result) => { // This returns special classes which can't be cloned - // accross to the main script, so don't try. + // across to the main script, so don't try. return {}; }); break; @@ -76,7 +97,7 @@ class IndexedDbStoreWorker { postMessage({ command: 'cmd_fail', seq: msg.seq, - // Canb't be an Error because they're not structured cloneable + // Can't be an Error because they're not structured cloneable error: "Unrecognised command", }); return; @@ -89,13 +110,16 @@ class IndexedDbStoreWorker { result: ret, }); }, (err) => { + console.error("Error running command: "+msg.command); + console.error(err); this.postMessage.call(null, { command: 'cmd_fail', seq: msg.seq, - error: err, + // Just send a string because Error objects aren't cloneable + error: "Error running command", }); }); } } -module.exports = IndexedDbStoreWorker; +module.exports = IndexedDBStoreWorker; diff --git a/src/store/indexeddb.js b/src/store/indexeddb.js index 510b8fa63..50f816426 100644 --- a/src/store/indexeddb.js +++ b/src/store/indexeddb.js @@ -67,7 +67,7 @@ const WRITE_DELAY_MS = 1000 * 60 * 5; // once every 5 minutes * window.indexedDB * @param {string=} opts.dbName Optional database name. The same name must be used * to open the same database. - * @param {string=} opts.workerScript Optional URL to a script to invooke a web + * @param {string=} opts.workerScript Optional URL to a script to invoke a web * worker with to run IndexedDB queries on the web worker. The IndexedDbStoreWorker * class is provided for this purpose and requires the application to provide a * trivial wrapper script around it.