1111using GitHub . VisualStudio ;
1212using GitHub . Models ;
1313using GitHub . Logging ;
14+ using GitHub . Helpers ;
1415using GitHub . Extensions ;
1516using Serilog ;
17+ using Task = System . Threading . Tasks . Task ;
1618
1719namespace 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