@tanstack/store 0.0.1-beta.113

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.
@@ -0,0 +1,45 @@
1
+ {
2
+ "version": 2,
3
+ "tree": {
4
+ "name": "root",
5
+ "children": [
6
+ {
7
+ "name": "index.production.js",
8
+ "children": [
9
+ {
10
+ "name": "packages/store/src/index.ts",
11
+ "uid": "5eea-3"
12
+ }
13
+ ]
14
+ }
15
+ ],
16
+ "isRoot": true
17
+ },
18
+ "nodeParts": {
19
+ "5eea-3": {
20
+ "renderedLength": 1119,
21
+ "gzipLength": 412,
22
+ "brotliLength": 0,
23
+ "mainUid": "5eea-2"
24
+ }
25
+ },
26
+ "nodeMetas": {
27
+ "5eea-2": {
28
+ "id": "/packages/store/src/index.ts",
29
+ "moduleParts": {
30
+ "index.production.js": "5eea-3"
31
+ },
32
+ "imported": [],
33
+ "importedBy": [],
34
+ "isEntry": true
35
+ }
36
+ },
37
+ "env": {
38
+ "rollup": "2.79.1"
39
+ },
40
+ "options": {
41
+ "gzip": true,
42
+ "brotli": false,
43
+ "sourcemap": false
44
+ }
45
+ }
@@ -0,0 +1,31 @@
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
+ type AnyUpdater = (...args: any[]) => any;
12
+ type Listener = () => void;
13
+ interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
14
+ updateFn?: (previous: TState) => (updater: TUpdater) => TState;
15
+ onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
16
+ onUpdate?: () => void;
17
+ }
18
+ declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
19
+ listeners: Set<Listener>;
20
+ state: TState;
21
+ options?: StoreOptions<TState, TUpdater>;
22
+ _batching: boolean;
23
+ _flushing: number;
24
+ constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>);
25
+ subscribe: (listener: Listener) => () => void;
26
+ setState: (updater: TUpdater) => void;
27
+ _flush: () => void;
28
+ batch: (cb: () => void) => void;
29
+ }
30
+
31
+ export { AnyUpdater, Listener, Store };
@@ -0,0 +1,65 @@
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
+ (function (global, factory) {
12
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
13
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
14
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Store = {}));
15
+ })(this, (function (exports) { 'use strict';
16
+
17
+ class Store {
18
+ listeners = new Set();
19
+ _batching = false;
20
+ _flushing = 0;
21
+ constructor(initialState, options) {
22
+ this.state = initialState;
23
+ this.options = options;
24
+ }
25
+ subscribe = listener => {
26
+ this.listeners.add(listener);
27
+ const unsub = this.options?.onSubscribe?.(listener, this);
28
+ return () => {
29
+ this.listeners.delete(listener);
30
+ unsub?.();
31
+ };
32
+ };
33
+ setState = updater => {
34
+ const previous = this.state;
35
+ this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous);
36
+
37
+ // Always run onUpdate, regardless of batching
38
+ this.options?.onUpdate?.();
39
+
40
+ // Attempt to flush
41
+ this._flush();
42
+ };
43
+ _flush = () => {
44
+ if (this._batching) return;
45
+ const flushId = ++this._flushing;
46
+ this.listeners.forEach(listener => {
47
+ if (this._flushing !== flushId) return;
48
+ listener();
49
+ });
50
+ };
51
+ batch = cb => {
52
+ if (this._batching) return cb();
53
+ this._batching = true;
54
+ cb();
55
+ this._batching = false;
56
+ this._flush();
57
+ };
58
+ }
59
+
60
+ exports.Store = Store;
61
+
62
+ Object.defineProperty(exports, '__esModule', { value: true });
63
+
64
+ }));
65
+ //# sourceMappingURL=index.development.js.map
@@ -0,0 +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":";;;;;;;;;;;;;;;;EAgBO,MAAMA,KAAK,CAGhB;IACAC,SAAS,GAAG,IAAIC,GAAG,EAAY,CAAA;EAG/BC,EAAAA,SAAS,GAAG,KAAK,CAAA;EACjBC,EAAAA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;KACH,CAAA;IAEDa,KAAK,GAAIC,EAAc,IAAK;EAC1B,IAAA,IAAI,IAAI,CAACrB,SAAS,EAAE,OAAOqB,EAAE,EAAE,CAAA;MAC/B,IAAI,CAACrB,SAAS,GAAG,IAAI,CAAA;EACrBqB,IAAAA,EAAE,EAAE,CAAA;MACJ,IAAI,CAACrB,SAAS,GAAG,KAAK,CAAA;MACtB,IAAI,CAACiB,MAAM,EAAE,CAAA;KACd,CAAA;EACH;;;;;;;;;;"}
@@ -0,0 +1,12 @@
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
+ !function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t="undefined"!=typeof globalThis?globalThis:t||self).Store={})}(this,(function(t){"use strict";t.Store=class{listeners=new Set;_batching=!1;_flushing=0;constructor(t,s){this.state=t,this.options=s}subscribe=t=>{this.listeners.add(t);const s=this.options?.onSubscribe?.(t,this);return()=>{this.listeners.delete(t),s?.()}};setState=t=>{const s=this.state;this.state=this.options?.updateFn?this.options.updateFn(s)(t):t(s),this.options?.onUpdate?.(),this._flush()};_flush=()=>{if(this._batching)return;const t=++this._flushing;this.listeners.forEach((s=>{this._flushing===t&&s()}))};batch=t=>{if(this._batching)return t();this._batching=!0,t(),this._batching=!1,this._flush()}},Object.defineProperty(t,"__esModule",{value:!0})}));
12
+ //# sourceMappingURL=index.production.js.map
@@ -0,0 +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":";;;;;;;;;;qPAgBO,MAILA,UAAY,IAAIC,IAGhBC,WAAY,EACZC,UAAY,EAEZC,YAAYC,EAAsBC,GAChCC,KAAKC,MAAQH,EACbE,KAAKD,QAAUA,CACjB,CAEAG,UAAaC,IACXH,KAAKP,UAAUW,IAAID,GACnB,MAAME,EAAQL,KAAKD,SAASO,cAAcH,EAAUH,MACpD,MAAO,KACLA,KAAKP,UAAUc,OAAOJ,GACtBE,KAAS,CACV,EAGHG,SAAYC,IACV,MAAMC,EAAWV,KAAKC,MACtBD,KAAKC,MAAQD,KAAKD,SAASY,SACvBX,KAAKD,QAAQY,SAASD,EAAtBV,CAAgCS,GAC/BA,EAAgBC,GAGrBV,KAAKD,SAASa,aAGdZ,KAAKa,QAAQ,EAGfA,OAAS,KACP,GAAIb,KAAKL,UAAW,OACpB,MAAMmB,IAAYd,KAAKJ,UACvBI,KAAKP,UAAUsB,SAASZ,IAClBH,KAAKJ,YAAckB,GACvBX,GAAU,GACV,EAGJa,MAASC,IACP,GAAIjB,KAAKL,UAAW,OAAOsB,IAC3BjB,KAAKL,WAAY,EACjBsB,IACAjB,KAAKL,WAAY,EACjBK,KAAKa,QAAQ"}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@tanstack/store",
3
+ "author": "Tanner Linsley",
4
+ "version": "0.0.1-beta.113",
5
+ "license": "MIT",
6
+ "repository": "tanstack/store",
7
+ "homepage": "https://tanstack.com/store",
8
+ "description": "",
9
+ "publishConfig": {
10
+ "registry": "https://registry.npmjs.org/"
11
+ },
12
+ "keywords": [
13
+ "store",
14
+ "typescript"
15
+ ],
16
+ "funding": {
17
+ "type": "github",
18
+ "url": "https://github.com/sponsors/tannerlinsley"
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"
26
+ },
27
+ "files": [
28
+ "build/**",
29
+ "src"
30
+ ],
31
+ "sideEffects": false,
32
+ "scripts": {
33
+ "build": "rollup --config rollup.config.js"
34
+ }
35
+ }
package/src/index.ts ADDED
@@ -0,0 +1,70 @@
1
+ export type AnyUpdater = (...args: any[]) => any
2
+
3
+ export type Listener = () => void
4
+
5
+ interface StoreOptions<
6
+ TState,
7
+ TUpdater extends AnyUpdater = (cb: TState) => TState,
8
+ > {
9
+ updateFn?: (previous: TState) => (updater: TUpdater) => TState
10
+ onSubscribe?: (
11
+ listener: Listener,
12
+ store: Store<TState, TUpdater>,
13
+ ) => () => void
14
+ onUpdate?: () => void
15
+ }
16
+
17
+ export class Store<
18
+ TState,
19
+ TUpdater extends AnyUpdater = (cb: TState) => TState,
20
+ > {
21
+ listeners = new Set<Listener>()
22
+ state: TState
23
+ options?: StoreOptions<TState, TUpdater>
24
+ _batching = false
25
+ _flushing = 0
26
+
27
+ constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {
28
+ this.state = initialState
29
+ this.options = options
30
+ }
31
+
32
+ subscribe = (listener: Listener) => {
33
+ this.listeners.add(listener)
34
+ const unsub = this.options?.onSubscribe?.(listener, this)
35
+ return () => {
36
+ this.listeners.delete(listener)
37
+ unsub?.()
38
+ }
39
+ }
40
+
41
+ setState = (updater: TUpdater) => {
42
+ const previous = this.state
43
+ this.state = this.options?.updateFn
44
+ ? this.options.updateFn(previous)(updater)
45
+ : (updater as any)(previous)
46
+
47
+ // Always run onUpdate, regardless of batching
48
+ this.options?.onUpdate?.()
49
+
50
+ // Attempt to flush
51
+ this._flush()
52
+ }
53
+
54
+ _flush = () => {
55
+ if (this._batching) return
56
+ const flushId = ++this._flushing
57
+ this.listeners.forEach((listener) => {
58
+ if (this._flushing !== flushId) return
59
+ listener()
60
+ })
61
+ }
62
+
63
+ batch = (cb: () => void) => {
64
+ if (this._batching) return cb()
65
+ this._batching = true
66
+ cb()
67
+ this._batching = false
68
+ this._flush()
69
+ }
70
+ }