Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Reactive wait for Observable to complete

I found a variety of SO questions on this but couldn't figure out an F# solution. I need to block wait for an event to fire at me to check the data it returns. I am using Rx to receive event 3 times:

let disposable =
    Observable.take 3 ackNack
    |> Observable.subscribe (
        fun (sender, data) ->
            Console.WriteLine("{0}", data.AckNack)
            Assert.True(data.TotalAckCount > 0u)
    )

I would like to either turn results into a list, so they can be checked later on by the test framework (xUnit), or wait for all 3 events to complete and pass the Assert.True.

How would I wait for 3 events to fire before continuing? I can see there's an Observable.wait other sources suggest Async.RunSynchronously.

like image 803
alexm Avatar asked Apr 24 '17 09:04

alexm


1 Answers

I think the easiest option is to use Async.AwaitObservable function - sadly, this is not yet available in the F# core library, but you can get it from the FSharpx.Async package, or just copy the function soruce from GitHub.

Using the function, you should be able to write something like:

let _, data = 
  Observable.take 3 ackNack
  |> Async.AwaitObservable
  |> Async.RunSynchronously

Console.WriteLine("{0}", data.AckNack)
Assert.True(data.TotalAckCount > 0u)
like image 167
Tomas Petricek Avatar answered Oct 20 '22 10:10

Tomas Petricek