@tanstack/store 0.0.1 → 0.1.1

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/store",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.0.1",
4
+ "version": "0.1.1",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/store",
7
7
  "homepage": "https://tanstack.com/store",
@@ -17,21 +17,35 @@
17
17
  "type": "github",
18
18
  "url": "https://github.com/sponsors/tannerlinsley"
19
19
  },
20
- "module": "build/esm/index.js",
21
- "main": "build/cjs/index.js",
22
- "browser": "build/umd/index.production.js",
23
- "types": "build/types/index.d.ts",
24
- "engines": {
25
- "node": ">=12"
20
+ "type": "module",
21
+ "types": "build/legacy/index.d.ts",
22
+ "main": "build/legacy/index.cjs",
23
+ "module": "build/legacy/index.js",
24
+ "exports": {
25
+ ".": {
26
+ "import": {
27
+ "types": "./build/modern/index.d.ts",
28
+ "default": "./build/modern/index.js"
29
+ },
30
+ "require": {
31
+ "types": "./build/modern/index.d.cts",
32
+ "default": "./build/modern/index.cjs"
33
+ }
34
+ },
35
+ "./package.json": "./package.json"
26
36
  },
37
+ "sideEffects": false,
27
38
  "files": [
28
- "build/**",
39
+ "build",
29
40
  "src"
30
41
  ],
31
- "sideEffects": false,
32
42
  "scripts": {
33
- "build": "rollup --config rollup.config.js",
34
- "test": "vitest",
35
- "test:dev": "vitest --watch"
43
+ "clean": "rimraf ./build && rimraf ./coverage",
44
+ "test:eslint": "eslint --ext .ts,.tsx ./src",
45
+ "test:types": "tsc",
46
+ "test:lib": "vitest run --coverage",
47
+ "test:lib:dev": "pnpm run test:lib --watch",
48
+ "test:build": "publint --strict",
49
+ "build": "tsup"
36
50
  }
37
51
  }
