Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import {
Input,
Output,
booleanAttribute,
effect,
inject,
signal,
untracked,
} from '@angular/core';
import { toObservable } from '@angular/core/rxjs-interop';
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop';
import { NgControl, NgModel, type ControlValueAccessor } from '@angular/forms';
import { createInjectionToken } from 'ngxtension/create-injection-token';
import { skip } from 'rxjs';
import { filter, skip, tap } from 'rxjs';

const noop = () => undefined;

Expand Down Expand Up @@ -232,38 +231,36 @@ export class NgxControlValueAccessor<T = any> implements ControlValueAccessor {
public readonly compareTo$ =
signal<NgxControlValueAccessorCompareTo<T>>(injectCvaCompareTo());

protected readonly notifyNgControlOnValueChanges = effect(() => {
const value = this.value$();

/**
* no-op if THIS value is already _equal_ to the value of the control => we dont need to notify the control.
*/
if (this.compareTo(this.ngControl?.value, value)) {
return;
}

untracked(() => {
this.onChange(value);
});
});

protected readonly notifyNgControlOnDisabledChanges = effect(() => {
const disabled = this.disabled$();

untracked(() => {
protected readonly notifyNgControlOnValueChanges = toObservable(this.value$)
.pipe(
/**
* no-op if THIS disabled state is already _equal_ to the disabled state of the control or there is no control
* no-op if THIS value is already _equal_ to the value of the control => we dont need to notify the control.
*/
if (
this.ngControl?.control == null ||
this.ngControl.disabled === disabled
) {
return;
}

this.ngControl.control[disabled ? 'disable' : 'enable']();
});
});
filter((value) => !this.compareTo(this.ngControl?.value, value)),
tap((value) => {
this.onChange(value);
}),
takeUntilDestroyed(),
)
.subscribe();

protected readonly notifyNgControlOnDisabledChanges = toObservable(
this.disabled$,
)
.pipe(
tap((disabled) => {
/**
* no-op if THIS disabled state is already _equal_ to the disabled state of the control or there is no control
*/
if (!this.ngControl?.control || this.ngControl.disabled === disabled) {
return;
}

this.ngControl.control[disabled ? 'disable' : 'enable']();
}),
takeUntilDestroyed(),
)
.subscribe();

/** The value of this. If a control is present, it reflects it's value. */
@Input()
Expand Down