Skip to content

Commit 8163329

Browse files
committed
expose source value in useOnyx
1 parent 99b08bc commit 8163329

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

lib/useOnyx.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type FetchStatus = 'loading' | 'loaded';
7474

7575
type ResultMetadata = {
7676
status: FetchStatus;
77+
sourceValue?: OnyxValue<OnyxKey>;
7778
};
7879

7980
type UseOnyxResult<TValue> = [NonNullable<TValue> | undefined, ResultMetadata];
@@ -158,6 +159,9 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
158159
// Indicates if we should get the newest cached value from Onyx during `getSnapshot()` execution.
159160
const shouldGetCachedValueRef = useRef(true);
160161

162+
// Inside useOnyx.ts, we need to track the sourceValue separately
163+
const sourceValueRef = useRef<OnyxValue<OnyxKey> | undefined>(undefined);
164+
161165
useEffect(() => {
162166
// These conditions will ensure we can only handle dynamic collection member keys from the same collection.
163167
if (options?.allowDynamicKey || previousKey === key) {
@@ -284,7 +288,13 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
284288

285289
// If the new value is `null` we default it to `undefined` to ensure the consumer gets a consistent result from the hook.
286290
const newStatus = newFetchStatus ?? 'loaded';
287-
resultRef.current = [previousValueRef.current ?? undefined, {status: newStatus}];
291+
resultRef.current = [
292+
previousValueRef.current ?? undefined,
293+
{
294+
status: newStatus,
295+
sourceValue: sourceValueRef.current,
296+
},
297+
];
288298

289299
// If `canBeMissing` is set to `false` and the Onyx value of that key is not defined,
290300
// we log an alert so it can be acknowledged by the consumer. Additionally, we won't log alerts
@@ -304,7 +314,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
304314

305315
connectionRef.current = connectionManager.connect<CollectionKeyBase>({
306316
key,
307-
callback: () => {
317+
callback: (value, callbackKey, sourceValue) => {
308318
isConnectingRef.current = false;
309319
onStoreChangeFnRef.current = onStoreChange;
310320

@@ -315,6 +325,8 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
315325
// Signals that we want to get the newest cached value again in `getSnapshot()`.
316326
shouldGetCachedValueRef.current = true;
317327

328+
sourceValueRef.current = sourceValue;
329+
318330
// Finally, we signal that the store changed, making `getSnapshot()` be called again.
319331
onStoreChange();
320332
},

0 commit comments

Comments
 (0)