Skip to content

Commit

Permalink
fix: inject ChangeDetectorRef and call markForCheck in sideEffects
Browse files Browse the repository at this point in the history
  • Loading branch information
Waterstraal committed Sep 22, 2022
1 parent 40ebdb3 commit e76f270
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions projects/ngx-sub-form/src/lib/create-form.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import { ChangeDetectorRef, inject } from '@angular/core';
import { UntypedFormControl } from '@angular/forms';
import isEqual from 'fast-deep-equal';
import { getObservableLifecycle } from 'ngx-observable-lifecycle';
import { combineLatest, concat, EMPTY, identity, merge, Observable, of, timer } from 'rxjs';
import {
delay,
filter,
map,
mapTo,
shareReplay,
startWith,
switchMap,
take,
takeUntil,
tap,
withLatestFrom,
} from 'rxjs/operators';
import {
createFormDataFromOptions,
getControlValueAccessorBindings,
getFormGroupErrors,
handleFormArrays,
patchClassInstance,
} from './helpers';
import { delay, filter, map, mapTo, shareReplay, startWith, switchMap, take, takeUntil, tap, withLatestFrom, } from 'rxjs/operators';
import { createFormDataFromOptions, getControlValueAccessorBindings, getFormGroupErrors, handleFormArrays, patchClassInstance, } from './helpers';
import {
ComponentHooks,
ControlValueAccessorComponentInstance,
Expand Down Expand Up @@ -73,6 +56,8 @@ export function createForm<ControlInterface, FormInterface>(
afterViewInit: getObservableLifecycle(componentInstance).ngAfterViewInit,
};

const changeDetectorRef = inject(ChangeDetectorRef);

lifecyleHooks.onDestroy.pipe(take(1)).subscribe(() => {
isRemoved = true;
});
Expand Down Expand Up @@ -215,6 +200,15 @@ export function createForm<ControlInterface, FormInterface>(
formGroup.reset(value, { emitEvent: false });
}),
),
supportChangeDetectionStrategyOnPush: concat(
lifecyleHooks.afterViewInit.pipe(take(1)),
merge(controlValue$, setDisabledState$).pipe(
delay(0),
tap(() => {
changeDetectorRef.markForCheck();
}),
),
),
setDisabledState$: setDisabledState$.pipe(
tap((shouldDisable: boolean) => {
shouldDisable ? formGroup.disable({ emitEvent: false }) : formGroup.enable({ emitEvent: false });
Expand Down

0 comments on commit e76f270

Please sign in to comment.