Search code examples
f#system.reactiveusingcomputation-expression

F# workflow builder for Rx


There's a nice F# workflow builder for Rx here:

http://blogs.msdn.com/b/dsyme/archive/2011/05/30/nice-f-syntax-for-rx-reactive-extensions.aspx

I've been trying to make a Using implementation for the workflow but keep banging my head against the wall. Maybe it's too late here.

How would one create this?

type RxBuilder () =

    // ...

    member this.Using (disposable, f) =
        Observable.Using(???)

Thanks in advance.


Solution

  • Firstly, the original code needs to be updated slightly for the latest Rx releases. Namely, For and While should be implemented as:

    member this.For (xs : 'a seq, f: 'a -> 'b IObservable) =
        Observable.SelectMany(xs.ToObservable(), new Func<_, IObservable<_>>(f)) 
    
    member this.While (f, xs: 'a IObservable) =
        Observable.TakeWhile (xs, new Func<_,bool>(fun _ -> f()))
    

    Then, based on this, you can use the following for an implementation of Using:

    member this.Using(res:#IDisposable, body) = this.TryFinally(body res, fun () -> match res with null -> () | disp -> disp.Dispose())