@wora/cache-persist
Version:
@wora Cache Persist
102 lines • 3.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var StorageProxy_1 = require("./StorageProxy");
var hasOwn = Object.prototype.hasOwnProperty;
var Cache = (function () {
function Cache(options) {
this.rehydrated = false;
this.subscriptions = new Set();
this.storageProxy = StorageProxy_1.StorageProxy(this, options);
this.rehydrated = !this.storageProxy.getStorage();
this.data = options && options.initialState ? options.initialState : {};
this.mergeState =
options && options.mergeState
? options.mergeState
: function (restoredState, initialState) { return (restoredState ? restoredState : initialState); };
}
Cache.prototype.getStorage = function () {
return this.storageProxy.getStorage();
};
Cache.prototype.isRehydrated = function () {
return this.rehydrated;
};
Cache.prototype.restore = function () {
var _this = this;
if (this.promisesRestore) {
return this.promisesRestore;
}
this.promisesRestore = this.storageProxy
.restore()
.then(function (restoredState) { return Promise.all([restoredState, _this.mergeState(restoredState, _this.data)]); })
.then(function (_a) {
var restoredState = _a[0], newState = _a[1];
_this.data = newState;
if (restoredState !== newState) {
_this.replace(newState);
return _this.flush();
}
})
.then(function () {
_this.rehydrated = true;
return _this;
})
.catch(function (e) {
_this.promisesRestore = null;
throw e;
});
return this.promisesRestore;
};
Cache.prototype.replace = function (newData) {
var _this = this;
var keys = this.getAllKeys().concat(Object.keys(newData));
this.data = Object.assign({}, newData);
keys.forEach(function (key) { return _this.storageProxy.push(key); });
};
Cache.prototype.purge = function () {
var _this = this;
var keys = this.getAllKeys();
this.data = Object.create(null);
keys.forEach(function (key) { return _this.storageProxy.push(key); });
};
Cache.prototype.getState = function () {
return this.data;
};
Cache.prototype.has = function (key) {
return hasOwn.call(this.data, key);
};
Cache.prototype.get = function (key) {
return this.data[key];
};
Cache.prototype.set = function (key, value) {
this.data[key] = value;
this.storageProxy.push(key);
};
Cache.prototype.delete = function (key) {
this.set(key, null);
};
Cache.prototype.remove = function (key) {
delete this.data[key];
this.storageProxy.push(key);
};
Cache.prototype.flush = function () {
return this.storageProxy.flush();
};
Cache.prototype.getAllKeys = function () {
return Object.keys(this.data);
};
Cache.prototype.subscribe = function (callback) {
var _this = this;
var subscription = { callback: callback };
var dispose = function () { return _this.subscriptions.delete(subscription); };
this.subscriptions.add(subscription);
return dispose;
};
Cache.prototype.notify = function (payload) {
if (payload === void 0) { payload = {}; }
var _a = payload.state, state = _a === void 0 ? this.getState() : _a, _b = payload.action, action = _b === void 0 ? '' : _b;
this.subscriptions.forEach(function (subscription) { return subscription.callback(state, action); });
};
return Cache;
}());
exports.Cache = Cache;
//# sourceMappingURL=Cache.js.map