简体   繁体   English

BaconJS的Pub-Sub

[英]Pub-Sub with BaconJS

Can I use BaconJS to do pubsub? 我可以使用BaconJS进行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: Bacon.js具有两种类型的Observable

  • EventStream
  • Property

Subscribers to EventStream s receive only the events that occur during the subscription. EventStream的订阅服务器仅接收订阅期间发生的事件。 Subscribers to Property s receive, upon subscription, the last event the occurred. Property的订阅者在订阅时会收到发生的最后一个事件。

There are various ways of creating Observables in Bacon.js, some of which create an EventStream and some that create a Property . 在Bacon.js中有多种创建Observables的方法,其中一些创建EventStreamEventStream一些创建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. 作为一个脚注,如果您在RxJS中尝试相同的操作,则会注意到您没有相同的问题。 But don't be fooled. 但不要上当。 Your RxJS observable isn't hanging onto the most recent event and giving it to the next subscriber(s). 您可观察到的RxJS不会挂在最近的事件上,而是将其提供给下一个订阅者。 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. 仅在调用subscribe时才发生subscribe 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. 当您使用pipe从另一个pipe创建一个可观察到的对象时,也会发生这种情况,因此,如果您想将FRP代码分解为一小段以提高可读性,您将遇到RxJS的冷可观察对象的问题。 (RxJS has things like shareReplay to make observables hot, but it's extra work to do what you probably want 99% of the time.) (RxJS具有诸如shareReplay类的shareReplay以使可观察的shareReplay变得炙手可热,但要在99%的时间内完成您可能想要的操作,这是额外的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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