1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-09-01 21:21:58 +03:00

Pass a store implementation rather than kind string.

Makes it easier to test.
This commit is contained in:
Kegan Dougal
2015-06-30 11:39:57 +01:00
parent 796afb104b
commit 5d5d76d154

View File

@@ -111,30 +111,32 @@
* @module store/webstorage
*/
var utils = require("../utils");
/**
* Construct a web storage store, capable of storing rooms and users.
* @constructor
* @param {string} kind The kind of storage. Either 'local' or 'session'.
* @param {WebStorage} store A web storage implementation, e.g.
* 'window.localStorage' or 'window.sessionStorage' or a custom implementation.
* @param {integer} batchSize The number of events to store per key/value (room
* scoped). Use -1 to store all events for a room under one key/value.
* @throws if the global 'localStorage' does not exist and kind='local', or the
* global 'sessionStorage' does not exist and kind='session', or kind is not
* 'local' or 'session'.
* @throws if the supplied 'store' does not meet the Storage interface of the
* WebStorage API.
*/
function WebStorageStore(kind, batchSize) {
if (["local", "session"].indexOf(kind) === -1) {
throw new Error("Invalid kind: " + kind);
}
if (kind === "local" && !global.localStorage ||
kind === "session" && !global.sessionStorage) {
throw new Error("Global for " + kind + " storage not found.");
}
var stores = {
local: global.localStorage,
session: global.sessionStorage
};
this.store = stores[kind];
function WebStorageStore(store, batchSize) {
this.store = store;
this.batchSize = batchSize;
if (!utils.isFunction(store.getItem) || !utils.isFunction(store.setItem) ||
!utils.isFunction(store.removeItem) || !utils.isFunction(store.key)) {
throw new Error(
"Supplied store does not meet the WebStorage API interface"
);
}
if (!parseInt(store.length) && store.length !== 0) {
throw new Error(
"Supplied store does not meet the WebStorage API interface (length)"
);
}
}
WebStorageStore.prototype = {