angular icon indicating copy to clipboard operation
angular copied to clipboard

Add observed property to OutputEmitterRef

Open GuillaumeNury opened this issue 11 months ago • 17 comments

Which @angular/* package(s) are relevant/related to the feature request?

core

Description

With the @Output decorator, I used to check the observed of the EventEmitter (because it inherits Subject) to enable/disable some features.

For example, <lu-select (clueChange)="..." /> would display a search bar in LuSelect component but <lu-select /> would not.

This solution seems elegant as it avoid an additional input that would lead to unwanted cases:

  • Both <lu-select enableClue (clueChange)="..." /> and <lu-select /> would work
  • <lu-select (clueChange)="..." /> would be useless because the search bar is not disabled because enableClue = false
  • <lu-select enableClue /> would be useless because the search bar is displayed, but nobody listen to its value

Proposed solution

Add a getterproperty observed in OutputEmitterRef

(in this file)

export class OutputEmitterRef<T> implements OutputRef<T> {
  private listeners: Array<(value: T) => void>|null = null;

  get observed(): boolean {
    return !!this.listeners?.length;
  }

  //...
}

Alternatives considered

Replace existing usages of emitter.observed by inputs.

GuillaumeNury avatar Mar 13 '24 09:03 GuillaumeNury