UNPKG

@tanstack/store

Version:

Framework agnostic type-safe store w/ reactive framework adapters

39 lines (38 loc) 1.26 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const scheduler = require("./scheduler.cjs"); const types = require("./types.cjs"); class Store { constructor(initialState, options) { this.listeners = /* @__PURE__ */ new Set(); this.subscribe = (listener) => { var _a, _b; this.listeners.add(listener); const unsub = (_b = (_a = this.options) == null ? void 0 : _a.onSubscribe) == null ? void 0 : _b.call(_a, listener, this); return () => { this.listeners.delete(listener); unsub == null ? void 0 : unsub(); }; }; this.prevState = initialState; this.state = initialState; this.options = options; } setState(updater) { var _a, _b, _c; this.prevState = this.state; if ((_a = this.options) == null ? void 0 : _a.updateFn) { this.state = this.options.updateFn(this.prevState)(updater); } else { if (types.isUpdaterFunction(updater)) { this.state = updater(this.prevState); } else { this.state = updater; } } (_c = (_b = this.options) == null ? void 0 : _b.onUpdate) == null ? void 0 : _c.call(_b); scheduler.__flush(this); } } exports.Store = Store; //# sourceMappingURL=store.cjs.map