@bradensbay/globals-react 0.2.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/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # @bradensbay/globals-react
2
+
3
+ The react binding for [Globals](https://github.com/christianGRogers/globals).
4
+
5
+ Full guide: [docs/bindings.md](../../docs/bindings.md).
6
+
7
+ ## The point
8
+
9
+ Reads are synchronous. A render can call `get()` on the line it needs the value, with no
10
+ `await` and no replicated copy of state in the window.
11
+
12
+ Writes are asynchronous, and the return type says so.
13
+
14
+ ## Equality
15
+
16
+ The default is `Object.is`, which is right for a scalar and wrong for a container. Every
17
+ commit builds a fresh decode cache, so a selector returning a nested object returns a new
18
+ proxy each time and notifies on every commit.
19
+
20
+ For a container, use the node comparing variant. An untouched subtree keeps its arena node
21
+ across commits, so it notifies only when the subtree actually changed. The reasoning behind
22
+ why comparing offsets is sound is in [docs/bindings.md](../../docs/bindings.md).
@@ -0,0 +1,55 @@
1
+ import { type ReactNode } from "react";
2
+ import { type ReadableStore, type Snapshot } from "@bradensbay/globals-core";
3
+ export interface StoreProviderProps {
4
+ store: ReadableStore;
5
+ children: ReactNode;
6
+ }
7
+ export declare function StoreProvider({ store, children }: StoreProviderProps): ReactNode;
8
+ export declare function useStore(): ReadableStore;
9
+ /**
10
+ * Subscribe to a slice of state.
11
+ *
12
+ * The selector runs on every render and on every commit. It must be cheap.
13
+ *
14
+ * Selecting a scalar is the shape that always works, because React compares with Object.is
15
+ * and a number is a number. Selecting a container does not: every commit builds a fresh
16
+ * decode cache, so the view is a new proxy each time and the component rerenders on every
17
+ * commit whether or not the subtree changed.
18
+ *
19
+ * For a container, use useNodeSelector, which compares the arena node behind the value. That
20
+ * is the question a container selector is actually asking, and structural sharing makes the
21
+ * answer cheap.
22
+ */
23
+ export declare function useSelector<T>(selector: (state: unknown) => T): T;
24
+ /**
25
+ * Subscribe to a container slice, compared by the arena node behind it.
26
+ *
27
+ * An untouched subtree keeps its arena node across commits, so this rerenders only when the
28
+ * selected subtree actually changed. The cached value is held across renders and replaced
29
+ * only when the node moves, which is what gives useSyncExternalStore the stable identity it
30
+ * needs.
31
+ */
32
+ export declare function useNodeSelector<T>(selector: (state: unknown) => T): T;
33
+ /**
34
+ * Read one path.
35
+ *
36
+ * Cheaper than a selector over the whole state, because it walks the arena directly and
37
+ * never builds views for the nodes on the way down. Prefer it for a leaf value.
38
+ */
39
+ export declare function usePath<T = unknown>(path: readonly (string | number)[]): T;
40
+ /** The whole state. Convenient, and it rerenders on every commit, so use it deliberately. */
41
+ export declare function useGlobalState<T = unknown>(): T;
42
+ /**
43
+ * Hold a snapshot for the lifetime of a component.
44
+ *
45
+ * For a component that must see one consistent version across several reads and across
46
+ * child renders, such as a virtualised list measuring and then rendering rows. The pin is
47
+ * released on unmount, and refreshed when the store commits.
48
+ *
49
+ * Holding a snapshot blocks reclamation of that version, so a component that holds one and
50
+ * never updates will eventually be force advanced and see StaleSnapshotError. That is the
51
+ * bounded retention rule doing its job rather than a bug.
52
+ */
53
+ export declare function usePinnedSnapshot(): Snapshot;
54
+ export type { ReadableStore, Snapshot };
55
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0H,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAC/J,OAAO,EAAY,KAAK,aAAa,EAAE,KAAK,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAgBvF,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,aAAa,CAAC;IACrB,QAAQ,EAAE,SAAS,CAAC;CACrB;AAED,wBAAgB,aAAa,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,kBAAkB,GAAG,SAAS,CAEhF;AAED,wBAAgB,QAAQ,IAAI,aAAa,CAMxC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CA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EAAE,WAAW,CAAC,CAAC;AACnE,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,iBAAiB;IAC/B,MAAM,KAAK,GAAG,QAAQ,EAAE,CAAC;IACzB,MAAM,IAAI,GAAG,MAAM,CAAuB,SAAS,CAAC,CAAC;IAErD,MAAM,SAAS,GAAG,WAAW,CAAC,CAAC,QAAoB,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAC5F,MAAM,UAAU,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAC7D,MAAM,OAAO,GAAG,oBAAoB,CAAC,SAAS,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC;IAExE,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,EAAE;QAC5B,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC;QAChC,OAAO,IAAI,CAAC,OAAO,CAAC;QACpB,uDAAuD;IACzD,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;IAErB,SAAS,CACP,GAAG,EAAE,CAAC,GAAG,EAAE;QACT,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,SAAS,CAAC;IAC3B,CAAC,EACD,EAAE,CACH,CAAC;IAEF,OAAO,QAAQ,CAAC;AAClB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@bradensbay/globals-react",
3
+ "version": "0.2.0",
4
+ "description": "React binding: useSyncExternalStore over a shared memory store.",
5
+ "type": "module",
6
+ "main": "./dist/src/index.js",
7
+ "types": "./dist/src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/src/index.d.ts",
11
+ "import": "./dist/src/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist/src",
16
+ "README.md"
17
+ ],
18
+ "engines": {
19
+ "node": ">=20.11.0"
20
+ },
21
+ "dependencies": {
22
+ "@bradensbay/globals-core": "0.2.0"
23
+ },
24
+ "peerDependencies": {
25
+ "react": ">=18"
26
+ },
27
+ "sideEffects": false,
28
+ "license": "MIT",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/christianGRogers/globals.git",
32
+ "directory": "packages/react"
33
+ }
34
+ }