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
37 changes: 26 additions & 11 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ function mergeObject<TObject extends Record<string, unknown>>(

const targetObject = isMergeableObject(target) ? target : undefined;

// Track whether the merge actually changed anything compared to target.
// If nothing changed, we return the original target reference for reference stability.
let hasChanged = !targetObject;

// First we want to copy over all keys from the target into the destination object,
// in case "target" is a mergable object.
// If "shouldRemoveNestedNulls" is true, we want to remove null values from the merged object
Expand All @@ -103,6 +107,7 @@ function mergeObject<TObject extends Record<string, unknown>>(
const shouldOmitNullishProperty = options.shouldRemoveNestedNulls && (targetProperty === null || sourceProperty === null);

if (targetProperty === undefined || shouldOmitNullishProperty) {
hasChanged = true;
continue;
}

Expand All @@ -125,6 +130,9 @@ function mergeObject<TObject extends Record<string, unknown>>(

// If the source value is not a mergable object, we need to set the key directly.
if (!isMergeableObject(sourceProperty)) {
if (destination[key] !== sourceProperty) {
hasChanged = true;
}
destination[key] = sourceProperty;
continue;
}
Expand All @@ -134,6 +142,7 @@ function mergeObject<TObject extends Record<string, unknown>>(
// To achieve this, we first mark these nested objects with an internal flag.
// When calling fastMerge again with "mark" removal mode, the marked objects will be removed.
if (options.objectRemovalMode === 'mark' && targetProperty === null) {
hasChanged = true;
targetProperty = {[ONYX_INTERNALS__REPLACE_OBJECT_MARK]: true};
metadata.replaceNullPatches.push([[...basePath, key], {...sourceProperty}]);
}
Expand All @@ -142,6 +151,7 @@ function mergeObject<TObject extends Record<string, unknown>>(
// has the internal flag set, we replace the entire destination object with the source one and remove
// the flag.
if (options.objectRemovalMode === 'replace' && sourceProperty[ONYX_INTERNALS__REPLACE_OBJECT_MARK]) {
hasChanged = true;
// We do a spread here in order to have a new object reference and allow us to delete the internal flag
// of the merged object only.
const sourcePropertyWithoutMark = {...sourceProperty};
Expand All @@ -150,10 +160,14 @@ function mergeObject<TObject extends Record<string, unknown>>(
continue;
}

destination[key] = fastMerge(targetProperty, sourceProperty, options, metadata, [...basePath, key]).result;
const merged = fastMerge(targetProperty, sourceProperty, options, metadata, [...basePath, key]).result;
if (merged !== targetProperty) {
hasChanged = true;
}
destination[key] = merged;
}

return destination as TObject;
return hasChanged ? (destination as TObject) : (targetObject as TObject);
}

/** Checks whether the given object is an object and not null/undefined. */
Expand All @@ -170,35 +184,36 @@ function isMergeableObject<TObject extends Record<string, unknown>>(value: unkno
return isNonNullObject && !(value instanceof RegExp) && !(value instanceof Date) && !Array.isArray(value);
}

/** Deep removes the nested null values from the given value. */
/** Deep removes the nested null values from the given value. Returns the original reference if no nulls were found. */
function removeNestedNullValues<TValue extends OnyxInput<OnyxKey> | null>(value: TValue): TValue {
if (value === null || value === undefined || typeof value !== 'object') {
if (value === null || value === undefined || typeof value !== 'object' || Array.isArray(value)) {
return value;
}

if (Array.isArray(value)) {
return [...value] as TValue;
}

let hasChanged = false;
const result: Record<string, unknown> = {};

// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const key in value) {
const propertyValue = value[key];

if (propertyValue === null || propertyValue === undefined) {
hasChanged = true;
continue;
}

if (typeof propertyValue === 'object' && !Array.isArray(propertyValue)) {
const valueWithoutNestedNulls = removeNestedNullValues(propertyValue);
result[key] = valueWithoutNestedNulls;
const cleaned = removeNestedNullValues(propertyValue);
if (cleaned !== propertyValue) {
hasChanged = true;
}
result[key] = cleaned;
} else {
result[key] = propertyValue;
}
}

return result as TValue;
return hasChanged ? (result as TValue) : value;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep removeNestedNullValues from returning caller-owned object

Returning value unchanged when hasChanged is false makes Onyx retain the exact object reference passed by the caller. setWithRetry() and storage-preparation paths use removeNestedNullValues() and then cache/store that result directly, so later in-place mutations of the original input can silently mutate cached state without going through Onyx update/broadcast flow (and hasValueChanged can miss it because cache and input now alias). Before this commit, this helper always produced a fresh object/array container, which prevented that reference leakage.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before removeNestedNullValues only returned a new shallow container for objects — nested references were still shared with the caller. So any deep mutations in E/App or Onyx would have leaked through regardless.

This change doesn't make the situation worse, it just avoids a redundant shallow copy when nothing was removed.

}

/** Formats the action name by uppercasing and adding the key if provided. */
Expand Down
213 changes: 0 additions & 213 deletions tests/unit/fastMergeTest.ts

This file was deleted.

Loading
Loading