UNPKG

opendash

Version:

open.DASH data visualization framework

56 lines 1.85 kB
import produce from "immer"; var Store = /** @class */ (function () { function Store(initialState) { this.watcher = new Set(); this.selectorCache = new Map(); this._state = initialState; } Store.prototype.state = function () { return this._state; }; Store.prototype.select = function (selector) { return selector(this._state); }; Store.prototype.update = function (callbackOrState) { if (typeof callbackOrState === "function") { this._state = produce(this._state, callbackOrState); } else { this._state = callbackOrState; } this.watcher.forEach(function (callback) { try { callback(); } catch (error) { console.error("Error in Store subscription callback:", error); } }); }; Store.prototype.subscribe = function (callback) { var _this = this; this.watcher.add(callback); return function () { _this.watcher.delete(callback); }; }; Store.prototype.subscribeSelection = function (selector, callback) { var _this = this; var selectorCallback = function () { var cache = _this.selectorCache.get(selector); var selection = selector(_this._state); if (!cache || cache !== selection) { _this.selectorCache.set(selector, selection); callback(); } }; this.watcher.add(selectorCallback); return function () { _this.watcher.delete(selectorCallback); _this.selectorCache.delete(selector); }; }; return Store; }()); export { Store }; //# sourceMappingURL=Store.js.map