Reputation: 6592
Can I use BaconJS to do pubsub? When I've tried creating an event stream and subscribe to it, the first observer consumes the stream and future observers can't replay the stream's historical values.
Upvotes: 0
Views: 68
Reputation: 8809
Bacon.js has two types of Observable
s:
EventStream
Property
Subscribers to EventStream
s receive only the events that occur during the subscription. Subscribers to Property
s receive, upon subscription, the last event the occurred.
There are various ways of creating Observables
in Bacon.js, some of which create an EventStream
and some that create a Property
. You can easily convert between the two when needed.
It's all documented here.
As a footnote, if you try the same thing in RxJS, you will notice that you don't have the same problem. But don't be fooled. Your RxJS observable isn't hanging onto the most recent event and giving it to the next subscriber(s). Instead, it's recreating its underlying resources for every subscriber. This can be dangerous. Suppose you have an observable that hits an endpoint. Every subscriber to that observable will cause it to hit that endpoint. Subscription doesn't happen only when subscribe
is called. It also happens when you create one observable from another using pipe
, so if you like to break down your FRP code into bite-sized pieces for readability, you're going to run into this issue with RxJS's cold observables. (RxJS has things like shareReplay
to make observables hot, but it's extra work to do what you probably want 99% of the time.)
Upvotes: 0