@@ -88,9 +88,10 @@ type SnapshotCache<T> = {
8888} ;
8989
9090type StateSubscription = {
91- onStoreChange : ( ) => void ;
9291 keys : readonly OnyxKey [ ] ;
9392 unsubscribe : ( ) => void ;
93+ /** Moves the subscription to the keys the latest selector run read. */
94+ syncKeys : ( ) => void ;
9495} ;
9596
9697/** Runs the selector with `liveStateView` recording, and returns the set of keys it read. */
@@ -165,9 +166,33 @@ function useOnyxState<T>(selector: UseOnyxStateSelector<T>, options: UseOnyxStat
165166 // Only `subscribe` has to be referentially stable, since it controls re-subscription.
166167 // `getSnapshot` may change identity freely, so it closes over the latest selector directly.
167168 const subscribe = useCallback ( ( onStoreChange : ( ) => void ) => {
169+ // React's store-change handler calls `getSnapshot` synchronously, so once it returns the latest
170+ // run's keys are in `lastSnapshotRef`. Moving the subscription here rather than only after a
171+ // commit matters when a run reads a new key but returns an equal output: nothing commits then.
172+ const handleStoreChange = ( ) => {
173+ onStoreChange ( ) ;
174+ subscription . syncKeys ( ) ;
175+ } ;
176+
168177 // React subscribes after the commit, so the snapshot behind the committed value is already there.
169178 const keys = lastSnapshotRef . current ?. deps ?? [ ] ;
170- const subscription : StateSubscription = { onStoreChange, keys, unsubscribe : onyxSubscriptionManager . subscribeState ( onStoreChange , keys ) } ;
179+ const subscription : StateSubscription = {
180+ keys,
181+ unsubscribe : onyxSubscriptionManager . subscribeState ( handleStoreChange , keys ) ,
182+ syncKeys : ( ) => {
183+ const lastSnapshot = lastSnapshotRef . current ;
184+ if ( lastSnapshot === null || subscription . keys === lastSnapshot . deps ) {
185+ return ;
186+ }
187+ subscription . unsubscribe ( ) ;
188+ subscription . keys = lastSnapshot . deps ;
189+ subscription . unsubscribe = onyxSubscriptionManager . subscribeState ( handleStoreChange , lastSnapshot . deps ) ;
190+ // A write that landed on a newly read key before this point fired nobody.
191+ if ( ! areDepValuesUnchanged ( lastSnapshot . deps , lastSnapshot . depValues ) ) {
192+ handleStoreChange ( ) ;
193+ }
194+ } ,
195+ } ;
171196 subscriptionRef . current = subscription ;
172197
173198 // `Onyx.init` hydrates the cache without notifying anyone, so a selector that ran before it
@@ -180,7 +205,7 @@ function useOnyxState<T>(selector: UseOnyxStateSelector<T>, options: UseOnyxStat
180205 if ( ! isActive ) {
181206 return ;
182207 }
183- onStoreChange ( ) ;
208+ handleStoreChange ( ) ;
184209 } ) ;
185210 }
186211
@@ -230,39 +255,32 @@ function useOnyxState<T>(selector: UseOnyxStateSelector<T>, options: UseOnyxStat
230255 } , [ selector , selectorEquality ] ) ;
231256
232257 const value = useSyncExternalStore ( subscribe , getSnapshot ) ;
258+ // The run behind `value`. A store change between the commit and the effects below can overwrite the ref.
259+ const renderedSnapshot = lastSnapshotRef . current ;
233260
234- // Move the subscription to the keys the latest run read. It runs after the commit, so a
235- // write that landed in between on a newly read key fired nobody; comparing the values the run saw with
236- // the current ones catches it, the same check React does when it first subscribes.
261+ // A render can run the selector without a store change, say with a new selector, so the keys can move here too.
237262 useEffect ( ( ) => {
238- const subscription = subscriptionRef . current ;
239- const lastSnapshot = lastSnapshotRef . current ;
240- if ( subscription === null || lastSnapshot === null || subscription . keys === lastSnapshot . deps ) {
241- return ;
242- }
243- subscription . unsubscribe ( ) ;
244- subscription . keys = lastSnapshot . deps ;
245- subscription . unsubscribe = onyxSubscriptionManager . subscribeState ( subscription . onStoreChange , lastSnapshot . deps ) ;
246- if ( ! areDepValuesUnchanged ( lastSnapshot . deps , lastSnapshot . depValues ) ) {
247- subscription . onStoreChange ( ) ;
248- }
263+ subscriptionRef . current ?. syncKeys ( ) ;
249264 } ) ;
250265
251266 // Snapshot the deps after a commit that changed the output, to serve as `previousState` on the next
252267 // selector run. Doing it here rather than in `getSnapshot` keeps that function pure and gives
253268 // `previousState` its meaning, the deps behind the last committed output: a change whose output
254269 // the equality check swallowed never commits, so it never advances past a value nobody saw. Running
255- // only on output changes also keeps a re-render from a parent from allocating a new view.
270+ // only on output changes also keeps a re-render from a parent from allocating a new view. The values
271+ // come from the committed run, not the store, so a write landing before this effect still reads as a change.
256272 useEffect ( ( ) => {
257- const currentDepValues : Record < OnyxKey , OnyxValue < OnyxKey > > = { } ;
258- for ( const dep of lastSnapshotRef . current ?. deps ?? [ ] ) {
259- currentDepValues [ dep ] = onyxSubscriptionManager . getState ( dep ) ;
273+ const committedDepValues : Record < OnyxKey , OnyxValue < OnyxKey > > = { } ;
274+ if ( renderedSnapshot !== null ) {
275+ for ( const [ index , dep ] of renderedSnapshot . deps . entries ( ) ) {
276+ committedDepValues [ dep ] = renderedSnapshot . depValues [ index ] ;
277+ }
260278 }
261279 previousViewRef . current = createStateView ( ( key ) => {
262280 previousStateReadRef . current = true ;
263- return currentDepValues [ key ] ;
281+ return committedDepValues [ key ] ;
264282 } ) ;
265- } , [ value ] ) ;
283+ } , [ value ] ) ; // eslint-disable-line react-hooks/exhaustive-deps
266284
267285 return value ;
268286}
0 commit comments