Skip to content

Commit

Permalink
Refactored/simplified updates to be solely dependent on tree traversal.
Browse files Browse the repository at this point in the history
Still ways to go, because this still relies on a full tree update, but it's going well so far.
  • Loading branch information
hexus committed Feb 9, 2019
1 parent e5d1496 commit d88acf2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 40 deletions.
2 changes: 1 addition & 1 deletion build/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/old.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/pragma.js

Large diffs are not rendered by default.

65 changes: 28 additions & 37 deletions src/services/FormProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -929,23 +929,35 @@ export default class FormProcessor
//let diff = this.diffFormData(data);
//console.timeEnd('diffFormData');

this.updatePath('', data);
}

/**
* Update the field at the given path.
*
* @param {string} path - The path of the field to update.
* @param {Object} data - The data to update with.
* @param {number} [direction=BOTH] - Update direction (UP: -1, BOTH: 0, DOWN: 1)
*/
updatePath(path, data, direction = BOTH)
{
let field = this.getField(path);

if (!field) {
return;
}

this.clearValueCache(path);

// Update the field and its children
console.time('traverseTree');
traverseTree(
this.tree,
field,
(field) => {
// Pre-order operation
this.updateFieldInheritance(field, data);

// TODO: Skip fields that aren't in the diff using has()
// if (field.path &&
// (
// !has(diff.added, field.path) &&
// !has(diff.updated, field.path) &&
// !has(diff.deleted, field.path)
// )
// ) {
// return false;
// }
// TODO: Skip fields that have already been visited

// Skip omitted fields
return !field.omit;
Expand All @@ -961,18 +973,8 @@ export default class FormProcessor
}
);
console.timeEnd('traverseTree');
}

/**
* Update the field at the given path.
*
* @param {string} path - The path of the field to update.
* @param {Object} data - The data to update with.
* @param {number} [direction=BOTH] - Update direction (UP: -1, BOTH: 0, DOWN: 1)
*/
updatePath(path, data, direction = BOTH)
{
this.updateField(this.getField(path), data, direction);

// TODO: Update parent fields and their dependencies
}

/**
Expand All @@ -993,7 +995,7 @@ export default class FormProcessor
}

for (let i = 0; i < fields.length; i++) {
this.updateField(fields[i], data, direction);
this.updatePath(fields[i].path, data, direction);
}
}

Expand All @@ -1014,9 +1016,6 @@ export default class FormProcessor
return;
}

//console.log('updateField()', field.path);
//console.count('updateField()');

this.clearValueCache(field.path);

// Update the field's children
Expand All @@ -1030,11 +1029,6 @@ export default class FormProcessor
}
}

// Skip if this field has already been updated
// if (this.updatedFields[field.path]) {
// return;
// }

// Apply default values
this.applyDefaults([field]);

Expand All @@ -1043,9 +1037,6 @@ export default class FormProcessor

// Update the field's value (and update all fields dependent on this one)
this.updateFieldValue(field, data, direction <= 0);

// Mark the field as updated
//this.updatedFields[field.path] = true;
}

/**
Expand Down Expand Up @@ -1104,7 +1095,7 @@ export default class FormProcessor
}

// Update fields listed as dependencies
this.updateFields(this.getFieldExpressionDependencies(field), data);
//this.updateFields(this.getFieldExpressionDependencies(field), data);
}

/**
Expand Down Expand Up @@ -1485,7 +1476,7 @@ export default class FormProcessor
this.removeData(field, data);

// Update the parent field
this.updateField(this.getFieldParent(field), data);
this.updatePath(this.getFieldParent(field).path, data);
}

/**
Expand Down

0 comments on commit d88acf2

Please sign in to comment.