Thanks to visit codestin.com
Credit goes to github.com

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

Ensure that the GitHubPaneViewModel is initalized. #1426

Merged
merged 6 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 33 additions & 20 deletions src/GitHub.App/ViewModels/GitHubPane/GitHubPaneViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public sealed class GitHubPaneViewModel : ViewModelBase, IGitHubPaneViewModel, I
readonly ReactiveCommand<Unit> refresh;
readonly ReactiveCommand<Unit> showPullRequests;
readonly ReactiveCommand<object> openInBrowser;
readonly SemaphoreSlim initializing = new SemaphoreSlim(1);
bool initialized;
IViewModel content;
ILocalRepositoryModel localRepository;
string searchQuery;
Expand Down Expand Up @@ -198,26 +200,37 @@ public void Dispose()
/// <inheritdoc/>
public async Task InitializeAsync(IServiceProvider paneServiceProvider)
{
await UpdateContent(teamExplorerContext.ActiveRepository);
teamExplorerContext.WhenAnyValue(x => x.ActiveRepository)
.Skip(1)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(x => UpdateContent(x).Forget());

connectionManager.Connections.CollectionChanged += (_, __) => UpdateContent(LocalRepository).Forget();

BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.pullRequestCommand, showPullRequests);
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.backCommand, navigator.NavigateBack);
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.forwardCommand, navigator.NavigateForward);
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.refreshCommand, refresh);
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.githubCommand, openInBrowser);

paneServiceProvider.AddCommandHandler(Guids.guidGitHubToolbarCmdSet, PkgCmdIDList.helpCommand,
(_, __) =>
{
browser.OpenUrl(new Uri(GitHubUrls.Documentation));
usageTracker.IncrementCounter(x => x.NumberOfGitHubPaneHelpClicks).Forget();
});
await initializing.WaitAsync();
if (initialized) return;

try
{
await UpdateContent(teamExplorerContext.ActiveRepository);
teamExplorerContext.WhenAnyValue(x => x.ActiveRepository)
.Skip(1)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(x => UpdateContent(x).Forget());

connectionManager.Connections.CollectionChanged += (_, __) => UpdateContent(LocalRepository).Forget();

BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.pullRequestCommand, showPullRequests);
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.backCommand, navigator.NavigateBack);
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.forwardCommand, navigator.NavigateForward);
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.refreshCommand, refresh);
BindNavigatorCommand(paneServiceProvider, PkgCmdIDList.githubCommand, openInBrowser);

paneServiceProvider.AddCommandHandler(Guids.guidGitHubToolbarCmdSet, PkgCmdIDList.helpCommand,
(_, __) =>
{
browser.OpenUrl(new Uri(GitHubUrls.Documentation));
usageTracker.IncrementCounter(x => x.NumberOfGitHubPaneHelpClicks).Forget();
});
}
finally
{
initialized = true;
initializing.Release();
}
}

/// <inheritdoc/>
Expand Down
12 changes: 12 additions & 0 deletions src/GitHub.Exports.Reactive/Services/IPullRequestSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ public interface IPullRequestSessionManager : INotifyPropertyChanged
/// </returns>
IPullRequestSession CurrentSession { get; }

/// <summary>
/// Ensures that the service is initialized.
/// </summary>
/// <returns>A task that when completed indicates that the service is initialized.</returns>
/// <remarks>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplying? ;-)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, is seems I missed the line with simplying and it isn't showing up for me as an in-line comment. What's going on? 😕

/// If you are simply monitoring changes to the <see cref="CurrentSession"/> then you
/// don't need to call this method: <see cref="CurrentSession"/> will be updated on
/// initialization. If however, you want to be sure that <see cref="CurrentSession"/> is
/// initialized, you can await the task returned from this method.
/// </remarks>
Task EnsureInitialized();

/// <summary>
/// Gets an <see cref="IPullRequestSessionFile"/> that tracks the live state of a document.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class PullRequestSessionManager : ReactiveObject, IPullRequestSessionMana
readonly IModelServiceFactory modelServiceFactory;
readonly Dictionary<Tuple<string, int>, WeakReference<PullRequestSession>> sessions =
new Dictionary<Tuple<string, int>, WeakReference<PullRequestSession>>();
TaskCompletionSource<object> initialized;
IPullRequestSession currentSession;
ILocalRepositoryModel repository;

Expand Down Expand Up @@ -65,6 +66,7 @@ public PullRequestSessionManager(
this.sessionService = sessionService;
this.connectionManager = connectionManager;
this.modelServiceFactory = modelServiceFactory;
initialized = new TaskCompletionSource<object>(null);

Observable.FromEventPattern(teamExplorerContext, nameof(teamExplorerContext.StatusChanged))
.ObserveOn(RxApp.MainThreadScheduler)
Expand All @@ -82,6 +84,10 @@ public IPullRequestSession CurrentSession
private set { this.RaiseAndSetIfChanged(ref currentSession, value); }
}

/// <inheritdoc/>
public Task EnsureInitialized() => initialized.Task;

/// <inheritdoc/>
public async Task<IPullRequestSessionFile> GetLiveFile(
string relativePath,
ITextView textView,
Expand Down Expand Up @@ -230,6 +236,7 @@ async Task StatusChanged()
}

