@tinkered/react 0.18.0 → 0.18.2

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 CHANGED
@@ -18,18 +18,18 @@ On Bun, the main runtime:
18
18
 
19
19
  ```bash
20
20
  R=https://github.com/lagz0ne/tinkered/\
21
- releases/download/v0.18.0
22
- bun add $R/tinker-core-0.18.0.tgz \
23
- $R/tinker-react-0.18.0.tgz
21
+ releases/download/v0.18.2
22
+ bun add $R/tinker-core-0.18.2.tgz \
23
+ $R/tinker-react-0.18.2.tgz
24
24
  ```
25
25
 
26
26
  On Node 22.18 or later, with npm:
27
27
 
28
28
  ```bash
29
29
  R=https://github.com/lagz0ne/tinkered/\
30
- releases/download/v0.18.0
31
- npm install $R/tinker-core-0.18.0.tgz \
32
- $R/tinker-react-0.18.0.tgz
30
+ releases/download/v0.18.2
31
+ npm install $R/tinker-core-0.18.2.tgz \
32
+ $R/tinker-react-0.18.2.tgz
33
33
  ```
34
34
 
35
35
  Each version's URLs are in the
@@ -73,6 +73,7 @@ function App() {
73
73
 
74
74
  - `ScopeProvider` supplies an app-owned scope with `scope={handle}`.
75
75
  Its `create` form owns the scope and closes it on unmount.
76
+ A new `scope` value mounts the subtree again (ADR 0144).
76
77
  - A root given `capsules` is a starting handle (Core ADR 0128).
77
78
  The provider waits for its `ready` with React's `use`.
78
79
  A `<Suspense>` above shows its fallback until start ends.
@@ -201,6 +202,7 @@ This appendix states each behaviour the seam tests pin, one line per promise, gr
201
202
 
202
203
  - The subtree uses the exact app-owned scope, which the provider never closes.
203
204
  - Under StrictMode, create mode leaks no scope and never exposes a closed one.
205
+ - A new scope on the provider mounts its subtree again.
204
206
 
205
207
  ### useScope
206
208
 
@@ -221,6 +223,8 @@ This appendix states each behaviour the seam tests pin, one line per promise, gr
221
223
  - The writable setter keeps its identity across a cell update.
222
224
  - Reading through a new scope shows the new scope's value.
223
225
  - Reads a cell and re-renders when it is set externally.
226
+ - A render after the scope closed raises Disposed.
227
+ - useData with no provider raises NoProvider.
224
228
  - An unchanged object value keeps its identity across a parent re-render.
225
229
  - A write after unmount neither updates the removed subtree nor breaks the still-open scope.
226
230
 
package/dist/index.mjs CHANGED
@@ -22,11 +22,21 @@ function isError(value, kind) {
22
22
  * renderer with no DOM that does commit takes this path too, and its views do not re-render on a
23
23
  * change: React Native, a Web Worker renderer, Ink, react-test-renderer. */
24
24
  const IS_SERVER = typeof document === "undefined";
25
+ let serial = 0;
25
26
  const ScopeContext = createContext(void 0);
26
27
  /** An independent scope clears an outer React session's reset key. */
27
- const bind = (scope) => ({
28
+ const bind = (scope, ns) => ({
28
29
  scope,
29
- ns: void 0
30
+ ns,
31
+ key: ++serial
32
+ });
33
+ /** Provide a Bound to a subtree. The key makes React mount the subtree again for a new Bound
34
+ * (ADR 0144). The owning providers did that before the key: they render nothing between two
35
+ * scopes. */
36
+ const provide = (bound, children) => createElement(ScopeContext.Provider, {
37
+ key: bound.key,
38
+ value: bound,
39
+ children
30
40
  });
31
41
  /** Start a scope's teardown without awaiting; a scope owns its own shutdown and never throws
32
42
  * (ADR 0027), so the unmounting subtree does not wait on the result. */
@@ -42,10 +52,7 @@ const identity = (value) => value;
42
52
  function StartedScopeProvider(props) {
43
53
  const scope = use(props.scope.ready);
44
54
  const value = useMemo(() => bind(scope), [scope]);
45
- return createElement(ScopeContext.Provider, {
46
- value,
47
- children: props.children
48
- });
55
+ return provide(value, props.children);
49
56
  }
50
57
  const WAITING = { kind: "waiting" };
51
58
  /** Own a created scope for a subtree: create it in an effect (never during render, so a discarded
@@ -85,10 +92,7 @@ function OwnedScopeProvider(props) {
85
92
  }, []);
86
93
  if (owned.kind === "failed") throw owned.error;
87
94
  if (owned.kind === "waiting") return null;
88
- return createElement(ScopeContext.Provider, {
89
- value: owned.bound,
90
- children: props.children
91
- });
95
+ return provide(owned.bound, props.children);
92
96
  }
93
97
  /** Put a `@tinkered/core` scope on React context for the subtree. Hooks resolve against the nearest
94
98
  * provider's scope. This independent scope clears an outer React session's reset key. */
@@ -104,10 +108,7 @@ function ScopeProvider(props) {
104
108
  scope,
105
109
  children: props.children
106
110
  });
107
- return createElement(ScopeContext.Provider, {
108
- value,
109
- children: props.children
110
- });
111
+ return provide(value, props.children);
111
112
  }
112
113
  /** Open a child session for a subtree: created on mount from the nearest scope, force-closed on
113
114
  * unmount — a React subtree's mount lifetime IS a session lifetime (ADR 0031). Hooks under it resolve
@@ -133,10 +134,7 @@ function SessionProvider(props) {
133
134
  const [head] = ns === void 0 ? [inherited] : "tags" in ns ? [ns] : ns;
134
135
  setOwned({
135
136
  parent,
136
- bound: {
137
- scope: session,
138
- ns: head
139
- }
137
+ bound: bind(session, head)
140
138
  });
141
139
  return () => {
142
140
  setOwned(void 0);
@@ -144,10 +142,7 @@ function SessionProvider(props) {
144
142
  };
145
143
  }, [parent, inherited]);
146
144
  if (owned === void 0 || owned.parent !== parent) return null;
147
- return createElement(ScopeContext.Provider, {
148
- value: owned.bound,
149
- children: props.children
150
- });
145
+ return provide(owned.bound, props.children);
151
146
  }
152
147
  /** Read the nearest scope `Handle`. Raises `NoProvider` when used outside a {@link ScopeProvider}. */
153
148
  function useScope() {
@@ -185,15 +180,30 @@ function readData(scope, cell, a, b) {
185
180
  return [value, (next) => scope.controller(cell).set(next)];
186
181
  }
187
182
  function useDataStore(cell, selector, isEqual) {
188
- const controller = readableOf(useScope(), cell);
189
- const own = useRef(void 0);
183
+ const held = useHeld();
184
+ const controller = readableOf(held.bound.scope, cell);
190
185
  if (selector === void 0 && isEqual === void 0) return controller[RAW] ?? createRawStore(controller);
191
- const kept = own.current;
192
- const store = kept !== void 0 && kept.controller === controller ? kept : own.current = createDataStore(controller);
186
+ const kept = held.store;
187
+ const store = kept !== void 0 && kept.controller === controller ? kept : held.store = createDataStore(controller);
193
188
  store.select = selector ?? identity;
194
189
  store.equal = isEqual ?? Object.is;
195
190
  return store;
196
191
  }
192
+ /** The Bound of one `useData` call. The context is read in the first render only: a later render
193
+ * that read it would cost a context item for this component and a check for each sibling
194
+ * (ADR 0144). It is safe because a provider keys its subtree with its Bound. `use` is the context
195
+ * read that React allows inside a condition. */
196
+ function useHeld() {
197
+ const own = useRef(void 0);
198
+ const held = own.current;
199
+ if (held !== void 0) return held;
200
+ const bound = use(ScopeContext);
201
+ if (!bound) raise("NoProvider", { hook: "useScope" });
202
+ return own.current = {
203
+ bound,
204
+ store: void 0
205
+ };
206
+ }
197
207
  /** A capsule's cell export: a read edge (ADR 0123). */
198
208
  const isView = (target) => target.kind !== void 0;
199
209
  /** A writable pair only for a cell: a capsule's cell export answers the bare value. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinkered/react",
3
- "version": "0.18.0",
3
+ "version": "0.18.2",
4
4
  "description": "Tinkered React: a thin React adapter over @tinkered/core.",
5
5
  "keywords": [
6
6
  "tanstack-intent"
@@ -29,12 +29,12 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@tinkered/core": "^0.18.0"
32
+ "@tinkered/core": "^0.18.2"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@stryker-mutator/core": "^10.0.0",
36
36
  "@stryker-mutator/vitest-runner": "^10.0.0",
37
- "@tinkered/core": "^0.18.0",
37
+ "@tinkered/core": "^0.18.2",
38
38
  "@types/node": "^26.1.1",
39
39
  "@types/react": "^19",
40
40
  "@types/react-dom": "^19",
@@ -15,7 +15,7 @@ metadata:
15
15
  type: framework
16
16
  framework: react
17
17
  library: '@tinkered/react'
18
- library_version: '0.18.0'
18
+ library_version: '0.18.2'
19
19
  requires:
20
20
  - '@tinkered/core#core'
21
21
  sources: