Skip to content

Commit df39e04

Browse files
committed
Merge branch 'main' into perf/improve-matching-keys-lookup
2 parents eefa6d4 + 5796442 commit df39e04

13 files changed

Lines changed: 6281 additions & 9529 deletions

babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
presets: ['module:metro-react-native-babel-preset'],
2+
presets: ['module:@react-native/babel-preset'],
33
};

jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ module.exports = {
1010
__DEV__: true,
1111
WebSocket: {},
1212
},
13-
timers: 'fake',
1413
testEnvironment: 'jsdom',
15-
setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect', './jestSetup.js'],
16-
testTimeout: 10000,
14+
setupFilesAfterEnv: ['./jestSetup.js'],
15+
testTimeout: 60000,
16+
transformIgnorePatterns: ['node_modules/(?!((@)?react-native|@ngneat/falso|uuid)/)'],
1717
};

lib/useOnyx.ts

Lines changed: 3 additions & 32 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

@@ -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

Comments
 (0)