Skip to content

Commit

Permalink
Merge pull request #205 from zakhenry/fix/invalid-after-tick
Browse files Browse the repository at this point in the history
fix(Value emission): Fix invalid values being published that were valid in the previous tick
  • Loading branch information
maxime1992 authored Mar 16, 2021
2 parents b74c43a + c45fc12 commit 6dbed3d
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions projects/ngx-sub-form/src/lib/ngx-sub-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ɵmarkDirty as markDirty } from '@angular/core';
import { FormControl } from '@angular/forms';
import isEqual from 'fast-deep-equal';
import { getObservableLifecycle } from 'ngx-observable-lifecycle';
import { EMPTY, forkJoin, merge, Observable, of, timer } from 'rxjs';
import { EMPTY, forkJoin, identity, merge, Observable, of, timer } from 'rxjs';
import {
delay,
exhaustMap,
Expand Down Expand Up @@ -148,27 +148,21 @@ export function createForm<ControlInterface, FormInterface>(
if (!isRoot<ControlInterface, FormInterface>(options)) {
return formGroup.valueChanges.pipe(delay(0));
} else {
// @todo following could probably be refactored a bit to avoid code duplication
if (options.manualSave$) {
return options.manualSave$.pipe(
withLatestFrom(formGroup.valueChanges),
map(([_, formValue]) => formValue),
filter(() => formGroup.valid),
delay(0),
filter(formValue => formGroup.valid && !isEqual(transformedValue, formValue)),
filter(formValue => !isEqual(transformedValue, formValue)),
);
} else {
if (options.handleEmissionRate) {
return formGroup.valueChanges.pipe(
options.handleEmissionRate,
delay(0),
filter(formValue => formGroup.valid && !isEqual(transformedValue, formValue)),
);
} else {
return formGroup.valueChanges.pipe(
delay(0),
filter(formValue => formGroup.valid && !isEqual(transformedValue, formValue)),
);
}
return formGroup.valueChanges.pipe(
filter(() => formGroup.valid),
delay(0),
filter(formValue => !isEqual(transformedValue, formValue)),
options.handleEmissionRate ?? identity,
);
}
}
}),
Expand Down

0 comments on commit 6dbed3d

Please sign in to comment.