反应式等待Observable完成

反应式等待Observable完成

本文介绍了F#反应式等待Observable完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此我发现了各种各样的SO问题,但找不到F#解决方案.我需要阻止等待事件触发,以检查它返回的数据.我正在使用Rx接收事件3次:

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)
    )

我想将结果转换为列表,以便稍后可以通过测试框架(xUnit)对其进行检查,或者等待所有3个事件完成并通过Assert.True.

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.

在继续之前,我将如何等待3个事件触发?我可以看到还有Observable.wait其他来源建议Async.RunSynchronously.

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

推荐答案

我认为最简单的选择是使用Async.AwaitObservable函数-不幸的是,这在F#核心库中尚不可用,但是您可以从FSharpx.Async包,或仅复制功能来自GitHub .

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)

这篇关于F#反应式等待Observable完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:24