nicolas
nicolas

Reputation: 9805

Event and Observable in FSharp

Is it equivalent/better to work

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

Is that the correct thinking ?

Upvotes: 20

Views: 3756

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243051

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

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

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

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.

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.

Upvotes: 29

Related Questions