DEV Community

Discussion on: NgNotes - Don't ever subscribe!

Collapse
 
anduser96 profile image
Andrei Gatej

Interesting approach!

One thing that seems a little counterintuitive to me is that fact that EventEmitter is used for something else that its inherent purpose: emitting events for parent components.

Otherwise, I agree that the stream logic should be computed through pipeable operators.

I think you can achieve the same by using a BehaviorSubject and exposing it as an observable:

private counterStore = new BehaviorSubject<number>(0);

public counter$ = this.counterStore.asObservable()
  .pipe(
    scan((counter: number, change: number) => counter + change, 0),
   )

increment () {
 this.counterStore.next(1);
}

decrement () {
 this.counterStore.next(-1);
}