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

Commit dc81286

Browse files
committed
Lazily initialize when we're on a GitHub repo
Initialize when an active repository appears in IVSGitExt.
1 parent c6750b9 commit dc81286

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

src/GitHub.InlineReviews/Services/PullRequestStatusBarManager.cs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
using GitHub.VisualStudio;
1212
using GitHub.Models;
1313
using GitHub.Logging;
14+
using GitHub.Helpers;
1415
using GitHub.Extensions;
1516
using Serilog;
17+
using Task = System.Threading.Tasks.Task;
1618

1719
namespace GitHub.InlineReviews.Services
1820
{
@@ -22,29 +24,54 @@ public class PullRequestStatusBarManager : IPullRequestStatusBarManager
2224
static readonly ILogger log = LogManager.ForContext<PullRequestStatusBarManager>();
2325
const string StatusBarPartName = "PART_SccStatusBarHost";
2426

25-
readonly IGitHubServiceProvider serviceProvider;
27+
readonly IVSGitExt gitExt;
2628
readonly Window mainWindow;
27-
readonly IPullRequestSessionManager pullRequestSessionManager;
29+
readonly Lazy<IPullRequestSessionManager> pullRequestSessionManager;
2830
readonly IUsageTracker usageTracker;
31+
readonly IGitHubServiceProvider serviceProvider;
32+
33+
bool initialized;
2934

3035
[ImportingConstructor]
31-
public PullRequestStatusBarManager(IGitHubServiceProvider serviceProvider, IPullRequestSessionManager pullRequestSessionManager, IUsageTracker usageTracker)
32-
: this()
36+
public PullRequestStatusBarManager(IVSGitExt gitExt, Lazy<IPullRequestSessionManager> pullRequestSessionManager,
37+
IUsageTracker usageTracker, IGitHubServiceProvider serviceProvider)
3338
{
34-
this.serviceProvider = serviceProvider;
39+
this.gitExt = gitExt;
3540
this.pullRequestSessionManager = pullRequestSessionManager;
3641
this.usageTracker = usageTracker;
42+
this.serviceProvider = serviceProvider;
43+
mainWindow = Application.Current.MainWindow;
3744
}
3845

39-
public PullRequestStatusBarManager()
46+
public void Initialize()
4047
{
41-
mainWindow = Application.Current.MainWindow;
48+
TryInitialize();
49+
gitExt.ActiveRepositoriesChanged += TryInitialize;
4250
}
4351

44-
public void Initialize()
52+
void TryInitialize()
53+
{
54+
if (!initialized && gitExt.ActiveRepositories.Count > 0)
55+
{
56+
initialized = true;
57+
InitializeAsync().Forget();
58+
gitExt.ActiveRepositoriesChanged -= TryInitialize;
59+
}
60+
}
61+
62+
async Task InitializeAsync()
4563
{
46-
RefreshCurrentSession();
47-
pullRequestSessionManager.PropertyChanged += PullRequestSessionManager_PropertyChanged;
64+
try
65+
{
66+
await ThreadingHelper.SwitchToMainThreadAsync(); // Switch from VSGitExt to Main thread
67+
68+
RefreshCurrentSession();
69+
pullRequestSessionManager.Value.PropertyChanged += PullRequestSessionManager_PropertyChanged;
70+
}
71+
catch (Exception e)
72+
{
73+
log.Error(e, "Error initializing");
74+
}
4875
}
4976

5077
void PullRequestSessionManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
@@ -57,7 +84,7 @@ void PullRequestSessionManager_PropertyChanged(object sender, PropertyChangedEve
5784

5885
void RefreshCurrentSession()
5986
{
60-
var pullRequest = pullRequestSessionManager.CurrentSession?.PullRequest;
87+
var pullRequest = pullRequestSessionManager.Value.CurrentSession?.PullRequest;
6188
var viewModel = pullRequest != null ? CreatePullRequestStatusViewModel(pullRequest) : null;
6289
ShowStatus(viewModel);
6390
}

0 commit comments

Comments
 (0)