@tanstack/store 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Store: () => Store
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+ var Store = class {
27
+ constructor(initialState, options) {
28
+ this.listeners = /* @__PURE__ */ new Set();
29
+ this._batching = false;
30
+ this._flushing = 0;
31
+ this._nextPriority = null;
32
+ this.subscribe = (listener) => {
33
+ var _a, _b;
34
+ this.listeners.add(listener);
35
+ const unsub = (_b = (_a = this.options) == null ? void 0 : _a.onSubscribe) == null ? void 0 : _b.call(_a, listener, this);
36
+ return () => {
37
+ this.listeners.delete(listener);
38
+ unsub == null ? void 0 : unsub();
39
+ };
40
+ };
41
+ this.setState = (updater, opts) => {
42
+ var _a, _b, _c, _d, _e;
43
+ const previous = this.state;
44
+ this.state = ((_a = this.options) == null ? void 0 : _a.updateFn) ? this.options.updateFn(previous)(updater) : updater(previous);
45
+ const priority = (opts == null ? void 0 : opts.priority) ?? ((_b = this.options) == null ? void 0 : _b.defaultPriority) ?? "high";
46
+ if (this._nextPriority === null) {
47
+ this._nextPriority = priority;
48
+ } else if (this._nextPriority === "high") {
49
+ this._nextPriority = priority;
50
+ } else {
51
+ this._nextPriority = ((_c = this.options) == null ? void 0 : _c.defaultPriority) ?? "high";
52
+ }
53
+ (_e = (_d = this.options) == null ? void 0 : _d.onUpdate) == null ? void 0 : _e.call(_d, {
54
+ priority: this._nextPriority
55
+ });
56
+ this._flush();
57
+ };
58
+ this._flush = () => {
59
+ if (this._batching)
60
+ return;
61
+ const flushId = ++this._flushing;
62
+ this.listeners.forEach((listener) => {
63
+ if (this._flushing !== flushId)
64
+ return;
65
+ listener({
66
+ priority: this._nextPriority ?? "high"
67
+ });
68
+ });
69
+ };
70
+ this.batch = (cb) => {
71
+ if (this._batching)
72
+ return cb();
73
+ this._batching = true;
74
+ cb();
75
+ this._batching = false;
76
+ this._flush();
77
+ };
78
+ this.state = initialState;
79
+ this.options = options;
80
+ }
81
+ };
82
+ // Annotate the CommonJS export names for ESM import in node:
83
+ 0 && (module.exports = {
84
+ Store
85
+ });
86
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = (opts: { priority: Priority }) => void\n\nexport type Priority = 'high' | 'low'\n\ninterface StoreOptions<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n updateFn?: (previous: TState) => (updater: TUpdater) => TState\n onSubscribe?: (\n listener: Listener,\n store: Store<TState, TUpdater>,\n ) => () => void\n onUpdate?: (opts: { priority: Priority }) => void\n defaultPriority?: Priority\n}\n\nexport class Store<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n listeners = new Set<Listener>()\n state: TState\n options?: StoreOptions<TState, TUpdater>\n _batching = false\n _flushing = 0\n _nextPriority: null | Priority = null\n\n constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {\n this.state = initialState\n this.options = options\n }\n\n subscribe = (listener: Listener) => {\n this.listeners.add(listener)\n const unsub = this.options?.onSubscribe?.(listener, this)\n return () => {\n this.listeners.delete(listener)\n unsub?.()\n }\n }\n\n setState = (\n updater: TUpdater,\n opts?: {\n priority: Priority\n },\n ) => {\n const previous = this.state\n this.state = this.options?.updateFn\n ? this.options.updateFn(previous)(updater)\n : (updater as any)(previous)\n\n const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high'\n if (this._nextPriority === null) {\n this._nextPriority = priority\n } else if (this._nextPriority === 'high') {\n this._nextPriority = priority\n } else {\n this._nextPriority = this.options?.defaultPriority ?? 'high'\n }\n\n // Always run onUpdate, regardless of batching\n this.options?.onUpdate?.({\n priority: this._nextPriority,\n })\n\n // Attempt to flush\n this._flush()\n }\n\n _flush = () => {\n if (this._batching) return\n const flushId = ++this._flushing\n this.listeners.forEach((listener) => {\n if (this._flushing !== flushId) return\n listener({\n priority: this._nextPriority ?? 'high',\n })\n })\n }\n\n batch = (cb: () => void) => {\n if (this._batching) return cb()\n this._batching = true\n cb()\n this._batching = false\n this._flush()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBO,IAAM,QAAN,MAGL;AAAA,EAQA,YAAY,cAAsB,SAA0C;AAP5E,qBAAY,oBAAI,IAAc;AAG9B,qBAAY;AACZ,qBAAY;AACZ,yBAAiC;AAOjC,qBAAY,CAAC,aAAuB;AAnCtC;AAoCI,WAAK,UAAU,IAAI,QAAQ;AAC3B,YAAM,SAAQ,gBAAK,YAAL,mBAAc,gBAAd,4BAA4B,UAAU;AACpD,aAAO,MAAM;AACX,aAAK,UAAU,OAAO,QAAQ;AAC9B;AAAA,MACF;AAAA,IACF;AAEA,oBAAW,CACT,SACA,SAGG;AAjDP;AAkDI,YAAM,WAAW,KAAK;AACtB,WAAK,UAAQ,UAAK,YAAL,mBAAc,YACvB,KAAK,QAAQ,SAAS,QAAQ,EAAE,OAAO,IACtC,QAAgB,QAAQ;AAE7B,YAAM,YAAW,6BAAM,eAAY,UAAK,YAAL,mBAAc,oBAAmB;AACpE,UAAI,KAAK,kBAAkB,MAAM;AAC/B,aAAK,gBAAgB;AAAA,MACvB,WAAW,KAAK,kBAAkB,QAAQ;AACxC,aAAK,gBAAgB;AAAA,MACvB,OAAO;AACL,aAAK,kBAAgB,UAAK,YAAL,mBAAc,oBAAmB;AAAA,MACxD;AAGA,uBAAK,YAAL,mBAAc,aAAd,4BAAyB;AAAA,QACvB,UAAU,KAAK;AAAA,MACjB;AAGA,WAAK,OAAO;AAAA,IACd;AAEA,kBAAS,MAAM;AACb,UAAI,KAAK;AAAW;AACpB,YAAM,UAAU,EAAE,KAAK;AACvB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,YAAI,KAAK,cAAc;AAAS;AAChC,iBAAS;AAAA,UACP,UAAU,KAAK,iBAAiB;AAAA,QAClC,CAAC;AAAA,MACH,CAAC;AAAA,IACH;AAEA,iBAAQ,CAAC,OAAmB;AAC1B,UAAI,KAAK;AAAW,eAAO,GAAG;AAC9B,WAAK,YAAY;AACjB,SAAG;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IACd;AA3DE,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EACjB;AA0DF;","names":[]}
@@ -0,0 +1,30 @@
1
+ type AnyUpdater = (...args: any[]) => any;
2
+ type Listener = (opts: {
3
+ priority: Priority;
4
+ }) => void;
5
+ type Priority = 'high' | 'low';
6
+ interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
7
+ updateFn?: (previous: TState) => (updater: TUpdater) => TState;
8
+ onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
9
+ onUpdate?: (opts: {
10
+ priority: Priority;
11
+ }) => void;
12
+ defaultPriority?: Priority;
13
+ }
14
+ declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
15
+ listeners: Set<Listener>;
16
+ state: TState;
17
+ options?: StoreOptions<TState, TUpdater>;
18
+ _batching: boolean;
19
+ _flushing: number;
20
+ _nextPriority: null | Priority;
21
+ constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>);
22
+ subscribe: (listener: Listener) => () => void;
23
+ setState: (updater: TUpdater, opts?: {
24
+ priority: Priority;
25
+ }) => void;
26
+ _flush: () => void;
27
+ batch: (cb: () => void) => void;
28
+ }
29
+
30
+ export { AnyUpdater, Listener, Priority, Store };
@@ -0,0 +1,30 @@
1
+ type AnyUpdater = (...args: any[]) => any;
2
+ type Listener = (opts: {
3
+ priority: Priority;
4
+ }) => void;
5
+ type Priority = 'high' | 'low';
6
+ interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
7
+ updateFn?: (previous: TState) => (updater: TUpdater) => TState;
8
+ onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
9
+ onUpdate?: (opts: {
10
+ priority: Priority;
11
+ }) => void;
12
+ defaultPriority?: Priority;
13
+ }
14
+ declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
15
+ listeners: Set<Listener>;
16
+ state: TState;
17
+ options?: StoreOptions<TState, TUpdater>;
18
+ _batching: boolean;
19
+ _flushing: number;
20
+ _nextPriority: null | Priority;
21
+ constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>);
22
+ subscribe: (listener: Listener) => () => void;
23
+ setState: (updater: TUpdater, opts?: {
24
+ priority: Priority;
25
+ }) => void;
26
+ _flush: () => void;
27
+ batch: (cb: () => void) => void;
28
+ }
29
+
30
+ export { AnyUpdater, Listener, Priority, Store };
@@ -0,0 +1,61 @@
1
+ // src/index.ts
2
+ var Store = class {
3
+ constructor(initialState, options) {
4
+ this.listeners = /* @__PURE__ */ new Set();
5
+ this._batching = false;
6
+ this._flushing = 0;
7
+ this._nextPriority = null;
8
+ this.subscribe = (listener) => {
9
+ var _a, _b;
10
+ this.listeners.add(listener);
11
+ const unsub = (_b = (_a = this.options) == null ? void 0 : _a.onSubscribe) == null ? void 0 : _b.call(_a, listener, this);
12
+ return () => {
13
+ this.listeners.delete(listener);
14
+ unsub == null ? void 0 : unsub();
15
+ };
16
+ };
17
+ this.setState = (updater, opts) => {
18
+ var _a, _b, _c, _d, _e;
19
+ const previous = this.state;
20
+ this.state = ((_a = this.options) == null ? void 0 : _a.updateFn) ? this.options.updateFn(previous)(updater) : updater(previous);
21
+ const priority = (opts == null ? void 0 : opts.priority) ?? ((_b = this.options) == null ? void 0 : _b.defaultPriority) ?? "high";
22
+ if (this._nextPriority === null) {
23
+ this._nextPriority = priority;
24
+ } else if (this._nextPriority === "high") {
25
+ this._nextPriority = priority;
26
+ } else {
27
+ this._nextPriority = ((_c = this.options) == null ? void 0 : _c.defaultPriority) ?? "high";
28
+ }
29
+ (_e = (_d = this.options) == null ? void 0 : _d.onUpdate) == null ? void 0 : _e.call(_d, {
30
+ priority: this._nextPriority
31
+ });
32
+ this._flush();
33
+ };
34
+ this._flush = () => {
35
+ if (this._batching)
36
+ return;
37
+ const flushId = ++this._flushing;
38
+ this.listeners.forEach((listener) => {
39
+ if (this._flushing !== flushId)
40
+ return;
41
+ listener({
42
+ priority: this._nextPriority ?? "high"
43
+ });
44
+ });
45
+ };
46
+ this.batch = (cb) => {
47
+ if (this._batching)
48
+ return cb();
49
+ this._batching = true;
50
+ cb();
51
+ this._batching = false;
52
+ this._flush();
53
+ };
54
+ this.state = initialState;
55
+ this.options = options;
56
+ }
57
+ };
58
+ export {
59
+ Store
60
+ };
61
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = (opts: { priority: Priority }) => void\n\nexport type Priority = 'high' | 'low'\n\ninterface StoreOptions<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n updateFn?: (previous: TState) => (updater: TUpdater) => TState\n onSubscribe?: (\n listener: Listener,\n store: Store<TState, TUpdater>,\n ) => () => void\n onUpdate?: (opts: { priority: Priority }) => void\n defaultPriority?: Priority\n}\n\nexport class Store<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n listeners = new Set<Listener>()\n state: TState\n options?: StoreOptions<TState, TUpdater>\n _batching = false\n _flushing = 0\n _nextPriority: null | Priority = null\n\n constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {\n this.state = initialState\n this.options = options\n }\n\n subscribe = (listener: Listener) => {\n this.listeners.add(listener)\n const unsub = this.options?.onSubscribe?.(listener, this)\n return () => {\n this.listeners.delete(listener)\n unsub?.()\n }\n }\n\n setState = (\n updater: TUpdater,\n opts?: {\n priority: Priority\n },\n ) => {\n const previous = this.state\n this.state = this.options?.updateFn\n ? this.options.updateFn(previous)(updater)\n : (updater as any)(previous)\n\n const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high'\n if (this._nextPriority === null) {\n this._nextPriority = priority\n } else if (this._nextPriority === 'high') {\n this._nextPriority = priority\n } else {\n this._nextPriority = this.options?.defaultPriority ?? 'high'\n }\n\n // Always run onUpdate, regardless of batching\n this.options?.onUpdate?.({\n priority: this._nextPriority,\n })\n\n // Attempt to flush\n this._flush()\n }\n\n _flush = () => {\n if (this._batching) return\n const flushId = ++this._flushing\n this.listeners.forEach((listener) => {\n if (this._flushing !== flushId) return\n listener({\n priority: this._nextPriority ?? 'high',\n })\n })\n }\n\n batch = (cb: () => void) => {\n if (this._batching) return cb()\n this._batching = true\n cb()\n this._batching = false\n this._flush()\n }\n}\n"],"mappings":";AAmBO,IAAM,QAAN,MAGL;AAAA,EAQA,YAAY,cAAsB,SAA0C;AAP5E,qBAAY,oBAAI,IAAc;AAG9B,qBAAY;AACZ,qBAAY;AACZ,yBAAiC;AAOjC,qBAAY,CAAC,aAAuB;AAnCtC;AAoCI,WAAK,UAAU,IAAI,QAAQ;AAC3B,YAAM,SAAQ,gBAAK,YAAL,mBAAc,gBAAd,4BAA4B,UAAU;AACpD,aAAO,MAAM;AACX,aAAK,UAAU,OAAO,QAAQ;AAC9B;AAAA,MACF;AAAA,IACF;AAEA,oBAAW,CACT,SACA,SAGG;AAjDP;AAkDI,YAAM,WAAW,KAAK;AACtB,WAAK,UAAQ,UAAK,YAAL,mBAAc,YACvB,KAAK,QAAQ,SAAS,QAAQ,EAAE,OAAO,IACtC,QAAgB,QAAQ;AAE7B,YAAM,YAAW,6BAAM,eAAY,UAAK,YAAL,mBAAc,oBAAmB;AACpE,UAAI,KAAK,kBAAkB,MAAM;AAC/B,aAAK,gBAAgB;AAAA,MACvB,WAAW,KAAK,kBAAkB,QAAQ;AACxC,aAAK,gBAAgB;AAAA,MACvB,OAAO;AACL,aAAK,kBAAgB,UAAK,YAAL,mBAAc,oBAAmB;AAAA,MACxD;AAGA,uBAAK,YAAL,mBAAc,aAAd,4BAAyB;AAAA,QACvB,UAAU,KAAK;AAAA,MACjB;AAGA,WAAK,OAAO;AAAA,IACd;AAEA,kBAAS,MAAM;AACb,UAAI,KAAK;AAAW;AACpB,YAAM,UAAU,EAAE,KAAK;AACvB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,YAAI,KAAK,cAAc;AAAS;AAChC,iBAAS;AAAA,UACP,UAAU,KAAK,iBAAiB;AAAA,QAClC,CAAC;AAAA,MACH,CAAC;AAAA,IACH;AAEA,iBAAQ,CAAC,OAAmB;AAC1B,UAAI,KAAK;AAAW,eAAO,GAAG;AAC9B,WAAK,YAAY;AACjB,SAAG;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IACd;AA3DE,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EACjB;AA0DF;","names":[]}
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Store: () => Store
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+ var Store = class {
27
+ constructor(initialState, options) {
28
+ this.listeners = /* @__PURE__ */ new Set();
29
+ this._batching = false;
30
+ this._flushing = 0;
31
+ this._nextPriority = null;
32
+ this.subscribe = (listener) => {
33
+ this.listeners.add(listener);
34
+ const unsub = this.options?.onSubscribe?.(listener, this);
35
+ return () => {
36
+ this.listeners.delete(listener);
37
+ unsub?.();
38
+ };
39
+ };
40
+ this.setState = (updater, opts) => {
41
+ const previous = this.state;
42
+ this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous);
43
+ const priority = opts?.priority ?? this.options?.defaultPriority ?? "high";
44
+ if (this._nextPriority === null) {
45
+ this._nextPriority = priority;
46
+ } else if (this._nextPriority === "high") {
47
+ this._nextPriority = priority;
48
+ } else {
49
+ this._nextPriority = this.options?.defaultPriority ?? "high";
50
+ }
51
+ this.options?.onUpdate?.({
52
+ priority: this._nextPriority
53
+ });
54
+ this._flush();
55
+ };
56
+ this._flush = () => {
57
+ if (this._batching)
58
+ return;
59
+ const flushId = ++this._flushing;
60
+ this.listeners.forEach((listener) => {
61
+ if (this._flushing !== flushId)
62
+ return;
63
+ listener({
64
+ priority: this._nextPriority ?? "high"
65
+ });
66
+ });
67
+ };
68
+ this.batch = (cb) => {
69
+ if (this._batching)
70
+ return cb();
71
+ this._batching = true;
72
+ cb();
73
+ this._batching = false;
74
+ this._flush();
75
+ };
76
+ this.state = initialState;
77
+ this.options = options;
78
+ }
79
+ };
80
+ // Annotate the CommonJS export names for ESM import in node:
81
+ 0 && (module.exports = {
82
+ Store
83
+ });
84
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = (opts: { priority: Priority }) => void\n\nexport type Priority = 'high' | 'low'\n\ninterface StoreOptions<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n updateFn?: (previous: TState) => (updater: TUpdater) => TState\n onSubscribe?: (\n listener: Listener,\n store: Store<TState, TUpdater>,\n ) => () => void\n onUpdate?: (opts: { priority: Priority }) => void\n defaultPriority?: Priority\n}\n\nexport class Store<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n listeners = new Set<Listener>()\n state: TState\n options?: StoreOptions<TState, TUpdater>\n _batching = false\n _flushing = 0\n _nextPriority: null | Priority = null\n\n constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {\n this.state = initialState\n this.options = options\n }\n\n subscribe = (listener: Listener) => {\n this.listeners.add(listener)\n const unsub = this.options?.onSubscribe?.(listener, this)\n return () => {\n this.listeners.delete(listener)\n unsub?.()\n }\n }\n\n setState = (\n updater: TUpdater,\n opts?: {\n priority: Priority\n },\n ) => {\n const previous = this.state\n this.state = this.options?.updateFn\n ? this.options.updateFn(previous)(updater)\n : (updater as any)(previous)\n\n const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high'\n if (this._nextPriority === null) {\n this._nextPriority = priority\n } else if (this._nextPriority === 'high') {\n this._nextPriority = priority\n } else {\n this._nextPriority = this.options?.defaultPriority ?? 'high'\n }\n\n // Always run onUpdate, regardless of batching\n this.options?.onUpdate?.({\n priority: this._nextPriority,\n })\n\n // Attempt to flush\n this._flush()\n }\n\n _flush = () => {\n if (this._batching) return\n const flushId = ++this._flushing\n this.listeners.forEach((listener) => {\n if (this._flushing !== flushId) return\n listener({\n priority: this._nextPriority ?? 'high',\n })\n })\n }\n\n batch = (cb: () => void) => {\n if (this._batching) return cb()\n this._batching = true\n cb()\n this._batching = false\n this._flush()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBO,IAAM,QAAN,MAGL;AAAA,EAQA,YAAY,cAAsB,SAA0C;AAP5E,qBAAY,oBAAI,IAAc;AAG9B,qBAAY;AACZ,qBAAY;AACZ,yBAAiC;AAOjC,qBAAY,CAAC,aAAuB;AAClC,WAAK,UAAU,IAAI,QAAQ;AAC3B,YAAM,QAAQ,KAAK,SAAS,cAAc,UAAU,IAAI;AACxD,aAAO,MAAM;AACX,aAAK,UAAU,OAAO,QAAQ;AAC9B,gBAAQ;AAAA,MACV;AAAA,IACF;AAEA,oBAAW,CACT,SACA,SAGG;AACH,YAAM,WAAW,KAAK;AACtB,WAAK,QAAQ,KAAK,SAAS,WACvB,KAAK,QAAQ,SAAS,QAAQ,EAAE,OAAO,IACtC,QAAgB,QAAQ;AAE7B,YAAM,WAAW,MAAM,YAAY,KAAK,SAAS,mBAAmB;AACpE,UAAI,KAAK,kBAAkB,MAAM;AAC/B,aAAK,gBAAgB;AAAA,MACvB,WAAW,KAAK,kBAAkB,QAAQ;AACxC,aAAK,gBAAgB;AAAA,MACvB,OAAO;AACL,aAAK,gBAAgB,KAAK,SAAS,mBAAmB;AAAA,MACxD;AAGA,WAAK,SAAS,WAAW;AAAA,QACvB,UAAU,KAAK;AAAA,MACjB,CAAC;AAGD,WAAK,OAAO;AAAA,IACd;AAEA,kBAAS,MAAM;AACb,UAAI,KAAK;AAAW;AACpB,YAAM,UAAU,EAAE,KAAK;AACvB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,YAAI,KAAK,cAAc;AAAS;AAChC,iBAAS;AAAA,UACP,UAAU,KAAK,iBAAiB;AAAA,QAClC,CAAC;AAAA,MACH,CAAC;AAAA,IACH;AAEA,iBAAQ,CAAC,OAAmB;AAC1B,UAAI,KAAK;AAAW,eAAO,GAAG;AAC9B,WAAK,YAAY;AACjB,SAAG;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IACd;AA3DE,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EACjB;AA0DF;","names":[]}
@@ -0,0 +1,30 @@
1
+ type AnyUpdater = (...args: any[]) => any;
2
+ type Listener = (opts: {
3
+ priority: Priority;
4
+ }) => void;
5
+ type Priority = 'high' | 'low';
6
+ interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
7
+ updateFn?: (previous: TState) => (updater: TUpdater) => TState;
8
+ onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
9
+ onUpdate?: (opts: {
10
+ priority: Priority;
11
+ }) => void;
12
+ defaultPriority?: Priority;
13
+ }
14
+ declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
15
+ listeners: Set<Listener>;
16
+ state: TState;
17
+ options?: StoreOptions<TState, TUpdater>;
18
+ _batching: boolean;
19
+ _flushing: number;
20
+ _nextPriority: null | Priority;
21
+ constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>);
22
+ subscribe: (listener: Listener) => () => void;
23
+ setState: (updater: TUpdater, opts?: {
24
+ priority: Priority;
25
+ }) => void;
26
+ _flush: () => void;
27
+ batch: (cb: () => void) => void;
28
+ }
29
+
30
+ export { AnyUpdater, Listener, Priority, Store };
@@ -0,0 +1,30 @@
1
+ type AnyUpdater = (...args: any[]) => any;
2
+ type Listener = (opts: {
3
+ priority: Priority;
4
+ }) => void;
5
+ type Priority = 'high' | 'low';
6
+ interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
7
+ updateFn?: (previous: TState) => (updater: TUpdater) => TState;
8
+ onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
9
+ onUpdate?: (opts: {
10
+ priority: Priority;
11
+ }) => void;
12
+ defaultPriority?: Priority;
13
+ }
14
+ declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
15
+ listeners: Set<Listener>;
16
+ state: TState;
17
+ options?: StoreOptions<TState, TUpdater>;
18
+ _batching: boolean;
19
+ _flushing: number;
20
+ _nextPriority: null | Priority;
21
+ constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>);
22
+ subscribe: (listener: Listener) => () => void;
23
+ setState: (updater: TUpdater, opts?: {
24
+ priority: Priority;
25
+ }) => void;
26
+ _flush: () => void;
27
+ batch: (cb: () => void) => void;
28
+ }
29
+
30
+ export { AnyUpdater, Listener, Priority, Store };
@@ -0,0 +1,59 @@
1
+ // src/index.ts
2
+ var Store = class {
3
+ constructor(initialState, options) {
4
+ this.listeners = /* @__PURE__ */ new Set();
5
+ this._batching = false;
6
+ this._flushing = 0;
7
+ this._nextPriority = null;
8
+ this.subscribe = (listener) => {
9
+ this.listeners.add(listener);
10
+ const unsub = this.options?.onSubscribe?.(listener, this);
11
+ return () => {
12
+ this.listeners.delete(listener);
13
+ unsub?.();
14
+ };
15
+ };
16
+ this.setState = (updater, opts) => {
17
+ const previous = this.state;
18
+ this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous);
19
+ const priority = opts?.priority ?? this.options?.defaultPriority ?? "high";
20
+ if (this._nextPriority === null) {
21
+ this._nextPriority = priority;
22
+ } else if (this._nextPriority === "high") {
23
+ this._nextPriority = priority;
24
+ } else {
25
+ this._nextPriority = this.options?.defaultPriority ?? "high";
26
+ }
27
+ this.options?.onUpdate?.({
28
+ priority: this._nextPriority
29
+ });
30
+ this._flush();
31
+ };
32
+ this._flush = () => {
33
+ if (this._batching)
34
+ return;
35
+ const flushId = ++this._flushing;
36
+ this.listeners.forEach((listener) => {
37
+ if (this._flushing !== flushId)
38
+ return;
39
+ listener({
40
+ priority: this._nextPriority ?? "high"
41
+ });
42
+ });
43
+ };
44
+ this.batch = (cb) => {
45
+ if (this._batching)
46
+ return cb();
47
+ this._batching = true;
48
+ cb();
49
+ this._batching = false;
50
+ this._flush();
51
+ };
52
+ this.state = initialState;
53
+ this.options = options;
54
+ }
55
+ };
56
+ export {
57
+ Store
58
+ };
59
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = (opts: { priority: Priority }) => void\n\nexport type Priority = 'high' | 'low'\n\ninterface StoreOptions<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n updateFn?: (previous: TState) => (updater: TUpdater) => TState\n onSubscribe?: (\n listener: Listener,\n store: Store<TState, TUpdater>,\n ) => () => void\n onUpdate?: (opts: { priority: Priority }) => void\n defaultPriority?: Priority\n}\n\nexport class Store<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n listeners = new Set<Listener>()\n state: TState\n options?: StoreOptions<TState, TUpdater>\n _batching = false\n _flushing = 0\n _nextPriority: null | Priority = null\n\n constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {\n this.state = initialState\n this.options = options\n }\n\n subscribe = (listener: Listener) => {\n this.listeners.add(listener)\n const unsub = this.options?.onSubscribe?.(listener, this)\n return () => {\n this.listeners.delete(listener)\n unsub?.()\n }\n }\n\n setState = (\n updater: TUpdater,\n opts?: {\n priority: Priority\n },\n ) => {\n const previous = this.state\n this.state = this.options?.updateFn\n ? this.options.updateFn(previous)(updater)\n : (updater as any)(previous)\n\n const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high'\n if (this._nextPriority === null) {\n this._nextPriority = priority\n } else if (this._nextPriority === 'high') {\n this._nextPriority = priority\n } else {\n this._nextPriority = this.options?.defaultPriority ?? 'high'\n }\n\n // Always run onUpdate, regardless of batching\n this.options?.onUpdate?.({\n priority: this._nextPriority,\n })\n\n // Attempt to flush\n this._flush()\n }\n\n _flush = () => {\n if (this._batching) return\n const flushId = ++this._flushing\n this.listeners.forEach((listener) => {\n if (this._flushing !== flushId) return\n listener({\n priority: this._nextPriority ?? 'high',\n })\n })\n }\n\n batch = (cb: () => void) => {\n if (this._batching) return cb()\n this._batching = true\n cb()\n this._batching = false\n this._flush()\n }\n}\n"],"mappings":";AAmBO,IAAM,QAAN,MAGL;AAAA,EAQA,YAAY,cAAsB,SAA0C;AAP5E,qBAAY,oBAAI,IAAc;AAG9B,qBAAY;AACZ,qBAAY;AACZ,yBAAiC;AAOjC,qBAAY,CAAC,aAAuB;AAClC,WAAK,UAAU,IAAI,QAAQ;AAC3B,YAAM,QAAQ,KAAK,SAAS,cAAc,UAAU,IAAI;AACxD,aAAO,MAAM;AACX,aAAK,UAAU,OAAO,QAAQ;AAC9B,gBAAQ;AAAA,MACV;AAAA,IACF;AAEA,oBAAW,CACT,SACA,SAGG;AACH,YAAM,WAAW,KAAK;AACtB,WAAK,QAAQ,KAAK,SAAS,WACvB,KAAK,QAAQ,SAAS,QAAQ,EAAE,OAAO,IACtC,QAAgB,QAAQ;AAE7B,YAAM,WAAW,MAAM,YAAY,KAAK,SAAS,mBAAmB;AACpE,UAAI,KAAK,kBAAkB,MAAM;AAC/B,aAAK,gBAAgB;AAAA,MACvB,WAAW,KAAK,kBAAkB,QAAQ;AACxC,aAAK,gBAAgB;AAAA,MACvB,OAAO;AACL,aAAK,gBAAgB,KAAK,SAAS,mBAAmB;AAAA,MACxD;AAGA,WAAK,SAAS,WAAW;AAAA,QACvB,UAAU,KAAK;AAAA,MACjB,CAAC;AAGD,WAAK,OAAO;AAAA,IACd;AAEA,kBAAS,MAAM;AACb,UAAI,KAAK;AAAW;AACpB,YAAM,UAAU,EAAE,KAAK;AACvB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,YAAI,KAAK,cAAc;AAAS;AAChC,iBAAS;AAAA,UACP,UAAU,KAAK,iBAAiB;AAAA,QAClC,CAAC;AAAA,MACH,CAAC;AAAA,IACH;AAEA,iBAAQ,CAAC,OAAmB;AAC1B,UAAI,KAAK;AAAW,eAAO,GAAG;AAC9B,WAAK,YAAY;AACjB,SAAG;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IACd;AA3DE,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EACjB;AA0DF;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/store",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.1.1",
4
+ "version": "0.1.3",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/store",
7
7
  "homepage": "https://tanstack.com/store",