2

I'm trying to figure out how to use the Observable.create function in the FSharp.Control.Reactive library but seem to be missing something.

The function signature is defined as:

((IObserver<'a> -> unit -> unit) -> IObservable<'a>)

I've tried a few different ways of creating the observable such as:

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

and

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

Which compiles but doesn't ever execute the OnNext so the printfn statement in the subscribe isn't called.

I can call the Observable.Create method directly from the System.Reactive.Linq namespace as follows:

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

And this will print "www" to the interactive. Likewise, I can use the Observable.createWithDisposable as follows:

Observable.createWithDisposable(fun obs -> obs.OnNext("yyy") ; Disposable.Empty )
|> Observable.subscribe (fun x -> printfn "%A" x)

And sending this to interactive prints "yyy"

So it is just the FSharp.Control.Reactive.Observable.create that I am having trouble with.

I've had a look but can't find any examples on how this should be called so any help would be appreciated.

Thanks

Andy B
  • 541
  • 2
  • 13
  • That signature looks quite bizarre to me, but try `fun obs () -> obs.OnNext("xxx")`. – ildjarn Apr 03 '15 at 16:44
  • Thanks. I tried this and it compiled but I didn't get the OnNext called. I've been looking at the implementation of Observable.create and it's defined like this: /// Creates an observable sequence from a specified Subscribe method implementation. let create subscribe = let subscribe observer = Action (subscribe observer) Observable.Create(Func,Action> subscribe) Which I don't think is correct. I might raise an issue on GitHub with the author. – Andy B Apr 03 '15 at 18:20
  • Agreed, that looks highly suspect to me. – ildjarn Apr 03 '15 at 18:30

2 Answers2

1

I raised an issue with the developer. They've removed the Observable.create functions now so you create the observable using the System.Reactive.Linq.Observable.Create() methods now.

That makes this question invalid now.

https://github.com/fsprojects/FSharp.Control.Reactive/commit/c8f6d245e75d55b2bd6077c53b311119422c97a1

Andy B
  • 541
  • 2
  • 13
0

I once asked that using c#.

Using F# Control.Reactive would be:

        Observable.create(
            fun observer ->                        
                Action (fun () -> 
                    foo.OnBar(
                       fun (args : Args) -> 
                         observer.OnNext(args)            
                         observer.OnCompleted())))