1919using ReactiveUI ;
2020using System . Collections . Generic ;
2121using System . Threading . Tasks ;
22+ using System . Diagnostics ;
2223
2324namespace GitHub . VisualStudio . UI . Views
2425{
@@ -40,6 +41,7 @@ public class GitHubPaneViewModel : TeamExplorerItemBase, IGitHubPaneViewModel
4041 bool navigatingViaArrows ;
4142 bool disabled ;
4243 Microsoft . VisualStudio . Shell . OleMenuCommand back , forward , refresh ;
44+ int latestReloadCallId ;
4345
4446 [ ImportingConstructor ]
4547 public GitHubPaneViewModel ( ISimpleApiClientFactory apiFactory , ITeamExplorerServiceHolder holder ,
@@ -110,22 +112,52 @@ protected override void RepoChanged(bool changed)
110112 Reload ( ) . Forget ( ) ;
111113 }
112114
115+ /// <summary>
116+ /// This method is reentrant, so all await calls need to be done before
117+ /// any actions are performed on the data. More recent calls to this method
118+ /// will cause previous calls pending on await calls to exit early.
119+ /// </summary>
120+ /// <returns></returns>
113121 async Task Reload ( [ AllowNull ] ViewWithData data = null , bool navigating = false )
114122 {
123+ if ( ! initialized )
124+ return ;
125+
126+ latestReloadCallId ++ ;
127+ var reloadCallId = latestReloadCallId ;
128+
115129 navigatingViaArrows = navigating ;
116130
117131 if ( ! IsGitHubRepo . HasValue )
118- IsGitHubRepo = await IsAGitHubRepo ( ) ;
132+ {
133+ var isGitHubRepo = await IsAGitHubRepo ( ) ;
134+ if ( reloadCallId != latestReloadCallId )
135+ return ;
136+
137+ IsGitHubRepo = isGitHubRepo ;
138+ }
139+
140+ var connection = await connectionManager . LookupConnection ( ActiveRepo ) ;
141+ if ( reloadCallId != latestReloadCallId )
142+ return ;
143+
144+ if ( connection == null )
145+ IsLoggedIn = false ;
146+ else
147+ {
148+ var isLoggedIn = await connection . IsLoggedIn ( hosts ) ;
149+ if ( reloadCallId != latestReloadCallId )
150+ return ;
151+
152+ IsLoggedIn = isLoggedIn ;
153+ }
119154
120155 if ( ! IsGitHubRepo . Value )
121156 {
122157 //LoadView(UIViewType.NotAGitHubRepo);
123158 return ;
124159 }
125160
126- var connection = await connectionManager . LookupConnection ( ActiveRepo ) ;
127- IsLoggedIn = await connection . IsLoggedIn ( hosts ) ;
128-
129161 if ( ! IsLoggedIn )
130162 {
131163 LoadView ( UIViewType . LoggedOut ) ;
0 commit comments