@@ -6,7 +6,7 @@ import type {Connection} from './OnyxConnectionManager';
66import connectionManager from './OnyxConnectionManager' ;
77import OnyxUtils from './OnyxUtils' ;
88import * as GlobalSettings from './GlobalSettings' ;
9- import type { CollectionKeyBase , KeyValueMapping , OnyxCollection , OnyxKey , OnyxValue } from './types' ;
9+ import type { CollectionKeyBase , OnyxKey , OnyxValue } from './types' ;
1010import useLiveRef from './useLiveRef' ;
1111import usePrevious from './usePrevious' ;
1212import decorateWithMetrics from './metrics' ;
@@ -79,35 +79,6 @@ type ResultMetadata<TValue> = {
7979
8080type 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-
11182function 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
@@ -300,7 +271,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
300271 // we log an alert so it can be acknowledged by the consumer. Additionally, we won't log alerts
301272 // if there's a `Onyx.clear()` task in progress.
302273 if ( options ?. canBeMissing === false && newStatus === 'loaded' && ! isOnyxValueDefined && ! OnyxCache . hasPendingTask ( TASK . CLEAR ) ) {
303- Logger . logAlert ( `useOnyx returned no data for key with canBeMissing set to false.` , { key, showAlert : true } ) ;
274+ Logger . logAlert ( `useOnyx returned no data for key with canBeMissing set to false for key $ {key } ` , { showAlert : true } ) ;
304275 }
305276 }
306277
0 commit comments