@@ -0,0 +1,83 @@
1
+ import { describe, test, expect, 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
+ })
@@ -1,72 +0,0 @@
1
- /**
2
- * @tanstack/store/src/index.ts
3
- *
4
- * Copyright (c) TanStack
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE.md file in the root directory of this source tree.
8
- *
9
- * @license MIT
10
- */
11
- 'use strict';
12
-
13
- Object.defineProperty(exports, '__esModule', { value: true });
14
-
15
- class Store {
16
- listeners = new Set();
17
- _batching = false;
18
- _flushing = 0;
19
- _nextPriority = null;
20
- constructor(initialState, options) {
21
- this.state = initialState;
22
- this.options = options;
23
- }
24
- subscribe = listener => {
25
- this.listeners.add(listener);
26
- const unsub = this.options?.onSubscribe?.(listener, this);
27
- return () => {
28
- this.listeners.delete(listener);
29
- unsub?.();
30
- };
31
- };
32
- setState = (updater, opts) => {
33
- const previous = this.state;
34
- this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous);
35
- const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high';
36
- if (this._nextPriority === null) {
37
- this._nextPriority = priority;
38
- } else if (this._nextPriority === 'high') {
39
- this._nextPriority = priority;
40
- } else {
41
- this._nextPriority = this.options?.defaultPriority ?? 'high';
42
- }
43
-
44
- // Always run onUpdate, regardless of batching
45
- this.options?.onUpdate?.({
46
- priority: this._nextPriority
47
- });
48
-
49
- // Attempt to flush
50
- this._flush();
51
- };
52
- _flush = () => {
53
- if (this._batching) return;
54
- const flushId = ++this._flushing;
55
- this.listeners.forEach(listener => {
56
- if (this._flushing !== flushId) return;
57
- listener({
58
- priority: this._nextPriority ?? 'high'
59
- });
60
- });
61
- };
62
- batch = cb => {
63
- if (this._batching) return cb();
64
- this._batching = true;
65
- cb();
66
- this._batching = false;
67
- this._flush();
68
- };
69
- }
70
-
71
- exports.Store = Store;
72
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","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"],"names":["Store","listeners","Set","_batching","_flushing","_nextPriority","constructor","initialState","options","state","subscribe","listener","add","unsub","onSubscribe","delete","setState","updater","opts","previous","updateFn","priority","defaultPriority","onUpdate","_flush","flushId","forEach","batch","cb"],"mappings":";;;;;;;;;;;;;;AAmBO,MAAMA,KAAK,CA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M,EAAE;MACxC,IAAI,CAACA,aAAa,GAAGgB,QAAQ,CAAA;AAC/B,KAAC,MAAM;MACL,IAAI,CAAChB,aAAa,GAAG,IAAI,CAACG,OAAO,EAAEc,eAAe,IAAI,MAAM,CAAA;AAC9D,KAAA;;AAEA;AACA,IAAA,IAAI,CAACd,OAAO,EAAEe,QAAQ,GAAG;MACvBF,QAAQ,EAAE,IAAI,CAAChB,aAAAA;AACjB,KAAC,CAAC,CAAA;;AAEF;IACA,IAAI,CAACmB,MAAM,EAAE,CAAA;GACd,CAAA;EAEDA,MAAM,GAAGA,MAAM;IACb,IAAI,IAAI,CAACrB,SAAS,EAAE,OAAA;AACpB,IAAA,MAAMsB,OAAO,GAAG,EAAE,IAAI,CAACrB,SAAS,CAAA;AAChC,IAAA,IAAI,CAACH,SAAS,CAACyB,OAAO,CAAEf,QAAQ,IAAK;AACnC,MAAA,IAAI,IAAI,CAACP,SAAS,KAAKqB,OAAO,EAAE,OAAA;AAChCd,MAAAA,QAAQ,CAAC;AACPU,QAAAA,QAAQ,EAAE,IAAI,CAAChB,aAAa,IAAI,MAAA;AAClC,OAAC,CAAC,CAAA;AACJ,KAAC,CAAC,CAAA;GACH,CAAA;EAEDsB,KAAK,GAAIC,EAAc,IAAK;AAC1B,IAAA,IAAI,IAAI,CAACzB,SAAS,EAAE,OAAOyB,EAAE,EAAE,CAAA;IAC/B,IAAI,CAACzB,SAAS,GAAG,IAAI,CAAA;AACrByB,IAAAA,EAAE,EAAE,CAAA;IACJ,IAAI,CAACzB,SAAS,GAAG,KAAK,CAAA;IACtB,IAAI,CAACqB,MAAM,EAAE,CAAA;GACd,CAAA;AACH;;;;"}
@@ -1,68 +0,0 @@
1
- /**
2
- * @tanstack/store/src/index.ts
3
- *
4
- * Copyright (c) TanStack
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE.md file in the root directory of this source tree.
8
- *
9
- * @license MIT
10
- */
11
- class Store {
12
- listeners = new Set();
13
- _batching = false;
14
- _flushing = 0;
15
- _nextPriority = null;
16
- constructor(initialState, options) {
17
- this.state = initialState;
18
- this.options = options;
19
- }
20
- subscribe = listener => {
21
- this.listeners.add(listener);
22
- const unsub = this.options?.onSubscribe?.(listener, this);
23
- return () => {
24
- this.listeners.delete(listener);
25
- unsub?.();
26
- };
27
- };
28
- setState = (updater, opts) => {
29
- const previous = this.state;
30
- this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous);
31
- const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high';
32
- if (this._nextPriority === null) {
33
- this._nextPriority = priority;
34
- } else if (this._nextPriority === 'high') {
35
- this._nextPriority = priority;
36
- } else {
37
- this._nextPriority = this.options?.defaultPriority ?? 'high';
38
- }
39
-
40
- // Always run onUpdate, regardless of batching
41
- this.options?.onUpdate?.({
42
- priority: this._nextPriority
43
- });
44
-
45
- // Attempt to flush
46
- this._flush();
47
- };
48
- _flush = () => {
49
- if (this._batching) return;
50
- const flushId = ++this._flushing;
51
- this.listeners.forEach(listener => {
52
- if (this._flushing !== flushId) return;
53
- listener({
54
- priority: this._nextPriority ?? 'high'
55
- });
56
- });
57
- };
58
- batch = cb => {
59
- if (this._batching) return cb();
60
- this._batching = true;
61
- cb();
62
- this._batching = false;
63
- this._flush();
64
- };
65
- }
66
-
67
- export { Store };
68
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","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"],"names":["Store","listeners","Set","_batching","_flushing","_nextPriority","constructor","initialState","options","state","subscribe","listener","add","unsub","onSubscribe","delete","setState","updater","opts","previous","updateFn","priority","defaultPriority","onUpdate","_flush","flushId","forEach","batch","cb"],"mappings":";;;;;;;;;;AAmBO,MAAMA,KAAK,CAGhB;AACAC,EAAAA,SAAS,GAAG,IAAIC,GAAG,EAAY,CAAA;AAG/BC,EAAAA,SAAS,GAAG,KAAK,CAAA;AACjBC,EAAAA,SAAS,GAAG,CAAC,CAAA;AACbC,EAAAA,aAAa,GAAoB,IAAI,CAAA;AAErCC,EAAAA,WAAWA,CAACC,YAAoB,EAAEC,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