cyra
Version:
single page application view engine
98 lines (97 loc) • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var config_1 = require("../config");
var _a = config_1.CyraConfig.APPSTORAGE, destinationMethods = _a.destinationMethods, appStorage = _a.appStorage;
var AppStorage = (function () {
function AppStorage() {
}
AppStorage.init = function () {
if (!this.storage.getItem(destinationMethods.key)) {
this.setItemSafe(destinationMethods.key, destinationMethods.default);
}
if (!this.storage.getItem(appStorage.key)) {
this.setItemSafe(appStorage.key, appStorage.default);
}
};
/**
* 多页方案清除目标页待执行函数
*/
AppStorage.clearMethodsQueue = function () {
this.setItemSafe(destinationMethods.key, destinationMethods.default);
};
/**
* 多页方案获取目标页待执行函数队列
* @return {Array<DestinationMethod>}
*/
AppStorage.getDestinationMethods = function () {
var methods = JSON.parse(this.storage.getItem(destinationMethods.key)) || [];
return methods;
};
/**
* 多页方案向目标页待执行函数队列添加函数
* @param {DestinationMethod} data
*/
AppStorage.addDestinationMethod = function (data) {
var methods = this.getDestinationMethods();
methods.push(data);
this.setItemSafe(destinationMethods.key, JSON.stringify(methods));
};
/**
* 页面数据获取接口
* @param {string} key
* @return {any}
*/
AppStorage.get = function (key) {
return this.cache[key];
};
/**
* 页面数据存储接口
* @param {string} key
* @param {any} data
*/
AppStorage.set = function (key, data) {
this.cache[key] = data;
};
/**
* 多页方案将 cache 数据存入 SessionStorage
*/
AppStorage.save = function () {
this.setItemSafe(appStorage.key, JSON.stringify(this.cache));
};
/**
* 多页方案从 SessionStorage 重建缓存
*/
AppStorage.restore = function () {
this.cache = JSON.parse(this.storage.getItem(appStorage.key)) || {};
};
/**
* 清除页面存储的指定数据
* @param {string} key
*/
AppStorage.clear = function (key) {
delete this.cache[key];
};
/**
* 清除所有页面存储数据
*/
AppStorage.clearAll = function () {
this.cache = {};
this.setItemSafe(appStorage.key, appStorage.default);
};
/**
* 安全设置localStorage
* @param {string} key
* @param {string} value
*/
AppStorage.setItemSafe = function (key, value) {
try {
this.storage.setItem(key, value);
}
catch (e) {
}
};
AppStorage.storage = window.sessionStorage;
AppStorage.cache = {};
return AppStorage;
}());
exports.AppStorage = AppStorage;