Files
med-notes/.pnpm-store/v10/files/49/a83f21daf159e6d09165e9331217061c5d471ebfa7d26fc008614e5d5d5f955fdd620e658f1ffcfadf3d53cf212aa1e198ee4f3773fcbcb16a879f986810d3
2025-05-09 05:30:08 +02:00

30 lines
1001 B
Plaintext

import { __flush } from "./scheduler.js";
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);
__flush(this);
};
this.prevState = initialState;
this.state = initialState;
this.options = options;
}
}
export {
Store
};
//# sourceMappingURL=store.js.map