@tanstack/store 0.5.0 → 0.5.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.
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -3
- package/dist/esm/index.d.ts +2 -3
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -2
- package/src/tests/index.test.tsx +0 -83
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: Array<any>) => any\n\nexport type Listener = () => void\n\nexport interface 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?: () => 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 _batching = false\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 _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":";;AAgBO,MAAM,MAGX;AAAA,EAOA,YAAY,cAAsB,SAA0C;AAN5E,SAAA,gCAAgB;AAGJ,SAAA,YAAA;AACA,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,MAAA;AAAA,IACV;AAGF,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,IAAA;AAGd,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;MAAA,CACV;AAAA,IAAA;AAGH,SAAA,QAAQ,CAAC,OAAmB;AACtB,UAAA,KAAK,UAAW,QAAO;AAC3B,WAAK,YAAY;AACd;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IAAA;AAxCZ,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EACjB;AAwCF;;"}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export type AnyUpdater = (...args: any
|
|
1
|
+
export type AnyUpdater = (...args: Array<any>) => any;
|
|
2
2
|
export type Listener = () => void;
|
|
3
|
-
interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
3
|
+
export interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
4
4
|
updateFn?: (previous: TState) => (updater: TUpdater) => TState;
|
|
5
5
|
onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
|
|
6
6
|
onUpdate?: () => void;
|
|
@@ -17,4 +17,3 @@ export declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) =>
|
|
|
17
17
|
_flush: () => void;
|
|
18
18
|
batch: (cb: () => void) => void;
|
|
19
19
|
}
|
|
20
|
-
export {};
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export type AnyUpdater = (...args: any
|
|
1
|
+
export type AnyUpdater = (...args: Array<any>) => any;
|
|
2
2
|
export type Listener = () => void;
|
|
3
|
-
interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
3
|
+
export interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
4
4
|
updateFn?: (previous: TState) => (updater: TUpdater) => TState;
|
|
5
5
|
onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
|
|
6
6
|
onUpdate?: () => void;
|
|
@@ -17,4 +17,3 @@ export declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) =>
|
|
|
17
17
|
_flush: () => void;
|
|
18
18
|
batch: (cb: () => void) => void;
|
|
19
19
|
}
|
|
20
|
-
export {};
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: Array<any>) => any\n\nexport type Listener = () => void\n\nexport interface 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?: () => 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 _batching = false\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 _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":"AAgBO,MAAM,MAGX;AAAA,EAOA,YAAY,cAAsB,SAA0C;AAN5E,SAAA,gCAAgB;AAGJ,SAAA,YAAA;AACA,SAAA,YAAA;AAOZ,SAAA,YAAY,CAAC,aAAuB;AAf/B;AAgBE,WAAA,UAAU,IAAI,QAAQ;AAC3B,YAAM,SAAQ,gBAAK,YAAL,mBAAc,gBAAd,4BAA4B,UAAU;AACpD,aAAO,MAAM;AACN,aAAA,UAAU,OAAO,QAAQ;AACtB;AAAA,MAAA;AAAA,IACV;AAGF,SAAA,WAAW,CAAC,YAAsB;AAxB7B;AAyBH,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,IAAA;AAGd,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;MAAA,CACV;AAAA,IAAA;AAGH,SAAA,QAAQ,CAAC,OAAmB;AACtB,UAAA,KAAK,UAAW,QAAO;AAC3B,WAAK,YAAY;AACd;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IAAA;AAxCZ,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EACjB;AAwCF;"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export type AnyUpdater = (...args: any
|
|
1
|
+
export type AnyUpdater = (...args: Array<any>) => any
|
|
2
2
|
|
|
3
3
|
export type Listener = () => void
|
|
4
4
|
|
|
5
|
-
interface StoreOptions<
|
|
5
|
+
export interface StoreOptions<
|
|
6
6
|
TState,
|
|
7
7
|
TUpdater extends AnyUpdater = (cb: TState) => TState,
|
|
8
8
|
> {
|
package/src/tests/index.test.tsx
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test, vi } from 'vitest'
|
|
2
|
-
import { Store } from '../index'
|
|
3
|
-
|
|
4
|
-
describe('store', () => {
|
|
5
|
-
test(`should set the initial value`, () => {
|
|
6
|
-
const store = new Store(0)
|
|
7
|
-
|
|
8
|
-
expect(store.state).toEqual(0)
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
test(`basic subscriptions should work`, () => {
|
|
12
|
-
const store = new Store(0)
|
|
13
|
-
|
|
14
|
-
const subscription = vi.fn()
|
|
15
|
-
|
|
16
|
-
const unsub = store.subscribe(subscription)
|
|
17
|
-
|
|
18
|
-
store.setState(() => 1)
|
|
19
|
-
|
|
20
|
-
expect(store.state).toEqual(1)
|
|
21
|
-
expect(subscription).toHaveBeenCalled()
|
|
22
|
-
|
|
23
|
-
unsub()
|
|
24
|
-
|
|
25
|
-
store.setState(() => 2)
|
|
26
|
-
|
|
27
|
-
expect(store.state).toEqual(2)
|
|
28
|
-
|
|
29
|
-
expect(subscription).toHaveBeenCalledTimes(1)
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
test(`setState passes previous state`, () => {
|
|
33
|
-
const store = new Store(3)
|
|
34
|
-
|
|
35
|
-
store.setState((v) => v + 1)
|
|
36
|
-
|
|
37
|
-
expect(store.state).toEqual(4)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
test(`updateFn acts as state transformer`, () => {
|
|
41
|
-
const store = new Store(1, {
|
|
42
|
-
updateFn: (v) => (updater) => Number(updater(v)),
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
store.setState((v) => `${v + 1}` as never)
|
|
46
|
-
|
|
47
|
-
expect(store.state).toEqual(2)
|
|
48
|
-
|
|
49
|
-
store.setState((v) => `${v + 2}` as never)
|
|
50
|
-
|
|
51
|
-
expect(store.state).toEqual(4)
|
|
52
|
-
|
|
53
|
-
expect(typeof store.state).toEqual('number')
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
test('Batch prevents listeners from being called during repeated setStates', () => {
|
|
57
|
-
const store = new Store(0)
|
|
58
|
-
|
|
59
|
-
const listener = vi.fn()
|
|
60
|
-
|
|
61
|
-
store.subscribe(listener)
|
|
62
|
-
|
|
63
|
-
store.batch(() => {
|
|
64
|
-
store.setState(() => 1)
|
|
65
|
-
store.setState(() => 2)
|
|
66
|
-
store.setState(() => 3)
|
|
67
|
-
store.setState(() => 4)
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
expect(store.state).toEqual(4)
|
|
71
|
-
// Listener is only called once because of batching
|
|
72
|
-
expect(listener).toHaveBeenCalledTimes(1)
|
|
73
|
-
|
|
74
|
-
store.setState(() => 1)
|
|
75
|
-
store.setState(() => 2)
|
|
76
|
-
store.setState(() => 3)
|
|
77
|
-
store.setState(() => 4)
|
|
78
|
-
|
|
79
|
-
expect(store.state).toEqual(4)
|
|
80
|
-
// Listener is called 4 times because of a lack of batching
|
|
81
|
-
expect(listener).toHaveBeenCalledTimes(5)
|
|
82
|
-
})
|
|
83
|
-
})
|