UNPKG

nxkit

Version:

This is a collection of tools, independent of any other libraries

198 lines (197 loc) 5.97 kB
"use strict"; /* ***** BEGIN LICENSE BLOCK ***** * Distributed under the BSD license: * * Copyright (c) 2015, xuewen.chu * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of xuewen.chu nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL xuewen.chu BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * ***** END LICENSE BLOCK ***** */ Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("./util"); const path_1 = require("./path"); const delay_call_1 = require("./delay_call"); const { haveNode, haveNgui, haveWeb } = util_1.default; if (haveWeb) { var sync_local = function (self) { }; var commit = function (self) { }; var format_key = function (self, key) { return self.m_prefix + key; }; var stringify_val = function (val) { return JSON.stringify(val); }; var paese_value = function (val) { try { return JSON.parse(val); } catch (e) { console.warn(e); } return null; }; } else { if (haveNgui) { var fs = __requireNgui__('_fs'); } else if (haveNode) { var fs = require('fs'); } var sync_local = function (self) { if (self.m_change) { fs.writeFileSync(self.m_path, JSON.stringify(self.m_value, null, 2)); self.m_change = false; } }; var commit = function (self) { self.m_change = true; self.m_sync.call(); }; var format_key = function (self, key) { return key; }; var stringify_val = function (val) { return val; }; var paese_value = function (val) { return val; }; } var shared = null; /** * @class Storage */ class Storage { constructor(path = path_1.default.cwd() + '/' + '.storage') { this.m_prefix = ''; this.m_change = false; this.m_value = {}; this.m_path = path_1.default.fallbackPath(path); this.m_value = {}; if (haveWeb) { this.m_sync = { call: util_1.default.noop }; this.m_prefix = util_1.default.hash(this.m_path || 'default') + '_'; this.m_value = localStorage; } else { this.m_sync = new delay_call_1.DelayCall(e => sync_local(this), 100); // 100ms后保存到文件 if (fs.existsSync(this.m_path)) { try { this.m_value = JSON.parse(fs.readFileSync(this.m_path, 'utf-8')) || {}; } catch (e) { } } } } get(key, defaultValue) { key = format_key(this, key); if (key in this.m_value) { return paese_value(this.m_value[key]); } else { if (defaultValue !== undefined) { this.m_value[key] = stringify_val(defaultValue); commit(this); return defaultValue; } } } has(key) { key = format_key(this, key); return key in this.m_value; } set(key, value) { key = format_key(this, key); this.m_value[key] = stringify_val(value); commit(this); } del(key) { this.delete(key); } delete(key) { key = format_key(this, key); delete this.m_value[key]; commit(this); } clear() { if (haveWeb) { var keys = []; for (var i in this.m_value) { if (i.substr(0, this.m_prefix.length) == this.m_prefix) { keys.push(this.m_value); } } for (var key of keys) { delete this.m_value[key]; } } else { this.m_value = {}; } commit(this); } commit() { sync_local(this); } save() { this.commit(); } } exports.Storage = Storage; function _shared() { if (!shared) { shared = new Storage(); } return shared; } exports.default = { get shared() { return _shared(); }, setShared: function (value) { shared = value; }, get: function (key, defaultValue) { return _shared().get(key, defaultValue); }, has: function (key) { return _shared().has(key); }, set: function (key, value) { return _shared().set(key, value); }, del: function (key) { return _shared().delete(key); }, clear: function () { return _shared().clear(); }, save: function () { _shared().commit(); }, commit: function () { _shared().commit(); }, };