From 62e7c7921e50d9cb632bcf0ee2e798d4088a4022 Mon Sep 17 00:00:00 2001 From: jparez Date: Fri, 26 Jul 2024 14:57:59 +0200 Subject: [PATCH] cancel rewrite findchangeKeys --- src/store/index.js | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index 41b2d07f..0f88529b 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -33,31 +33,31 @@ const createStore = (instance, reducer) => { }); }; - const arraysAreEqual = (firstArray, secondArray) => { - if (firstArray.length !== secondArray.length) { - return true; - } - for (let i = 0; i < firstArray.length; i++) { - if (firstArray[i] !== secondArray[i]) { - return true; - } - } - return false; - }; const findChangedKeys = (newState, oldState, path = []) => { let changedKeys = []; + const isObject = (obj) => obj && typeof obj === 'object'; + for (const key in newState) { const fullPath = [...path, key].join('.'); + if (!Object.prototype.hasOwnProperty.call(oldState, key)) { changedKeys.push(fullPath); } else if (isObject(newState[key]) && isObject(oldState[key])) { - if ( - Array.isArray(newState[key]) && - Array.isArray(oldState[key]) && - !arraysAreEqual(newState[key], oldState[key]) - ) { - changedKeys.push(fullPath); + if (Array.isArray(newState[key]) && Array.isArray(oldState[key])) { + if (newState[key].length !== oldState[key].length) { + changedKeys.push(fullPath); + } else { + let arrayHasChanged = false; + for (let i = 0; i < newState[key].length; i++) { + if (newState[key][i] !== oldState[key][i]) { + arrayHasChanged = true; + } + } + if (arrayHasChanged) { + changedKeys.push(fullPath); + } + } } else { changedKeys = changedKeys.concat(findChangedKeys(newState[key], oldState[key], [...path, key])); } @@ -65,6 +65,7 @@ const createStore = (instance, reducer) => { changedKeys.push(fullPath); } } + return changedKeys; };