Skip to content

Commit

Permalink
add back getCachedValue function
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispader committed Jun 11, 2024
1 parent 9c94b0c commit ec1c9ba
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/useOnyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type ResultMetadata = {

type UseOnyxResult<TKey extends OnyxKey, TValue> = [CachedValue<TKey, TValue>, ResultMetadata];

function getCachedValue<TKey extends OnyxKey, TValue>(key: TKey, selector?: Selector<TKey, unknown, unknown>): CachedValue<TKey, TValue> | undefined {
return (OnyxUtils.tryGetCachedValue(key, {selector}) ?? undefined) as CachedValue<TKey, TValue> | undefined;
}

function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
key: TKey,
options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<TReturnValue> & Required<UseOnyxSelectorOption<TKey, TReturnValue>>,
Expand Down Expand Up @@ -117,7 +121,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey
// If `newValue` is `null` or any other value if means that the cache does have a value for that key.
// This difference between `undefined` and other values is crucial and it's used to address the following
// conditions and use cases.
let newValue = (OnyxUtils.tryGetCachedValue(key, {selector: selectorRef.current}) ?? undefined) as CachedValue<TKey, TReturnValue> | undefined;
let newValue = getCachedValue<TKey, TReturnValue>(key, selectorRef.current);
const hasCacheForKey = cache.hasCacheForKey(key);

// Since the fetch status can be different given the use cases below, we define the variable right away.
Expand Down Expand Up @@ -148,7 +152,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey
}

return resultRef.current;
}, [key, selectorRef, options?.allowStaleData, options?.initialValue, options?.initWithStoredValues]);
}, [key, selectorRef, options?.allowStaleData, options?.initialValue]);

const subscribe = useCallback(
(onStoreChange: () => void) => {
Expand Down

0 comments on commit ec1c9ba

Please sign in to comment.