Skip to content

Commit

Permalink
Merge pull request #207 from cloudnc/feat/customisable-filter-predicate
Browse files Browse the repository at this point in the history
feat(Output control): Add option to provide custom predicate for controlling the output filter
  • Loading branch information
zak-cloudnc authored Mar 17, 2021
2 parents 6dbed3d + 0493115 commit b04afc5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
29 changes: 13 additions & 16 deletions projects/ngx-sub-form/src/lib/ngx-sub-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,19 @@ export function createForm<ControlInterface, FormInterface>(
if (!isRoot<ControlInterface, FormInterface>(options)) {
return formGroup.valueChanges.pipe(delay(0));
} else {
if (options.manualSave$) {
return options.manualSave$.pipe(
withLatestFrom(formGroup.valueChanges),
map(([_, formValue]) => formValue),
filter(() => formGroup.valid),
delay(0),
filter(formValue => !isEqual(transformedValue, formValue)),
);
} else {
return formGroup.valueChanges.pipe(
filter(() => formGroup.valid),
delay(0),
filter(formValue => !isEqual(transformedValue, formValue)),
options.handleEmissionRate ?? identity,
);
}
const formValues$ = options.manualSave$
? options.manualSave$.pipe(
withLatestFrom(formGroup.valueChanges),
map(([_, formValue]) => formValue),
)
: formGroup.valueChanges;

return formValues$.pipe(
filter(() => formGroup.valid),
delay(0),
filter(formValue => (options.outputFilterPredicate ?? isEqual)(transformedValue, formValue)),
options.handleEmissionRate ?? identity,
);
}
}),
map(value =>
Expand Down
8 changes: 6 additions & 2 deletions projects/ngx-sub-form/src/lib/ngx-sub-form.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ export type NgxRootFormOptions<ControlInterface, FormInterface = ControlInterfac
// if you want to transform it into a manual root form, provide the
// following observable which trigger a save every time a value is emitted
manualSave$?: Observable<void>;
// @todo it should either be `manualSave$` OR `handleEmissionRate` OR none of them
// if you're creating an automatic root form, you can customise the emission rate
// The default behavior is to compare the current transformed value of input$ with the current value of the form, and
// if these are equal emission on output$ is suppressed to prevent the from broadcasting the current value.
// Configure this option to provide your own custom predicate whether or not the form should emit.
outputFilterPredicate?: (currentInputValue: FormInterface, outputValue: FormInterface) => boolean;
// if you want to control how frequently the form emits on the output$, you can customise the emission rate with this
// option. e.g. `handleEmissionRate: formValue$ => formValue$.pipe(debounceTime(300)),`
handleEmissionRate?: (obs$: Observable<FormInterface>) => Observable<FormInterface>;
};

Expand Down

0 comments on commit b04afc5

Please sign in to comment.