Clint
Clint

Reputation: 6220

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.

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>
  2. DoTheActualLogIn returns an IObservable<bool>

The problem is that after performing the login, I need to call the close method against the progress controller.

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.

Upvotes: 1

Views: 77

Answers (2)

paulpdaniels
paulpdaniels

Reputation: 18663

Chris' answer is a good one, especially if you have to use the progressController further down the chain. 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 =>
        {

        });
}

Upvotes: 1

Chris
Chris

Reputation: 491

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 => {...})

Upvotes: 4

Related Questions