What angular2 RxJS subscription pattern should I use?

141 Views Asked by At

I am using RxJS with Angular2.

I often find myself emitting to a private subject in an Angular2 service which emits to a public stream / observable.

The typical pattern I am using in my component is ..

ngOnInit() {
  service.myStream$.subscribe( ... )
}

clickSomethingHandler() {
  service.emit(this.someValue);
}

And in my service ...

private mySubject = new Subject<any>();

public myStream$ = mySubject.map( ... ) // etc

public emit(value) {
    mySubject.next(value);
}

Is this the best approach? Any other approaches or improvements on this?

Thanks

0

There are 0 best solutions below