fsprojects / FSharp.Control.Reactive

Extensions and wrappers for using Reactive Extensions (Rx) with F#.
http://fsprojects.github.io/FSharp.Control.Reactive
Other
284 stars 60 forks source link

`Observable.Create` doesn't work as expected #126

Closed fuchen closed 4 years ago

fuchen commented 4 years ago

Description

open System
open System.Reactive.Linq
open System.Reactive.Disposables
open FSharp.Control.Reactive
open FSharp.Control.Reactive.Observable

[<EntryPoint>]
let main argv =
    fun (ob: IObserver<int>) ->
        ob.OnNext(1)
        ob.OnNext(2)
        ob.OnNext(3)
        ob.OnCompleted()
        fun ()-> () // Change to: `Disposable.Empty`, then everything works well
    |> Observable.Create
    |> Observable.subscribe (printfn "=%A")
    |> ignore
    0

Expected behavior

Should print: =1 =2 =3

Actual behavior

No output

Known workarounds

Return Disposable.Empty rather than fun ()-> (), then everything goes well.

panesofglass commented 4 years ago

Two options appear to present themselves:

  1. Remove the overloads, as they are not typically needed in current versions of F#
  2. Coerce the overload taking IObserver<'T> -> unit -> unit into IObserver<'T> -> Disposable.Empty

If the latter works, that might be the best. You can still try to opt into the other by explicitly wrapping fun () -> () in Action(fun () -> ()), but as you say, that appears to be causing problems.