繁体   English   中英

在序列停止响应式 UI 中生成的元素后的一段时间后,如何获取最后一个 Observable 序列值?

[英]How to take last Observable sequence value after some time after sequence stopped produced elements in Reactive UI?

当页面上的 2 个 ReactiveCommands 中的任何一个被执行时,我需要在我的应用程序中禁用 WPF UI。 命令被一一调用。 这意味着它们的调用方式如下:

Command1
   .Select(x => ...Prepare params for second...)
   .InvokeCommand(Command2)
   .DisposeWith(d);

我做了一个可观察的,如:

IObservable<bool> CanInteractWithUserObservable => Command1.IsExecuting
                                        .CombineLatest(Command2.IsExecuting, (c1,c2)=>(c1,c2))
                                        .Select(tuple => tuple.c1 || tuple.c2)
                                        .Select(res => !res)
                                        .ObserveOn(RxApp.MainThread);

并将其绑定到 Window.IsEnabled 属性。

一般来说,它是有效的,但是当 1 个命令完成其工作时,observable 返回值true的问题,但第二个还没有开始。 所以,总的输出是:

真的; // 开始前

错误的; // 执行第一个命令

真的; //第一个命令完成,第二个还没有开始(这是问题)

错误的; // 执行第二条命令

真的; // 两个命令都完成了

我需要听序列,并且只有在距离最后一个真实事件半秒左右后,我才应该在我的 CanInteractWithUserObservable 中发布更新以避免 UI 闪烁。

PS:可能超时方法可能对我有帮助,但我不知道如何使用它。

如果我理解正确,您希望等待两个事件完成,然后让您的 UI 发布。

Command1.IsExecuting
.CombineLatest(Command2.IsExecuting, (c1,c2)=>(c1,c2))
.Select(tuple => tuple.c1 && tuple.c2) // instead of or use and so only when both will be done value will be true
.SkipWhile(t=> t == false) // omit when both not completed 
.Delay(TimeSpan.FromSeconds(.5)) // i don't know you still want to wait 0.5 seconds more but Delay is the operator for that
.Select(res => !res)
.ObserveOn(RxApp.MainThread);

Timeout用于在 observable 未发出给定值数量的值时发出错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM