From 8163329321d7806a41e08352738fd4590531fbb5 Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Tue, 8 Apr 2025 09:10:04 +0200 Subject: [PATCH 1/2] expose source value in useOnyx --- lib/useOnyx.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/useOnyx.ts b/lib/useOnyx.ts index b5a5b9af6..3809b5ed7 100644 --- a/lib/useOnyx.ts +++ b/lib/useOnyx.ts @@ -74,6 +74,7 @@ type FetchStatus = 'loading' | 'loaded'; type ResultMetadata = { status: FetchStatus; + sourceValue?: OnyxValue; }; type UseOnyxResult = [NonNullable | undefined, ResultMetadata]; @@ -158,6 +159,9 @@ function useOnyx>( // Indicates if we should get the newest cached value from Onyx during `getSnapshot()` execution. const shouldGetCachedValueRef = useRef(true); + // Inside useOnyx.ts, we need to track the sourceValue separately + const sourceValueRef = useRef | undefined>(undefined); + useEffect(() => { // These conditions will ensure we can only handle dynamic collection member keys from the same collection. if (options?.allowDynamicKey || previousKey === key) { @@ -284,7 +288,13 @@ function useOnyx>( // If the new value is `null` we default it to `undefined` to ensure the consumer gets a consistent result from the hook. const newStatus = newFetchStatus ?? 'loaded'; - resultRef.current = [previousValueRef.current ?? undefined, {status: newStatus}]; + resultRef.current = [ + previousValueRef.current ?? undefined, + { + status: newStatus, + sourceValue: sourceValueRef.current, + }, + ]; // If `canBeMissing` is set to `false` and the Onyx value of that key is not defined, // we log an alert so it can be acknowledged by the consumer. Additionally, we won't log alerts @@ -304,7 +314,7 @@ function useOnyx>( connectionRef.current = connectionManager.connect({ key, - callback: () => { + callback: (value, callbackKey, sourceValue) => { isConnectingRef.current = false; onStoreChangeFnRef.current = onStoreChange; @@ -315,6 +325,8 @@ function useOnyx>( // Signals that we want to get the newest cached value again in `getSnapshot()`. shouldGetCachedValueRef.current = true; + sourceValueRef.current = sourceValue; + // Finally, we signal that the store changed, making `getSnapshot()` be called again. onStoreChange(); }, From 37255faffc8fab42de5036b6bf553c6a3a115385 Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Mon, 14 Apr 2025 09:16:01 +0200 Subject: [PATCH 2/2] update sourceValue type --- lib/useOnyx.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/useOnyx.ts b/lib/useOnyx.ts index 3809b5ed7..f5929428c 100644 --- a/lib/useOnyx.ts +++ b/lib/useOnyx.ts @@ -72,12 +72,12 @@ type UseOnyxOptions = BaseUseOnyxOptions & U type FetchStatus = 'loading' | 'loaded'; -type ResultMetadata = { +type ResultMetadata = { status: FetchStatus; - sourceValue?: OnyxValue; + sourceValue?: NonNullable | undefined; }; -type UseOnyxResult = [NonNullable | undefined, ResultMetadata]; +type UseOnyxResult = [NonNullable | undefined, ResultMetadata]; /** * Gets the cached value from the Onyx cache. If the key is a collection key, it will return all the values in the collection. @@ -160,7 +160,7 @@ function useOnyx>( const shouldGetCachedValueRef = useRef(true); // Inside useOnyx.ts, we need to track the sourceValue separately - const sourceValueRef = useRef | undefined>(undefined); + const sourceValueRef = useRef | undefined>(undefined); useEffect(() => { // These conditions will ensure we can only handle dynamic collection member keys from the same collection. @@ -325,7 +325,8 @@ function useOnyx>( // Signals that we want to get the newest cached value again in `getSnapshot()`. shouldGetCachedValueRef.current = true; - sourceValueRef.current = sourceValue; + // sourceValue is unknown type, so we need to cast it to the correct type. + sourceValueRef.current = sourceValue as NonNullable; // Finally, we signal that the store changed, making `getSnapshot()` be called again. onStoreChange();