diff --git a/lib/store/webstorage.js b/lib/store/webstorage.js index 506215bbf..261a0eda6 100644 --- a/lib/store/webstorage.js +++ b/lib/store/webstorage.js @@ -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 = {