Repository navigation
💥 Subscribe after commit, and say whether there is a value - #291
Merged
Merged
Conversation
mbret
marked this pull request as ready for review
November 13, 2026 00:19
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
mbret
marked this pull request as draft
November 13, 2026 00:50
A design is never chosen for the release type it gets. When a problem has a clean fix that breaks callers and a weaker one that ships as a patch, the clean fix wins and ships as a major version, and the pull request names the options it rejected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BvSDyJABLGit75HAjpnsqm
…value useObserve subscribed to its source during render, so that a source that emits synchronously had its value on the first render. A render that React throws away, and every server render, left that subscription open, and started the side effects of the source. The hook now subscribes in an effect, after React commits. Render reads only what a source holds without a subscription: the value of a BehaviorSubject or a signal. Each deps change starts a new observation, kept in state. Render replaces the observation and the state of its source together, so a source created on every render makes React throw "Too many re-renders" instead of subscribing again without end. status now says whether the source has a value, as the queries of TanStack Query do, and data is typed by it. observableState keeps the life of the stream. A BehaviorSubject that errored or is closed no longer throws during render, a new defaultValue applies while the source has no value, and the result keeps its identity across renders that change nothing. usePersistSignals uses useObserve again, so it reads and writes storage after mount only. Closes #261 Closes #267 Closes #279 Closes #280 Closes #290 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BvSDyJABLGit75HAjpnsqm BREAKING CHANGE: `useObserve` subscribes after commit, in an effect, and `status` says whether the source has a value. - `status: "pending"` means that the source has no value. It meant a live stream: read `observableState === "live"` for that. - `status: "success"` means that the source has a value. It meant a completed stream: read `observableState === "complete"` for that. - Without a source, `status` is `"pending"`, and `observableState` stays `"complete"`. - On the first render, and after a deps change, a source that is not a `BehaviorSubject` or a signal is `"pending"`, with `defaultValue`. A source that emits on subscription has its value in a second render, after mount. To read a value on the first render, keep it in a signal or a `BehaviorSubject`, or select part of a signal with `useSignalValue(signal, selector)`. - An observable created during render without a function, such as `source$.pipe(...)` written in the component, makes React throw "Too many re-renders". Create it in a function with `deps`. - `usePersistSignals` reads and writes storage after mount. With no entries, it returns `isHydrated: false` on the first render, then `true`.
mbret
force-pushed
the
claude/awesome-bell-2a040j
branch
from
November 13, 2026 18:21
2c0ebec to
098709a
Compare
mbret
marked this pull request as ready for review
November 13, 2026 18:24
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 098709aea2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
The previous commit called compareFn in the next handler of the subscription. RxJS reports a throw there as unhandled, so the hook stayed live with stale data. The comparison now runs in a filter operator, and a throw errors the stream, as distinctUntilChanged did before: status is "error", data keeps the last value, and the hook unsubscribes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BvSDyJABLGit75HAjpnsqm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
A render that never commits left its
useObservesubscription open for good, and started the side effects of the source. A Suspense boundary that suspends, an error boundary, an abandoned transition, a hidden<Activity>that unmounts and every server render make such renders. ThroughusePersistSignals, such a render kept saving the signals after unmount.statusalso repeatedobservableState, so nothing told whetherdatawas a value or the default. ABehaviorSubjectwas"pending"for its whole life.statussays whether the source has a value, as the queries of TanStack Query do."pending"on the first render and after a deps change, and that render can reach the screen.What's new
useObservesubscribes after commit, and render reads only aBehaviorSubjector a signal (closes useObserve: a render that never commits leaves its subscription open #261)statussays whether the source has a value, and typesdatauseObservebugs go with the old design (closes useObserve: throws during render for a BehaviorSubject that errored or is closed #267, useObserve: a NaN dependency recreates the store on every render #279, useObserve: a new defaultValue is ignored until the deps change #280, useObserve: a transition and an urgent render make the mounted component run its source again #290)Other changes
usePersistSignalsusesuseObserveagain, so it reads and writes storage after mount onlystatusand the stable source ruleWhat breaks, and how to migrate
status: "pending"means that the source has no value yet. It meant a live stream: readobservableState === "live"for that.status: "success"means that the source has a value. It meant a completed stream: readobservableState === "complete"for that.statusis"pending", andobservableStatestays"complete".BehaviorSubjector a signal is"pending", withdefaultValue. A source that emits on subscription has its value in a second render, after mount. To read a value on the first render, keep it in a signal or aBehaviorSubject, or select part of a signal withuseSignalValue(signal, selector).source$.pipe(...)written in the component, makes React throw "Too many re-renders" once the component renders again. Create it in a function withdeps.usePersistSignalsreads and writes storage after mount, not during render. With no entries,isHydratedisfalseon the first render, thentrue.Details
The four closed issues
BehaviorSubjectthat errored, or is closed, threw during render. Render now reads its value in atry.NaNdependency recreated the store on every render. The hook now compares the deps withObject.is, as React does.defaultValuewas ignored until the deps changed. It is now read on each render.A
BehaviorSubjectthat errored, or is closed, now reports its error on the first render. A closed one threw fromsubscribe, and #267 covered both.Options rejected
useMemo. It shows the same values. But React may drop a memo (Fast Refresh does), and a source created on every render loops without an error: each render makes a new memo, and each commit subscribes again. That loop runs in passive effects, which React does not stop, and in production nothing reports it. In state, the same source makes React throw "Too many re-renders".usePersistSignalshydrated over a signal update made meanwhile.One observation per deps change
Each deps change creates one observation: the source, the deps, and the state render shows before the effect subscribes. The hook keeps it in state, with the latest state of its source. When the deps change, render replaces both with
setState, and React renders again at once. This is the React pattern to store information from previous renders. A render that React throws away discards thatsetStatetoo. The state belongs to the observation, so:"pending";StrictMode,<Activity>) does not subscribe again to a source that ended.Tests seen to fail
main: every lifecycle and server test of a render that never commits, and the test of each closed issue. For example, useObserve: a transition and an urgent render make the mounted component run its source again #290 subscribes the committed source twice, and useObserve: throws during render for a BehaviorSubject that errored or is closed #267 throwsboom.setStateduring render, the test of a source created on every render runs until the test worker runs out of memory and aborts.datain the"success"type fails the type check.Performance
I measured a mount of 1,000 components in jsdom, on
mainand on this branch. "Renders" counts the calls of the component function.mainmainBehaviorSubjectSubjectBehaviorSubjector a signal renders once. Its value on subscription is equal to the value render read, so it sets no state.useSignalValue(signal, selector)renders it once.Left for other issues
<Activity>, persistence hydrates again over newer values. This predates the change.Release notes
The
BREAKING CHANGE:footer of theuseObservecommit makes this reactjrx 6, and lists the migration steps above.🔍 Self-reviewed with selfcorrect-review
🤖 Made within harness coverage
🤖 Generated with Claude Code
https://claude.ai/code/session_01BvSDyJABLGit75HAjpnsqm