@tanstack/store 0.5.4 → 0.6.0
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.
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +26 -0
- package/dist/esm/index.d.ts +26 -0
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +26 -0
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["/**\n * @private\n */\nexport type AnyUpdater = (...args: Array<any>) => any\n\n/**\n * @private\n */\nexport type Listener = () => void\n\nexport interface StoreOptions<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n /**\n * Replace the default update function with a custom one.\n */\n updateFn?: (previous: TState) => (updater: TUpdater) => TState\n /**\n * Called when a listener subscribes to the store.\n *\n * @return a function to unsubscribe the listener\n */\n onSubscribe?: (\n listener: Listener,\n store: Store<TState, TUpdater>,\n ) => () => void\n /**\n * Called after the state has been updated, used to derive other state.\n */\n onUpdate?: () => void\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 /**\n * @private\n */\n _batching = false\n /**\n * @private\n */\n _flushing = 0\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 = (updater: TUpdater) => {\n const previous = this.state\n this.state = this.options?.updateFn\n ? this.options.updateFn(previous)(updater)\n : (updater as any)(previous)\n\n // Always run onUpdate, regardless of batching\n this.options?.onUpdate?.()\n\n // Attempt to flush\n this._flush()\n }\n\n /**\n * @private\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 })\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"],"names":[],"mappings":";;AAiCO,MAAM,MAGX;AAAA,EAaA,YAAY,cAAsB,SAA0C;AAZ5E,SAAA,gCAAgB,IAAc;AAMlB,SAAA,YAAA;AAIA,SAAA,YAAA;AAOZ,SAAA,YAAY,CAAC,aAAuB;;AAC7B,WAAA,UAAU,IAAI,QAAQ;AAC3B,YAAM,SAAQ,gBAAK,YAAL,mBAAc,gBAAd,4BAA4B,UAAU;AACpD,aAAO,MAAM;AACN,aAAA,UAAU,OAAO,QAAQ;AACtB;AAAA,MACV;AAAA,IACF;AAEA,SAAA,WAAW,CAAC,YAAsB;;AAChC,YAAM,WAAW,KAAK;AACtB,WAAK,UAAQ,UAAK,YAAL,mBAAc,YACvB,KAAK,QAAQ,SAAS,QAAQ,EAAE,OAAO,IACtC,QAAgB,QAAQ;AAG7B,uBAAK,YAAL,mBAAc,aAAd;AAGA,WAAK,OAAO;AAAA,IACd;AAKA,SAAA,SAAS,MAAM;AACb,UAAI,KAAK,UAAW;AACd,YAAA,UAAU,EAAE,KAAK;AAClB,WAAA,UAAU,QAAQ,CAAC,aAAa;AAC/B,YAAA,KAAK,cAAc,QAAS;AACvB,iBAAA;AAAA,MAAA,CACV;AAAA,IACH;AAEA,SAAA,QAAQ,CAAC,OAAmB;AACtB,UAAA,KAAK,UAAW,QAAO,GAAG;AAC9B,WAAK,YAAY;AACd,SAAA;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IACd;AA5CE,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EAAA;AA4CnB;;"}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -1,19 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private
|
|
3
|
+
*/
|
|
1
4
|
export type AnyUpdater = (...args: Array<any>) => any;
|
|
5
|
+
/**
|
|
6
|
+
* @private
|
|
7
|
+
*/
|
|
2
8
|
export type Listener = () => void;
|
|
3
9
|
export interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
10
|
+
/**
|
|
11
|
+
* Replace the default update function with a custom one.
|
|
12
|
+
*/
|
|
4
13
|
updateFn?: (previous: TState) => (updater: TUpdater) => TState;
|
|
14
|
+
/**
|
|
15
|
+
* Called when a listener subscribes to the store.
|
|
16
|
+
*
|
|
17
|
+
* @return a function to unsubscribe the listener
|
|
18
|
+
*/
|
|
5
19
|
onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
|
|
20
|
+
/**
|
|
21
|
+
* Called after the state has been updated, used to derive other state.
|
|
22
|
+
*/
|
|
6
23
|
onUpdate?: () => void;
|
|
7
24
|
}
|
|
8
25
|
export declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
9
26
|
listeners: Set<Listener>;
|
|
10
27
|
state: TState;
|
|
11
28
|
options?: StoreOptions<TState, TUpdater>;
|
|
29
|
+
/**
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
12
32
|
_batching: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* @private
|
|
35
|
+
*/
|
|
13
36
|
_flushing: number;
|
|
14
37
|
constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>);
|
|
15
38
|
subscribe: (listener: Listener) => () => void;
|
|
16
39
|
setState: (updater: TUpdater) => void;
|
|
40
|
+
/**
|
|
41
|
+
* @private
|
|
42
|
+
*/
|
|
17
43
|
_flush: () => void;
|
|
18
44
|
batch: (cb: () => void) => void;
|
|
19
45
|
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,19 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private
|
|
3
|
+
*/
|
|
1
4
|
export type AnyUpdater = (...args: Array<any>) => any;
|
|
5
|
+
/**
|
|
6
|
+
* @private
|
|
7
|
+
*/
|
|
2
8
|
export type Listener = () => void;
|
|
3
9
|
export interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
10
|
+
/**
|
|
11
|
+
* Replace the default update function with a custom one.
|
|
12
|
+
*/
|
|
4
13
|
updateFn?: (previous: TState) => (updater: TUpdater) => TState;
|
|
14
|
+
/**
|
|
15
|
+
* Called when a listener subscribes to the store.
|
|
16
|
+
*
|
|
17
|
+
* @return a function to unsubscribe the listener
|
|
18
|
+
*/
|
|
5
19
|
onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
|
|
20
|
+
/**
|
|
21
|
+
* Called after the state has been updated, used to derive other state.
|
|
22
|
+
*/
|
|
6
23
|
onUpdate?: () => void;
|
|
7
24
|
}
|
|
8
25
|
export declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
9
26
|
listeners: Set<Listener>;
|
|
10
27
|
state: TState;
|
|
11
28
|
options?: StoreOptions<TState, TUpdater>;
|
|
29
|
+
/**
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
12
32
|
_batching: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* @private
|
|
35
|
+
*/
|
|
13
36
|
_flushing: number;
|
|
14
37
|
constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>);
|
|
15
38
|
subscribe: (listener: Listener) => () => void;
|
|
16
39
|
setState: (updater: TUpdater) => void;
|
|
40
|
+
/**
|
|
41
|
+
* @private
|
|
42
|
+
*/
|
|
17
43
|
_flush: () => void;
|
|
18
44
|
batch: (cb: () => void) => void;
|
|
19
45
|
}
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["/**\n * @private\n */\nexport type AnyUpdater = (...args: Array<any>) => any\n\n/**\n * @private\n */\nexport type Listener = () => void\n\nexport interface StoreOptions<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n /**\n * Replace the default update function with a custom one.\n */\n updateFn?: (previous: TState) => (updater: TUpdater) => TState\n /**\n * Called when a listener subscribes to the store.\n *\n * @return a function to unsubscribe the listener\n */\n onSubscribe?: (\n listener: Listener,\n store: Store<TState, TUpdater>,\n ) => () => void\n /**\n * Called after the state has been updated, used to derive other state.\n */\n onUpdate?: () => void\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 /**\n * @private\n */\n _batching = false\n /**\n * @private\n */\n _flushing = 0\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 = (updater: TUpdater) => {\n const previous = this.state\n this.state = this.options?.updateFn\n ? this.options.updateFn(previous)(updater)\n : (updater as any)(previous)\n\n // Always run onUpdate, regardless of batching\n this.options?.onUpdate?.()\n\n // Attempt to flush\n this._flush()\n }\n\n /**\n * @private\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 })\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"],"names":[],"mappings":"AAiCO,MAAM,MAGX;AAAA,EAaA,YAAY,cAAsB,SAA0C;AAZ5E,SAAA,gCAAgB,IAAc;AAMlB,SAAA,YAAA;AAIA,SAAA,YAAA;AAOZ,SAAA,YAAY,CAAC,aAAuB;AArB/B;AAsBE,WAAA,UAAU,IAAI,QAAQ;AAC3B,YAAM,SAAQ,gBAAK,YAAL,mBAAc,gBAAd,4BAA4B,UAAU;AACpD,aAAO,MAAM;AACN,aAAA,UAAU,OAAO,QAAQ;AACtB;AAAA,MACV;AAAA,IACF;AAEA,SAAA,WAAW,CAAC,YAAsB;AA9B7B;AA+BH,YAAM,WAAW,KAAK;AACtB,WAAK,UAAQ,UAAK,YAAL,mBAAc,YACvB,KAAK,QAAQ,SAAS,QAAQ,EAAE,OAAO,IACtC,QAAgB,QAAQ;AAG7B,uBAAK,YAAL,mBAAc,aAAd;AAGA,WAAK,OAAO;AAAA,IACd;AAKA,SAAA,SAAS,MAAM;AACb,UAAI,KAAK,UAAW;AACd,YAAA,UAAU,EAAE,KAAK;AAClB,WAAA,UAAU,QAAQ,CAAC,aAAa;AAC/B,YAAA,KAAK,cAAc,QAAS;AACvB,iBAAA;AAAA,MAAA,CACV;AAAA,IACH;AAEA,SAAA,QAAQ,CAAC,OAAmB;AACtB,UAAA,KAAK,UAAW,QAAO,GAAG;AAC9B,WAAK,YAAY;AACd,SAAA;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IACd;AA5CE,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EAAA;AA4CnB;"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,16 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private
|
|
3
|
+
*/
|
|
1
4
|
export type AnyUpdater = (...args: Array<any>) => any
|
|
2
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @private
|
|
8
|
+
*/
|
|
3
9
|
export type Listener = () => void
|
|
4
10
|
|
|
5
11
|
export interface StoreOptions<
|
|
6
12
|
TState,
|
|
7
13
|
TUpdater extends AnyUpdater = (cb: TState) => TState,
|
|
8
14
|
> {
|
|
15
|
+
/**
|
|
16
|
+
* Replace the default update function with a custom one.
|
|
17
|
+
*/
|
|
9
18
|
updateFn?: (previous: TState) => (updater: TUpdater) => TState
|
|
19
|
+
/**
|
|
20
|
+
* Called when a listener subscribes to the store.
|
|
21
|
+
*
|
|
22
|
+
* @return a function to unsubscribe the listener
|
|
23
|
+
*/
|
|
10
24
|
onSubscribe?: (
|
|
11
25
|
listener: Listener,
|
|
12
26
|
store: Store<TState, TUpdater>,
|
|
13
27
|
) => () => void
|
|
28
|
+
/**
|
|
29
|
+
* Called after the state has been updated, used to derive other state.
|
|
30
|
+
*/
|
|
14
31
|
onUpdate?: () => void
|
|
15
32
|
}
|
|
16
33
|
|
|
@@ -21,7 +38,13 @@ export class Store<
|
|
|
21
38
|
listeners = new Set<Listener>()
|
|
22
39
|
state: TState
|
|
23
40
|
options?: StoreOptions<TState, TUpdater>
|
|
41
|
+
/**
|
|
42
|
+
* @private
|
|
43
|
+
*/
|
|
24
44
|
_batching = false
|
|
45
|
+
/**
|
|
46
|
+
* @private
|
|
47
|
+
*/
|
|
25
48
|
_flushing = 0
|
|
26
49
|
|
|
27
50
|
constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {
|
|
@@ -51,6 +74,9 @@ export class Store<
|
|
|
51
74
|
this._flush()
|
|
52
75
|
}
|
|
53
76
|
|
|
77
|
+
/**
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
54
80
|
_flush = () => {
|
|
55
81
|
if (this._batching) return
|
|
56
82
|
const flushId = ++this._flushing
|