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

Commit ffd5072

Browse files
committed
Ensure GitHubPane initializes view model is initialized.
When the GitHub pane is shown from VS rather than our own code, kick off an initialization of the view model. This means we can't be sure of the state of the view model's initialization so make subsequent calls to the view model async either wait for an ongoing initialization, or exit if already initialized.
1 parent 87058c7 commit ffd5072

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

src/GitHub.App/ViewModels/GitHubPane/GitHubPaneViewModel.cs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public sealed class GitHubPaneViewModel : ViewModelBase, IGitHubPaneViewModel, I
4646
readonly ReactiveCommand<Unit> refresh;
4747
readonly ReactiveCommand<Unit> showPullRequests;
4848
readonly ReactiveCommand<object> openInBrowser;
49+
readonly SemaphoreSlim initializing = new SemaphoreSlim(1);
50+
bool initialized;
4951
IViewModel content;
5052
ILocalRepositoryModel localRepository;
5153
string searchQuery;
@@ -198,26 +200,37 @@ public void Dispose()
198200
/// <inheritdoc/>
199201
public async Task InitializeAsync(IServiceProvider paneServiceProvider)
200202
{
201-
await UpdateContent(teamExplorerContext.ActiveRepository);
202-
teamExplorerContext.WhenAnyValue(x => x.ActiveRepository)
203-
.Skip(1)
204-
.ObserveOn(RxApp.MainThreadScheduler)
205-
.Subscribe(x => UpdateContent(x).Forget());
206-
207-
connectionManager.Connections.CollectionChanged += (_, __) => UpdateContent(LocalRepository).Forget();
208-
209-
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.pullRequestCommand, showPullRequests);
210-
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.backCommand, navigator.NavigateBack);
211-
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.forwardCommand, navigator.NavigateForward);
212-
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.refreshCommand, refresh);
213-
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.githubCommand, openInBrowser);
214-
215-
paneServiceProvider.AddCommandHandler(Guids.guidGitHubToolbarCmdSet, PkgCmdIDList.helpCommand,
216-
(_, __) =>
217-
{
218-
browser.OpenUrl(new Uri(GitHubUrls.Documentation));
219-
usageTracker.IncrementCounter(x => x.NumberOfGitHubPaneHelpClicks).Forget();
220-
});
203+
await initializing.WaitAsync();
204+
if (initialized) return;
205+
206+
try
207+
{
208+
await UpdateContent(teamExplorerContext.ActiveRepository);
209+
teamExplorerContext.WhenAnyValue(x => x.ActiveRepository)
210+
.Skip(1)
211+
.ObserveOn(RxApp.MainThreadScheduler)
212+
.Subscribe(x => UpdateContent(x).Forget());
213+
214+
connectionManager.Connections.CollectionChanged += (_, __) => UpdateContent(LocalRepository).Forget();
215+
216+
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.pullRequestCommand, showPullRequests);
217+
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.backCommand, navigator.NavigateBack);
218+
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.forwardCommand, navigator.NavigateForward);
219+
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.refreshCommand, refresh);
220+
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.githubCommand, openInBrowser);
221+
222+
paneServiceProvider.AddCommandHandler(Guids.guidGitHubToolbarCmdSet, PkgCmdIDList.helpCommand,
223+
(_, __) =>
224+
{
225+
browser.OpenUrl(new Uri(GitHubUrls.Documentation));
226+
usageTracker.IncrementCounter(x => x.NumberOfGitHubPaneHelpClicks).Forget();
227+
});
228+
}
229+
finally
230+
{
231+
initialized = true;
232+
initializing.Release();
233+
}
221234
}
222235

223236
/// <inheritdoc/>

src/GitHub.VisualStudio/UI/GitHubPane.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public void Initialize(IServiceProvider serviceProvider)
8989

9090
var factory = provider.GetService<IViewViewModelFactory>();
9191
viewModel = provider.ExportProvider.GetExportedValue<IGitHubPaneViewModel>();
92+
viewModel.InitializeAsync(this).Forget();
9293

9394
View = factory.CreateView<IGitHubPaneViewModel>();
9495
View.DataContext = viewModel;

0 commit comments

Comments
 (0)