diff --git a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts index c510f3ce0c035..487613e879dcf 100644 --- a/src/libs/actions/OnyxDerived/configs/reportAttributes.ts +++ b/src/libs/actions/OnyxDerived/configs/reportAttributes.ts @@ -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'; diff --git a/src/libs/actions/OnyxDerived/index.ts b/src/libs/actions/OnyxDerived/index.ts index a40624a1692ef..946fe74fa2927 100644 --- a/src/libs/actions/OnyxDerived/index.ts +++ b/src/libs/actions/OnyxDerived/index.ts @@ -13,6 +13,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; 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. @@ -42,7 +43,7 @@ function init() { // @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); }); } @@ -84,7 +85,7 @@ function init() { 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++) { diff --git a/src/libs/actions/OnyxDerived/utils.ts b/src/libs/actions/OnyxDerived/utils.ts index 81883bfbacfec..0a24d65e011e0 100644 --- a/src/libs/actions/OnyxDerived/utils.ts +++ b/src/libs/actions/OnyxDerived/utils.ts @@ -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'; /** @@ -12,4 +14,16 @@ const hasKeyTriggeredCompute = 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) => + Onyx.set(key, value, { + skipCacheCheck: true, + skipNullRemoval: true, + }); + +export {hasKeyTriggeredCompute, setDerivedValue};