30 lines
1.1 KiB
Plaintext
30 lines
1.1 KiB
Plaintext
"use strict";
|
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
const scheduler = require("./scheduler.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.setState = (updater) => {
|
|
var _a, _b, _c;
|
|
this.prevState = this.state;
|
|
this.state = ((_a = this.options) == null ? void 0 : _a.updateFn) ? this.options.updateFn(this.prevState)(updater) : updater(this.prevState);
|
|
(_c = (_b = this.options) == null ? void 0 : _b.onUpdate) == null ? void 0 : _c.call(_b);
|
|
scheduler.__flush(this);
|
|
};
|
|
this.prevState = initialState;
|
|
this.state = initialState;
|
|
this.options = options;
|
|
}
|
|
}
|
|
exports.Store = Store;
|
|
//# sourceMappingURL=store.cjs.map
|