@tanstack/store 0.5.2 → 0.5.4
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 +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js.map +1 -1
- package/package.json +10 -9
- package/src/index.ts +1 -1
- 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,4 +1,4 @@
|
|
|
1
|
-
export type AnyUpdater = (...args: any
|
|
1
|
+
export type AnyUpdater = (...args: Array<any>) => any;
|
|
2
2
|
export type Listener = () => void;
|
|
3
3
|
export interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
4
4
|
updateFn?: (previous: TState) => (updater: TUpdater) => TState;
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type AnyUpdater = (...args: any
|
|
1
|
+
export type AnyUpdater = (...args: Array<any>) => any;
|
|
2
2
|
export type Listener = () => void;
|
|
3
3
|
export interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
4
4
|
updateFn?: (previous: TState) => (updater: TUpdater) => TState;
|
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
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/store",
|
|
3
|
+
"version": "0.5.4",
|
|
4
|
+
"description": "Framework agnostic type-safe store w/ reactive framework adapters",
|
|
3
5
|
"author": "Tanner Linsley",
|
|
4
|
-
"version": "0.5.2",
|
|
5
6
|
"license": "MIT",
|
|
6
|
-
"repository":
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/TanStack/store.git",
|
|
10
|
+
"directory": "packages/store"
|
|
11
|
+
},
|
|
7
12
|
"homepage": "https://tanstack.com/store",
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
"
|
|
13
|
+
"funding": {
|
|
14
|
+
"type": "github",
|
|
15
|
+
"url": "https://github.com/sponsors/tannerlinsley"
|
|
11
16
|
},
|
|
12
17
|
"keywords": [
|
|
13
18
|
"store",
|
|
14
19
|
"typescript"
|
|
15
20
|
],
|
|
16
|
-
"funding": {
|
|
17
|
-
"type": "github",
|
|
18
|
-
"url": "https://github.com/sponsors/tannerlinsley"
|
|
19
|
-
},
|
|
20
21
|
"type": "module",
|
|
21
22
|
"types": "dist/esm/index.d.ts",
|
|
22
23
|
"main": "dist/cjs/index.cjs",
|
package/src/index.ts
CHANGED
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
|
-
})
|