|
1 | 1 | import { AbstractControl, PristineChangeEvent, StatusChangeEvent } from "@angular/forms";
|
2 |
| -import { Observable, map, distinctUntilChanged, startWith, filter, combineLatest, of } from "rxjs"; |
| 2 | +import { Observable, map, distinctUntilChanged, filter, combineLatest, of, defer, concat } from "rxjs"; |
3 | 3 |
|
4 | 4 | import { CommandCreator, ICommand } from "./command.model";
|
5 | 5 | import { Command } from "./command";
|
@@ -42,21 +42,26 @@ export function canExecuteFromNgForm(
|
42 | 42 | { ...CAN_EXECUTE_FORM_OPTIONS_DEFAULTS, ...options }
|
43 | 43 | : CAN_EXECUTE_FORM_OPTIONS_DEFAULTS;
|
44 | 44 |
|
| 45 | + |
45 | 46 | const pristine$ = opts.dirty
|
46 |
| - ? form.events.pipe( |
47 |
| - filter(x => x instanceof PristineChangeEvent), |
48 |
| - map(x => x.pristine), |
49 |
| - distinctUntilChanged(), |
50 |
| - startWith(form.pristine), |
51 |
| - ) : of(true); |
| 47 | + ? concat( |
| 48 | + defer(() => of(form.pristine)), |
| 49 | + form.events.pipe( |
| 50 | + filter(x => x instanceof PristineChangeEvent), |
| 51 | + map(x => x.pristine), |
| 52 | + ) |
| 53 | + ).pipe(distinctUntilChanged(),) |
| 54 | + : of(true); |
52 | 55 |
|
53 | 56 | const valid$ = opts.validity
|
54 |
| - ? form.events.pipe( |
55 |
| - filter(x => x instanceof StatusChangeEvent), |
56 |
| - map(x => x.status === "VALID"), |
57 |
| - distinctUntilChanged(), |
58 |
| - startWith(form.valid), |
59 |
| - ) : of(true); |
| 57 | + ? concat( |
| 58 | + defer(() => of(form.valid)), |
| 59 | + form.events.pipe( |
| 60 | + filter(x => x instanceof StatusChangeEvent), |
| 61 | + map(x => x.status === "VALID"), |
| 62 | + ) |
| 63 | + ).pipe(distinctUntilChanged(),) |
| 64 | + : of(true); |
60 | 65 |
|
61 | 66 | return combineLatest([pristine$, valid$]).pipe(
|
62 | 67 | map(([pristine, valid]) => !!(!opts.validity || valid) && !!(!opts.dirty || !pristine)),
|
|
0 commit comments