Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Call useOnyx selector for entire collection #585

Merged
Merged
Show file tree
Hide file tree
Changes from 9 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
41 changes: 36 additions & 5 deletions lib/useOnyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import OnyxCache from './OnyxCache';
import type {Connection} from './OnyxConnectionManager';
import connectionManager from './OnyxConnectionManager';
import OnyxUtils from './OnyxUtils';
import type {CollectionKeyBase, OnyxCollection, OnyxEntry, OnyxKey, OnyxValue, Selector} from './types';
import type {CollectionKeyBase, OnyxCollection, OnyxEntry, OnyxKey, OnyxValue} from './types';
import useLiveRef from './useLiveRef';
import usePrevious from './usePrevious';

Expand Down Expand Up @@ -40,7 +40,7 @@ type UseOnyxSelectorOption<TKey extends OnyxKey, TReturnValue> = {
* when the subset of data changes. Otherwise, any change of data on any property would normally
* cause the component to re-render (and that can be expensive from a performance standpoint).
*/
selector?: Selector<TKey, unknown, TReturnValue>;
selector?: UseOnyxSelector<TKey, TReturnValue>;
kacper-mikolajczak marked this conversation as resolved.
Show resolved Hide resolved
};

type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = BaseUseOnyxOptions & UseOnyxInitialValueOption<TReturnValue> & UseOnyxSelectorOption<TKey, TReturnValue>;
Expand All @@ -57,8 +57,39 @@ 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;
type UseOnyxSelector<TKey extends OnyxKey, SelectorValue> = (data?: OnyxValue<TKey>) => SelectorValue;

function getCachedValue<TKey extends OnyxKey>(key: TKey): OnyxValue<TKey> | undefined {
kacper-mikolajczak marked this conversation as resolved.
Show resolved Hide resolved
kacper-mikolajczak marked this conversation as resolved.
Show resolved Hide resolved
if (!OnyxUtils.isCollectionKey(key)) {
return OnyxCache.get(key) as OnyxValue<TKey> | undefined;
kacper-mikolajczak marked this conversation as resolved.
Show resolved Hide resolved
}

const allCacheKeys = OnyxCache.getAllKeys();

// It is possible we haven't loaded all keys yet so we do not know if the
// collection actually exists.
if (allCacheKeys.size === 0) {
return;
}

const values: Record<string, unknown> = {};
kacper-mikolajczak marked this conversation as resolved.
Show resolved Hide resolved
allCacheKeys.forEach((cacheKey) => {
if (!cacheKey.startsWith(key)) {
return;
}

values[cacheKey] = OnyxCache.get(cacheKey);
});

return values as OnyxValue<TKey>;
kacper-mikolajczak marked this conversation as resolved.
Show resolved Hide resolved
kacper-mikolajczak marked this conversation as resolved.
Show resolved Hide resolved
}

function getSelectedValue<TKey extends OnyxKey, TValue = OnyxValue<TKey> | undefined>(key: TKey, selector?: UseOnyxSelector<TKey, TValue>) {
kacper-mikolajczak marked this conversation as resolved.
Show resolved Hide resolved
kacper-mikolajczak marked this conversation as resolved.
Show resolved Hide resolved
const value = getCachedValue(key);

const selectedValue = selector ? selector(value) : value;

return selectedValue;
}

function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
Expand Down Expand Up @@ -159,7 +190,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey
// If `newValueRef.current` is `null` or any other value it 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.
newValueRef.current = getCachedValue<TKey, TReturnValue>(key, selectorRef.current);
newValueRef.current = getSelectedValue(key, selectorRef.current) as CachedValue<TKey, TReturnValue>;

// We set this flag to `false` again since we don't want to get the newest cached value every time `getSnapshot()` is executed,
// and only when `Onyx.connect()` callback is fired.
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/useOnyxTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {act, renderHook} from '@testing-library/react-native';
import type {OnyxEntry} from '../../lib';
import type {OnyxCollection, OnyxEntry} from '../../lib';
import Onyx, {useOnyx} from '../../lib';
import OnyxUtils from '../../lib/OnyxUtils';
import StorageMock from '../../lib/storage';
Expand Down Expand Up @@ -176,7 +176,11 @@ describe('useOnyx', () => {
const {result} = renderHook(() =>
useOnyx(ONYXKEYS.COLLECTION.TEST_KEY, {
// @ts-expect-error bypass
selector: (entry: OnyxEntry<{id: string; name: string}>) => entry?.id,
selector: (entries: OnyxCollection<{id: string; name: string}>) =>
Object.entries(entries ?? {}).reduce<NonNullable<OnyxCollection<string>>>((acc, [key, value]) => {
acc[key] = value?.id;
return acc;
}, {}),
}),
);

Expand Down
Loading