Skip to content

Commit 93b428c

Browse files
committed
Merge branch 'main' into @chrispader/migrate-to-react-native-nitro-sqlite
2 parents b9d5fe0 + fc541c1 commit 93b428c

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

lib/useOnyx.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = BaseUseOnyxOptions & U
7272

7373
type FetchStatus = 'loading' | 'loaded';
7474

75-
type ResultMetadata = {
75+
type ResultMetadata<TValue> = {
7676
status: FetchStatus;
77+
sourceValue?: NonNullable<TValue> | undefined;
7778
};
7879

79-
type UseOnyxResult<TValue> = [NonNullable<TValue> | undefined, ResultMetadata];
80+
type UseOnyxResult<TValue> = [NonNullable<TValue> | undefined, ResultMetadata<TValue>];
8081

8182
/**
8283
* Gets the cached value from the Onyx cache. If the key is a collection key, it will return all the values in the collection.
@@ -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<NonNullable<TReturnValue> | 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,9 @@ 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+
// sourceValue is unknown type, so we need to cast it to the correct type.
329+
sourceValueRef.current = sourceValue as NonNullable<TReturnValue>;
330+
318331
// Finally, we signal that the store changed, making `getSnapshot()` be called again.
319332
onStoreChange();
320333
},

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-onyx",
3-
"version": "2.0.99",
3+
"version": "2.0.100",
44
"author": "Expensify, Inc.",
55
"homepage": "https://expensify.com",
66
"description": "State management for React Native",

0 commit comments

Comments
 (0)