简体   繁体   English

通过可观察序列的线程参数

[英]Threading argument through observable sequence

I'm working with reactive ui and I've run into a problem, essentially in my login method I want to show a progress dialog, attempt the login and then close the dialog and return the final result. 我正在使用反应式ui,遇到一个问题,本质上是在登录方法中,我想显示一个进度对话框,尝试登录,然后关闭对话框并返回最终结果。

The basic pseudocode of the problem is this: 问题的基本伪代码是这样的:

private IObservable<bool> AttemptLogin(CredentialPair pair)
{
    return Dialogs.ShowProgress("logging in", "doing stuff...")
        .SelectMany(progressController => DoTheActualLogin(pair))
        .Subscribe(boolForWhetherLoginSucceeded =>
        {

        });
}
  1. ShowProgress returns an IObservable<ProgressController> ShowProgress返回IObservable<ProgressController>
  2. DoTheActualLogIn returns an IObservable<bool> DoTheActualLogIn返回IObservable<bool>

The problem is that after performing the login, I need to call the close method against the progress controller. 问题是执行登录后,我需要针对进度控制器调用close方法。

I can't seem to work out a way of getting the controller further down the sequence. 我似乎无法找到一种使控制器更进一步的方法。

I'm sure there's a combinator for the sequences that I'm missing / a technique for doing something like this. 我确定有一个我缺少的序列的组合器/一种执行类似操作的技术。

I think this is what you're after: 我认为这是您追求的目标:

(from progressController in Dialogs.ShowProgress("logging in", "doing stuff...")
 from result in DoTheActualLogin(pair)
 select new { result, progressController  })
 .Subscribe(anon => {...}, ex => {...})

Chris' answer is a good one, especially if you have to use the progressController further down the chain. 克里斯的答案是一个很好的答案,尤其是如果您必须在链的更下游使用ProgressController时。 If you just need to clean it up after login you can do: 如果您只需要在登录后清理它,则可以执行以下操作:

private IObservable<bool> AttemptLogin(CredentialPair pair)
{
    return Dialogs.ShowProgress("logging in", "doing stuff...")
        .SelectMany(progressController => 
                    DoTheActualLogin(pair).Finally(() => progressController.Close()))
        .Subscribe(boolForWhetherLoginSucceeded =>
        {

        });
}

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

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