Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/libs/actions/OnyxDerived/configs/reportAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {generateIsEmptyReport, generateReportAttributes, generateReportName, isValidReport} from '@libs/ReportUtils';
import SidebarUtils from '@libs/SidebarUtils';
import createOnyxDerivedValueConfig from '@userActions/OnyxDerived/createOnyxDerivedValueConfig';
import hasKeyTriggeredCompute from '@userActions/OnyxDerived/utils';
import {hasKeyTriggeredCompute} from '@userActions/OnyxDerived/utils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReportAttributesDerivedValue} from '@src/types/onyx';
Expand Down
5 changes: 3 additions & 2 deletions src/libs/actions/OnyxDerived/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import ObjectUtils from '@src/types/utils/ObjectUtils';
import ONYX_DERIVED_VALUES from './ONYX_DERIVED_VALUES';
import type {DerivedValueContext} from './types';
import {setDerivedValue} from './utils';

/**
* Initialize all Onyx derived values, store them in Onyx, and setup listeners to update them when dependencies change.
Expand Down Expand Up @@ -42,7 +43,7 @@
// @ts-expect-error TypeScript can't confirm the shape of dependencyValues matches the compute function's parameters
derivedValue = compute(dependencyValues, initialContext);
dependencyValues = values;
Onyx.set(key, derivedValue ?? null);
setDerivedValue(key, derivedValue ?? null);
});
}

Expand Down Expand Up @@ -84,7 +85,7 @@
const newDerivedValue = compute(dependencyValues, context);
Log.info(`[OnyxDerived] updating value for ${key} in Onyx`);
derivedValue = newDerivedValue;
Onyx.set(key, derivedValue ?? null);
setDerivedValue(key, derivedValue);
};

for (let i = 0; i < dependencies.length; i++) {
Expand All @@ -92,7 +93,7 @@
const dependencyOnyxKey = dependencies[dependencyIndex];

if (OnyxUtils.isCollectionKey(dependencyOnyxKey)) {
Onyx.connect({

Check warning on line 96 in src/libs/actions/OnyxDerived/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: dependencyOnyxKey,
waitForCollectionCallback: true,
callback: (value, collectionKey, sourceValue) => {
Expand All @@ -103,7 +104,7 @@
});
} else if (dependencyOnyxKey === ONYXKEYS.NVP_PREFERRED_LOCALE) {
// Special case for locale, we want to recompute derived values when the locale change actually loads.
Onyx.connect({

Check warning on line 107 in src/libs/actions/OnyxDerived/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.ARE_TRANSLATIONS_LOADING,
initWithStoredValues: false,
callback: (value) => {
Expand All @@ -123,7 +124,7 @@
},
});
} else {
Onyx.connect({

Check warning on line 127 in src/libs/actions/OnyxDerived/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: dependencyOnyxKey,
callback: (value) => {
Log.info(`[OnyxDerived] dependency ${dependencyOnyxKey} for derived key ${key} changed, recomputing`);
Expand Down
18 changes: 16 additions & 2 deletions src/libs/actions/OnyxDerived/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Onyx from 'react-native-onyx';
import type {OnyxInput} from 'react-native-onyx';
import type {NonEmptyTuple} from 'type-fest';
import type {OnyxKey} from '@src/ONYXKEYS';
import type {OnyxDerivedKey, OnyxKey} from '@src/ONYXKEYS';
import type {DerivedValueContext} from './types';

/**
Expand All @@ -12,4 +14,16 @@ const hasKeyTriggeredCompute = <K extends OnyxKey, Deps extends NonEmptyTuple<Ex
return Object.keys(sourceValues).some((sourceKey) => sourceKey === key);
};

export default hasKeyTriggeredCompute;
/**
* Set a derived value in Onyx
* As a performance optimization, it skips the cache check and null removal
* For derived values, we fully control their lifecycle and recompute them when any dependency changes - so we don’t need a deep comparison
* Also, null may be a legitimate result of the computation, so pruning it is unnecessary
*/
const setDerivedValue = (key: OnyxDerivedKey, value: OnyxInput<OnyxDerivedKey>) =>
Onyx.set(key, value, {
skipCacheCheck: true,
skipNullRemoval: true,
});

export {hasKeyTriggeredCompute, setDerivedValue};
Loading