feat: performance improvement while saving large docs

This commit is contained in:
James
2021-09-15 16:39:43 -04:00
parent 7fc8f6dd3c
commit 901ad498b4
3 changed files with 100 additions and 9 deletions

View File

@@ -1,3 +1,4 @@
import equal from 'deep-equal';
import { unflatten, flatten } from 'flatley';
import flattenFilters from './flattenFilters';
import getSiblingData from './getSiblingData';
@@ -38,7 +39,26 @@ const unflattenRowsFromState = (state: Fields, path) => {
function fieldReducer(state: Fields, action): Fields {
switch (action.type) {
case 'REPLACE_STATE': {
return action.state;
const newState = {};
// Only update fields that have changed
// by comparing old value / initialValue to new
// ..
// This is a performance enhancement for saving
// large documents with hundreds of fields
Object.entries(action.state).forEach(([path, field]) => {
const oldField = state[path];
const newField = field;
if (!equal(oldField, newField)) {
newState[path] = newField;
} else if (oldField) {
newState[path] = oldField;
}
});
return newState;
}
case 'REMOVE': {