Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/useOnyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = BaseUseOnyxOptions & U

type FetchStatus = 'loading' | 'loaded';

type ResultMetadata = {
type ResultMetadata<TValue> = {
status: FetchStatus;
sourceValue?: NonNullable<TValue> | undefined;
};

type UseOnyxResult<TValue> = [NonNullable<TValue> | undefined, ResultMetadata];
type UseOnyxResult<TValue> = [NonNullable<TValue> | undefined, ResultMetadata<TValue>];

/**
* Gets the cached value from the Onyx cache. If the key is a collection key, it will return all the values in the collection.
Expand Down Expand Up @@ -158,6 +159,9 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
// 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<NonNullable<TReturnValue> | undefined>(undefined);

useEffect(() => {
// These conditions will ensure we can only handle dynamic collection member keys from the same collection.
if (options?.allowDynamicKey || previousKey === key) {
Expand Down Expand Up @@ -284,7 +288,13 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(

// 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
Expand All @@ -304,7 +314,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(

connectionRef.current = connectionManager.connect<CollectionKeyBase>({
key,
callback: () => {
callback: (value, callbackKey, sourceValue) => {
isConnectingRef.current = false;
onStoreChangeFnRef.current = onStoreChange;

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

// sourceValue is unknown type, so we need to cast it to the correct type.
sourceValueRef.current = sourceValue as NonNullable<TReturnValue>;

// Finally, we signal that the store changed, making `getSnapshot()` be called again.
onStoreChange();
},
Expand Down