Repository navigation
feat(core): add the internal useCommittedRef hook for latest-value refs - #11352
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
PR Analysis Report🔎 Preview ·
|
There was a problem hiding this comment.
Approval waits on an engineering owner's decision about spec authority. useCommittedRef implements the latest-value-ref rule that #10161 proposes for architecture:react-component-runtime (DEC-3 and an INV3 exception), but on main INV3 still requires every new Effect to name the external system it synchronizes, and this hook's insertion Effect names none. Either #10161 lands first, or an engineering owner records the decision by commenting /approve-spec <full head SHA> on this pull request; the author may do so if they are an engineering owner. Non-blocking: the file header's @output and @position repeat the hook's JSDoc (useCommittedRef.ts:8-12), with a suggested trim inline.
Claims and files checked
- Claim: the hook writes
currentin a dependency-freeuseInsertionEffect, after each commit and before layout Effects: verified (useCommittedRef.ts:25-27; the layout-order test). - Claim: the quoted "values only" comment: verified (
useCommittedRef.ts:24). - Claim: not in
hooks/index.tsor the package entry, so no new public API: verified (Core's exports reach hooks only throughhooks/index.ts, which does not list it). - Claim: it lives beside the internal
useIsomorphicLayoutEffect: verified (packages/core/src/hooks/). - Claim: no callers move: verified (only its test imports it).
- Claim: no React floor change, and
useInsertionEffectexists in every supported React: verified (peer range>=19.0.0). - Claim: one internal patch Changeset for Core: verified.
- Claim: no workflow or CI script changes: verified.
- Claim: Core has about 176 latest-value refs, about 70 written during render: partly checked (72 render-body ref writes in Core; the total was not counted).
- Claim: six tests pass, with the listed cases: the six cases exist and assert what the description lists; not run.
- Claim: a render-phase variant fails both dropped-render tests: not run.
- Claim: the new tests fail on
mainbecause the module is missing: the module is absent onmain; not run. - Claim: ESLint, Prettier, typecheck, provenance, Changeset, knowledge and pre-review checks pass: not rerun; the pre-review ref-copy rule does not match
useInsertionEffect, so zero findings is expected. - Changeset: an internal hook that keeps a ref at the latest committed prop or callback, used by nothing, with nothing new for apps to import: verified.
- File
.changeset/core-use-committed-ref.md: clear. - File
packages/core/src/hooks/useCommittedRef.ts: waits on the spec decision above; non-blocking header note. - File
packages/core/src/hooks/useCommittedRef.test.tsx: clear.
There was a problem hiding this comment.
Approval waits on an engineering owner's decision about spec authority, and one fix is needed.
useCommittedRef implements the latest-value-ref rule that #10161 proposes for architecture:react-component-runtime (DEC-3 and an INV3 exception), but on main INV3 still requires every new Effect to name the external system it synchronizes, and this hook's insertion Effect names none. Either #10161 lands first, or an engineering owner records the decision by commenting /approve-spec <full head SHA> on this pull request; the author may do so as an engineering owner.
- The JSDoc's Effect-cleanup example (
packages/core/src/hooks/useCommittedRef.ts:27-29) subscribes with the value read at setup and unsubscribes with the value read at cleanup, which differ once the value changes (the new test atuseCommittedRef.test.tsx:159-178shows the cleanup reading'second'after mounting with'first'), so code that copies it leaks the first subscription; please delete lines 21-30 and keep a value that selects a resource as an Effect dependency.
Non-blocking: the file header still repeats the hook's JSDoc (see the inline note on lines 8-12).
Claims and files checked
- Changeset: Core adds an internal hook that keeps a ref at the latest committed prop or callback for event and gesture callbacks: verified (
useCommittedRef.ts:32-39). - Changeset: nothing uses it yet: verified; only its own test imports it.
- Changeset: apps import nothing new: verified; it is not in
hooks/index.tsor anyexportsentry. - Description: it writes
currentinuseInsertionEffectwith no dependencies: verified (useCommittedRef.ts:35-37). - Description: the write survives a dropped render and lands before every layout Effect in the same commit: verified by reading the tests at
useCommittedRef.test.tsx:63-125; not run. - Description: event and gesture callbacks read it, render and
useMemonever do: contradicted at this head by the JSDoc cleanup paragraph (useCommittedRef.ts:22-30), the finding above. - Description: it sits beside
useIsomorphicLayoutEffectand outside the package entry: verified. - Description: no React floor change: verified; the peer range stays
react >=19.0.0. - Description: no callers move: verified.
- Description: seven tests, including a cleanup read in the lint-clean shape the JSDoc shows: seven cases are present; the test run and the lint statement were not reproduced.
packages/core/src/hooks/useCommittedRef.ts: blocking cleanup-example finding; non-blocking header note.packages/core/src/hooks/useCommittedRef.test.tsx: clear..changeset/core-use-committed-ref.md: clear.
There was a problem hiding this comment.
Approval waits on an engineering owner's decision about spec authority, and two fixes are needed.
useCommittedRef and useCommittedRefInto implement the latest-value-ref rule that #10161 proposes for architecture:react-component-runtime (DEC-3 and an INV3 exception), but on main INV3 still requires every new Effect to name the external system it synchronizes, and #10161's DEC-3 names only the read-only useCommittedRef, not a shape that writes into a caller's useRef. Either #10161 lands first and covers both shapes (or useCommittedRefInto leaves this pull request), or an engineering owner records the decision by commenting /approve-spec <full head SHA> on this pull request; the author may do so as an engineering owner.
- The JSDoc's Effect-cleanup example (
packages/core/src/hooks/useCommittedRef.ts:23-31) subscribes with the value read at setup and unsubscribes with the value read at cleanup, which differ once the value changes (the test atuseCommittedRef.test.tsx:193-212shows cleanup reading'second'after mounting with'first'), so please delete the paragraph and that test and keep a value that selects a resource as an Effect dependency. - The description says the first four evidence checks run for both call shapes, but the listener-installed-once and Effect-cleanup tests sit in
describe('useCommittedRef')only (useCommittedRef.test.tsx:160-225), so please run the listener test for both shapes throughdescribe.each(SHAPES)or reword that line.
Non-blocking: the file header still repeats the hooks' JSDoc (see the inline note on lines 8-13).
Claims and files checked
- Changeset: Core adds an internal hook that keeps a ref at the latest committed prop or callback for event and gesture callbacks: verified (
useCommittedRef.ts:33-51). - Changeset: nothing uses it yet, and apps import nothing new: verified; only its own test imports the file, and no Core entry point reaches it.
- Changeset: a
[chore] Internal:patch for@astryxdesign/core: verified. - Description: both shapes write
currentin a no-dependencyuseInsertionEffect, never during render: verified (useCommittedRef.ts:33-37,:45-51). - Description: the two call signatures: verified (
useCommittedRef.ts:33,:45). - Description: the write's comment text: verified (
useCommittedRef.ts:46). - Description: the hook sits beside the internal
useIsomorphicLayoutEffect, outsidehooks/index.tsand the package entry: verified. - Description: no callers move: verified.
- Description: no React floor change, since Core's
react >=19.0.0peer range shipsuseInsertionEffect: verified (packages/core/package.json). - Description: no new public API: verified from the exports map and source.
- Description: 12 tests: verified by count.
- Description: each of the first four checks runs for both call shapes: false; the listener and cleanup tests cover
useCommittedRefonly. - Description: the cleanup read is the lint-clean shape the JSDoc shows: accurate as written, but the shape releases a different value than it acquired (blocking finding above).
- Description:
useCommittedRefIntolets a callback declared before the value read the latest committed value: verified (useCommittedRef.test.tsx:227-251). - Description: a render-phase variant fails both dropped-render tests: not checked.
- Description: about 176 latest-value refs in Core, 70 written during render: not checked; a codebase count outside this change.
- File
.changeset/core-use-committed-ref.md: clear. - File
packages/core/src/hooks/useCommittedRef.ts: cleanup example (blocking); header repeats the JSDoc (non-blocking). - File
packages/core/src/hooks/useCommittedRef.test.tsx: the cleanup test pins the example; the listener and cleanup tests cover one shape.
|
/approve-spec 8fde373 |
|
Decision: D-20261112-150 |
There was a problem hiding this comment.
Changes requested: spec authority is settled, and two fixes remain at this head.
useCommittedRef and the ./internal/committed-ref entry follow architecture:react-component-runtime DEC-3 and its Owning code entry on main, and the engineering owner's /approve-spec for this head covers useCommittedRefInto until #11368 records it.
- The JSDoc's Effect-cleanup example (
packages/core/src/hooks/useCommittedRef.ts:23-31) subscribes the value read at setup and unsubscribes the value read at cleanup, which the pull request's own test (useCommittedRef.test.tsx:193-212) shows differ once the value changes, and it contradicts INV3's rule that the ref is read only in event and gesture callbacks, so please delete the paragraph and that test and keep a value that selects a resource as an Effect dependency. - The description says the listener-installed-once and Effect-cleanup checks run for both call shapes, but they sit in
describe('useCommittedRef')only (useCommittedRef.test.tsx:160-225), so please run the listener test throughdescribe.each(SHAPES)or reword that line.
Non-blocking: the file header still repeats the hooks' JSDoc (see the inline note on lines 8-13). CI has not run on this draft head.
Claims and files checked
- Changeset: Core adds two internal hooks that keep a ref at the latest committed prop or callback for event and gesture callbacks: verified (
useCommittedRef.ts:33-51). - Changeset: an internal entry lets Lab reach them: verified (
package.json:892-896,internal/committed-ref.ts:15). - Changeset: nothing uses them yet, and apps import nothing new: verified; only the test and the entry import the hook file, and the entry is a lint-limited internal subpath.
- Changeset: a
[chore] Internal:patch for@astryxdesign/core: verified. - Description: the runtime record change has merged, and the record change for the second shape should merge first or together: verified; #10161 is on
main, #11368 is open. - Description: the hooks are the ones INV3 and DEC-3 name: verified for
useCommittedRef;useCommittedRefIntois named only in #11368 and is covered by the owner's approval of this head. - Description: event and gesture callbacks read the ref, render and
useMemonever do: contradicted by the JSDoc's Effect-cleanup paragraph (blocking finding above). - Description: the hooks sit beside
useIsomorphicLayoutEffectand are never inhooks/index.tsor the package entry: verified. - Description: both shapes write
currentin a no-dependencyuseInsertionEffectafter every commit and before layout Effects: verified (useCommittedRef.ts:45-51) for layout-Effect setups; a descendant's layout cleanup runs earlier, as DEC-3 records. - Description: the two call signatures: verified (
useCommittedRef.ts:33,:45). - Description: the write's comment text: verified (
useCommittedRef.ts:46). - Description: the internal entry re-exports both by name and is in
scripts/sync-exports.jsand the generated exports map: verified (internal/committed-ref.ts:15,scripts/sync-exports.js:252; the exports check passes). - Description: lint allows the entry only in Core and Lab: verified; the existing rule matches every Core internal subpath outside Core and Lab.
- Description: no callers move: verified.
- Description: no React floor change, since
useInsertionEffectships in every supported version: verified (react >=19.0.0peer range). - Description: new public API: none: verified; internal subpaths are outside the public surface.
- Description: the new tests fail on
mainbecause the module is missing: verified by construction; the render-phase variant result was not checked. - Description: 12 tests pass: verified by count (four cases for both shapes, four single-shape cases); not run.
- Description: each of the first four checks runs for both call shapes: false; the listener and cleanup tests cover
useCommittedRefonly. - Description: an Effect cleanup reads the latest value through a function, the shape the JSDoc shows: accurate as written, but that shape releases a different value than it acquired (blocking finding above).
- Description: a callback declared before the value reads the latest value through
useCommittedRefInto: verified (useCommittedRef.test.tsx:227-251). - Description: about 176 latest-value refs, 70 written during render: the 70 matches the runtime record's gap count; the 176 was not checked.
- Description: no CI impact: verified; no workflow changes, and the exports check passes at this head.
- File
.changeset/core-use-committed-ref.md: clear. - File
packages/core/package.json: clear. - File
packages/core/src/hooks/useCommittedRef.ts: Effect-cleanup example (blocking); header repeats the JSDoc (non-blocking). - File
packages/core/src/hooks/useCommittedRef.test.tsx: the cleanup test pins the example; the listener and cleanup tests cover one shape. - File
packages/core/src/internal/committed-ref.ts: clear. - File
scripts/sync-exports.js: clear.
|
/approve-spec 9614618 |
There was a problem hiding this comment.
Approved; CI passes on this head.
useCommittedRef, useCommittedRefInto and the ./internal/committed-ref entry now match architecture:react-component-runtime DEC-3, INV3, INV17 and its Owning code entry on main, so both call shapes have spec authority there. The 12 new cases pass, and the exports map matches scripts/sync-exports.js.
- Non-blocking: the first
@positionsentence inpackages/core/src/internal/committed-ref.tsstill runs 34 words over four topics; the earlier inline note has the split wording frominternal/virtualization.ts.
Claims and files checked
- Changeset, two internal hooks keep a ref at the latest committed prop or callback for event and gesture callbacks: verified (
useCommittedRef.ts:21-39). - Changeset, an internal entry lets Lab reach them: verified (
package.json:892-896,internal/committed-ref.ts:15). - Changeset, nothing uses them yet and apps import nothing new: verified; only the entry and the test import the hook file, and lint keeps the entry to Core and Lab.
- Changeset, an internal chore patch for
@astryxdesign/core: verified; the changeset check passes. - Description, both runtime-record changes it builds on have landed: verified; both are merged into
main. - Description, both shapes write
currentin a no-dependency insertion Effect after each commit, before the commit's layout-Effect setups: verified (useCommittedRef.ts:35-38;useCommittedRef.test.tsx:132-172). - Description,
useCommittedRefreturns a stable read-only ref anduseCommittedRefIntowrites a ref created earlier withuseRef: verified (useCommittedRef.ts:21-25,:33-39;useCommittedRef.test.tsx:192-230). - Description, the write's comment uses the runtime record's wording: verified (
useCommittedRef.ts:34). - Description, neither shape is in
hooks/index.tsor the package entry: verified. - Description, the internal entry re-exports both by name, is listed in
scripts/sync-exports.jsand the generated exports map, and lint allows it only in Core and Lab: verified (scripts/sync-exports.js:252; the exports check passes; the lint rule matches every Core internal subpath). - Description, no callers move: verified.
- Description, no React floor change because
useInsertionEffectexists in every supported React: verified (peer range>=19.0.0). - Description, no new public API: verified; the only exports change is a Core internal subpath, and CI's API change report lists no rows.
- Description, 12 tests pass covering dropped renders, layout Effects, a listener installed once and server rendering for both shapes, plus ref stability and a callback declared before its value: verified in CI.
- Description, the tests fail on the base because the module is missing, and a render-time write fails both dropped-render tests: verified by reading (the module is absent on the base; those tests expect the committed value after a dropped render); not rerun.
- Description, about 176 latest-value refs with 70 writing during render: the 70 matches the runtime record's gap list; the 176 total is the author's count, not reproduced, and decides nothing.
- Description, no workflow or CI script changes: verified.
- Description, local lint, format, type, comment, changeset, knowledge and pre-review runs: CI lint and typecheck pass on this head, the changeset check passes, and the pre-review Effect check does not match insertion Effects, consistent with its zero findings.
.changeset/core-use-committed-ref.md: clear.packages/core/package.json: clear.packages/core/src/hooks/useCommittedRef.test.tsx: clear.packages/core/src/hooks/useCommittedRef.ts: clear.packages/core/src/internal/committed-ref.ts: the non-blocking header sentence above.scripts/sync-exports.js: clear.
| * @position An internal entry reserved for Astryx's own packages: | ||
| * undocumented, outside the public API and its compatibility promise, | ||
| * versioned in lockstep with Core, and allowed only in Core and Lab by the | ||
| * `@astryx/no-core-internal-import` lint rule. Core components import the |
There was a problem hiding this comment.
Non-blocking (source comment style: at most 25 words and one topic per sentence in a description): this sentence runs 34 words over four topics. internal/virtualization.ts already splits it:
* @position An internal entry reserved for Astryx's own packages. It is
* undocumented, outside the public API and its compatibility promise, and
* versioned in lockstep with Core. The `@astryx/no-core-internal-import`
* lint rule allows it only in Core and Lab. Core components import the
* same module by relative path. Named re-exports only.…10815) * refactor(List): write the reorder refs at commit, not in render Conformance tidy, no visible change. The reorder session's list getter and the mode hint's layer handle were copied into refs during render. Each is now written in a one-line layout effect, so a render React discards leaves nothing behind. No failure is reachable today. Every list getter returns the same element. The layer ref's only reader is the hint's layout effect. It reads the ref at the commit, right after the committed render wrote it. The cleanup uses the handle it captured. * fix(List): a reorder handle's name follows its row's place and label A reorder handle named itself from the DOM in a passive effect: after Item's label element when that element shows a name, else after the row's place ("Reorder item N"). The effect ran only when the handle rendered. A memoized row whose place changed, because a row above it went or the rows reordered, kept the old place in its handle's name. A label that gained or lost its text through its own state left the handle named the old way. The reorder session now reads both from the DOM, outside React. It registers the list's rows and its handles' label elements with Core's shared mutation observer, and releases them with the list. It keeps each handle's label naming, and the place only for a handle named after it. The handle reads them through useSyncExternalStore, as it reads reorder mode, and renders its name from them. Only a change renders the handle again, so its passive effect is gone. A row named after its place still warns in development, once, from the session. positionOf loses its last caller and goes. The session follows the list element the List renders, which changes when its numbering turns on or off. ListReorderNames.test.tsx covers a row above that goes, a reorder, and a label that gains and loses text: each fails on main and passes here. A reorder after the numbering turns on passes on both. * Merge main 1740654 into the reorder handle names Main brings in #11199, whose controller reads a collection's rows through a reader, and #11352's committed-ref hook. #11199 edits positionOf, which this branch removes; it stays removed. One hand edit: refreshNames reads this.rows.read(list) instead of rowsOf(list), as #11199's positionOf did, so a collection's own reader numbers its rows. * refactor(List): the reorder refs use the committed-ref hook The session's list getter and the mode hint's layer handle were each copied into a ref by a one-line layout effect. They now use Core's useCommittedRef, which writes them in a commit-phase insertion effect. The session's useMemo lists the stable getter ref in its dependencies. No behavior changes. Every getter List passes reads the same list ref when called, and the hint's effect reads the handle after either write. A handle's cleanup can reach the getter through release(), and gets the same list. The hint's cleanup hides the handle its setup showed, so no cleanup reads the layer ref. * fix(List): an unchanged handle name keeps its snapshot The names refresh compared each handle's name with the session's own entry, which a handle named some other way has none of before the first read. So it stored a new {position: null, labelNamesRow: null} and the handle rendered again with the same name on every mount (architecture:react-component-runtime INV9). It now compares with handleNameOf, whose shared unread name has the same fields. A test counts the handle's renders through the verb it reads each render: once per handle on mount, and once for a row that mounts later. * fix(List): a hydrated handle keeps its server snapshot The handle's server snapshot was its own unread name, and the session answered an unread handle with another object with the same fields. So after hydration React saw a new snapshot, and each handle rendered again with the same name (architecture:react-component-runtime INV9). The handle now exports one unread name, and the session answers with it. A test hydrates server markup and counts the handle's renders through the verb it reads each render: once per handle. * fix(List): a label that names its row keeps the unread snapshot The names refresh stored whether Item's label element names the row: null before the first read, and true after it, though the handle and the session read both alike. So a handle named by its row's text label rendered again with the same name after mounting and after hydrating (architecture:react-component-runtime INV9). The snapshot now records whether the label shows no name, which stays false until a read finds the label blank. Both render-count cases add a row named by its text label. * Merge main 06b7e61 into the reorder handle names Main brings in #10912, which changes Layer/useLayer.tsx, the layer the mode hint shows. The merge needs no hand edit. * Merge main 31aacff into the reorder handle names Main brings in #11336, which changes List/ListItem.tsx, the row that renders the reorder handle. The merge needs no hand edit.
Records: #10161 (merged) and #11368. #11368 should merge first or together with this PR.
Maintainer impact
Core has about 176 latest-value refs: a ref that holds the newest prop or callback, so a listener installed once can call the current one. Each one decides on its own when it writes, and 70 of them write during render. A render that React drops, for example a transition that suspends, leaves its value in such a ref, so a later click can call a handler for a screen the user never saw. This PR adds the shared internal hooks that
architecture:react-component-runtimeINV3 and DEC-3 name, so components can move onto one commit-safe timing.Intended invariant
architecture:react-component-runtimeINV3, INV17 and DEC-3, as #10161 (merged) and #11368 state them. A latest-value ref goes only throughuseCommittedRef, which writescurrentin a commit-phase insertion Effect and never during render. Event and gesture callbacks read it; render anduseMemonever do. The hooks live inpackages/core/src/hooks/useCommittedRef.ts, beside the internaluseIsomorphicLayoutEffect, and are never inhooks/index.tsor the package entry. Lab reaches them through@astryxdesign/core/internal/committed-ref.Change
packages/core/src/hooks/useCommittedRef.tshas two call shapes. Both writecurrentinuseInsertionEffectwith no dependencies, so the write happens after every commit and before any layout Effect in that commit:useCommittedRef<T>(value: T): { readonly current: T }returns a read-only ref.useCommittedRefInto<T>(ref: { current: T }, value: T): voidwrites into a ref that the component created earlier withuseRef. This is for callbacks declared above the value, where the value cannot exist yet when the ref is made.hooks/index.ts, and the package entry exports neither.packages/core/src/internal/committed-ref.ts: the internal entry@astryxdesign/core/internal/committed-ref, which re-exports both hooks by name for Lab. It is added toscripts/sync-exports.jsand the generatedexportsmap, like the other internal entries. Lint allows it only in Core and Lab.useInsertionEffectships in every React version Core supports (>=19.0.0).[chore] Internal:Changeset for@astryxdesign/core.New public API: none.
Evidence
Failure before the change: the new tests failed on
mainbecause the module did not exist. A render-phase variant of the same write fails both dropped-render tests: the ref holds the dropped value.Success after the change: 12 tests pass. These run for both call shapes:
renderToStringgives no warning and keeps the initial value.Also:
useCommittedRef: the ref object stays the same across renders;useCommittedRefInto: a callback declared before the value reads the latest committed value.Product/runtime behavior verified unchanged: nothing calls the hook yet.
CI impact: none. No workflow or CI script changes.
Scope
Components: none (internal hook)
Testing
vitest run packages/core/src/hooks/useCommittedRef.test.tsx.tsc.no-comment-provenance.baseline.mjs --check.pnpm check:changesetsandpnpm check:knowledge.pnpm review:pre: 0 findings, merges cleanly.