简体   繁体   中英

Pub-Sub with BaconJS

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.

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM