@@ -24,8 +24,8 @@ public class VSGitExt : IVSGitExt
2424 static readonly ILogger log = LogManager . ForContext < VSGitExt > ( ) ;
2525
2626 readonly Func < Type , Task < object > > getServiceAsync ;
27- readonly IVSUIContext context ;
2827 readonly ILocalRepositoryModelFactory repositoryFactory ;
28+ readonly object refreshLock = new object ( ) ;
2929
3030 IGitExt gitService ;
3131 IReadOnlyList < ILocalRepositoryModel > activeRepositories ;
@@ -41,94 +41,50 @@ public VSGitExt(Func<Type, Task<object>> getServiceAsync, IVSUIContextFactory fa
4141 this . getServiceAsync = getServiceAsync ;
4242 this . repositoryFactory = repositoryFactory ;
4343
44- // The IGitExt service isn't available when a TFS based solution is opened directly.
45- // It will become available when moving to a Git based solution (and cause a UIContext event to fire).
46- context = factory . GetUIContext ( new Guid ( Guids . GitSccProviderId ) ) ;
47-
4844 // Start with empty array until we have a chance to initialize.
4945 ActiveRepositories = Array . Empty < ILocalRepositoryModel > ( ) ;
5046
51- PendingTasks = InitializeAsync ( ) ;
52- }
53-
54- async Task InitializeAsync ( )
55- {
56- try
57- {
58- if ( ! context . IsActive || ! await TryInitialize ( ) )
59- {
60- // If we're not in the UIContext or TryInitialize fails, have another go when the UIContext changes.
61- context . UIContextChanged += ContextChanged ;
62- log . Debug ( "VSGitExt will be initialized later" ) ;
63- }
64- }
65- catch ( Exception e )
66- {
67- log . Error ( e , "Initializing" ) ;
68- }
69- }
70-
71- void ContextChanged ( object sender , VSUIContextChangedEventArgs e )
72- {
73- if ( e . Activated )
74- {
75- PendingTasks = ContextChangedAsync ( ) ;
76- }
47+ // The IGitExt service isn't available when a TFS based solution is opened directly.
48+ // It will become available when moving to a Git based solution (and cause a UIContext event to fire).
49+ var context = factory . GetUIContext ( new Guid ( Guids . GitSccProviderId ) ) ;
50+ context . WhenActivated ( ( ) => Initialize ( ) ) ;
7751 }
7852
79- async Task ContextChangedAsync ( )
53+ void Initialize ( )
8054 {
81- try
55+ PendingTasks = getServiceAsync ( typeof ( IGitExt ) ) . ContinueWith ( t =>
8256 {
83- // If we're in the UIContext and TryInitialize succeeds, we can stop listening for events.
84- // NOTE: this event can fire with UIContext=true in a TFS solution (not just Git).
85- if ( await TryInitialize ( ) )
57+ gitService = ( IGitExt ) t . Result ;
58+ if ( gitService == null )
8659 {
87- context . UIContextChanged -= ContextChanged ;
88- log . Debug ( "Initialized VSGitExt on UIContextChanged" ) ;
60+ log . Error ( "Couldn't find IGitExt service" ) ;
61+ return ;
8962 }
90- }
91- catch ( Exception e )
92- {
93- log . Error ( e , "UIContextChanged" ) ;
94- }
95- }
9663
97- async Task < bool > TryInitialize ( )
98- {
99- gitService = ( IGitExt ) await getServiceAsync ( typeof ( IGitExt ) ) ;
100- if ( gitService != null )
101- {
64+ RefreshActiveRepositories ( ) ;
10265 gitService . PropertyChanged += ( s , e ) =>
10366 {
10467 if ( e . PropertyName == nameof ( gitService . ActiveRepositories ) )
10568 {
106- // Execute tasks in sequence using thread pool (TaskScheduler.Default).
107- PendingTasks = PendingTasks . ContinueWith ( _ => RefreshActiveRepositories ( ) , TaskScheduler . Default ) ;
69+ RefreshActiveRepositories ( ) ;
10870 }
10971 } ;
110-
111- // Do this after we start listening so we don't miss an event.
112- await Task . Run ( ( ) => RefreshActiveRepositories ( ) ) ;
113-
114- log . Debug ( "Found IGitExt service and initialized VSGitExt" ) ;
115- return true ;
116- }
117-
118- log . Error ( "Couldn't find IGitExt service" ) ;
119- return false ;
72+ } , TaskScheduler . Default ) ;
12073 }
12174
12275 void RefreshActiveRepositories ( )
12376 {
12477 try
12578 {
126- log . Debug (
127- "IGitExt.ActiveRepositories (#{Id}) returned {Repositories}" ,
128- gitService . GetHashCode ( ) ,
129- gitService ? . ActiveRepositories . Select ( x => x . RepositoryPath ) ) ;
79+ lock ( refreshLock )
80+ {
81+ log . Debug (
82+ "IGitExt.ActiveRepositories (#{Id}) returned {Repositories}" ,
83+ gitService . GetHashCode ( ) ,
84+ gitService . ActiveRepositories . Select ( x => x . RepositoryPath ) ) ;
13085
131- ActiveRepositories = gitService ? . ActiveRepositories . Select ( x => repositoryFactory . Create ( x . RepositoryPath ) ) . ToList ( ) ;
86+ ActiveRepositories = gitService ? . ActiveRepositories . Select ( x => repositoryFactory . Create ( x . RepositoryPath ) ) . ToList ( ) ;
87+ }
13288 }
13389 catch ( Exception e )
13490 {
@@ -160,6 +116,6 @@ private set
160116 /// <summary>
161117 /// Tasks that are pending execution on the thread pool.
162118 /// </summary>
163- public Task PendingTasks { get ; private set ; }
119+ public Task PendingTasks { get ; private set ; } = Task . CompletedTask ;
164120 }
165121}
0 commit comments