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

Commit 0bfb1b3

Browse files
committed
Use JoinableTaskContext from MEF when available
1 parent becede3 commit 0bfb1b3

File tree

7 files changed

+57
-24
lines changed

7 files changed

+57
-24
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+
JoinableTaskFactory = joinableTaskContext?.Factory ?? ThreadHelper.JoinableTaskFactory;
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 JoinableTaskFactory.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 JoinableTaskFactory.SwitchToMainThreadAsync();
5462
twoFactorDialog.Cancel();
5563
}
64+
65+
JoinableTaskFactory JoinableTaskFactory { get; }
5666
}
5767
}

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+
JoinableTaskFactory = joinableTaskContext?.Factory ?? ThreadHelper.JoinableTaskFactory;
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 JoinableTaskFactory.SwitchToMainThreadAsync();
214216
}
215217

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

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

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

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+
JoinableTaskFactory = joinableTaskContext?.Factory ?? ThreadHelper.JoinableTaskFactory;
109113

110114
Files = files;
111115

@@ -471,7 +475,7 @@ public override async Task Refresh()
471475
{
472476
try
473477
{
474-
await ThreadingHelper.SwitchToMainThreadAsync();
478+
await JoinableTaskFactory.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+
JoinableTaskFactory JoinableTaskFactory { get; }
737743
}
738744
}

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ async Task<object> CreateService(IAsyncServiceContainer container, CancellationT
381381
Assumes.Present(sp);
382382

383383
var environment = new Rothko.Environment();
384-
return new UsageService(sp, environment);
384+
return new UsageService(sp, environment, ThreadHelper.JoinableTaskContext);
385385
}
386386
else if (serviceType == typeof(IUsageTracker))
387387
{
@@ -393,7 +393,7 @@ async Task<object> CreateService(IAsyncServiceContainer container, CancellationT
393393
Assumes.Present(serviceProvider);
394394
Assumes.Present(settings);
395395

396-
return new UsageTracker(serviceProvider, usageService, settings);
396+
return new UsageTracker(serviceProvider, usageService, settings, ThreadHelper.JoinableTaskContext);
397397
}
398398
else if (serviceType == typeof(IVSGitExt))
399399
{

src/GitHub.VisualStudio/Services/LocalRepositories.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55
using GitHub.Extensions;
6-
using GitHub.Helpers;
76
using GitHub.Models;
7+
using Microsoft.VisualStudio.Shell;
8+
using Microsoft.VisualStudio.Threading;
9+
using Task = System.Threading.Tasks.Task;
810

911
namespace GitHub.Services
1012
{
@@ -21,17 +23,18 @@ public class LocalRepositories : ILocalRepositories
2123
readonly IVSGitServices vsGitServices;
2224

2325
[ImportingConstructor]
24-
public LocalRepositories(IVSGitServices vsGitServices)
26+
public LocalRepositories(IVSGitServices vsGitServices, [Import(AllowDefault = true)] JoinableTaskContext joinableTaskContext)
2527
{
2628
this.vsGitServices = vsGitServices;
29+
JoinableTaskFactory = joinableTaskContext?.Factory ?? ThreadHelper.JoinableTaskFactory;
2730
}
2831

2932
/// <inheritdoc/>
3033
public async Task Refresh()
3134
{
32-
await ThreadingHelper.SwitchToPoolThreadAsync();
35+
await TaskScheduler.Default;
3336
var list = vsGitServices.GetKnownRepositories();
34-
await ThreadingHelper.SwitchToMainThreadAsync();
37+
await JoinableTaskFactory.SwitchToMainThreadAsync();
3538

3639
repositories.Except(list).ToList().ForEach(x => repositories.Remove(x));
3740
list.Except(repositories).ToList().ForEach(x => repositories.Add(x));
@@ -42,5 +45,7 @@ readonly ObservableCollectionEx<LocalRepositoryModel> repositories
4245

4346
/// <inheritdoc/>
4447
public IReadOnlyObservableCollection<LocalRepositoryModel> Repositories => repositories;
48+
49+
JoinableTaskFactory JoinableTaskFactory { get; }
4550
}
4651
}

src/GitHub.VisualStudio/Services/UsageService.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
using System.Text;
66
using System.Threading;
77
using System.Threading.Tasks;
8-
using GitHub.Helpers;
98
using GitHub.Logging;
109
using GitHub.Models;
1110
using Serilog;
1211
using Rothko;
12+
using Microsoft.VisualStudio.Threading;
13+
using Microsoft.VisualStudio.Shell;
1314
using Environment = System.Environment;
1415
using Task = System.Threading.Tasks.Task;
1516

@@ -31,10 +32,12 @@ public sealed class UsageService : IUsageService, IDisposable
3132
Guid? userGuid;
3233

3334
[ImportingConstructor]
34-
public UsageService(IGitHubServiceProvider serviceProvider, IEnvironment environment)
35+
public UsageService(IGitHubServiceProvider serviceProvider, IEnvironment environment,
36+
[Import(AllowDefault = true)] JoinableTaskContext joinableTaskContext)
3537
{
3638
this.serviceProvider = serviceProvider;
3739
this.environment = environment;
40+
JoinableTaskFactory = joinableTaskContext?.Factory ?? ThreadHelper.JoinableTaskFactory;
3841
}
3942

4043
public void Dispose()
@@ -135,7 +138,7 @@ async Task Initialize()
135138
{
136139
if (storePath == null)
137140
{
138-
await ThreadingHelper.SwitchToMainThreadAsync();
141+
await JoinableTaskFactory.SwitchToMainThreadAsync();
139142

140143
var program = serviceProvider.GetService<IProgram>();
141144

@@ -186,5 +189,7 @@ class UserData
186189
{
187190
public Guid UserGuid { get; set; }
188191
}
192+
193+
JoinableTaskFactory JoinableTaskFactory { get; }
189194
}
190195
}

src/GitHub.VisualStudio/Services/UsageTracker.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using System;
2-
using System.ComponentModel.Composition;
32
using System.Globalization;
43
using System.Linq;
54
using System.Linq.Expressions;
65
using System.Reflection;
76
using System.Threading.Tasks;
8-
using GitHub.Helpers;
97
using GitHub.Logging;
108
using GitHub.Models;
119
using GitHub.Settings;
10+
using Microsoft.VisualStudio.Threading;
1211
using Serilog;
1312
using Task = System.Threading.Tasks.Task;
1413

@@ -31,11 +30,13 @@ public sealed class UsageTracker : IUsageTracker, IDisposable
3130
public UsageTracker(
3231
IGitHubServiceProvider gitHubServiceProvider,
3332
IUsageService service,
34-
IPackageSettings settings)
33+
IPackageSettings settings,
34+
JoinableTaskContext joinableTaskContext)
3535
{
36-
this.gitHubServiceProvider = gitHubServiceProvider;
36+
makethis.gitHubServiceProvider = gitHubServiceProvider;
3737
this.service = service;
3838
this.userSettings = settings;
39+
JoinableTaskFactory = joinableTaskContext.Factory;
3940
timer = StartTimer();
4041
}
4142

@@ -69,7 +70,7 @@ async Task Initialize()
6970

7071
// The services needed by the usage tracker are loaded when they are first needed to
7172
// improve the startup time of the extension.
72-
await ThreadingHelper.SwitchToMainThreadAsync();
73+
await JoinableTaskFactory.SwitchToMainThreadAsync();
7374

7475
client = gitHubServiceProvider.TryGetService<IMetricsService>();
7576
connectionManager = gitHubServiceProvider.GetService<IConnectionManager>();
@@ -141,5 +142,7 @@ async Task<UsageModel> GetCurrentReport(UsageData data)
141142
current.Dimensions.IsEnterpriseUser = connectionManager.Connections.Any(x => !x.HostAddress.IsGitHubDotCom());
142143
return current;
143144
}
145+
146+
JoinableTaskFactory JoinableTaskFactory { get; }
144147
}
145148
}

0 commit comments

Comments
 (0)