Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Tidy up Rx in PullRequestStatusBarManager #1999

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 13 additions & 25 deletions src/GitHub.InlineReviews/Services/PullRequestStatusBarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public class PullRequestStatusBarManager
readonly Lazy<ITeamExplorerContext> teamExplorerContext;
readonly Lazy<IConnectionManager> connectionManager;

IDisposable currentSessionSubscription;

[ImportingConstructor]
public PullRequestStatusBarManager(
Lazy<IUsageTracker> usageTracker,
Expand Down Expand Up @@ -68,41 +66,31 @@ public void StartShowingStatus()
{
try
{
teamExplorerContext.Value.WhenAnyValue(x => x.ActiveRepository)
var activeReposities = teamExplorerContext.Value.WhenAnyValue(x => x.ActiveRepository);
var sessions = pullRequestSessionManager.Value.WhenAnyValue(x => x.CurrentSession);
activeReposities
.CombineLatest(sessions, (r, s) => (r, s))
.Throttle(TimeSpan.FromSeconds(1))
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(x => RefreshActiveRepository(x));
.Subscribe(x => RefreshCurrentSession(x.r, x.s).Forget(log));
}
catch (Exception e)
{
log.Error(e, "Error initializing");
}
}

void RefreshActiveRepository(ILocalRepositoryModel repository)
{
currentSessionSubscription?.Dispose();
currentSessionSubscription = pullRequestSessionManager.Value.WhenAnyValue(x => x.CurrentSession)
.Subscribe(x => RefreshCurrentSession(repository, x).Forget());
}

async Task RefreshCurrentSession(ILocalRepositoryModel repository, IPullRequestSession session)
{
try
{
var showStatus = await IsDotComOrEnterpriseRepository(repository);
if (!showStatus)
{
ShowStatus(null);
return;
}

var viewModel = CreatePullRequestStatusViewModel(session);
ShowStatus(viewModel);
}
catch (Exception e)
var showStatus = await IsDotComOrEnterpriseRepository(repository);
if (!showStatus)
{
log.Error(e, nameof(RefreshCurrentSession));
ShowStatus(null);
return;
}

var viewModel = CreatePullRequestStatusViewModel(session);
ShowStatus(viewModel);
}

async Task<bool> IsDotComOrEnterpriseRepository(ILocalRepositoryModel repository)
Expand Down