perf: refs #8217 getDifferences fix

This commit is contained in:
Javier Segarra 2025-04-07 15:04:26 +02:00
parent 9b4f3c71c0
commit 5f43c4eaca
4 changed files with 18 additions and 49 deletions

View File

@ -98,6 +98,7 @@ defineExpose({
reset, reset,
hasChanges, hasChanges,
saveChanges, saveChanges,
getChanges, getChanges,
formData, formData,
originalData, originalData,

View File

@ -221,7 +221,7 @@ async function save() {
isLoading.value = true; isLoading.value = true;
try { try {
formData.value = trimData(formData.value); formData.value = trimData(formData.value);
const body = $props.mapper(formData.value, originalData.value); const body = $props.mapper(originalData.value, formData.value);
const method = $props.urlCreate ? 'post' : 'patch'; const method = $props.urlCreate ? 'post' : 'patch';
const url = const url =
$props.urlCreate || $props.urlUpdate || $props.url || arrayData.store.url; $props.urlCreate || $props.urlUpdate || $props.url || arrayData.store.url;

View File

@ -1,54 +1,22 @@
export default function getDifferences(obj1, obj2) { export default function getDifferences(initialData, formData) {
const diff = {}; const differences = {};
delete initialData.$index;
delete formData.$index;
const copyObj1 = { ...obj1 }; for (const key in formData) {
const copyObj2 = obj2 ? { ...obj2 } : {}; if (formData.hasOwnProperty(key)) {
delete copyObj1.$index;
if (copyObj2) delete copyObj2.$index;
for (const key in copyObj1) {
if (copyObj1.hasOwnProperty(key)) {
if ( if (
!copyObj2.hasOwnProperty(key) || !initialData ||
!deepCompare(copyObj1[key], copyObj2[key]) !initialData.hasOwnProperty(key) ||
formData[key] !== initialData[key]
) { ) {
diff[key] = copyObj1[key]; //Añadir la diferencia solo si existe initialData
if (initialData) {
differences[key] = formData[key];
}
} }
} }
} }
for (const key in copyObj2) { return differences;
if (copyObj2.hasOwnProperty(key) && !copyObj1.hasOwnProperty(key)) {
diff[key] = copyObj2[key];
}
}
return diff;
}
function deepCompare(obj1, obj2) {
if (
typeof obj1 !== 'object' ||
obj1 === null ||
typeof obj2 !== 'object' ||
obj2 === null
) {
return obj1 === obj2;
}
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) {
return false;
}
for (const key of keys1) {
if (!obj2.hasOwnProperty(key) || !deepCompare(obj1[key], obj2[key])) {
return false;
}
}
return true;
} }

View File

@ -1,9 +1,9 @@
import getDifferences from './getDifferences'; import getDifferences from './getDifferences';
import getUpdatedValues from './getUpdatedValues'; import getUpdatedValues from './getUpdatedValues';
export default function onBeforeSave(formData, originalData) { export default function onBeforeSave(originalData, formData) {
return getUpdatedValues( return getUpdatedValues(
Object.keys(getDifferences(formData, originalData)), Object.keys(getDifferences(originalData, formData)),
formData, formData,
); );
} }