You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-09 10:22:46 +03:00
Introduce the concept of IndexedDBStoreBackend
So IndexedDBStore can meet the Store interface and we don't need to mess with the guts of MatrixInMemoryStore.
This commit is contained in:
@@ -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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with 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;
|
module.exports.MatrixInMemoryStore = require("./store/memory").MatrixInMemoryStore;
|
||||||
/** The {@link module:store/indexeddb.IndexedDBStore|IndexedDBStore} class. */
|
/** The {@link module:store/indexeddb.IndexedDBStore|IndexedDBStore} class. */
|
||||||
module.exports.IndexedDBStore = require("./store/indexeddb").IndexedDBStore;
|
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. */
|
/** The {@link module:http-api.MatrixHttpApi|MatrixHttpApi} class. */
|
||||||
module.exports.MatrixHttpApi = require("./http-api").MatrixHttpApi;
|
module.exports.MatrixHttpApi = require("./http-api").MatrixHttpApi;
|
||||||
/** The {@link module:http-api.MatrixError|MatrixError} class. */
|
/** The {@link module:http-api.MatrixError|MatrixError} class. */
|
||||||
|
@@ -16,6 +16,8 @@ limitations under the License.
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
import q from "q";
|
import q from "q";
|
||||||
|
import MatrixInMemoryStore from "./memory";
|
||||||
|
import utils from "../utils";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is an internal module. See {@link IndexedDBStore} for the public class.
|
* This is an internal module. See {@link IndexedDBStore} for the public class.
|
||||||
@@ -25,20 +27,22 @@ import q from "q";
|
|||||||
const VERSION = 1;
|
const VERSION = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new Indexed Database store. This requires a call to <code>connect()</code> before
|
* Construct a new Indexed Database store backend. This requires a call to
|
||||||
* this store can be used.
|
* <code>connect()</code> before this store can be used.
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {Object} indexedDBInterface The Indexed DB interface e.g <code>window.indexedDB</code>
|
* @param {Object} indexedDBInterface The Indexed DB interface e.g
|
||||||
|
* <code>window.indexedDB</code>
|
||||||
*/
|
*/
|
||||||
module.exports.IndexedDBStore = function IndexedDBStore(indexedDBInterface) {
|
const IndexedDBStoreBackend = function IndexedDBStoreBackend(indexedDBInterface) {
|
||||||
this.indexedDB = indexedDBInterface;
|
this.indexedDB = indexedDBInterface;
|
||||||
this.db = null;
|
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.
|
* @return {Promise} Resolves if successfully connected.
|
||||||
*/
|
*/
|
||||||
connect: function() {
|
connect: function() {
|
||||||
@@ -133,9 +137,30 @@ module.exports.IndexedDBStore.prototype = {
|
|||||||
return promiseifyTxn(txn);
|
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
|
||||||
|
* <code>startup()</code>. 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 <code>startup()</code> 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) {
|
function createDatabase(db) {
|
||||||
// Make room store, clobber based on room ID. (roomId property of Room objects)
|
// Make room store, clobber based on room ID. (roomId property of Room objects)
|
||||||
db.createObjectStore("rooms", { keyPath: ["roomId"] });
|
db.createObjectStore("rooms", { keyPath: ["roomId"] });
|
||||||
@@ -172,3 +197,6 @@ function promiseifyRequest(req) {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.IndexedDBStore = IndexedDBStore;
|
||||||
|
module.exports.IndexedDBStoreBackend = IndexedDBStoreBackend;
|
||||||
|
Reference in New Issue
Block a user