1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Very rough WIP of an IndexedDBStore

This commit is contained in:
Kegan Dougal
2017-01-13 16:22:59 +00:00
parent 0fa9f7c609
commit 8bcb048f53
4 changed files with 76 additions and 12 deletions

View File

@@ -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. */

74
src/store/indexeddb.js Normal file
View File

@@ -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 <code>connect()</code> before
* this store can be used.
* @constructor
* @param {Object} indexedDBInterface The Indexed DB interface e.g <code>window.indexedDB</code>
*/
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);
};
});
}

View File

@@ -274,10 +274,4 @@ module.exports.MatrixInMemoryStore.prototype = {
getAccountData: function(eventType) {
return this.accountData[eventType];
},
// TODO
//setMaxHistoryPerRoom: function(maxHistory) {},
// TODO
//reapOldMessages: function() {},
};

View File

@@ -179,12 +179,6 @@ StubStore.prototype = {
getAccountData: function(eventType) {
},
// TODO
//setMaxHistoryPerRoom: function(maxHistory) {},
// TODO
//reapOldMessages: function() {},
};
/** Stub Store class. */