diff --git a/src/matrix.js b/src/matrix.js index b744ae61b..b54501023 100644 --- a/src/matrix.js +++ b/src/matrix.js @@ -1,5 +1,5 @@ /* -Copyright 2015, 2016 OpenMarket Ltd +Copyright 2015, 2016, 2017 OpenMarket Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -23,6 +23,8 @@ module.exports.EventStatus = require("./models/event").EventStatus; module.exports.MatrixInMemoryStore = require("./store/memory").MatrixInMemoryStore; /** The {@link module:store/indexeddb.IndexedDBStore|IndexedDBStore} class. */ module.exports.IndexedDBStore = require("./store/indexeddb").IndexedDBStore; +/** The {@link module:store/indexeddb.IndexedDBStoreBackend|IndexedDBStoreBackend} class. */ +module.exports.IndexedDBStoreBackend = require("./store/indexeddb").IndexedDBStoreBackend; /** The {@link module:http-api.MatrixHttpApi|MatrixHttpApi} class. */ module.exports.MatrixHttpApi = require("./http-api").MatrixHttpApi; /** The {@link module:http-api.MatrixError|MatrixError} class. */ diff --git a/src/store/indexeddb.js b/src/store/indexeddb.js index b0c92e745..2f4f5de69 100644 --- a/src/store/indexeddb.js +++ b/src/store/indexeddb.js @@ -16,6 +16,8 @@ limitations under the License. "use strict"; import q from "q"; +import MatrixInMemoryStore from "./memory"; +import utils from "../utils"; /** * This is an internal module. See {@link IndexedDBStore} for the public class. @@ -25,20 +27,22 @@ import q from "q"; const VERSION = 1; /** - * Construct a new Indexed Database store. This requires a call to connect() before - * this store can be used. + * Construct a new Indexed Database store backend. This requires a call to + * connect() before this store can be used. * @constructor - * @param {Object} indexedDBInterface The Indexed DB interface e.g window.indexedDB + * @param {Object} indexedDBInterface The Indexed DB interface e.g + * window.indexedDB */ -module.exports.IndexedDBStore = function IndexedDBStore(indexedDBInterface) { +const IndexedDBStoreBackend = function IndexedDBStoreBackend(indexedDBInterface) { this.indexedDB = indexedDBInterface; this.db = null; }; -module.exports.IndexedDBStore.prototype = { +IndexedDBStoreBackend.prototype = { /** - * Attempt to connect to the database. This can fail if the user does not grant permission. + * Attempt to connect to the database. This can fail if the user does not + * grant permission. * @return {Promise} Resolves if successfully connected. */ connect: function() { @@ -133,9 +137,30 @@ module.exports.IndexedDBStore.prototype = { return promiseifyTxn(txn); }); }, - }; +/** + * Construct a new Indexed Database store, which extends MatrixInMemoryStore. + * + * This store functions like a MatrixInMemoryStore except it periodically persists + * the contents of the store to an IndexedDB backend. + * + * All data is still kept in-memory but can be loaded from disk by calling + * startup(). This can make startup times quicker as a complete + * sync from the server is not required. This does not reduce memory usage as all + * the data is eagerly fetched when startup() is called. + * + * @constructor + * @extends MatrixInMemoryStore + * @param {IndexedDBStoreBackend} backend The indexed db backend instance. + * @param {Object=} opts Options for MatrixInMemoryStore. + */ +const IndexedDBStore = function IndexedDBStore(backend, opts) { + MatrixInMemoryStore.call(this, opts); + this.backend = backend; +}; +utils.inherits(IndexedDBStore, MatrixInMemoryStore); + function createDatabase(db) { // Make room store, clobber based on room ID. (roomId property of Room objects) db.createObjectStore("rooms", { keyPath: ["roomId"] }); @@ -172,3 +197,6 @@ function promiseifyRequest(req) { }; }); } + +module.exports.IndexedDBStore = IndexedDBStore; +module.exports.IndexedDBStoreBackend = IndexedDBStoreBackend;