rxjs5 - RxJS 5, converting an observable to a BehaviorSubject(?) -


i have parent observable that, once has subscriber, lookup , emit single value, complete.

i'd convert observable (or behavior subject or whatever works) following: once has @ least 1 subscriber, gets result parent observable (once). emits value of subscribers, , emits single value future subscribers, when subscribe. should continue behavior if subscriber count drops zero.

it seems should easy. here didn't work:

thevalue$: observable<boolean> = parent$ .take(1) .share() 

other things didn't work: publishreplay(), publish(). worked better:

thevalue$ = new behaviorsubject<boolean>(false);  parent$ .take(1) .subscribe( value => thevalue$.next(value)); 

there problem approach, though: parent$ subscribed before thevalue$ gets first subscriber.

is there better way handle this?

sharereplay should want:

import 'rxjs/add/operator/sharereplay'; ... thevalue$: observable<boolean> = parent$.sharereplay(1); 

sharereplay added in rxjs version 5.4.0. returns reference counted observable subscribe source - parent$ - upon first subscription being made. , subscriptions made after source completes receive replayed notifications.

sharereplay - , refcount in general - explained in more detail in article wrote recently: rxjs: how use refcount.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -