2

Is it possible to plug in an Observable into a rxjs Subject?

In Bacon i can plug in a new stream easily with the plug method, but in rxjs I haven't find a one-liner yet. so now I do it like this:

var subject = new Rx.Subject();
var clickStream = new Rx.Observable.fromEvent(button, 'click');
clickStream.subscribe(function(e){
  subject.onNext(e);
});

I can't use merge, because I render this button later. I would expect a method exists like this:

subject.plug(clickStream);

Thanks B.

balazs
  • 5,698
  • 7
  • 37
  • 45

1 Answers1

3

What you want to do is:

var subject = new Rx.Subject();
var clickStream = Rx.Observable.fromEvent(button, 'click');

clickStream.subscribe(subject);

You don't need to pass a function that call subject.onNext because a Rx.Subject is, at same time, an observer and an observable. So, you can subscribe to/from an subject.

MichelMattos
  • 176
  • 1
  • 5
  • oh, thanks didn't know that [subscribe](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/subscribe.md) can work like this, but now reading the docs it's clear – balazs Feb 17 '15 at 08:35