Skip to content

Commit be313c9

Browse files
committed
Simplify useOnyxState subscription handling
1 parent 07d7dfd commit be313c9

1 file changed

Lines changed: 31 additions & 44 deletions

File tree

‎lib/useOnyxState.ts‎

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,6 @@ type SnapshotCache<T> = {
8787
depValues: Array<OnyxValue<OnyxKey>>;
8888
};
8989

90-
type StateSubscription = {
91-
keys: readonly OnyxKey[];
92-
unsubscribe: () => void;
93-
/** Moves the subscription to the keys the latest selector run read. */
94-
syncKeys: () => void;
95-
};
96-
9790
/** Runs the selector with `liveStateView` recording, and returns the set of keys it read. */
9891
function recordReads<T>(run: () => T): {output: T; keys: Set<OnyxKey>} {
9992
const keys = new Set<OnyxKey>();
@@ -161,60 +154,54 @@ function useOnyxState<T>(selector: UseOnyxStateSelector<T>, options: UseOnyxStat
161154
// `getSnapshot` calls and every one of them computes the same output.
162155
const previousViewRef = useRef<OnyxStateView | undefined>(undefined);
163156
const lastSnapshotRef = useRef<SnapshotCache<T> | null>(null);
164-
const subscriptionRef = useRef<StateSubscription | null>(null);
157+
const syncKeysRef = useRef<(() => void) | null>(null);
165158

166159
// Only `subscribe` has to be referentially stable, since it controls re-subscription.
167160
// `getSnapshot` may change identity freely, so it closes over the latest selector directly.
168161
const subscribe = useCallback((onStoreChange: () => void) => {
162+
let isActive = true;
163+
// React subscribes after the commit, so the snapshot behind the committed value is already there.
164+
let keys = lastSnapshotRef.current?.deps ?? [];
165+
let unsubscribe = onyxSubscriptionManager.subscribeState(handleStoreChange, keys);
166+
167+
/** Moves the subscription to the keys the latest selector run read. */
168+
function syncKeys() {
169+
const lastSnapshot = lastSnapshotRef.current;
170+
if (!isActive || lastSnapshot === null || keys === lastSnapshot.deps) {
171+
return;
172+
}
173+
unsubscribe();
174+
keys = lastSnapshot.deps;
175+
unsubscribe = onyxSubscriptionManager.subscribeState(handleStoreChange, keys);
176+
// A write that landed on a newly read key before this point fired nobody.
177+
if (!areDepValuesUnchanged(keys, lastSnapshot.depValues)) {
178+
handleStoreChange();
179+
}
180+
}
181+
169182
// React's store-change handler calls `getSnapshot` synchronously, so once it returns the latest
170183
// run's keys are in `lastSnapshotRef`. Moving the subscription here rather than only after a
171184
// commit matters when a run reads a new key but returns an equal output: nothing commits then.
172-
const handleStoreChange = () => {
185+
function handleStoreChange() {
186+
if (!isActive) {
187+
return;
188+
}
173189
onStoreChange();
174-
subscription.syncKeys();
175-
};
176-
177-
// React subscribes after the commit, so the snapshot behind the committed value is already there.
178-
const keys = lastSnapshotRef.current?.deps ?? [];
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-
};
196-
subscriptionRef.current = subscription;
190+
syncKeys();
191+
}
192+
syncKeysRef.current = syncKeys;
197193

198194
// `Onyx.init` hydrates the cache without notifying anyone, so a selector that ran before it
199195
// finished saw an empty state and would never hear the stored values arrive. Re-read once
200196
// init lands.
201197
const initTask = OnyxUtils.getDeferredInitTask();
202-
let isActive = true;
203198
if (!initTask.isResolved) {
204-
initTask.promise.then(() => {
205-
if (!isActive) {
206-
return;
207-
}
208-
handleStoreChange();
209-
});
199+
initTask.promise.then(handleStoreChange);
210200
}
211201

212202
return () => {
213203
isActive = false;
214-
subscription.unsubscribe();
215-
if (subscriptionRef.current === subscription) {
216-
subscriptionRef.current = null;
217-
}
204+
unsubscribe();
218205
};
219206
}, []);
220207

@@ -260,7 +247,7 @@ function useOnyxState<T>(selector: UseOnyxStateSelector<T>, options: UseOnyxStat
260247

261248
// A render can run the selector without a store change, say with a new selector, so the keys can move here too.
262249
useEffect(() => {
263-
subscriptionRef.current?.syncKeys();
250+
syncKeysRef.current?.();
264251
});
265252

266253
// Snapshot the deps after a commit that changed the output, to serve as `previousState` on the next

0 commit comments

Comments
 (0)