Skip to content

Add observed property to OutputEmitterRef #54837

@GuillaumeNury

Description

@GuillaumeNury

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions