mhelvens mhelvens - 7 months ago 46
Javascript Question

RxJS 5: Can I create a `BehaviorObservable`?

I'm creating a class for my app that keeps an internal

. Is there a way I can expose a corresponding '
', which pipes through the values of the subject, but only has the 'read-only' stuff of the
interface? The
methods should only be available internally.

It's not just a matter (I believe) of using
to pipe the subject's values through. Users of my API should
to the exposed observable, and then immediately get a callback for the stored current value.

I may be able to hack something together, but I'm sure I'm just missing something that RxJS can already do.


Are you by any chance looking for the .asObservable() method - which existed in Rxjs v4. Don't know if that is still the case in Rxjs v5 though.

According to this, the method should have been included in release 5.0.0-beta.2. Also I quote here their work-around if the functionality is not there :

You can get the same functionality by creating an observable with the private subject's subscribe function:

> const subj = new rx.Subject();
> const exposed = new rx.Observable(fn => subj.subscribe(fn));

For more details about subjects' semantics, you can have a look here.