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

Commit cf9f62c

Browse files
Merge pull request #2333 from github/fixes/rehostable-JoinableTaskContext
Use JoinableTaskContext from MEF
2 parents 8783e10 + 99253ff commit cf9f62c

File tree

23 files changed

+125
-195
lines changed

23 files changed

+125
-195
lines changed

src/GitHub.App/Authentication/TwoFactorChallengeHandler.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
using ReactiveUI;
77
using System.Threading.Tasks;
88
using GitHub.Api;
9-
using GitHub.Helpers;
109
using GitHub.Extensions;
1110
using GitHub.ViewModels.Dialog;
11+
using Microsoft.VisualStudio.Threading;
12+
using Microsoft.VisualStudio.Shell;
13+
using Task = System.Threading.Tasks.Task;
1214

1315
namespace GitHub.Authentication
1416
{
@@ -17,6 +19,12 @@ namespace GitHub.Authentication
1719
[PartCreationPolicy(CreationPolicy.Shared)]
1820
public class TwoFactorChallengeHandler : ReactiveObject, IDelegatingTwoFactorChallengeHandler
1921
{
22+
[ImportingConstructor]
23+
public TwoFactorChallengeHandler([Import(AllowDefault = true)] JoinableTaskContext joinableTaskContext)
24+
{
25+
JoinableTaskContext = joinableTaskContext ?? ThreadHelper.JoinableTaskContext;
26+
}
27+
2028
ILogin2FaViewModel twoFactorDialog;
2129
public IViewModel CurrentViewModel
2230
{
@@ -33,7 +41,7 @@ public async Task<TwoFactorChallengeResult> HandleTwoFactorException(TwoFactorAu
3341
{
3442
Guard.ArgumentNotNull(exception, nameof(exception));
3543

36-
await ThreadingHelper.SwitchToMainThreadAsync();
44+
await JoinableTaskContext.Factory.SwitchToMainThreadAsync();
3745

3846
var userError = new TwoFactorRequiredUserError(exception);
3947
var result = await twoFactorDialog.Show(userError);
@@ -50,8 +58,10 @@ public async Task<TwoFactorChallengeResult> HandleTwoFactorException(TwoFactorAu
5058

5159
public async Task ChallengeFailed(Exception exception)
5260
{
53-
await ThreadingHelper.SwitchToMainThreadAsync();
61+
await JoinableTaskContext.Factory.SwitchToMainThreadAsync();
5462
twoFactorDialog.Cancel();
5563
}
64+
65+
JoinableTaskContext JoinableTaskContext { get; }
5666
}
5767
}

src/GitHub.App/Factories/ModelServiceFactory.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using GitHub.Models;
99
using GitHub.Services;
1010
using Microsoft.VisualStudio.Shell;
11+
using Microsoft.VisualStudio.Threading;
1112

1213
namespace GitHub.Factories
1314
{
@@ -25,11 +26,13 @@ public sealed class ModelServiceFactory : IModelServiceFactory, IDisposable
2526
public ModelServiceFactory(
2627
IApiClientFactory apiClientFactory,
2728
IHostCacheFactory hostCacheFactory,
28-
IAvatarProvider avatarProvider)
29+
IAvatarProvider avatarProvider,
30+
[Import(AllowDefault = true)] JoinableTaskContext joinableTaskContext)
2931
{
3032
this.apiClientFactory = apiClientFactory;
3133
this.hostCacheFactory = hostCacheFactory;
3234
this.avatarProvider = avatarProvider;
35+
JoinableTaskContext = joinableTaskContext ?? ThreadHelper.JoinableTaskContext;
3336
}
3437

3538
public async Task<IModelService> CreateAsync(IConnection connection)
@@ -60,9 +63,11 @@ await hostCacheFactory.Create(connection.HostAddress),
6063

6164
public IModelService CreateBlocking(IConnection connection)
6265
{
63-
return ThreadHelper.JoinableTaskFactory.Run(() => CreateAsync(connection));
66+
return JoinableTaskContext.Factory.Run(() => CreateAsync(connection));
6467
}
6568

6669
public void Dispose() => cacheLock.Dispose();
70+
71+
JoinableTaskContext JoinableTaskContext { get; }
6772
}
6873
}

src/GitHub.App/Services/GitHubCredentialProvider.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using GitHub.Primitives;
77
using LibGit2Sharp;
88
using Microsoft.VisualStudio.Shell;
9+
using Microsoft.VisualStudio.Threading;
910
using Serilog;
1011

1112
namespace GitHub.Services
@@ -18,11 +19,12 @@ class GitHubCredentialProvider : IGitHubCredentialProvider
1819
readonly IKeychain keychain;
1920

2021
[ImportingConstructor]
21-
public GitHubCredentialProvider(IKeychain keychain)
22+
public GitHubCredentialProvider(IKeychain keychain, [Import(AllowDefault = true)] JoinableTaskContext joinableTaskContext)
2223
{
2324
Guard.ArgumentNotNull(keychain, nameof(keychain));
2425

2526
this.keychain = keychain;
27+
JoinableTaskContext = joinableTaskContext ?? ThreadHelper.JoinableTaskContext;
2628
}
2729

2830
/// <summary>
@@ -38,7 +40,7 @@ public Credentials HandleCredentials(string url, string username, SupportedCrede
3840

3941
try
4042
{
41-
var credentials = ThreadHelper.JoinableTaskFactory.Run(async () => await keychain.Load(host));
43+
var credentials = JoinableTaskContext.Factory.Run(async () => await keychain.Load(host));
4244
return new UsernamePasswordCredentials
4345
{
4446
Username = credentials.Item1,
@@ -51,5 +53,7 @@ public Credentials HandleCredentials(string url, string username, SupportedCrede
5153
return null;
5254
}
5355
}
56+
57+
JoinableTaskContext JoinableTaskContext { get; }
5458
}
5559
}

src/GitHub.App/Services/RepositoryCloneService.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
using System.Threading.Tasks;
99
using GitHub.Api;
1010
using GitHub.Extensions;
11-
using GitHub.Helpers;
1211
using GitHub.Logging;
1312
using GitHub.Models;
1413
using GitHub.Primitives;
1514
using Microsoft.VisualStudio.Shell;
15+
using Microsoft.VisualStudio.Threading;
1616
using Octokit.GraphQL;
1717
using Octokit.GraphQL.Model;
1818
using Rothko;
@@ -50,7 +50,8 @@ public RepositoryCloneService(
5050
IGraphQLClientFactory graphqlFactory,
5151
IGitHubContextService gitHubContextService,
5252
IUsageTracker usageTracker,
53-
IGitHubServiceProvider sp)
53+
IGitHubServiceProvider sp,
54+
[Import(AllowDefault = true)] JoinableTaskContext joinableTaskContext)
5455
{
5556
this.operatingSystem = operatingSystem;
5657
this.vsGitServices = vsGitServices;
@@ -59,6 +60,7 @@ public RepositoryCloneService(
5960
this.gitHubContextService = gitHubContextService;
6061
this.usageTracker = usageTracker;
6162
dte = new Lazy<EnvDTE.DTE>(() => sp.GetService<EnvDTE.DTE>());
63+
JoinableTaskContext = joinableTaskContext ?? ThreadHelper.JoinableTaskContext;
6264

6365
defaultClonePath = GetLocalClonePathFromGitProvider(operatingSystem.Environment.GetUserRepositoriesPath());
6466
}
@@ -208,9 +210,9 @@ public async Task CloneRepository(
208210
// vsGitServices.Clone() as this must be called on the main thread.
209211
if (!DestinationDirectoryExists(repositoryPath))
210212
{
211-
await ThreadingHelper.SwitchToPoolThreadAsync();
213+
await TaskScheduler.Default;
212214
operatingSystem.Directory.CreateDirectory(repositoryPath);
213-
await ThreadingHelper.SwitchToMainThreadAsync();
215+
await JoinableTaskContext.Factory.SwitchToMainThreadAsync();
214216
}
215217

216218
try
@@ -251,6 +253,8 @@ string GetLocalClonePathFromGitProvider(string fallbackPath)
251253

252254
public string DefaultClonePath { get { return defaultClonePath; } }
253255

256+
JoinableTaskContext JoinableTaskContext { get; }
257+
254258
class OrganizationAdapter
255259
{
256260
public IReadOnlyList<RepositoryListItemModel> Repositories { get; set; }

src/GitHub.App/Services/TeamExplorerContext.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ public class TeamExplorerContext : ITeamExplorerContext
4949
TeamExplorerContext(
5050
IVSGitExt gitExt,
5151
[Import(typeof(SVsServiceProvider))] IServiceProvider sp,
52-
IPullRequestService pullRequestService) : this(
52+
IPullRequestService pullRequestService,
53+
[Import(AllowDefault = true)] JoinableTaskContext joinableTaskContext) : this(
5354
gitExt,
5455
new AsyncLazy<DTE>(async () =>
5556
{
56-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
57+
await (joinableTaskContext ?? ThreadHelper.JoinableTaskContext).Factory.SwitchToMainThreadAsync();
5758
return (DTE)sp.GetService(typeof(DTE));
5859
}),
5960
pullRequestService,
60-
ThreadHelper.JoinableTaskContext)
61+
joinableTaskContext ?? ThreadHelper.JoinableTaskContext)
6162
{
6263
}
6364

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using GitHub.Commands;
1414
using GitHub.Extensions;
1515
using GitHub.Factories;
16-
using GitHub.Helpers;
1716
using GitHub.Logging;
1817
using GitHub.Models;
1918
using GitHub.Services;
@@ -25,6 +24,9 @@
2524
using static System.FormattableString;
2625
using ReactiveCommand = ReactiveUI.ReactiveCommand;
2726
using GitHub.Primitives;
27+
using Microsoft.VisualStudio.Threading;
28+
using Microsoft.VisualStudio.Shell;
29+
using Task = System.Threading.Tasks.Task;
2830

2931
namespace GitHub.ViewModels.GitHubPane
3032
{
@@ -85,7 +87,8 @@ public PullRequestDetailViewModel(
8587
ISyncSubmodulesCommand syncSubmodulesCommand,
8688
IViewViewModelFactory viewViewModelFactory,
8789
IGitService gitService,
88-
IOpenIssueishDocumentCommand openDocumentCommand)
90+
IOpenIssueishDocumentCommand openDocumentCommand,
91+
[Import(AllowDefault = true)] JoinableTaskContext joinableTaskContext)
8992
{
9093
Guard.ArgumentNotNull(pullRequestsService, nameof(pullRequestsService));
9194
Guard.ArgumentNotNull(sessionManager, nameof(sessionManager));
@@ -106,6 +109,7 @@ public PullRequestDetailViewModel(
106109
this.viewViewModelFactory = viewViewModelFactory;
107110
this.gitService = gitService;
108111
this.openDocumentCommand = openDocumentCommand;
112+
JoinableTaskContext = joinableTaskContext ?? ThreadHelper.JoinableTaskContext;
109113

110114
Files = files;
111115

@@ -471,7 +475,7 @@ public override async Task Refresh()
471475
{
472476
try
473477
{
474-
await ThreadingHelper.SwitchToMainThreadAsync();
478+
await JoinableTaskContext.Factory.SwitchToMainThreadAsync();
475479

476480
Error = null;
477481
OperationError = null;
@@ -734,5 +738,7 @@ public UpdateCommandState(
734738
public string SyncSubmodulesToolTip { get; }
735739
public int SubmodulesToSync { get; }
736740
}
741+
742+
JoinableTaskContext JoinableTaskContext { get; }
737743
}
738744
}

src/GitHub.Exports/Helpers/ThreadingHelper.cs

Lines changed: 0 additions & 115 deletions
This file was deleted.

src/GitHub.Exports/Services/ITeamExplorerServiceHolder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface ITeamExplorerServiceHolder
3636
/// <summary>
3737
/// A service for avoiding deadlocks and marshaling tasks onto the UI thread.
3838
/// </summary>
39-
JoinableTaskFactory JoinableTaskFactory { get; }
39+
JoinableTaskContext JoinableTaskContext { get; }
4040

4141
IGitAwareItem HomeSection { get; }
4242
}

0 commit comments

Comments
 (0)