UNPKG

@deepstream/client

Version:
65 lines (64 loc) 2.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalstorageStorage = void 0; var LocalstorageStorage = /** @class */ (function () { function LocalstorageStorage(options) { this.options = options; this.isReady = true; if (typeof localStorage === 'undefined' || localStorage === null) { try { var LocalStorage = require('node-localstorage').LocalStorage; this.storage = new LocalStorage(options.localstorage.nodeStoragePath, options.localstorage.nodeStorageSize * 1024 * 1024); } catch (e) { throw new Error('Attempting to use localStorage outside of browser without node-localstorage polyfill'); } } else { this.storage = localStorage; } } LocalstorageStorage.prototype.get = function (recordName, callback) { var ignore = this.options.ignorePrefixes.some(function (prefix) { return recordName.startsWith(prefix); }); if (ignore) { callback(recordName, -1, null); return; } var item = this.storage.getItem(recordName); if (item) { var doc = JSON.parse(item); setTimeout(callback.bind(this, recordName, doc.version, doc.data), 0); return; } setTimeout(callback.bind(this, recordName, -1, null), 0); }; LocalstorageStorage.prototype.set = function (recordName, version, data, callback) { var ignore = this.options.ignorePrefixes.some(function (prefix) { return recordName.startsWith(prefix); }); if (ignore) { callback(null, recordName); return; } this.storage.setItem(recordName, JSON.stringify({ recordName: recordName, version: version, data: data })); setTimeout(callback, 0); }; LocalstorageStorage.prototype.delete = function (recordName, callback) { var ignore = this.options.ignorePrefixes.some(function (prefix) { return recordName.startsWith(prefix); }); if (ignore) { callback(null, recordName); return; } this.storage.removeItem(recordName); setTimeout(callback, 0); }; LocalstorageStorage.prototype.reset = function (callback) { try { this.storage.clear(); callback(null); } catch (error) { callback(error); } }; return LocalstorageStorage; }()); exports.LocalstorageStorage = LocalstorageStorage;