@tanstack/preact-store 0.10.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Tanner Linsley
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const compat = require("preact/compat");
4
+ const hooks = require("preact/hooks");
5
+ const store = require("@tanstack/store");
6
+ function useSyncExternalStoreWithSelector(subscribe, getSnapshot, selector, isEqual) {
7
+ const selectedSnapshotRef = hooks.useRef();
8
+ const getSelectedSnapshot = () => {
9
+ const snapshot = getSnapshot();
10
+ const selected = selector(snapshot);
11
+ if (selectedSnapshotRef.current === void 0 || !isEqual(selectedSnapshotRef.current, selected)) {
12
+ selectedSnapshotRef.current = selected;
13
+ }
14
+ return selectedSnapshotRef.current;
15
+ };
16
+ return compat.useSyncExternalStore(subscribe, getSelectedSnapshot);
17
+ }
18
+ function useStore(store2, selector = (d) => d, options = {}) {
19
+ const equal = options.equal ?? shallow;
20
+ const slice = useSyncExternalStoreWithSelector(
21
+ store2.subscribe,
22
+ () => store2.state,
23
+ selector,
24
+ equal
25
+ );
26
+ return slice;
27
+ }
28
+ function shallow(objA, objB) {
29
+ if (Object.is(objA, objB)) {
30
+ return true;
31
+ }
32
+ if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
33
+ return false;
34
+ }
35
+ if (objA instanceof Map && objB instanceof Map) {
36
+ if (objA.size !== objB.size) return false;
37
+ for (const [k, v] of objA) {
38
+ if (!objB.has(k) || !Object.is(v, objB.get(k))) return false;
39
+ }
40
+ return true;
41
+ }
42
+ if (objA instanceof Set && objB instanceof Set) {
43
+ if (objA.size !== objB.size) return false;
44
+ for (const v of objA) {
45
+ if (!objB.has(v)) return false;
46
+ }
47
+ return true;
48
+ }
49
+ if (objA instanceof Date && objB instanceof Date) {
50
+ if (objA.getTime() !== objB.getTime()) return false;
51
+ return true;
52
+ }
53
+ const keysA = getOwnKeys(objA);
54
+ if (keysA.length !== getOwnKeys(objB).length) {
55
+ return false;
56
+ }
57
+ for (const key of keysA) {
58
+ if (!Object.prototype.hasOwnProperty.call(objB, key) || !Object.is(objA[key], objB[key])) {
59
+ return false;
60
+ }
61
+ }
62
+ return true;
63
+ }
64
+ function getOwnKeys(obj) {
65
+ return Object.keys(obj).concat(
66
+ Object.getOwnPropertySymbols(obj)
67
+ );
68
+ }
69
+ exports.shallow = shallow;
70
+ exports.useStore = useStore;
71
+ Object.keys(store).forEach((k) => {
72
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
73
+ enumerable: true,
74
+ get: () => store[k]
75
+ });
76
+ });
77
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["import { useSyncExternalStore } from 'preact/compat'\nimport { useRef } from 'preact/hooks'\nimport type { Derived, Store } from '@tanstack/store'\n\nexport * from '@tanstack/store'\n\n/**\n * @private\n */\nexport type NoInfer<T> = [T][T extends any ? 0 : never]\ntype EqualityFn<T> = (objA: T, objB: T) => boolean\ninterface UseStoreOptions<T> {\n equal?: EqualityFn<T>\n}\n\nfunction useSyncExternalStoreWithSelector<TSnapshot, TSelected>(\n subscribe: (onStoreChange: () => void) => () => void,\n getSnapshot: () => TSnapshot,\n selector: (snapshot: TSnapshot) => TSelected,\n isEqual: (a: TSelected, b: TSelected) => boolean,\n): TSelected {\n const selectedSnapshotRef = useRef<TSelected | undefined>()\n\n const getSelectedSnapshot = () => {\n const snapshot = getSnapshot()\n const selected = selector(snapshot)\n\n if (\n selectedSnapshotRef.current === undefined ||\n !isEqual(selectedSnapshotRef.current, selected)\n ) {\n selectedSnapshotRef.current = selected\n }\n\n return selectedSnapshotRef.current\n }\n\n return useSyncExternalStore(subscribe, getSelectedSnapshot)\n}\n\nexport function useStore<TState, TSelected = NoInfer<TState>>(\n store: Store<TState, any>,\n selector?: (state: NoInfer<TState>) => TSelected,\n options?: UseStoreOptions<TSelected>,\n): TSelected\nexport function useStore<TState, TSelected = NoInfer<TState>>(\n store: Derived<TState, any>,\n selector?: (state: NoInfer<TState>) => TSelected,\n options?: UseStoreOptions<TSelected>,\n): TSelected\nexport function useStore<TState, TSelected = NoInfer<TState>>(\n store: Store<TState, any> | Derived<TState, any>,\n selector: (state: NoInfer<TState>) => TSelected = (d) => d as any,\n options: UseStoreOptions<TSelected> = {},\n): TSelected {\n const equal = options.equal ?? shallow\n const slice = useSyncExternalStoreWithSelector(\n store.subscribe,\n () => store.state,\n selector,\n equal,\n )\n\n return slice\n}\n\nexport function shallow<T>(objA: T, objB: T) {\n if (Object.is(objA, objB)) {\n return true\n }\n\n if (\n typeof objA !== 'object' ||\n objA === null ||\n typeof objB !== 'object' ||\n objB === null\n ) {\n return false\n }\n\n if (objA instanceof Map && objB instanceof Map) {\n if (objA.size !== objB.size) return false\n for (const [k, v] of objA) {\n if (!objB.has(k) || !Object.is(v, objB.get(k))) return false\n }\n return true\n }\n\n if (objA instanceof Set && objB instanceof Set) {\n if (objA.size !== objB.size) return false\n for (const v of objA) {\n if (!objB.has(v)) return false\n }\n return true\n }\n\n if (objA instanceof Date && objB instanceof Date) {\n if (objA.getTime() !== objB.getTime()) return false\n return true\n }\n\n const keysA = getOwnKeys(objA)\n if (keysA.length !== getOwnKeys(objB).length) {\n return false\n }\n\n for (const key of keysA) {\n if (\n !Object.prototype.hasOwnProperty.call(objB, key as string) ||\n !Object.is(objA[key as keyof T], objB[key as keyof T])\n ) {\n return false\n }\n }\n return true\n}\n\nfunction getOwnKeys(obj: object): Array<string | symbol> {\n return (Object.keys(obj) as Array<string | symbol>).concat(\n Object.getOwnPropertySymbols(obj),\n )\n}\n\n// force\n"],"names":["useRef","useSyncExternalStore","store"],"mappings":";;;;;AAeA,SAAS,iCACP,WACA,aACA,UACA,SACW;AACX,QAAM,sBAAsBA,MAAAA,OAAA;AAE5B,QAAM,sBAAsB,MAAM;AAChC,UAAM,WAAW,YAAA;AACjB,UAAM,WAAW,SAAS,QAAQ;AAElC,QACE,oBAAoB,YAAY,UAChC,CAAC,QAAQ,oBAAoB,SAAS,QAAQ,GAC9C;AACA,0BAAoB,UAAU;AAAA,IAChC;AAEA,WAAO,oBAAoB;AAAA,EAC7B;AAEA,SAAOC,OAAAA,qBAAqB,WAAW,mBAAmB;AAC5D;AAYO,SAAS,SACdC,QACA,WAAkD,CAAC,MAAM,GACzD,UAAsC,IAC3B;AACX,QAAM,QAAQ,QAAQ,SAAS;AAC/B,QAAM,QAAQ;AAAA,IACZA,OAAM;AAAA,IACN,MAAMA,OAAM;AAAA,IACZ;AAAA,IACA;AAAA,EAAA;AAGF,SAAO;AACT;AAEO,SAAS,QAAW,MAAS,MAAS;AAC3C,MAAI,OAAO,GAAG,MAAM,IAAI,GAAG;AACzB,WAAO;AAAA,EACT;AAEA,MACE,OAAO,SAAS,YAChB,SAAS,QACT,OAAO,SAAS,YAChB,SAAS,MACT;AACA,WAAO;AAAA,EACT;AAEA,MAAI,gBAAgB,OAAO,gBAAgB,KAAK;AAC9C,QAAI,KAAK,SAAS,KAAK,KAAM,QAAO;AACpC,eAAW,CAAC,GAAG,CAAC,KAAK,MAAM;AACzB,UAAI,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,KAAK,IAAI,CAAC,CAAC,EAAG,QAAO;AAAA,IACzD;AACA,WAAO;AAAA,EACT;AAEA,MAAI,gBAAgB,OAAO,gBAAgB,KAAK;AAC9C,QAAI,KAAK,SAAS,KAAK,KAAM,QAAO;AACpC,eAAW,KAAK,MAAM;AACpB,UAAI,CAAC,KAAK,IAAI,CAAC,EAAG,QAAO;AAAA,IAC3B;AACA,WAAO;AAAA,EACT;AAEA,MAAI,gBAAgB,QAAQ,gBAAgB,MAAM;AAChD,QAAI,KAAK,QAAA,MAAc,KAAK,QAAA,EAAW,QAAO;AAC9C,WAAO;AAAA,EACT;AAEA,QAAM,QAAQ,WAAW,IAAI;AAC7B,MAAI,MAAM,WAAW,WAAW,IAAI,EAAE,QAAQ;AAC5C,WAAO;AAAA,EACT;AAEA,aAAW,OAAO,OAAO;AACvB,QACE,CAAC,OAAO,UAAU,eAAe,KAAK,MAAM,GAAa,KACzD,CAAC,OAAO,GAAG,KAAK,GAAc,GAAG,KAAK,GAAc,CAAC,GACrD;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAAS,WAAW,KAAqC;AACvD,SAAQ,OAAO,KAAK,GAAG,EAA6B;AAAA,IAClD,OAAO,sBAAsB,GAAG;AAAA,EAAA;AAEpC;;;;;;;;;"}
@@ -0,0 +1,13 @@
1
+ import { Derived, Store } from '@tanstack/store';
2
+ export * from '@tanstack/store';
3
+ /**
4
+ * @private
5
+ */
6
+ export type NoInfer<T> = [T][T extends any ? 0 : never];
7
+ type EqualityFn<T> = (objA: T, objB: T) => boolean;
8
+ interface UseStoreOptions<T> {
9
+ equal?: EqualityFn<T>;
10
+ }
11
+ export declare function useStore<TState, TSelected = NoInfer<TState>>(store: Store<TState, any>, selector?: (state: NoInfer<TState>) => TSelected, options?: UseStoreOptions<TSelected>): TSelected;
12
+ export declare function useStore<TState, TSelected = NoInfer<TState>>(store: Derived<TState, any>, selector?: (state: NoInfer<TState>) => TSelected, options?: UseStoreOptions<TSelected>): TSelected;
13
+ export declare function shallow<T>(objA: T, objB: T): boolean;
@@ -0,0 +1,13 @@
1
+ import { Derived, Store } from '@tanstack/store';
2
+ export * from '@tanstack/store';
3
+ /**
4
+ * @private
5
+ */
6
+ export type NoInfer<T> = [T][T extends any ? 0 : never];
7
+ type EqualityFn<T> = (objA: T, objB: T) => boolean;
8
+ interface UseStoreOptions<T> {
9
+ equal?: EqualityFn<T>;
10
+ }
11
+ export declare function useStore<TState, TSelected = NoInfer<TState>>(store: Store<TState, any>, selector?: (state: NoInfer<TState>) => TSelected, options?: UseStoreOptions<TSelected>): TSelected;
12
+ export declare function useStore<TState, TSelected = NoInfer<TState>>(store: Derived<TState, any>, selector?: (state: NoInfer<TState>) => TSelected, options?: UseStoreOptions<TSelected>): TSelected;
13
+ export declare function shallow<T>(objA: T, objB: T): boolean;
@@ -0,0 +1,71 @@
1
+ import { useSyncExternalStore } from "preact/compat";
2
+ import { useRef } from "preact/hooks";
3
+ export * from "@tanstack/store";
4
+ function useSyncExternalStoreWithSelector(subscribe, getSnapshot, selector, isEqual) {
5
+ const selectedSnapshotRef = useRef();
6
+ const getSelectedSnapshot = () => {
7
+ const snapshot = getSnapshot();
8
+ const selected = selector(snapshot);
9
+ if (selectedSnapshotRef.current === void 0 || !isEqual(selectedSnapshotRef.current, selected)) {
10
+ selectedSnapshotRef.current = selected;
11
+ }
12
+ return selectedSnapshotRef.current;
13
+ };
14
+ return useSyncExternalStore(subscribe, getSelectedSnapshot);
15
+ }
16
+ function useStore(store, selector = (d) => d, options = {}) {
17
+ const equal = options.equal ?? shallow;
18
+ const slice = useSyncExternalStoreWithSelector(
19
+ store.subscribe,
20
+ () => store.state,
21
+ selector,
22
+ equal
23
+ );
24
+ return slice;
25
+ }
26
+ function shallow(objA, objB) {
27
+ if (Object.is(objA, objB)) {
28
+ return true;
29
+ }
30
+ if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
31
+ return false;
32
+ }
33
+ if (objA instanceof Map && objB instanceof Map) {
34
+ if (objA.size !== objB.size) return false;
35
+ for (const [k, v] of objA) {
36
+ if (!objB.has(k) || !Object.is(v, objB.get(k))) return false;
37
+ }
38
+ return true;
39
+ }
40
+ if (objA instanceof Set && objB instanceof Set) {
41
+ if (objA.size !== objB.size) return false;
42
+ for (const v of objA) {
43
+ if (!objB.has(v)) return false;
44
+ }
45
+ return true;
46
+ }
47
+ if (objA instanceof Date && objB instanceof Date) {
48
+ if (objA.getTime() !== objB.getTime()) return false;
49
+ return true;
50
+ }
51
+ const keysA = getOwnKeys(objA);
52
+ if (keysA.length !== getOwnKeys(objB).length) {
53
+ return false;
54
+ }
55
+ for (const key of keysA) {
56
+ if (!Object.prototype.hasOwnProperty.call(objB, key) || !Object.is(objA[key], objB[key])) {
57
+ return false;
58
+ }
59
+ }
60
+ return true;
61
+ }
62
+ function getOwnKeys(obj) {
63
+ return Object.keys(obj).concat(
64
+ Object.getOwnPropertySymbols(obj)
65
+ );
66
+ }
67
+ export {
68
+ shallow,
69
+ useStore
70
+ };
71
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["import { useSyncExternalStore } from 'preact/compat'\nimport { useRef } from 'preact/hooks'\nimport type { Derived, Store } from '@tanstack/store'\n\nexport * from '@tanstack/store'\n\n/**\n * @private\n */\nexport type NoInfer<T> = [T][T extends any ? 0 : never]\ntype EqualityFn<T> = (objA: T, objB: T) => boolean\ninterface UseStoreOptions<T> {\n equal?: EqualityFn<T>\n}\n\nfunction useSyncExternalStoreWithSelector<TSnapshot, TSelected>(\n subscribe: (onStoreChange: () => void) => () => void,\n getSnapshot: () => TSnapshot,\n selector: (snapshot: TSnapshot) => TSelected,\n isEqual: (a: TSelected, b: TSelected) => boolean,\n): TSelected {\n const selectedSnapshotRef = useRef<TSelected | undefined>()\n\n const getSelectedSnapshot = () => {\n const snapshot = getSnapshot()\n const selected = selector(snapshot)\n\n if (\n selectedSnapshotRef.current === undefined ||\n !isEqual(selectedSnapshotRef.current, selected)\n ) {\n selectedSnapshotRef.current = selected\n }\n\n return selectedSnapshotRef.current\n }\n\n return useSyncExternalStore(subscribe, getSelectedSnapshot)\n}\n\nexport function useStore<TState, TSelected = NoInfer<TState>>(\n store: Store<TState, any>,\n selector?: (state: NoInfer<TState>) => TSelected,\n options?: UseStoreOptions<TSelected>,\n): TSelected\nexport function useStore<TState, TSelected = NoInfer<TState>>(\n store: Derived<TState, any>,\n selector?: (state: NoInfer<TState>) => TSelected,\n options?: UseStoreOptions<TSelected>,\n): TSelected\nexport function useStore<TState, TSelected = NoInfer<TState>>(\n store: Store<TState, any> | Derived<TState, any>,\n selector: (state: NoInfer<TState>) => TSelected = (d) => d as any,\n options: UseStoreOptions<TSelected> = {},\n): TSelected {\n const equal = options.equal ?? shallow\n const slice = useSyncExternalStoreWithSelector(\n store.subscribe,\n () => store.state,\n selector,\n equal,\n )\n\n return slice\n}\n\nexport function shallow<T>(objA: T, objB: T) {\n if (Object.is(objA, objB)) {\n return true\n }\n\n if (\n typeof objA !== 'object' ||\n objA === null ||\n typeof objB !== 'object' ||\n objB === null\n ) {\n return false\n }\n\n if (objA instanceof Map && objB instanceof Map) {\n if (objA.size !== objB.size) return false\n for (const [k, v] of objA) {\n if (!objB.has(k) || !Object.is(v, objB.get(k))) return false\n }\n return true\n }\n\n if (objA instanceof Set && objB instanceof Set) {\n if (objA.size !== objB.size) return false\n for (const v of objA) {\n if (!objB.has(v)) return false\n }\n return true\n }\n\n if (objA instanceof Date && objB instanceof Date) {\n if (objA.getTime() !== objB.getTime()) return false\n return true\n }\n\n const keysA = getOwnKeys(objA)\n if (keysA.length !== getOwnKeys(objB).length) {\n return false\n }\n\n for (const key of keysA) {\n if (\n !Object.prototype.hasOwnProperty.call(objB, key as string) ||\n !Object.is(objA[key as keyof T], objB[key as keyof T])\n ) {\n return false\n }\n }\n return true\n}\n\nfunction getOwnKeys(obj: object): Array<string | symbol> {\n return (Object.keys(obj) as Array<string | symbol>).concat(\n Object.getOwnPropertySymbols(obj),\n )\n}\n\n// force\n"],"names":[],"mappings":";;;AAeA,SAAS,iCACP,WACA,aACA,UACA,SACW;AACX,QAAM,sBAAsB,OAAA;AAE5B,QAAM,sBAAsB,MAAM;AAChC,UAAM,WAAW,YAAA;AACjB,UAAM,WAAW,SAAS,QAAQ;AAElC,QACE,oBAAoB,YAAY,UAChC,CAAC,QAAQ,oBAAoB,SAAS,QAAQ,GAC9C;AACA,0BAAoB,UAAU;AAAA,IAChC;AAEA,WAAO,oBAAoB;AAAA,EAC7B;AAEA,SAAO,qBAAqB,WAAW,mBAAmB;AAC5D;AAYO,SAAS,SACd,OACA,WAAkD,CAAC,MAAM,GACzD,UAAsC,IAC3B;AACX,QAAM,QAAQ,QAAQ,SAAS;AAC/B,QAAM,QAAQ;AAAA,IACZ,MAAM;AAAA,IACN,MAAM,MAAM;AAAA,IACZ;AAAA,IACA;AAAA,EAAA;AAGF,SAAO;AACT;AAEO,SAAS,QAAW,MAAS,MAAS;AAC3C,MAAI,OAAO,GAAG,MAAM,IAAI,GAAG;AACzB,WAAO;AAAA,EACT;AAEA,MACE,OAAO,SAAS,YAChB,SAAS,QACT,OAAO,SAAS,YAChB,SAAS,MACT;AACA,WAAO;AAAA,EACT;AAEA,MAAI,gBAAgB,OAAO,gBAAgB,KAAK;AAC9C,QAAI,KAAK,SAAS,KAAK,KAAM,QAAO;AACpC,eAAW,CAAC,GAAG,CAAC,KAAK,MAAM;AACzB,UAAI,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,KAAK,IAAI,CAAC,CAAC,EAAG,QAAO;AAAA,IACzD;AACA,WAAO;AAAA,EACT;AAEA,MAAI,gBAAgB,OAAO,gBAAgB,KAAK;AAC9C,QAAI,KAAK,SAAS,KAAK,KAAM,QAAO;AACpC,eAAW,KAAK,MAAM;AACpB,UAAI,CAAC,KAAK,IAAI,CAAC,EAAG,QAAO;AAAA,IAC3B;AACA,WAAO;AAAA,EACT;AAEA,MAAI,gBAAgB,QAAQ,gBAAgB,MAAM;AAChD,QAAI,KAAK,QAAA,MAAc,KAAK,QAAA,EAAW,QAAO;AAC9C,WAAO;AAAA,EACT;AAEA,QAAM,QAAQ,WAAW,IAAI;AAC7B,MAAI,MAAM,WAAW,WAAW,IAAI,EAAE,QAAQ;AAC5C,WAAO;AAAA,EACT;AAEA,aAAW,OAAO,OAAO;AACvB,QACE,CAAC,OAAO,UAAU,eAAe,KAAK,MAAM,GAAa,KACzD,CAAC,OAAO,GAAG,KAAK,GAAc,GAAG,KAAK,GAAc,CAAC,GACrD;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAAS,WAAW,KAAqC;AACvD,SAAQ,OAAO,KAAK,GAAG,EAA6B;AAAA,IAClD,OAAO,sBAAsB,GAAG;AAAA,EAAA;AAEpC;"}
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "@tanstack/preact-store",
3
+ "version": "0.10.0",
4
+ "description": "Framework agnostic type-safe store w/ reactive framework adapters",
5
+ "author": "Tanner Linsley",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/TanStack/store.git",
10
+ "directory": "packages/preact-store"
11
+ },
12
+ "homepage": "https://tanstack.com/store",
13
+ "funding": {
14
+ "type": "github",
15
+ "url": "https://github.com/sponsors/tannerlinsley"
16
+ },
17
+ "keywords": [
18
+ "store",
19
+ "preact",
20
+ "typescript"
21
+ ],
22
+ "type": "module",
23
+ "types": "dist/esm/index.d.ts",
24
+ "main": "dist/cjs/index.cjs",
25
+ "module": "dist/esm/index.js",
26
+ "exports": {
27
+ ".": {
28
+ "import": {
29
+ "types": "./dist/esm/index.d.ts",
30
+ "default": "./dist/esm/index.js"
31
+ },
32
+ "require": {
33
+ "types": "./dist/cjs/index.d.cts",
34
+ "default": "./dist/cjs/index.cjs"
35
+ }
36
+ },
37
+ "./package.json": "./package.json"
38
+ },
39
+ "sideEffects": false,
40
+ "files": [
41
+ "dist",
42
+ "src"
43
+ ],
44
+ "dependencies": {
45
+ "@tanstack/store": "0.8.0"
46
+ },
47
+ "devDependencies": {
48
+ "@preact/preset-vite": "^2.10.2",
49
+ "@testing-library/preact": "^3.2.4",
50
+ "eslint-config-preact": "^2.0.0",
51
+ "jsdom": "^25.0.1",
52
+ "preact": "^10.27.2",
53
+ "typescript-eslint": "^8.48.0",
54
+ "vitest": "^3.2.4"
55
+ },
56
+ "peerDependencies": {
57
+ "preact": "^10.0.0"
58
+ },
59
+ "scripts": {
60
+ "clean": "premove ./dist ./coverage",
61
+ "test:eslint": "eslint ./src ./tests",
62
+ "test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
63
+ "test:types:ts50": "node ../../node_modules/typescript50/lib/tsc.js",
64
+ "test:types:ts51": "node ../../node_modules/typescript51/lib/tsc.js",
65
+ "test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js",
66
+ "test:types:ts53": "node ../../node_modules/typescript53/lib/tsc.js",
67
+ "test:types:ts54": "tsc",
68
+ "test:lib": "vitest",
69
+ "test:lib:dev": "pnpm run test:lib --watch",
70
+ "test:build": "publint --strict",
71
+ "build": "vite build"
72
+ }
73
+ }
package/src/index.ts ADDED
@@ -0,0 +1,124 @@
1
+ import { useSyncExternalStore } from 'preact/compat'
2
+ import { useRef } from 'preact/hooks'
3
+ import type { Derived, Store } from '@tanstack/store'
4
+
5
+ export * from '@tanstack/store'
6
+
7
+ /**
8
+ * @private
9
+ */
10
+ export type NoInfer<T> = [T][T extends any ? 0 : never]
11
+ type EqualityFn<T> = (objA: T, objB: T) => boolean
12
+ interface UseStoreOptions<T> {
13
+ equal?: EqualityFn<T>
14
+ }
15
+
16
+ function useSyncExternalStoreWithSelector<TSnapshot, TSelected>(
17
+ subscribe: (onStoreChange: () => void) => () => void,
18
+ getSnapshot: () => TSnapshot,
19
+ selector: (snapshot: TSnapshot) => TSelected,
20
+ isEqual: (a: TSelected, b: TSelected) => boolean,
21
+ ): TSelected {
22
+ const selectedSnapshotRef = useRef<TSelected | undefined>()
23
+
24
+ const getSelectedSnapshot = () => {
25
+ const snapshot = getSnapshot()
26
+ const selected = selector(snapshot)
27
+
28
+ if (
29
+ selectedSnapshotRef.current === undefined ||
30
+ !isEqual(selectedSnapshotRef.current, selected)
31
+ ) {
32
+ selectedSnapshotRef.current = selected
33
+ }
34
+
35
+ return selectedSnapshotRef.current
36
+ }
37
+
38
+ return useSyncExternalStore(subscribe, getSelectedSnapshot)
39
+ }
40
+
41
+ export function useStore<TState, TSelected = NoInfer<TState>>(
42
+ store: Store<TState, any>,
43
+ selector?: (state: NoInfer<TState>) => TSelected,
44
+ options?: UseStoreOptions<TSelected>,
45
+ ): TSelected
46
+ export function useStore<TState, TSelected = NoInfer<TState>>(
47
+ store: Derived<TState, any>,
48
+ selector?: (state: NoInfer<TState>) => TSelected,
49
+ options?: UseStoreOptions<TSelected>,
50
+ ): TSelected
51
+ export function useStore<TState, TSelected = NoInfer<TState>>(
52
+ store: Store<TState, any> | Derived<TState, any>,
53
+ selector: (state: NoInfer<TState>) => TSelected = (d) => d as any,
54
+ options: UseStoreOptions<TSelected> = {},
55
+ ): TSelected {
56
+ const equal = options.equal ?? shallow
57
+ const slice = useSyncExternalStoreWithSelector(
58
+ store.subscribe,
59
+ () => store.state,
60
+ selector,
61
+ equal,
62
+ )
63
+
64
+ return slice
65
+ }
66
+
67
+ export function shallow<T>(objA: T, objB: T) {
68
+ if (Object.is(objA, objB)) {
69
+ return true
70
+ }
71
+
72
+ if (
73
+ typeof objA !== 'object' ||
74
+ objA === null ||
75
+ typeof objB !== 'object' ||
76
+ objB === null
77
+ ) {
78
+ return false
79
+ }
80
+
81
+ if (objA instanceof Map && objB instanceof Map) {
82
+ if (objA.size !== objB.size) return false
83
+ for (const [k, v] of objA) {
84
+ if (!objB.has(k) || !Object.is(v, objB.get(k))) return false
85
+ }
86
+ return true
87
+ }
88
+
89
+ if (objA instanceof Set && objB instanceof Set) {
90
+ if (objA.size !== objB.size) return false
91
+ for (const v of objA) {
92
+ if (!objB.has(v)) return false
93
+ }
94
+ return true
95
+ }
96
+
97
+ if (objA instanceof Date && objB instanceof Date) {
98
+ if (objA.getTime() !== objB.getTime()) return false
99
+ return true
100
+ }
101
+
102
+ const keysA = getOwnKeys(objA)
103
+ if (keysA.length !== getOwnKeys(objB).length) {
104
+ return false
105
+ }
106
+
107
+ for (const key of keysA) {
108
+ if (
109
+ !Object.prototype.hasOwnProperty.call(objB, key as string) ||
110
+ !Object.is(objA[key as keyof T], objB[key as keyof T])
111
+ ) {
112
+ return false
113
+ }
114
+ }
115
+ return true
116
+ }
117
+
118
+ function getOwnKeys(obj: object): Array<string | symbol> {
119
+ return (Object.keys(obj) as Array<string | symbol>).concat(
120
+ Object.getOwnPropertySymbols(obj),
121
+ )
122
+ }
123
+
124
+ // force