@tanstack/store 0.0.1-beta.90 → 0.0.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/build/cjs/index.js +17 -4
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.js +17 -4
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +7 -7
- package/build/types/index.d.ts +14 -5
- package/build/umd/index.development.js +17 -4
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +2 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +4 -2
- package/src/index.ts +27 -5
package/build/cjs/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* store
|
|
2
|
+
* @tanstack/store/src/index.ts
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) TanStack
|
|
5
5
|
*
|
|
@@ -16,6 +16,7 @@ class Store {
|
|
|
16
16
|
listeners = new Set();
|
|
17
17
|
_batching = false;
|
|
18
18
|
_flushing = 0;
|
|
19
|
+
_nextPriority = null;
|
|
19
20
|
constructor(initialState, options) {
|
|
20
21
|
this.state = initialState;
|
|
21
22
|
this.options = options;
|
|
@@ -28,12 +29,22 @@ class Store {
|
|
|
28
29
|
unsub?.();
|
|
29
30
|
};
|
|
30
31
|
};
|
|
31
|
-
setState = updater => {
|
|
32
|
+
setState = (updater, opts) => {
|
|
32
33
|
const previous = this.state;
|
|
33
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
|
+
}
|
|
34
43
|
|
|
35
44
|
// Always run onUpdate, regardless of batching
|
|
36
|
-
this.options?.onUpdate?.(
|
|
45
|
+
this.options?.onUpdate?.({
|
|
46
|
+
priority: this._nextPriority
|
|
47
|
+
});
|
|
37
48
|
|
|
38
49
|
// Attempt to flush
|
|
39
50
|
this._flush();
|
|
@@ -43,7 +54,9 @@ class Store {
|
|
|
43
54
|
const flushId = ++this._flushing;
|
|
44
55
|
this.listeners.forEach(listener => {
|
|
45
56
|
if (this._flushing !== flushId) return;
|
|
46
|
-
listener(
|
|
57
|
+
listener({
|
|
58
|
+
priority: this._nextPriority ?? 'high'
|
|
59
|
+
});
|
|
47
60
|
});
|
|
48
61
|
};
|
|
49
62
|
batch = cb => {
|
package/build/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = () => void\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?: () => 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":["Store","listeners","Set","_batching","_flushing","constructor","initialState","options","state","subscribe","listener","add","unsub","onSubscribe","delete","setState","updater","previous","updateFn","onUpdate","_flush","flushId","forEach","batch","cb"],"mappings":";;;;;;;;;;;;;;
|
|
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,OAAwC,EAAE;IAC1E,IAAI,CAACC,KAAK,GAAGF,YAAY,CAAA;IACzB,IAAI,CAACC,OAAO,GAAGA,OAAO,CAAA;AACxB,GAAA;EAEAE,SAAS,GAAIC,QAAkB,IAAK;AAClC,IAAA,IAAI,CAACV,SAAS,CAACW,GAAG,CAACD,QAAQ,CAAC,CAAA;IAC5B,MAAME,KAAK,GAAG,IAAI,CAACL,OAAO,EAAEM,WAAW,GAAGH,QAAQ,EAAE,IAAI,CAAC,CAAA;AACzD,IAAA,OAAO,MAAM;AACX,MAAA,IAAI,CAACV,SAAS,CAACc,MAAM,CAACJ,QAAQ,CAAC,CAAA;AAC/BE,MAAAA,KAAK,IAAI,CAAA;KACV,CAAA;GACF,CAAA;AAEDG,EAAAA,QAAQ,GAAGA,CACTC,OAAiB,EACjBC,IAEC,KACE;AACH,IAAA,MAAMC,QAAQ,GAAG,I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IC,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;;;;"}
|
package/build/esm/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* store
|
|
2
|
+
* @tanstack/store/src/index.ts
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) TanStack
|
|
5
5
|
*
|
|
@@ -12,6 +12,7 @@ class Store {
|
|
|
12
12
|
listeners = new Set();
|
|
13
13
|
_batching = false;
|
|
14
14
|
_flushing = 0;
|
|
15
|
+
_nextPriority = null;
|
|
15
16
|
constructor(initialState, options) {
|
|
16
17
|
this.state = initialState;
|
|
17
18
|
this.options = options;
|
|
@@ -24,12 +25,22 @@ class Store {
|
|
|
24
25
|
unsub?.();
|
|
25
26
|
};
|
|
26
27
|
};
|
|
27
|
-
setState = updater => {
|
|
28
|
+
setState = (updater, opts) => {
|
|
28
29
|
const previous = this.state;
|
|
29
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
|
+
}
|
|
30
39
|
|
|
31
40
|
// Always run onUpdate, regardless of batching
|
|
32
|
-
this.options?.onUpdate?.(
|
|
41
|
+
this.options?.onUpdate?.({
|
|
42
|
+
priority: this._nextPriority
|
|
43
|
+
});
|
|
33
44
|
|
|
34
45
|
// Attempt to flush
|
|
35
46
|
this._flush();
|
|
@@ -39,7 +50,9 @@ class Store {
|
|
|
39
50
|
const flushId = ++this._flushing;
|
|
40
51
|
this.listeners.forEach(listener => {
|
|
41
52
|
if (this._flushing !== flushId) return;
|
|
42
|
-
listener(
|
|
53
|
+
listener({
|
|
54
|
+
priority: this._nextPriority ?? 'high'
|
|
55
|
+
});
|
|
43
56
|
});
|
|
44
57
|
};
|
|
45
58
|
batch = cb => {
|
package/build/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = () => void\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?: () => 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":["Store","listeners","Set","_batching","_flushing","constructor","initialState","options","state","subscribe","listener","add","unsub","onSubscribe","delete","setState","updater","previous","updateFn","onUpdate","_flush","flushId","forEach","batch","cb"],"mappings":";;;;;;;;;;
|
|
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,OAAwC,EAAE;IAC1E,IAAI,CAACC,KAAK,GAAGF,YAAY,CAAA;IACzB,IAAI,CAACC,OAAO,GAAGA,OAAO,CAAA;AACxB,GAAA;EAEAE,SAAS,GAAIC,QAAkB,IAAK;AAClC,IAAA,IAAI,CAACV,SAAS,CAACW,GAAG,CAACD,QAAQ,CAAC,CAAA;IAC5B,MAAME,KAAK,GAAG,IAAI,CAACL,OAAO,EAAEM,WAAW,GAAGH,QAAQ,EAAE,IAAI,CAAC,CAAA;AACzD,IAAA,OAAO,MAAM;AACX,MAAA,IAAI,CAACV,SAAS,CAACc,MAAM,CAACJ,QAAQ,CAAC,CAAA;AAC/BE,MAAAA,KAAK,IAAI,CAAA;KACV,CAAA;GACF,CAAA;AAEDG,EAAAA,QAAQ,GAAGA,CACTC,OAAiB,EACjBC,IAEC,KACE;AACH,IAAA,MAAMC,QAAQ,GAAG,IAAI,CAACV,KAAK,CAAA;IAC3B,IAAI,CAACA,KAAK,GAAG,IAAI,CAACD,OAAO,EAAEY,QAAQ,GAC/B,IAAI,CAACZ,OAAO,CAACY,QAAQ,CAACD,QAAQ,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AAAA,EAAE,EAAE,CAAA;IACJ,IAAI,CAACzB,SAAS,GAAG,KAAK,CAAA;IACtB,IAAI,CAACqB,MAAM,EAAE,CAAA;GACd,CAAA;AACH;;;;"}
|
package/build/stats-html.html
CHANGED
|
@@ -4024,7 +4024,7 @@ var drawChart = (function (exports) {
|
|
|
4024
4024
|
</script>
|
|
4025
4025
|
<script>
|
|
4026
4026
|
/*<!--*/
|
|
4027
|
-
const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.production.js","children":[{"name":"packages/store/src/index.ts","uid":"
|
|
4027
|
+
const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.production.js","children":[{"name":"packages/store/src/index.ts","uid":"1b85-1"}]}],"isRoot":true},"nodeParts":{"1b85-1":{"renderedLength":1603,"gzipLength":519,"brotliLength":0,"mainUid":"1b85-0"}},"nodeMetas":{"1b85-0":{"id":"/packages/store/src/index.ts","moduleParts":{"index.production.js":"1b85-1"},"imported":[],"importedBy":[],"isEntry":true}},"env":{"rollup":"2.79.1"},"options":{"gzip":true,"brotli":false,"sourcemap":false}};
|
|
4028
4028
|
|
|
4029
4029
|
const run = () => {
|
|
4030
4030
|
const width = window.innerWidth;
|
package/build/stats-react.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"children": [
|
|
9
9
|
{
|
|
10
10
|
"name": "packages/store/src/index.ts",
|
|
11
|
-
"uid": "
|
|
11
|
+
"uid": "1b85-3"
|
|
12
12
|
}
|
|
13
13
|
]
|
|
14
14
|
}
|
|
@@ -16,18 +16,18 @@
|
|
|
16
16
|
"isRoot": true
|
|
17
17
|
},
|
|
18
18
|
"nodeParts": {
|
|
19
|
-
"
|
|
20
|
-
"renderedLength":
|
|
21
|
-
"gzipLength":
|
|
19
|
+
"1b85-3": {
|
|
20
|
+
"renderedLength": 1603,
|
|
21
|
+
"gzipLength": 519,
|
|
22
22
|
"brotliLength": 0,
|
|
23
|
-
"mainUid": "
|
|
23
|
+
"mainUid": "1b85-2"
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"nodeMetas": {
|
|
27
|
-
"
|
|
27
|
+
"1b85-2": {
|
|
28
28
|
"id": "/packages/store/src/index.ts",
|
|
29
29
|
"moduleParts": {
|
|
30
|
-
"index.production.js": "
|
|
30
|
+
"index.production.js": "1b85-3"
|
|
31
31
|
},
|
|
32
32
|
"imported": [],
|
|
33
33
|
"importedBy": [],
|
package/build/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* store
|
|
2
|
+
* @tanstack/store/src/index.ts
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) TanStack
|
|
5
5
|
*
|
|
@@ -9,11 +9,17 @@
|
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
11
11
|
type AnyUpdater = (...args: any[]) => any;
|
|
12
|
-
type Listener = (
|
|
12
|
+
type Listener = (opts: {
|
|
13
|
+
priority: Priority;
|
|
14
|
+
}) => void;
|
|
15
|
+
type Priority = 'high' | 'low';
|
|
13
16
|
interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
14
17
|
updateFn?: (previous: TState) => (updater: TUpdater) => TState;
|
|
15
18
|
onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
|
|
16
|
-
onUpdate?: (
|
|
19
|
+
onUpdate?: (opts: {
|
|
20
|
+
priority: Priority;
|
|
21
|
+
}) => void;
|
|
22
|
+
defaultPriority?: Priority;
|
|
17
23
|
}
|
|
18
24
|
declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
19
25
|
listeners: Set<Listener>;
|
|
@@ -21,11 +27,14 @@ declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState
|
|
|
21
27
|
options?: StoreOptions<TState, TUpdater>;
|
|
22
28
|
_batching: boolean;
|
|
23
29
|
_flushing: number;
|
|
30
|
+
_nextPriority: null | Priority;
|
|
24
31
|
constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>);
|
|
25
32
|
subscribe: (listener: Listener) => () => void;
|
|
26
|
-
setState: (updater: TUpdater
|
|
33
|
+
setState: (updater: TUpdater, opts?: {
|
|
34
|
+
priority: Priority;
|
|
35
|
+
}) => void;
|
|
27
36
|
_flush: () => void;
|
|
28
37
|
batch: (cb: () => void) => void;
|
|
29
38
|
}
|
|
30
39
|
|
|
31
|
-
export { AnyUpdater, Listener, Store };
|
|
40
|
+
export { AnyUpdater, Listener, Priority, Store };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* store
|
|
2
|
+
* @tanstack/store/src/index.ts
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) TanStack
|
|
5
5
|
*
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
listeners = new Set();
|
|
19
19
|
_batching = false;
|
|
20
20
|
_flushing = 0;
|
|
21
|
+
_nextPriority = null;
|
|
21
22
|
constructor(initialState, options) {
|
|
22
23
|
this.state = initialState;
|
|
23
24
|
this.options = options;
|
|
@@ -30,12 +31,22 @@
|
|
|
30
31
|
unsub?.();
|
|
31
32
|
};
|
|
32
33
|
};
|
|
33
|
-
setState = updater => {
|
|
34
|
+
setState = (updater, opts) => {
|
|
34
35
|
const previous = this.state;
|
|
35
36
|
this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous);
|
|
37
|
+
const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high';
|
|
38
|
+
if (this._nextPriority === null) {
|
|
39
|
+
this._nextPriority = priority;
|
|
40
|
+
} else if (this._nextPriority === 'high') {
|
|
41
|
+
this._nextPriority = priority;
|
|
42
|
+
} else {
|
|
43
|
+
this._nextPriority = this.options?.defaultPriority ?? 'high';
|
|
44
|
+
}
|
|
36
45
|
|
|
37
46
|
// Always run onUpdate, regardless of batching
|
|
38
|
-
this.options?.onUpdate?.(
|
|
47
|
+
this.options?.onUpdate?.({
|
|
48
|
+
priority: this._nextPriority
|
|
49
|
+
});
|
|
39
50
|
|
|
40
51
|
// Attempt to flush
|
|
41
52
|
this._flush();
|
|
@@ -45,7 +56,9 @@
|
|
|
45
56
|
const flushId = ++this._flushing;
|
|
46
57
|
this.listeners.forEach(listener => {
|
|
47
58
|
if (this._flushing !== flushId) return;
|
|
48
|
-
listener(
|
|
59
|
+
listener({
|
|
60
|
+
priority: this._nextPriority ?? 'high'
|
|
61
|
+
});
|
|
49
62
|
});
|
|
50
63
|
};
|
|
51
64
|
batch = cb => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.development.js","sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = () => void\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?: () => 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":["Store","listeners","Set","_batching","_flushing","constructor","initialState","options","state","subscribe","listener","add","unsub","onSubscribe","delete","setState","updater","previous","updateFn","onUpdate","_flush","flushId","forEach","batch","cb"],"mappings":";;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.development.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":";;;;;;;;;;;;;;;;EAmBO,MAAMA,KAAK,CAGhB;EACAC,EAAAA,SAAS,GAAG,IAAIC,GAAG,EAAY,CAAA;EAG/BC,EAAAA,SAAS,GAAG,KAAK,CAAA;EACjBC,EAAAA,SAAS,GAAG,CAAC,CAAA;EACbC,EAAAA,aAAa,GAAoB,IAAI,CAAA;EAErCC,EAAAA,WAAWA,CAACC,YAAoB,EAAEC,OAAwC,EAAE;MAC1E,IAAI,CAACC,KAAK,GAAGF,YAAY,CAAA;MACzB,IAAI,CAACC,OAAO,GAAGA,OAAO,CAAA;EACxB,GAAA;IAEAE,SAAS,GAAIC,QAAkB,IAAK;EAClC,IAAA,IAAI,CAACV,SAAS,CAACW,GAAG,CAACD,QAAQ,CAAC,CAAA;MAC5B,MAAME,KAAK,GAAG,IAAI,CAACL,OAAO,EAAEM,WAAW,GAAGH,QAAQ,EAAE,IAAI,CAAC,CAAA;EACzD,IAAA,OAAO,MAAM;EACX,MAAA,IAAI,CAACV,SAAS,CAACc,MAAM,CAACJ,QAAQ,CAAC,CAAA;EAC/BE,MAAAA,KAAK,IAAI,CAAA;OACV,CAAA;KACF,CAAA;EAEDG,EAAAA,QAAQ,GAAGA,CACTC,OAAiB,EACjBC,IAEC,KACE;EACH,IAAA,MAAMC,QAAQ,GAAG,IAAI,CAACV,KAAK,CAAA;MAC3B,IAAI,CAACA,KAAK,GAAG,IAAI,CAACD,OAAO,EAAEY,QAAQ,GAC/B,IAAI,CAACZ,OAAO,CAACY,QAAQ,CAACD,QAAQ,CA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AA,EAAE,EAAE,CAAA;MACJ,IAAI,CAACzB,SAAS,GAAG,KAAK,CAAA;MACtB,IAAI,CAACqB,MAAM,EAAE,CAAA;KACd,CAAA;EACH;;;;;;;;;;"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* store
|
|
2
|
+
* @tanstack/store/src/index.ts
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) TanStack
|
|
5
5
|
*
|
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
11
|
-
!function(t,
|
|
11
|
+
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports):"function"==typeof define&&define.amd?define(["exports"],i):i((t="undefined"!=typeof globalThis?globalThis:t||self).Store={})}(this,(function(t){"use strict";t.Store=class{listeners=new Set;_batching=!1;_flushing=0;_nextPriority=null;constructor(t,i){this.state=t,this.options=i}subscribe=t=>{this.listeners.add(t);const i=this.options?.onSubscribe?.(t,this);return()=>{this.listeners.delete(t),i?.()}};setState=(t,i)=>{const s=this.state;this.state=this.options?.updateFn?this.options.updateFn(s)(t):t(s);const e=i?.priority??this.options?.defaultPriority??"high";null===this._nextPriority||"high"===this._nextPriority?this._nextPriority=e:this._nextPriority=this.options?.defaultPriority??"high",this.options?.onUpdate?.({priority:this._nextPriority}),this._flush()};_flush=()=>{if(this._batching)return;const t=++this._flushing;this.listeners.forEach((i=>{this._flushing===t&&i({priority:this._nextPriority??"high"})}))};batch=t=>{if(this._batching)return t();this._batching=!0,t(),this._batching=!1,this._flush()}},Object.defineProperty(t,"__esModule",{value:!0})}));
|
|
12
12
|
//# sourceMappingURL=index.production.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.production.js","sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = () => void\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?: () => 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":["listeners","Set","_batching","_flushing","constructor","initialState","options","this","state","subscribe","listener","add","unsub","onSubscribe","delete","setState","updater","previous","updateFn","onUpdate","_flush","flushId","forEach","batch","cb"],"mappings":";;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.production.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":["listeners","Set","_batching","_flushing","_nextPriority","constructor","initialState","options","this","state","subscribe","listener","add","unsub","onSubscribe","delete","setState","updater","opts","previous","updateFn","priority","defaultPriority","onUpdate","_flush","flushId","forEach","batch","cb"],"mappings":";;;;;;;;;;qPAmBO,MAILA,UAAY,IAAIC,IAGhBC,WAAY,EACZC,UAAY,EACZC,cAAiC,KAEjCC,YAAYC,EAAsBC,GAChCC,KAAKC,MAAQH,EACbE,KAAKD,QAAUA,CACjB,CAEAG,UAAaC,IACXH,KAAKR,UAAUY,IAAID,GACnB,MAAME,EAAQL,KAAKD,SAASO,cAAcH,EAAUH,MACpD,MAAO,KACLA,KAAKR,UAAUe,OAAOJ,GACtBE,KAAS,CACV,EAGHG,SAAWA,CACTC,EACAC,KAIA,MAAMC,EAAWX,KAAKC,MACtBD,KAAKC,MAAQD,KAAKD,SAASa,SACvBZ,KAAKD,QAAQa,SAASD,EAAtBX,CAAgCS,GAC/BA,EAAgBE,GAErB,MAAME,EAAWH,GAAMG,UAAYb,KAAKD,SAASe,iBAAmB,OACzC,OAAvBd,KAAKJ,eAEyB,SAAvBI,KAAKJ,cADdI,KAAKJ,cAAgBiB,EAIrBb,KAAKJ,cAAgBI,KAAKD,SAASe,iBAAmB,OAIxDd,KAAKD,SAASgB,WAAW,CACvBF,SAAUb,KAAKJ,gBAIjBI,KAAKgB,QAAQ,EAGfA,OAASA,KACP,GAAIhB,KAAKN,UAAW,OACpB,MAAMuB,IAAYjB,KAAKL,UACvBK,KAAKR,UAAU0B,SAASf,IAClBH,KAAKL,YAAcsB,GACvBd,EAAS,CACPU,SAAUb,KAAKJ,eAAiB,QAChC,GACF,EAGJuB,MAASC,IACP,GAAIpB,KAAKN,UAAW,OAAO0B,IAC3BpB,KAAKN,WAAY,EACjB0B,IACApB,KAAKN,WAAY,EACjBM,KAAKgB,QAAQ"}
|
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.0.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "tanstack/store",
|
|
7
7
|
"homepage": "https://tanstack.com/store",
|
|
@@ -30,6 +30,8 @@
|
|
|
30
30
|
],
|
|
31
31
|
"sideEffects": false,
|
|
32
32
|
"scripts": {
|
|
33
|
-
"build": "rollup --config rollup.config.js"
|
|
33
|
+
"build": "rollup --config rollup.config.js",
|
|
34
|
+
"test": "vitest",
|
|
35
|
+
"test:dev": "vitest --watch"
|
|
34
36
|
}
|
|
35
37
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export type AnyUpdater = (...args: any[]) => any
|
|
2
2
|
|
|
3
|
-
export type Listener = () => void
|
|
3
|
+
export type Listener = (opts: { priority: Priority }) => void
|
|
4
|
+
|
|
5
|
+
export type Priority = 'high' | 'low'
|
|
4
6
|
|
|
5
7
|
interface StoreOptions<
|
|
6
8
|
TState,
|
|
@@ -11,7 +13,8 @@ interface StoreOptions<
|
|
|
11
13
|
listener: Listener,
|
|
12
14
|
store: Store<TState, TUpdater>,
|
|
13
15
|
) => () => void
|
|
14
|
-
onUpdate?: () => void
|
|
16
|
+
onUpdate?: (opts: { priority: Priority }) => void
|
|
17
|
+
defaultPriority?: Priority
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
export class Store<
|
|
@@ -23,6 +26,7 @@ export class Store<
|
|
|
23
26
|
options?: StoreOptions<TState, TUpdater>
|
|
24
27
|
_batching = false
|
|
25
28
|
_flushing = 0
|
|
29
|
+
_nextPriority: null | Priority = null
|
|
26
30
|
|
|
27
31
|
constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {
|
|
28
32
|
this.state = initialState
|
|
@@ -38,14 +42,30 @@ export class Store<
|
|
|
38
42
|
}
|
|
39
43
|
}
|
|
40
44
|
|
|
41
|
-
setState = (
|
|
45
|
+
setState = (
|
|
46
|
+
updater: TUpdater,
|
|
47
|
+
opts?: {
|
|
48
|
+
priority: Priority
|
|
49
|
+
},
|
|
50
|
+
) => {
|
|
42
51
|
const previous = this.state
|
|
43
52
|
this.state = this.options?.updateFn
|
|
44
53
|
? this.options.updateFn(previous)(updater)
|
|
45
54
|
: (updater as any)(previous)
|
|
46
55
|
|
|
56
|
+
const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high'
|
|
57
|
+
if (this._nextPriority === null) {
|
|
58
|
+
this._nextPriority = priority
|
|
59
|
+
} else if (this._nextPriority === 'high') {
|
|
60
|
+
this._nextPriority = priority
|
|
61
|
+
} else {
|
|
62
|
+
this._nextPriority = this.options?.defaultPriority ?? 'high'
|
|
63
|
+
}
|
|
64
|
+
|
|
47
65
|
// Always run onUpdate, regardless of batching
|
|
48
|
-
this.options?.onUpdate?.(
|
|
66
|
+
this.options?.onUpdate?.({
|
|
67
|
+
priority: this._nextPriority,
|
|
68
|
+
})
|
|
49
69
|
|
|
50
70
|
// Attempt to flush
|
|
51
71
|
this._flush()
|
|
@@ -56,7 +76,9 @@ export class Store<
|
|
|
56
76
|
const flushId = ++this._flushing
|
|
57
77
|
this.listeners.forEach((listener) => {
|
|
58
78
|
if (this._flushing !== flushId) return
|
|
59
|
-
listener(
|
|
79
|
+
listener({
|
|
80
|
+
priority: this._nextPriority ?? 'high',
|
|
81
|
+
})
|
|
60
82
|
})
|
|
61
83
|
}
|
|
62
84
|
|