Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2431f20

Browse files
atscottdylhunn
authored andcommitted
refactor(forms): Update status, touched, and pristine to use private setter (#56573)
This refactor updates the touched, status, and pristine properties to have a private setter. PR Close #56573
1 parent ccc8c80 commit 2431f20

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

‎packages/forms/src/model/abstract_model.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,9 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
594594
get status(): FormControlStatus {
595595
return untracked(this.statusReactive)!;
596596
}
597+
private set status(v: FormControlStatus) {
598+
untracked(() => this.statusReactive.set(v));
599+
}
597600
/** @internal */
598601
readonly _status = computed(() => this.statusReactive());
599602
private readonly statusReactive = signal<FormControlStatus | undefined>(undefined);
@@ -678,6 +681,9 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
678681
get pristine(): boolean {
679682
return untracked(this.pristineReactive);
680683
}
684+
private set pristine(v: boolean) {
685+
untracked(() => this.pristineReactive.set(v));
686+
}
681687
/** @internal */
682688
readonly _pristine = computed(() => this.pristineReactive());
683689
private readonly pristineReactive = signal(true);
@@ -702,6 +708,9 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
702708
get touched(): boolean {
703709
return untracked(this.touchedReactive);
704710
}
711+
private set touched(v: boolean) {
712+
untracked(() => this.touchedReactive.set(v));
713+
}
705714
/** @internal */
706715
readonly _touched = computed(() => this.touchedReactive());
707716
private readonly touchedReactive = signal(false);
@@ -970,7 +979,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
970979
opts: {onlySelf?: boolean; emitEvent?: boolean; sourceControl?: AbstractControl} = {},
971980
): void {
972981
const changed = this.touched === false;
973-
untracked(() => this.touchedReactive.set(true));
982+
this.touched = true;
974983

975984
const sourceControl = opts.sourceControl ?? this;
976985
if (this._parent && !opts.onlySelf) {
@@ -1030,7 +1039,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
10301039
opts: {onlySelf?: boolean; emitEvent?: boolean; sourceControl?: AbstractControl} = {},
10311040
): void {
10321041
const changed = this.touched === true;
1033-
untracked(() => this.touchedReactive.set(false));
1042+
this.touched = false;
10341043
this._pendingTouched = false;
10351044

10361045
const sourceControl = opts.sourceControl ?? this;
@@ -1076,7 +1085,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
10761085
opts: {onlySelf?: boolean; emitEvent?: boolean; sourceControl?: AbstractControl} = {},
10771086
): void {
10781087
const changed = this.pristine === true;
1079-
untracked(() => this.pristineReactive.set(false));
1088+
this.pristine = false;
10801089

10811090
const sourceControl = opts.sourceControl ?? this;
10821091
if (this._parent && !opts.onlySelf) {
@@ -1120,7 +1129,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
11201129
opts: {onlySelf?: boolean; emitEvent?: boolean; sourceControl?: AbstractControl} = {},
11211130
): void {
11221131
const changed = this.pristine === false;
1123-
untracked(() => this.pristineReactive.set(true));
1132+
this.pristine = true;
11241133
this._pendingDirty = false;
11251134

11261135
const sourceControl = opts.sourceControl ?? this;
@@ -1167,7 +1176,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
11671176
markAsPending(
11681177
opts: {onlySelf?: boolean; emitEvent?: boolean; sourceControl?: AbstractControl} = {},
11691178
): void {
1170-
untracked(() => this.statusReactive.set(PENDING));
1179+
this.status = PENDING;
11711180

11721181
const sourceControl = opts.sourceControl ?? this;
11731182
if (opts.emitEvent !== false) {
@@ -1209,7 +1218,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
12091218
// parent's dirtiness based on the children.
12101219
const skipPristineCheck = this._parentMarkedDirty(opts.onlySelf);
12111220

1212-
untracked(() => this.statusReactive.set(DISABLED));
1221+
this.status = DISABLED;
12131222
(this as Writable<this>).errors = null;
12141223
this._forEachChild((control: AbstractControl) => {
12151224
/** We don't propagate the source control downwards */
@@ -1252,7 +1261,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
12521261
// parent's dirtiness based on the children.
12531262
const skipPristineCheck = this._parentMarkedDirty(opts.onlySelf);
12541263

1255-
untracked(() => this.statusReactive.set(VALID));
1264+
this.status = VALID;
12561265
this._forEachChild((control: AbstractControl) => {
12571266
control.enable({...opts, onlySelf: true});
12581267
});
@@ -1340,7 +1349,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
13401349
const shouldHaveEmitted = this._cancelExistingSubscription();
13411350

13421351
(this as Writable<this>).errors = this._runValidator();
1343-
untracked(() => this.statusReactive.set(this._calculateStatus()));
1352+
this.status = this._calculateStatus();
13441353

13451354
if (this.status === VALID || this.status === PENDING) {
13461355
// If the canceled subscription should have emitted
@@ -1369,7 +1378,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
13691378
}
13701379

13711380
private _setInitialStatus() {
1372-
untracked(() => this.statusReactive.set(this._allControlsDisabled() ? DISABLED : VALID));
1381+
this.status = this._allControlsDisabled() ? DISABLED : VALID;
13731382
}
13741383

13751384
private _runValidator(): ValidationErrors | null {
@@ -1378,7 +1387,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
13781387

13791388
private _runAsyncValidator(shouldHaveEmitted: boolean, emitEvent?: boolean): void {
13801389
if (this.asyncValidator) {
1381-
untracked(() => this.statusReactive.set(PENDING));
1390+
this.status = PENDING;
13821391
this._hasOwnPendingAsyncValidator = {emitEvent: emitEvent !== false};
13831392
const obs = toObservable(this.asyncValidator(this));
13841393
this._asyncValidationSubscription = obs.subscribe((errors: ValidationErrors | null) => {
@@ -1594,7 +1603,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
15941603
changedControl: AbstractControl,
15951604
shouldHaveEmitted?: boolean,
15961605
): void {
1597-
untracked(() => this.statusReactive.set(this._calculateStatus()));
1606+
this.status = this._calculateStatus();
15981607

15991608
if (emitEvent) {
16001609
(this.statusChanges as EventEmitter<FormControlStatus>).emit(this.status);
@@ -1660,7 +1669,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
16601669
_updatePristine(opts: {onlySelf?: boolean}, changedControl: AbstractControl): void {
16611670
const newPristine = !this._anyControlsDirty();
16621671
const changed = this.pristine !== newPristine;
1663-
untracked(() => this.pristineReactive.set(newPristine));
1672+
this.pristine = newPristine;
16641673

16651674
if (this._parent && !opts.onlySelf) {
16661675
this._parent._updatePristine(opts, changedControl);
@@ -1673,7 +1682,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
16731682

16741683
/** @internal */
16751684
_updateTouched(opts: {onlySelf?: boolean} = {}, changedControl: AbstractControl): void {
1676-
untracked(() => this.touchedReactive.set(this._anyControlsTouched()));
1685+
this.touched = this._anyControlsTouched();
16771686
this._events.next(new TouchedChangeEvent(this.touched, changedControl));
16781687

16791688
if (this._parent && !opts.onlySelf) {

0 commit comments

Comments
 (0)