CurrentSession = session;
initialized.SetResult(null);
}
catch (Exception e)
{
Expand Down
7 changes: 5 additions & 2 deletions src/GitHub.VisualStudio/GitHubPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected override Task InitializeAsync(CancellationToken cancellationToken, IPr
return Task.CompletedTask;
}

public IGitHubPaneViewModel ShowHomePane()
public async Task<IGitHubPaneViewModel> ShowGitHubPane()
{
var pane = ShowToolWindow(new Guid(GitHubPane.GitHubPaneGuid));
if (pane == null)
Expand All @@ -168,7 +168,10 @@ public IGitHubPaneViewModel ShowHomePane()
{
ErrorHandler.Failed(frame.Show());
}
return (IGitHubPaneViewModel)((FrameworkElement)pane.Content).DataContext;

var viewModel = (IGitHubPaneViewModel)((FrameworkElement)pane.Content).DataContext;
await viewModel.InitializeAsync(pane);
return viewModel;
}

static ToolWindowPane ShowToolWindow(Guid windowGuid)
Expand Down
3 changes: 2 additions & 1 deletion src/GitHub.VisualStudio/IServiceProviderPackage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using GitHub.ViewModels.GitHubPane;

namespace GitHub.VisualStudio
Expand All @@ -12,6 +13,6 @@ public interface IServiceProviderPackage : IServiceProvider, Microsoft.VisualStu
[ComVisible(true)]
public interface IGitHubToolWindowManager
{
IGitHubPaneViewModel ShowHomePane();
Task<IGitHubPaneViewModel> ShowGitHubPane();
}
}
25 changes: 17 additions & 8 deletions src/GitHub.VisualStudio/Menus/OpenPullRequests.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using GitHub.Exports;
using GitHub.UI;
using GitHub.VisualStudio.UI;
using System;
using GitHub.Services;
using System;
using GitHub.Exports;
using GitHub.Extensions;
using GitHub.Logging;
using GitHub.Services;
using Serilog;

namespace GitHub.VisualStudio.Menus
{
[ExportMenu(MenuType = MenuType.OpenPullRequests)]
public class OpenPullRequests : MenuBase, IMenuHandler
{
static readonly ILogger log = LogManager.ForContext<ShowCurrentPullRequest>();

public OpenPullRequests(IGitHubServiceProvider serviceProvider)
: base(serviceProvider)
{
Expand All @@ -19,10 +21,17 @@ public OpenPullRequests(IGitHubServiceProvider serviceProvider)
public Guid Guid => Guids.guidGitHubCmdSet;
public int CmdId => PkgCmdIDList.openPullRequestsCommand;

public void Activate(object data = null)
public async void Activate(object data = null)
{
var host = ServiceProvider.TryGetService<IGitHubToolWindowManager>().ShowHomePane();
host.ShowPullRequests().Forget();
try
{
var host = await ServiceProvider.TryGetService<IGitHubToolWindowManager>().ShowGitHubPane();
await host.ShowPullRequests();
}
catch (Exception ex)
{
log.Error(ex, "Error showing opening pull requests");
}
}
}
}
37 changes: 24 additions & 13 deletions src/GitHub.VisualStudio/Menus/ShowCurrentPullRequest.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System;
using GitHub.Exports;
using GitHub.UI;
using GitHub.Services;
using GitHub.Extensions;
using GitHub.Models;
using GitHub.Logging;
using GitHub.Services;
using Serilog;

namespace GitHub.VisualStudio.Menus
{
[ExportMenu(MenuType = MenuType.OpenPullRequests)]
public class ShowCurrentPullRequest : MenuBase, IMenuHandler
{
static readonly ILogger log = LogManager.ForContext<ShowCurrentPullRequest>();

public ShowCurrentPullRequest(IGitHubServiceProvider serviceProvider)
: base(serviceProvider)
{
Expand All @@ -19,19 +21,28 @@ public ShowCurrentPullRequest(IGitHubServiceProvider serviceProvider)
public Guid Guid => Guids.guidGitHubCmdSet;
public int CmdId => PkgCmdIDList.showCurrentPullRequestCommand;

public void Activate(object data = null)
public async void Activate(object data = null)
{
var pullRequestSessionManager = ServiceProvider.ExportProvider.GetExportedValueOrDefault<IPullRequestSessionManager>();
var session = pullRequestSessionManager?.CurrentSession;
if (session == null)
try
{
return; // No active PR session.
}
var pullRequestSessionManager = ServiceProvider.ExportProvider.GetExportedValueOrDefault<IPullRequestSessionManager>();
await pullRequestSessionManager.EnsureInitialized();

var pullRequest = session.PullRequest;
var manager = ServiceProvider.TryGetService<IGitHubToolWindowManager>();
var host = manager.ShowHomePane();
host.ShowPullRequest(session.RepositoryOwner, host.LocalRepository.Name, pullRequest.Number);
var session = pullRequestSessionManager?.CurrentSession;
if (session == null)
{
return; // No active PR session.
}

var pullRequest = session.PullRequest;
var manager = ServiceProvider.TryGetService<IGitHubToolWindowManager>();
var host = await manager.ShowGitHubPane();
await host.ShowPullRequest(session.RepositoryOwner, host.LocalRepository.Name, pullRequest.Number);
}
catch (Exception ex)
{
log.Error(ex, "Error showing current pull request");
}
}
}
}
2 changes: 1 addition & 1 deletion src/GitHub.VisualStudio/Menus/ShowGitHubPane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ShowGitHubPane(IGitHubServiceProvider serviceProvider)

public void Activate(object data = null)
{
ServiceProvider.TryGetService<IGitHubToolWindowManager>()?.ShowHomePane();
ServiceProvider.TryGetService<IGitHubToolWindowManager>()?.ShowGitHubPane();
}
}
}