20

Is it equivalent/better to work

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

Is that the correct thinking ?

1 Answer 1

29

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.

2
  • The difference is far from being obvious at first, and the word (E)event quite overloaded.
    – nicolas
    Commented Mar 21, 2013 at 14:27
  • 9
    @Nicolas That's true. It follows from historical reasons (F# had Event module before IObservable existed - and maybe the right thing was to remove it). But to be honest, things would be easier if .NET did not have special "events" in the first place, but that has been decided too long ago :-). Commented Mar 21, 2013 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.