事件和可观察的FSharp [英] Event and Observable in FSharp

查看:131
本文介绍了事件和可观察的FSharp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

等于/更好地工作

  • with the Event module on Event type
  • or with Observable on the publish property of an event

功能上似乎是等同的,我想区别是'语义':

Functionally it seems equivalent, and I guess the difference is 'semantic' :


  • 我们在边界内部有权访问
    的内部状态吗?

  • 或者我们将事件界面视为被动来源,一个流被暴露给我们

  • Are we inside the boundary where it makes sense to have access to the internal state of the event ?
  • Or are we considering this event interface as a passive source from which a stream was exposed to us

这是正确的想法吗?

推荐答案

事件 Observable 之间的主要区别在于它们处理状态和非订阅。

The main difference between Event and Observable is in how they handle state and un-subscription.


  • 事件附加到源事件,不要给你任何方式取消订阅。如果您使用状态组合器(如 Event.scan ),然后将多个观察者附加到生成的事件,那么它们都将看到相同的状态。

  • Event functions attach to the source event and do not give you any way to unsubscribe. If you use stateful combinators (like Event.scan) and then attach multiple observers to the resulting event, then they will all see the same state.

可观察函数构造处理流水线的规范。当您将一个处理程序附加到 IObservable 值时,您将返回一个可用于删除所有处理程序的 IDisposable 。附加到 IObservable 的每个处理程序将获得一个新的状态(因为运行时根据规范创建一个新的处理链)。

Observable functions construct "specification" of the processing pipeline. When you attach a handler to IObservable value, you get back an IDisposable that can be used to remove all handlers. Each handler attached to IObservable will get a new state (because the runtime creates a new processing chain according to the "specification").

实际上,主要区别在于statofullness - 如果要共享状态,可以使用事件模块 - 使用可观察实现相同的方式是可能的,但更难。

In practice, the main difference is in the statfullness - if you want to share state, you can use the Event module - implementing the same using Observable is possible but harder.

如果您在 async ,那么你应该使用 Observable AwaitObservable 内置 AwaitEvent ),因为使用事件组合器会泄漏内存 - 它会附加永远不会被删除的事件处理程序。

If you're using events inside async, then you should use Observable and AwaitObservable (instead of built-in AwaitEvent), because using event combinators will leak memory - it will attach event handlers that are never removed.

这篇关于事件和可观察的FSharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