diff --git a/src/matrix.js b/src/matrix.js index b434d6551..da6e7c906 100644 --- a/src/matrix.js +++ b/src/matrix.js @@ -21,6 +21,8 @@ module.exports.MatrixEvent = require("./models/event").MatrixEvent; module.exports.EventStatus = require("./models/event").EventStatus; /** The {@link module:store/memory.MatrixInMemoryStore|MatrixInMemoryStore} class. */ 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: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 new file mode 100644 index 000000000..ea5a297f3 --- /dev/null +++ b/src/store/indexeddb.js @@ -0,0 +1,74 @@ +/* +Copyright 2015, 2016 OpenMarket 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. +*/ +"use strict"; + +/** + * This is an internal module. See {@link IndexedDBStore} for the public class. + * @module store/indexeddb + */ + +const VERSION = 1; + +/** + * Construct a new Indexed Database store. This requires a call to connect() before + * this store can be used. + * @constructor + * @param {Object} indexedDBInterface The Indexed DB interface e.g window.indexedDB + */ +module.exports.IndexedDBStore = function IndexedDBStore(indexedDBInterface) { + this.indexedDB = indexedDBInterface; + this.db = null; +}; + + +module.exports.IndexedDBStore.prototype = { + /** + * Attempt to connect to the database. This can fail if the user does not grant permission. + * @return {Promise} Resolves if successfully connected. + */ + connect: function() { + if (this.db) { + return Promise.resolve(); + } + const req = this.indexedDB.open("matrix-js-sdk", VERSION); + req.onupgradeneeded = (ev) => { + const db = req.result; + const oldVersion = ev.oldVersion; + if (oldVersion < 1) { // The database did not previously exist. + createDatabase(db); + } + // Expand as needed. + } + return promiseify(req).then((ev) => { + this.db = ev.target.result; + }); + } +} + +function createDatabase(db) { + // TODO: Make object stores and indexes. +} + +function promiseify(req) { + return new Promise((resolve, reject) => { + req.onsuccess = function(event) { + resolve(event); + }; + req.onerror = function(event) { + reject(event); + }; + }); +} \ No newline at end of file diff --git a/src/store/memory.js b/src/store/memory.js index c91651efa..d138e3a14 100644 --- a/src/store/memory.js +++ b/src/store/memory.js @@ -274,10 +274,4 @@ module.exports.MatrixInMemoryStore.prototype = { getAccountData: function(eventType) { return this.accountData[eventType]; }, - - // TODO - //setMaxHistoryPerRoom: function(maxHistory) {}, - - // TODO - //reapOldMessages: function() {}, }; diff --git a/src/store/stub.js b/src/store/stub.js index c635c93f6..ad5e3580c 100644 --- a/src/store/stub.js +++ b/src/store/stub.js @@ -179,12 +179,6 @@ StubStore.prototype = { getAccountData: function(eventType) { }, - - // TODO - //setMaxHistoryPerRoom: function(maxHistory) {}, - - // TODO - //reapOldMessages: function() {}, }; /** Stub Store class. */