F# Reactive wait for Observable to complete

 ̄綄美尐妖づ 提交于 2020-03-01 05:41:26

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/43584824/f-reactive-wait-for-observable-to-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!