Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit 55cbe3c

Browse files
author
Kent C. Dodds
committed
perf(formly-form): Add optimization for braket property selector on model & formState
If you're specifying a that starts with or then we don't need another watcher because we're already deep watching these objects closes #439
1 parent b471465 commit 55cbe3c

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/directives/formly-form.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,7 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
209209
const expression = field.model;
210210
const index = $scope.fields.indexOf(field);
211211

212-
if (formlyUtil.startsWith(expression, 'model.') || formlyUtil.startsWith(expression, 'formState.')) {
213-
isNewModel = false;
214-
}
212+
isNewModel = !refrencesCurrentlyWatchedModel(expression);
215213

216214
field.model = evalCloseToFormlyExpression(expression, undefined, field, index);
217215
if (!field.model) {
@@ -225,6 +223,12 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
225223
return isNewModel;
226224
}
227225

226+
function refrencesCurrentlyWatchedModel(expression) {
227+
return ['model', 'formState'].some(item => {
228+
return formlyUtil.startsWith(expression, `${item}.`) || formlyUtil.startsWith(expression, `${item}[`);
229+
});
230+
}
231+
228232
function attachKey(field, index) {
229233
if (!isFieldGroup(field)) {
230234
field.key = field.key || index || 0;

src/directives/formly-form.test.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,23 @@ describe('formly-form', () => {
430430
});
431431

432432
it('starting with "model." should be assigned with only one watcher', () => {
433-
scope.fields[0].model = 'model.nested';
433+
testModelAccessor('model.nested');
434+
});
435+
436+
it('starting with "model[" should be assigned with only one watcher', () => {
437+
testModelAccessor('model["nested"]');
438+
});
439+
440+
it('starting with "formState." should be assigned with only one watcher', () => {
441+
testFormStateAccessor('formState.nested');
442+
});
443+
444+
it('starting with "formState[" should be assigned with only one watcher', () => {
445+
testFormStateAccessor('formState["nested"]');
446+
});
447+
448+
function testModelAccessor(accessor) {
449+
scope.fields[0].model = accessor;
434450

435451
compileAndDigest();
436452
$timeout.flush();
@@ -442,16 +458,16 @@ describe('formly-form', () => {
442458
$timeout.flush();
443459

444460
expect(spy).to.have.been.calledOnce;
445-
});
461+
}
446462

447-
it('starting with "formState." should be assigned with only one watcher', () => {
463+
function testFormStateAccessor(accessor) {
448464
const formWithOptions = '<formly-form model="model" fields="fields" options="options"></formly-form>';
449465
scope.options = {
450466
formState: {
451467
nested: {}
452468
}
453469
};
454-
scope.fields[0].model = 'formState.nested';
470+
scope.fields[0].model = accessor;
455471

456472
compileAndDigest(formWithOptions);
457473
$timeout.flush();
@@ -463,7 +479,7 @@ describe('formly-form', () => {
463479
$timeout.flush();
464480

465481
expect(spy).to.have.been.calledOnce;
466-
});
482+
}
467483
});
468484

469485
describe('hideExpression', () => {

0 commit comments

Comments
 (0)