@tinkered/react 0.31.10 → 0.31.12

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/dist/index.d.mts CHANGED
@@ -33,16 +33,22 @@ export type ScopeProviderProps = {
33
33
  /** Put a `@tinkered/core` scope on React context for the subtree. Hooks resolve against the nearest
34
34
  * provider's scope. This independent scope clears an outer React session's reset key. */
35
35
  export declare function ScopeProvider(props: ScopeProviderProps): ReactNode;
36
- /** Open a child session for a subtree: created on mount from the nearest scope, force-closed on
37
- * unmount — a React subtree's mount lifetime IS a session lifetime (ADR 0031). Hooks under it resolve
38
- * against the session (the nearest `Handle`), so a `data` write is shadowed and does not reach the
39
- * parent. Created in an effect (StrictMode-safe, like {@link ScopeProvider}'s `create` mode): a
40
- * discarded or replayed mount closes its own session and the next live mount opens a fresh one. The
36
+ /** Open a child session for a subtree: created on mount from the nearest scope, force-closed at
37
+ * most one microtask after unmount — a React subtree's mount lifetime IS a session lifetime (ADR 0031). Hooks
38
+ * under it resolve against the session (the nearest `Handle`), so a `data` write is shadowed and does
39
+ * not reach the parent. Created in an effect (StrictMode-safe, like {@link ScopeProvider}'s `create`
40
+ * mode): StrictMode's first mount closes its session, which no render used yet, and the next live
41
+ * mount opens a fresh one; a replay after the children rendered keeps the session (see below). The
41
42
  * session is published paired with the parent it belongs to; if the nearest scope changes, the old
42
43
  * session is never exposed for the new parent (render null until the effect opens a fresh one).
43
44
  * The reset namespace head is saved with this session, including an inherited key.
44
45
  * `options` apply when the session is created — changing them for the same parent has no effect until
45
- * the provider remounts. */
46
+ * the provider remounts.
47
+ *
48
+ * A session its children never rendered with closes in the cleanup. A rendered one closes one
49
+ * microtask later: StrictMode replays the effect each time a `<Suspense>` shows this provider again,
50
+ * and that replay takes the same session back, so the children keep the data they loaded and do
51
+ * not suspend in a loop. A real unmount has no replay, so the session still closes. */
46
52
  export declare function SessionProvider(props: {
47
53
  readonly children: ReactNode;
48
54
  readonly options?: Scope.Options;
package/dist/index.mjs CHANGED
@@ -110,16 +110,22 @@ function ScopeProvider(props) {
110
110
  });
111
111
  return provide(value, props.children);
112
112
  }
113
- /** Open a child session for a subtree: created on mount from the nearest scope, force-closed on
114
- * unmount — a React subtree's mount lifetime IS a session lifetime (ADR 0031). Hooks under it resolve
115
- * against the session (the nearest `Handle`), so a `data` write is shadowed and does not reach the
116
- * parent. Created in an effect (StrictMode-safe, like {@link ScopeProvider}'s `create` mode): a
117
- * discarded or replayed mount closes its own session and the next live mount opens a fresh one. The
113
+ /** Open a child session for a subtree: created on mount from the nearest scope, force-closed at
114
+ * most one microtask after unmount — a React subtree's mount lifetime IS a session lifetime (ADR 0031). Hooks
115
+ * under it resolve against the session (the nearest `Handle`), so a `data` write is shadowed and does
116
+ * not reach the parent. Created in an effect (StrictMode-safe, like {@link ScopeProvider}'s `create`
117
+ * mode): StrictMode's first mount closes its session, which no render used yet, and the next live
118
+ * mount opens a fresh one; a replay after the children rendered keeps the session (see below). The
118
119
  * session is published paired with the parent it belongs to; if the nearest scope changes, the old
119
120
  * session is never exposed for the new parent (render null until the effect opens a fresh one).
120
121
  * The reset namespace head is saved with this session, including an inherited key.
121
122
  * `options` apply when the session is created — changing them for the same parent has no effect until
122
- * the provider remounts. */
123
+ * the provider remounts.
124
+ *
125
+ * A session its children never rendered with closes in the cleanup. A rendered one closes one
126
+ * microtask later: StrictMode replays the effect each time a `<Suspense>` shows this provider again,
127
+ * and that replay takes the same session back, so the children keep the data they loaded and do
128
+ * not suspend in a loop. A real unmount has no replay, so the session still closes. */
123
129
  function SessionProvider(props) {
124
130
  const above = useBound("useScope");
125
131
  const parent = above.scope;
@@ -127,20 +133,44 @@ function SessionProvider(props) {
127
133
  const optionsRef = useRef(props.options);
128
134
  optionsRef.current = props.options;
129
135
  const [owned, setOwned] = useState(void 0);
136
+ const renderedRef = useRef(owned);
137
+ renderedRef.current = owned;
138
+ const releasedRef = useRef(void 0);
130
139
  useEffect(() => {
140
+ const released = releasedRef.current;
141
+ releasedRef.current = void 0;
142
+ if (released !== void 0 && released.parent === parent && released.inherited === inherited) return () => release(released);
143
+ if (released !== void 0) closeScope(released.bound.scope);
131
144
  const options = optionsRef.current;
132
145
  const session = parent.createSession(options);
133
146
  const ns = options?.ns;
134
147
  const [head] = ns === void 0 ? [inherited] : "tags" in ns ? [ns] : ns;
135
- setOwned({
148
+ const next = {
136
149
  parent,
150
+ inherited,
137
151
  bound: bind(session, head)
138
- });
139
- return () => {
140
- setOwned(void 0);
141
- closeScope(session);
142
152
  };
153
+ setOwned(next);
154
+ return () => release(next);
143
155
  }, [parent, inherited]);
156
+ /** Let go of the effect's session: close it now if no render used it, else hold it for the
157
+ * replay and close it in a microtask the replay did not cancel. That microtask also clears
158
+ * `owned`: an `<Activity>` that hides the provider runs this cleanup and keeps it mounted, so the
159
+ * hidden children must not keep the closed session; it shows a fresh one when the effect runs
160
+ * again. After an unmount React 19 ignores the state update. */
161
+ function release(current) {
162
+ if (renderedRef.current !== current) {
163
+ setOwned(void 0);
164
+ return closeScope(current.bound.scope);
165
+ }
166
+ releasedRef.current = current;
167
+ queueMicrotask(() => {
168
+ if (releasedRef.current !== current) return;
169
+ releasedRef.current = void 0;
170
+ setOwned(void 0);
171
+ closeScope(current.bound.scope);
172
+ });
173
+ }
144
174
  if (owned === void 0 || owned.parent !== parent) return null;
145
175
  return provide(owned.bound, props.children);
146
176
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinkered/react",
3
- "version": "0.31.10",
3
+ "version": "0.31.12",
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.31.10"
32
+ "@tinkered/core": "^0.31.12"
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.31.10",
37
+ "@tinkered/core": "^0.31.12",
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.31.10'
18
+ library_version: '0.31.12'
19
19
  requires:
20
20
  - '@tinkered/core#core'
21
21
  sources: