大多数(如果不是全部)在线示例都注册了一个处理程序,但随后返回一个离散的Observable值(即Observable.Return(RecoveryOptionResult.CancelOperation)
)。为了正确实现,最好将RecoveryOptions
列表作为按钮列表(或类似的东西)呈现给用户,并将流控制传递给用户。
我正在努力的是如何 等待 用户点击按钮(或更具体地说,如何等待其中一个RecoveryOption
命令设置RecoveryResult
。
我设法将做的东西放在一起,但我无法想象这种方式是正确的。我缺乏使用activeui的经验使我无法概念化监控ReactiveList<IRecoveryCommand>
的正确方法。
以下是我的黑客代码。
// UserError.RegisterHandler(x => HandleErrorAsync(x));
private async Task<RecoveryOptionResult> HandleErrorAsync(UserError error)
{
// present error UI...
// use ReactiveCommand's IsExecuting observable to monitor changes (since RecoverResult is not an observable)
// is there a better way to do this??? this seems sub-optimal
await error.RecoveryOptions
.Select(x => x.IsExecuting)
.Merge()
.Where(_ => error.RecoveryOptions.Any(x => x.RecoveryResult.HasValue))
.FirstAsync();
// recovery option was clicked in the UI
// get the recovery option that was chosen
return error.RecoveryOptions
.Where(x => x.RecoveryResult.HasValue)
.Select(x => x.RecoveryResult.Value)
.First();
}
主要问题是RecoveryResult
不是可观察的。因此,我必须监控IsExecuting
是可观察的,然后检查RecoveryResult
值。但是,似乎必须有更好的方法来做到这一点。
答案 0 :(得分:1)
今天再次看到这一点,我注意到我无法观察RecoveryResult
的原因是因为RecoveryOptions
是ReactiveList<IRecoveryCommand>
而IRecoveryCommand
无法观察到。解决此问题的简单方法是假设所有恢复选项实际上都是RecoveryCommand
个对象(可观察),但更合适的答案是根据IRecoveryCommand
契约生成可观察流。 / p>
我们可以通过执行以下操作来调整RxUI docs on Recovery Options中描述的代码以支持RxUI 6.5:
public static IObservable<RecoveryOptionResult> GetResultAsync(this UserError This, RecoveryOptionResult defaultResult = RecoveryOptionResult.CancelOperation)
{
return This.RecoveryOptions.Any() ?
This.RecoveryOptions
.Select(x => x.IsExecuting
.Skip(1) // we can skip the first event because it's just the initial state
.Where(_ => x.RecoveryResult.HasValue) // only stream results that have a value
.Select(_ => x.RecoveryResult.Value)) // project out the result value
.Merge() // merge the list of command events into a single event stream
.FirstAsync() : //only consume the first event
Observable.Return(defaultResult);
}
如果您想支持任何类型的IRecoveryCommand
,则需要此扩展方法,因为它将其可观察流基于它所知道的仅有的两个可观察对象之一。但是,如果您可以确定您只处理RecoveryCommand对象,则可以执行以下操作:
public static IObservable<RecoveryOptionResult> GetResultAsync(this UserError This, RecoveryOptionResult defaultResult = RecoveryOptionResult.CancelOperation)
{
return This.RecoveryOptions.Any() ?
This.RecoveryOptions
.Cast<RecoveryCommand>()
.Select(x => x // our command is now observable
// we don't Skip(1) here because we're not observing a property any more
.Where(_ => x.RecoveryResult.HasValue)
.Select(_ => x.RecoveryResult.Value))
.Merge()
.FirstAsync() :
Observable.Return(defaultResult);
}
我会在接下来的时候留下这个答案,希望@ paul-betts可以确认或否认这是合适的策略。