What is the RxJS equivalent of the Bacon.js bus?

How can I create an Observable with which I can just pass events directly, for example using Bacon.js Bus?

+3


source to share


1 answer


The RxJS equivalent is called Subject. This is both the Observer and the Observed. Therefore, in order to push events to it, you use the Observer interface: the onNext, onError, and onCompleted methods. Then you can subscribe to it, match the map, zip code, filter it like any Observable. Here's an example from the official docs:

var subject = new Rx.Subject();

var subscription = subject.subscribe(
    function (x) { console.log('onNext: ' + x); },
    function (e) { console.log('onError: ' + e.message); },
    function () { console.log('onCompleted'); });

subject.onNext(1);
// => onNext: 1

subject.onNext(2);
// => onNext: 2

subject.onCompleted();
// => onCompleted

subscription.dispose();

      



You can check out the theme getting started guide here and the Subject API docs.

+11


source







All Articles