fsprojects / FSharp.Control.Reactive

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

Observable.create doesn't seem to work as expected #61

Closed andybrackley closed 9 years ago

andybrackley commented 9 years ago

I have the following sample code:

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

let obs = Observable.Create(fun (obs : IObserver) -> obs.OnNext("www"); Action( fun _ -> printfn "www -> Disposing" ) ) |> Observable.subscribe (fun x -> printfn "%A" x)

let obs1 = Observable.create (fun obs -> obs.OnNext("xxx"); fun _ -> () ) |> Observable.subscribe (fun x -> printfn "%A" x)

let obs2 = Observable.createWithDisposable(fun obs -> obs.OnNext("yyy") ; Disposable.Create( fun _ -> printfn "yyy -> Disposing" )) |> Observable.subscribe (fun x -> printfn "%A" x)

[] let main argv = use o = obs use o1 = obs1 use o2 = obs2 0 // return an integer exit code

--- This produces the output in my F# Console Application --- "www" "yyy" yyy -> Disposing www -> Disposing Press any key to continue . . .

Notice that the "xxx" is missing from the output. This is the version using Observable.create from the FSharp.Reactive.Control library and it appears that the OnNext is never called. I've tried a few variations including:

let obs1 = Observable.create (fun obs () -> obs.OnNext("xxx"); ) |> Observable.subscribe (fun x -> printfn "%A" x)

and

let obs1 = Observable.create (fun obs -> fun _ -> obs.OnNext("xxx"); ) |> Observable.subscribe (fun x -> printfn "%A" x)

but can't get this to work.

I've raised a StackOverflow question: http://stackoverflow.com/questions/29436180/how-do-you-use-observable-create-in-the-fsharp-control-reactive-library

Am I doing something wrong or is the implementation broken?

Thanks

andybrackley commented 9 years ago

I have this unit test to demonstrate the issue at least as I understand it should work:

[] let Observable.create will propogate the OnNext() call to the subscriber() = let result = ResizeArray() let observable = Observable.create( fun obs -> obs.OnNext("xxx"); fun _ -> result.Add("Disposed"))

observable |> Observable.take 1 |> Observable.subscribe ( result.Add ) |> ignore

Assert.That(result, Has.Count.EqualTo 2) Assert.That(result.[0], Is.EqualTo "xxx") Assert.That(result.[1], Is.EqualTo "Disposed")

panesofglass commented 9 years ago

See the new test here. I removed those functions from the latest release, as you should use the Observable.Create overloads instead. I thought I had removed those in the past as part of a cleanup task.

Please close if that resolves the problem; otherwise, please let me know if you have another test case with which to test.

andybrackley commented 9 years ago

That's great. Thanks.