UNPKG

@awayjs/core

Version:
142 lines (141 loc) 5.23 kB
import { __extends } from "tslib"; import { EventBase } from '../events/EventBase'; import { EventDispatcher } from '../events/EventDispatcher'; var StoreEvent = /** @class */ (function (_super) { __extends(StoreEvent, _super); function StoreEvent(type, storeName, propName, newValue, oldValue, store) { var _this = _super.call(this, type) || this; _this.storeName = storeName; _this.propName = propName; _this.newValue = newValue; _this.oldValue = oldValue; _this.store = store; return _this; } return StoreEvent; }(EventBase)); export { StoreEvent }; var ConfigManager = /** @class */ (function (_super) { __extends(ConfigManager, _super); function ConfigManager() { var _this = _super !== null && _super.apply(this, arguments) || this; _this._proxies = Object.create(null); _this._lib = Object.create(null); return _this; } Object.defineProperty(ConfigManager, "instance", { get: function () { return this._instance || (this._instance = new ConfigManager()); }, enumerable: false, configurable: true }); ConfigManager.prototype.propertyChange = function (storeName, prop, oldValue, newValue, setter) { if (setter === void 0) { setter = false; } if (setter) { // eslint-disable-next-line max-len console.debug("[ConfigManager] Setters is deprecated, use 'set('".concat(storeName, "', '").concat(prop, "', ").concat(newValue, ") instead'")); } this.dispatchEvent(new StoreEvent(ConfigManager.CHANGE_EVENT, storeName, prop, oldValue, newValue, this._lib[storeName])); }; ConfigManager.prototype.wrapObject = function (storeName, obj) { var root = this; return new Proxy(obj, { set: function (target, p, value, receiver) { if (target[p] === value) { return true; } root.propertyChange(storeName, p, target[p], value, true); target[p] = value; return true; } }); }; /** * Return referenced object of store. * NOTE! Direct modification of it property will emit warning * @param name */ ConfigManager.prototype.getStoreRef = function (name) { return this._proxies[name]; }; /** * Similar `set` but register store from object and merge values if already exist, return observabled object * @param name * @param obj */ ConfigManager.prototype.addStore = function (name, obj) { if (name in this._lib) { Object.assign(this._lib[name], obj); return this._proxies[name]; } return this._proxies[name] = this.wrapObject(name, this._lib[name] = obj); }; /** * Set specific value or series of values to store, emit changeEvent when value changes * @param storeName * @param objOrName * @param valueOrNone */ ConfigManager.prototype.set = function (storeName, objOrName, valueOrNone) { var _a; if (!(storeName in this._lib)) { this.addStore(storeName, {}); } var obj = typeof objOrName === 'string' ? (_a = {}, _a[objOrName] = valueOrNone, _a) : objOrName; var store = this._lib[storeName]; var change = false; for (var keyName in obj) { var oldVal = store[keyName]; var newVal = obj[keyName]; store[keyName] = newVal; change = change || newVal !== oldVal; this.propertyChange(storeName, keyName, oldVal, newVal, false); } return change; }; /** * Get stored value from specific store by * @param storeName * @param key */ ConfigManager.prototype.get = function (storeName, key) { var _a; return (_a = this._lib[storeName]) === null || _a === void 0 ? void 0 : _a[key]; }; /** * Serialize store to JSON */ ConfigManager.prototype.serialize = function () { return JSON.stringify(this._lib); }; /** * Deserialize data to current active manager * @param jsonObject * @param quiet - no emit change event, only create stores */ ConfigManager.prototype.deserialize = function (jsonObject, quiet) { if (quiet === void 0) { quiet = true; } jsonObject = typeof jsonObject === 'string' ? JSON.parse(jsonObject) : jsonObject; var manager = this; for (var key in jsonObject) { var value = jsonObject[key]; if (!value || typeof value !== 'object' || !jsonObject.hasOwnProperty(key)) { continue; } if (quiet) { manager.addStore(key, value); } else { manager.set(key, value); } } }; ConfigManager.deserialize = function (jsonObject, quiet) { if (quiet === void 0) { quiet = true; } return ConfigManager.instance.deserialize(jsonObject, quiet); }; ConfigManager.CHANGE_EVENT = 'change'; return ConfigManager; }(EventDispatcher)); export { ConfigManager };