@tanstack/react-store 0.0.1-beta.53

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,60 @@
1
+ {
2
+ "version": 2,
3
+ "tree": {
4
+ "name": "root",
5
+ "children": [
6
+ {
7
+ "name": "index.production.js",
8
+ "children": [
9
+ {
10
+ "name": "packages/react-store/src/index.tsx",
11
+ "uid": "da8f-162"
12
+ }
13
+ ]
14
+ }
15
+ ],
16
+ "isRoot": true
17
+ },
18
+ "nodeParts": {
19
+ "da8f-162": {
20
+ "renderedLength": 1347,
21
+ "gzipLength": 484,
22
+ "brotliLength": 0,
23
+ "mainUid": "da8f-161"
24
+ }
25
+ },
26
+ "nodeMetas": {
27
+ "da8f-161": {
28
+ "id": "/packages/react-store/src/index.tsx",
29
+ "moduleParts": {
30
+ "index.production.js": "da8f-162"
31
+ },
32
+ "imported": [
33
+ {
34
+ "uid": "da8f-163"
35
+ }
36
+ ],
37
+ "importedBy": [],
38
+ "isEntry": true
39
+ },
40
+ "da8f-163": {
41
+ "id": "use-sync-external-store/shim/with-selector",
42
+ "moduleParts": {},
43
+ "imported": [],
44
+ "importedBy": [
45
+ {
46
+ "uid": "da8f-161"
47
+ }
48
+ ],
49
+ "isExternal": true
50
+ }
51
+ },
52
+ "env": {
53
+ "rollup": "2.79.1"
54
+ },
55
+ "options": {
56
+ "gzip": true,
57
+ "brotli": false,
58
+ "sourcemap": false
59
+ }
60
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * react-store
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
+ import { AnyUpdater, Store } from '@tanstack/store';
12
+
13
+ declare function useStore<TState, TSelected = TState, TUpdater extends AnyUpdater = AnyUpdater>(store: Store<TState, TUpdater>, selector?: (state: TState) => TSelected, compareShallow?: boolean): TSelected;
14
+
15
+ export { useStore };
@@ -0,0 +1,68 @@
1
+ /**
2
+ * react-store
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, require('use-sync-external-store/shim/with-selector')) :
13
+ typeof define === 'function' && define.amd ? define(['exports', 'use-sync-external-store/shim/with-selector'], factory) :
14
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactLoaders = {}, global.withSelector));
15
+ })(this, (function (exports, withSelector) { 'use strict';
16
+
17
+ function useStore(store, selector = d => d, compareShallow) {
18
+ const slice = withSelector.useSyncExternalStoreWithSelector(store.subscribe, () => store.state, () => store.state, selector, compareShallow ? shallow : undefined);
19
+ return slice;
20
+ }
21
+ function shallow(objA, objB) {
22
+ if (Object.is(objA, objB)) {
23
+ return true;
24
+ }
25
+ if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
26
+ return false;
27
+ }
28
+
29
+ // if (objA instanceof Map && objB instanceof Map) {
30
+ // if (objA.size !== objB.size) return false
31
+
32
+ // for (const [key, value] of objA) {
33
+ // if (!Object.is(value, objB.get(key))) {
34
+ // return false
35
+ // }
36
+ // }
37
+ // return true
38
+ // }
39
+
40
+ // if (objA instanceof Set && objB instanceof Set) {
41
+ // if (objA.size !== objB.size) return false
42
+
43
+ // for (const value of objA) {
44
+ // if (!objB.has(value)) {
45
+ // return false
46
+ // }
47
+ // }
48
+ // return true
49
+ // }
50
+
51
+ const keysA = Object.keys(objA);
52
+ if (keysA.length !== Object.keys(objB).length) {
53
+ return false;
54
+ }
55
+ for (let i = 0; i < keysA.length; i++) {
56
+ if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) {
57
+ return false;
58
+ }
59
+ }
60
+ return true;
61
+ }
62
+
63
+ exports.useStore = useStore;
64
+
65
+ Object.defineProperty(exports, '__esModule', { value: true });
66
+
67
+ }));
68
+ //# sourceMappingURL=index.development.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.development.js","sources":["../../src/index.tsx"],"sourcesContent":["import { Store, AnyUpdater } from '@tanstack/store'\n\nimport { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'\n\nexport function useStore<\n TState,\n TSelected = TState,\n TUpdater extends AnyUpdater = AnyUpdater,\n>(\n store: Store<TState, TUpdater>,\n selector: (state: TState) => TSelected = (d) => d as any,\n compareShallow?: boolean,\n) {\n const slice = useSyncExternalStoreWithSelector(\n store.subscribe,\n () => store.state,\n () => store.state,\n selector,\n compareShallow ? shallow : undefined,\n )\n\n return slice\n}\n\nfunction 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\n // for (const [key, value] of objA) {\n // if (!Object.is(value, objB.get(key))) {\n // return false\n // }\n // }\n // return true\n // }\n\n // if (objA instanceof Set && objB instanceof Set) {\n // if (objA.size !== objB.size) return false\n\n // for (const value of objA) {\n // if (!objB.has(value)) {\n // return false\n // }\n // }\n // return true\n // }\n\n const keysA = Object.keys(objA)\n if (keysA.length !== Object.keys(objB).length) {\n return false\n }\n\n for (let i = 0; i < keysA.length; i++) {\n if (\n !Object.prototype.hasOwnProperty.call(objB, keysA[i] as string) ||\n !Object.is(objA[keysA[i] as keyof T], objB[keysA[i] as keyof T])\n ) {\n return false\n }\n }\n return true\n}\n"],"names":["useStore","store","selector","d","compareShallow","slice","useSyncExternalStoreWithSelector","subscribe","state","shallow","undefined","objA","objB","Object","is","keysA","keys","length","i","prototype","hasOwnProperty","call"],"mappings":";;;;;;;;;;;;;;;;EAIO,SAASA,QAAQ,CAKtB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C,CAAC,CAAY,EAAEN,IAAI,CAACG,KAAK,CAACG,CAAC,CAAC,CAAY,CAAC,EAChE;EACA,MAAA,OAAO,KAAK,CAAA;EACd,KAAA;EACF,GAAA;EACA,EAAA,OAAO,IAAI,CAAA;EACb;;;;;;;;;;"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * react-store
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(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("use-sync-external-store/shim/with-selector")):"function"==typeof define&&define.amd?define(["exports","use-sync-external-store/shim/with-selector"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactLoaders={},e.withSelector)}(this,(function(e,t){"use strict";function n(e,t){if(Object.is(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;const n=Object.keys(e);if(n.length!==Object.keys(t).length)return!1;for(let o=0;o<n.length;o++)if(!Object.prototype.hasOwnProperty.call(t,n[o])||!Object.is(e[n[o]],t[n[o]]))return!1;return!0}e.useStore=function(e,o=(e=>e),r){return t.useSyncExternalStoreWithSelector(e.subscribe,(()=>e.state),(()=>e.state),o,r?n:void 0)},Object.defineProperty(e,"__esModule",{value:!0})}));
12
+ //# sourceMappingURL=index.production.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.production.js","sources":["../../src/index.tsx"],"sourcesContent":["import { Store, AnyUpdater } from '@tanstack/store'\n\nimport { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'\n\nexport function useStore<\n TState,\n TSelected = TState,\n TUpdater extends AnyUpdater = AnyUpdater,\n>(\n store: Store<TState, TUpdater>,\n selector: (state: TState) => TSelected = (d) => d as any,\n compareShallow?: boolean,\n) {\n const slice = useSyncExternalStoreWithSelector(\n store.subscribe,\n () => store.state,\n () => store.state,\n selector,\n compareShallow ? shallow : undefined,\n )\n\n return slice\n}\n\nfunction 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\n // for (const [key, value] of objA) {\n // if (!Object.is(value, objB.get(key))) {\n // return false\n // }\n // }\n // return true\n // }\n\n // if (objA instanceof Set && objB instanceof Set) {\n // if (objA.size !== objB.size) return false\n\n // for (const value of objA) {\n // if (!objB.has(value)) {\n // return false\n // }\n // }\n // return true\n // }\n\n const keysA = Object.keys(objA)\n if (keysA.length !== Object.keys(objB).length) {\n return false\n }\n\n for (let i = 0; i < keysA.length; i++) {\n if (\n !Object.prototype.hasOwnProperty.call(objB, keysA[i] as string) ||\n !Object.is(objA[keysA[i] as keyof T], objB[keysA[i] as keyof T])\n ) {\n return false\n }\n }\n return true\n}\n"],"names":["shallow","objA","objB","Object","is","keysA","keys","length","i","prototype","hasOwnProperty","call","store","selector","d","compareShallow","useSyncExternalStoreWithSelector","subscribe","state","undefined"],"mappings":";;;;;;;;;;wWAwBA,SAASA,EAAWC,EAASC,GAC3B,GAAIC,OAAOC,GAAGH,EAAMC,GAClB,OAAO,EAGT,GACkB,iBAATD,GACE,OAATA,GACgB,iBAATC,GACE,OAATA,EAEA,OAAO,EAyBT,MAAMG,EAAQF,OAAOG,KAAKL,GAC1B,GAAII,EAAME,SAAWJ,OAAOG,KAAKJ,GAAMK,OACrC,OAAO,EAGT,IAAK,IAAIC,EAAI,EAAGA,EAAIH,EAAME,OAAQC,IAChC,IACGL,OAAOM,UAAUC,eAAeC,KAAKT,EAAMG,EAAMG,MACjDL,OAAOC,GAAGH,EAAKI,EAAMG,IAAgBN,EAAKG,EAAMG,KAEjD,OAAO,EAGX,OAAO,CACT,YAtEO,SAKLI,EACAC,EAA0CC,IAAMA,GAChDC,GAUA,OARcC,EAAgCA,iCAC5CJ,EAAMK,WACN,IAAML,EAAMM,QACZ,IAAMN,EAAMM,OACZL,EACAE,EAAiBf,OAAUmB,EAI/B"}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@tanstack/react-store",
3
+ "author": "Tanner Linsley",
4
+ "version": "0.0.1-beta.53",
5
+ "license": "MIT",
6
+ "repository": "tanstack/react-store",
7
+ "homepage": "https://tanstack.com/",
8
+ "description": "",
9
+ "publishConfig": {
10
+ "registry": "https://registry.npmjs.org/"
11
+ },
12
+ "keywords": [
13
+ "loaders",
14
+ "react",
15
+ "typescript"
16
+ ],
17
+ "funding": {
18
+ "type": "github",
19
+ "url": "https://github.com/sponsors/tannerlinsley"
20
+ },
21
+ "module": "build/esm/index.js",
22
+ "main": "build/cjs/index.js",
23
+ "browser": "build/umd/index.production.js",
24
+ "types": "build/types/index.d.ts",
25
+ "engines": {
26
+ "node": ">=12"
27
+ },
28
+ "files": [
29
+ "build/**",
30
+ "src"
31
+ ],
32
+ "sideEffects": false,
33
+ "peerDependencies": {
34
+ "react": ">=16",
35
+ "react-dom": ">=16"
36
+ },
37
+ "dependencies": {
38
+ "use-sync-external-store": "^1.2.0",
39
+ "@tanstack/loaders": "0.0.1-beta.52"
40
+ }
41
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,75 @@
1
+ import { Store, AnyUpdater } from '@tanstack/store'
2
+
3
+ import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'
4
+
5
+ export function useStore<
6
+ TState,
7
+ TSelected = TState,
8
+ TUpdater extends AnyUpdater = AnyUpdater,
9
+ >(
10
+ store: Store<TState, TUpdater>,
11
+ selector: (state: TState) => TSelected = (d) => d as any,
12
+ compareShallow?: boolean,
13
+ ) {
14
+ const slice = useSyncExternalStoreWithSelector(
15
+ store.subscribe,
16
+ () => store.state,
17
+ () => store.state,
18
+ selector,
19
+ compareShallow ? shallow : undefined,
20
+ )
21
+
22
+ return slice
23
+ }
24
+
25
+ function shallow<T>(objA: T, objB: T) {
26
+ if (Object.is(objA, objB)) {
27
+ return true
28
+ }
29
+
30
+ if (
31
+ typeof objA !== 'object' ||
32
+ objA === null ||
33
+ typeof objB !== 'object' ||
34
+ objB === null
35
+ ) {
36
+ return false
37
+ }
38
+
39
+ // if (objA instanceof Map && objB instanceof Map) {
40
+ // if (objA.size !== objB.size) return false
41
+
42
+ // for (const [key, value] of objA) {
43
+ // if (!Object.is(value, objB.get(key))) {
44
+ // return false
45
+ // }
46
+ // }
47
+ // return true
48
+ // }
49
+
50
+ // if (objA instanceof Set && objB instanceof Set) {
51
+ // if (objA.size !== objB.size) return false
52
+
53
+ // for (const value of objA) {
54
+ // if (!objB.has(value)) {
55
+ // return false
56
+ // }
57
+ // }
58
+ // return true
59
+ // }
60
+
61
+ const keysA = Object.keys(objA)
62
+ if (keysA.length !== Object.keys(objB).length) {
63
+ return false
64
+ }
65
+
66
+ for (let i = 0; i < keysA.length; i++) {
67
+ if (
68
+ !Object.prototype.hasOwnProperty.call(objB, keysA[i] as string) ||
69
+ !Object.is(objA[keysA[i] as keyof T], objB[keysA[i] as keyof T])
70
+ ) {
71
+ return false
72
+ }
73
+ }
74
+ return true
75
+ }