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
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import {mergeCardListWithWorkspaceFeeds} from '@libs/CardUtils';
import createOnyxDerivedValueConfig from '@userActions/OnyxDerived/createOnyxDerivedValueConfig';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {CardList} from '@src/types/onyx';

export default createOnyxDerivedValueConfig({
key: ONYXKEYS.DERIVED.NON_PERSONAL_AND_WORKSPACE_CARD_LIST,
dependencies: [ONYXKEYS.CARD_LIST, ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST],
compute: ([cardList, workspaceCardFeeds], {areAllConnectionsSet, currentValue}) => {
if (!areAllConnectionsSet) {
return currentValue ?? (CONST.EMPTY_OBJECT as CardList);
}

compute: ([cardList, workspaceCardFeeds]) => {
return mergeCardListWithWorkspaceFeeds(workspaceCardFeeds ?? CONST.EMPTY_OBJECT, cardList, true);
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import {mergeCardListWithWorkspaceFeeds} from '@libs/CardUtils';
import createOnyxDerivedValueConfig from '@userActions/OnyxDerived/createOnyxDerivedValueConfig';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {CardList} from '@src/types/onyx';

export default createOnyxDerivedValueConfig({
key: ONYXKEYS.DERIVED.PERSONAL_AND_WORKSPACE_CARD_LIST,
dependencies: [ONYXKEYS.CARD_LIST, ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST],
compute: ([cardList, workspaceCardFeeds], {areAllConnectionsSet, currentValue}) => {
if (!areAllConnectionsSet) {
return currentValue ?? (CONST.EMPTY_OBJECT as CardList);
}

compute: ([cardList, workspaceCardFeeds]) => {
return mergeCardListWithWorkspaceFeeds(workspaceCardFeeds ?? CONST.EMPTY_OBJECT, cardList);
},
});
12 changes: 1 addition & 11 deletions src/libs/actions/OnyxDerived/configs/reportAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,7 @@ export default createOnyxDerivedValueConfig({
ONYXKEYS.COLLECTION.POLICY,
ONYXKEYS.COLLECTION.REPORT_METADATA,
],
compute: (
[reports, preferredLocale, transactionViolations, reportActions, reportNameValuePairs, transactions, personalDetails, session, policies],
{currentValue, sourceValues, areAllConnectionsSet},
) => {
if (!areAllConnectionsSet) {
return {
reports: {},
locale: null,
};
}

compute: ([reports, preferredLocale, transactionViolations, reportActions, reportNameValuePairs, transactions, personalDetails, session, policies], {currentValue, sourceValues}) => {
// Check if display names changed when personal details are updated
let displayNamesChanged = false;
if (hasKeyTriggeredCompute(ONYXKEYS.PERSONAL_DETAILS_LIST, sourceValues)) {
Expand Down
27 changes: 11 additions & 16 deletions src/libs/actions/OnyxDerived/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,12 @@ function init() {

// Create an array to hold the current values for each dependency.
// We cast its type to match the tuple expected by config.compute.
let dependencyValues = new Array(totalConnections) as Parameters<typeof compute>[0];
const dependencyValues = new Array(totalConnections) as Parameters<typeof compute>[0];

OnyxUtils.get(key).then((storedDerivedValue) => {
let derivedValue = storedDerivedValue;
if (derivedValue) {
Log.info(`Derived value for ${key} restored from disk`);
} else {
OnyxUtils.tupleGet(dependencies).then((values) => {
const initialContext: DerivedValueContext<typeof key, typeof dependencies> = {
currentValue: derivedValue,
sourceValues: undefined,
areAllConnectionsSet: false,
};
// @ts-expect-error TypeScript can't confirm the shape of dependencyValues matches the compute function's parameters
derivedValue = compute(dependencyValues, initialContext);
dependencyValues = values;
setDerivedValue(key, derivedValue ?? null);
});
}

const setDependencyValue = <Index extends number>(i: Index, value: Parameters<typeof compute>[0][Index]) => {
Expand All @@ -68,17 +56,24 @@ function init() {
const context: DerivedValueContext<typeof key, typeof dependencies> = {
currentValue: undefined,
sourceValues: undefined,
areAllConnectionsSet: false,
};

const recomputeDerivedValue = (sourceKey?: string, sourceValue?: unknown, triggeredByIndex?: number) => {
// If this recompute was triggered by a connection callback, check if it initializes the connection
if (triggeredByIndex !== undefined) {
if (!areAllConnectionsSet && triggeredByIndex !== undefined) {
checkAndMarkConnectionInitialized(triggeredByIndex);
}

// Before all connections are established, don't write to Onyx.
// This prevents overwriting a valid disk-cached value with empty defaults,
// and avoids N-1 unnecessary Onyx writes during initialization.
// We still update dependencyValues via setDependencyValue so data accumulates correctly.
if (!areAllConnectionsSet) {
Log.info(`[OnyxDerived] not all connections set for ${key}, deferring Onyx write`);
return;
}

context.currentValue = derivedValue;
context.areAllConnectionsSet = areAllConnectionsSet;
context.sourceValues = sourceKey && sourceValue !== undefined ? {[sourceKey]: sourceValue} : undefined;

// @ts-expect-error TypeScript can't confirm the shape of dependencyValues matches the compute function's parameters
Expand Down
1 change: 0 additions & 1 deletion src/libs/actions/OnyxDerived/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type DerivedSourceValues<Deps extends readonly OnyxKey[]> = Partial<{
type DerivedValueContext<Key extends OnyxKey, Deps extends NonEmptyTuple<Exclude<OnyxKey, Key>>> = {
currentValue?: OnyxValue<Key>;
sourceValues?: DerivedSourceValues<Deps>;
areAllConnectionsSet: boolean;
};

/**
Expand Down
1 change: 0 additions & 1 deletion tests/unit/CardFeedErrorsDerivedValueTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type {CardFeedWithDomainID, CardFeedWithNumber} from '@src/types/onyx/Car
const DERIVED_VALUE_CONTEXT: DerivedValueContext<typeof cardFeedErrorsConfig.key, typeof cardFeedErrorsConfig.dependencies> = {
currentValue: undefined,
sourceValues: undefined,
areAllConnectionsSet: false,
};

const CARD_FEEDS = {
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/OnyxDerivedTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('OnyxDerived', () => {
const transaction = createRandomTransaction(1);

// When the report attributes are recomputed with both report and transaction updates
reportAttributes.compute([reports, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined], {areAllConnectionsSet: true});
reportAttributes.compute([reports, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined], {});
const reportAttributesComputedValue = reportAttributes.compute([reports, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined], {
sourceValues: {
[ONYXKEYS.COLLECTION.REPORT]: {
Expand All @@ -133,7 +133,6 @@ describe('OnyxDerived', () => {
[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction,
},
},
areAllConnectionsSet: true,
}).reports;

// Then the computed report attributes should contain both reports
Expand Down Expand Up @@ -591,6 +590,9 @@ describe('OnyxDerived', () => {
describe('todos', () => {
beforeAll(async () => {
onyxDerivedTestSetup();
// Initialize dependency keys so Onyx.clear() in beforeEach triggers derived value recomputation
await Onyx.set(ONYXKEYS.SESSION, {});
await waitForBatchedUpdates();
});

const CURRENT_USER_ACCOUNT_ID = 1;
Expand Down
Loading