angularrxjs

How to check if anyone is subscribed to EventEmitter in Angular?


There is a EventEmitter:

@Output() edit: EventEmitter<any> = new EventEmitter();

How to check if anyone is subscribed to EventEmitter in Angular?


Solution

  • Angular EventEmitter is an RXJS Subject:

    class EventEmitter<T> extends Subject<T>
    

    Check if any subscribers/observers

    Subject.observed: bool -- true if there are 1 or more subscribers

    RXJS Source Code

    /**
     * A Subject is a special type of Observable that allows values to be
     * multicasted to many Observers. Subjects are like EventEmitters.
     *
     * Every Subject is an Observable and an Observer. You can subscribe to a
     * Subject, and you can call next to feed values as well as error and complete.
     */
    export class Subject<T> extends Observable<T> implements SubscriptionLike {
      ...
    
      private currentObservers = new Map<number, Observer<T>>()
    
      get observed() {
        return this.currentObservers.size > 0;
      }
    

    Notes

    See: Delegation: EventEmitter or Observable in Angular