Skip to content

Commit df7fede

Browse files
committed
Use tryGetCachedValue from OnyxUtils
1 parent 293484d commit df7fede

1 file changed

Lines changed: 2 additions & 31 deletions

File tree

lib/useOnyx.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {Connection} from './OnyxConnectionManager';
66
import connectionManager from './OnyxConnectionManager';
77
import OnyxUtils from './OnyxUtils';
88
import * as GlobalSettings from './GlobalSettings';
9-
import type {CollectionKeyBase, KeyValueMapping, OnyxCollection, OnyxKey, OnyxValue} from './types';
9+
import type {CollectionKeyBase, OnyxKey, OnyxValue} from './types';
1010
import useLiveRef from './useLiveRef';
1111
import usePrevious from './usePrevious';
1212
import decorateWithMetrics from './metrics';
@@ -79,35 +79,6 @@ type ResultMetadata<TValue> = {
7979

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

82-
/**
83-
* Gets the cached value from the Onyx cache. If the key is a collection key, it will return all the values in the collection.
84-
* It is a fork of `tryGetCachedValue` from `OnyxUtils` caused by different selector logic in `useOnyx`. It should be unified in the future, when `withOnyx` is removed.
85-
*/
86-
function tryGetCachedValue<TKey extends OnyxKey>(key: TKey): OnyxValue<OnyxKey> {
87-
if (!OnyxUtils.isCollectionKey(key)) {
88-
return OnyxCache.get(key);
89-
}
90-
91-
const allCacheKeys = OnyxCache.getAllKeys();
92-
93-
// It is possible we haven't loaded all keys yet so we do not know if the
94-
// collection actually exists.
95-
if (allCacheKeys.size === 0) {
96-
return;
97-
}
98-
99-
const values: OnyxCollection<KeyValueMapping[TKey]> = {};
100-
allCacheKeys.forEach((cacheKey) => {
101-
if (!cacheKey.startsWith(key)) {
102-
return;
103-
}
104-
105-
values[cacheKey] = OnyxCache.get(cacheKey);
106-
});
107-
108-
return values;
109-
}
110-
11182
function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
11283
key: TKey,
11384
options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<TReturnValue> & Required<UseOnyxSelectorOption<TKey, TReturnValue>>,
@@ -233,7 +204,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
233204
// update `newValueRef` when `Onyx.connect()` callback is fired.
234205
if (isFirstConnectionRef.current || shouldGetCachedValueRef.current) {
235206
// Gets the value from cache and maps it with selector. It changes `null` to `undefined` for `useOnyx` compatibility.
236-
const value = tryGetCachedValue(key) as OnyxValue<TKey>;
207+
const value = OnyxUtils.tryGetCachedValue(key) as OnyxValue<TKey>;
237208
const selectedValue = selectorRef.current ? selectorRef.current(value) : value;
238209
newValueRef.current = (selectedValue ?? undefined) as TReturnValue | undefined;
239210

0 commit comments

Comments
 (0)