From e6e928919d4c2321ef652c6f62272d46ea69b42c Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Wed, 9 Jul 2025 09:31:07 +0200 Subject: [PATCH 1/2] wrap Onyx.set for derived values in a method --- .../OnyxDerived/configs/reportAttributes.ts | 2 +- src/libs/actions/OnyxDerived/index.ts | 3 ++- src/libs/actions/OnyxDerived/utils.ts | 18 ++++++++++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) 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 16cbe70857a30..10b9ffdf96991 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. @@ -41,7 +42,7 @@ function init() { }; derivedValue = compute(dependencyValues, initialContext); dependencyValues = values; - Onyx.set(key, derivedValue ?? null); + setDerivedValue(key, derivedValue ?? null); }); } 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}; From 81405ab1c90bb5073a673e1963b90bc195ec354e Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Fri, 11 Jul 2025 11:18:03 +0200 Subject: [PATCH 2/2] use setDerivedValue on recompute --- src/libs/actions/OnyxDerived/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/OnyxDerived/index.ts b/src/libs/actions/OnyxDerived/index.ts index 64066f412880f..946fe74fa2927 100644 --- a/src/libs/actions/OnyxDerived/index.ts +++ b/src/libs/actions/OnyxDerived/index.ts @@ -85,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++) {