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

Commit 5893086

Browse files
committed
Stop using ThreadHelper.JoinableTaskFactory directly
1 parent 29d812a commit 5893086

File tree

7 files changed

+28
-27
lines changed

7 files changed

+28
-27
lines changed

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+
JoinableTaskFactory = joinableTaskContext?.Factory ?? ThreadHelper.JoinableTaskFactory;
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 JoinableTaskFactory.Run(() => CreateAsync(connection));
6467
}
6568

6669
public void Dispose() => cacheLock.Dispose();
70+
71+
JoinableTaskFactory JoinableTaskFactory { 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+
JoinableTaskFactory = joinableTaskContext?.Factory ?? ThreadHelper.JoinableTaskFactory;
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 = JoinableTaskFactory.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+
JoinableTaskFactory JoinableTaskFactory { get; }
5458
}
5559
}

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.TeamFoundation.14/Base/TeamExplorerServiceHolder.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,17 @@ public class TeamExplorerServiceHolder : ITeamExplorerServiceHolder
1414
{
1515
IServiceProvider serviceProvider;
1616

17-
/// <summary>
18-
/// This class relies on IVSGitExt that provides information when VS switches repositories.
19-
/// </summary>
20-
/// <param name="gitExt">Used for monitoring the active repository.</param>
21-
[ImportingConstructor]
22-
TeamExplorerServiceHolder(ITeamExplorerContext teamExplorerContext) :
23-
this(teamExplorerContext, ThreadHelper.JoinableTaskContext)
24-
{
25-
}
26-
2717
/// <summary>
2818
/// This constructor can be used for unit testing.
2919
/// </summary>
3020
/// <param name="gitService">Used for monitoring the active repository.</param>
3121
/// <param name="joinableTaskContext">Used for switching to the Main thread.</param>
32-
public TeamExplorerServiceHolder(ITeamExplorerContext teamExplorerContext, JoinableTaskContext joinableTaskContext)
22+
[ImportingConstructor]
23+
public TeamExplorerServiceHolder(ITeamExplorerContext teamExplorerContext,
24+
[Import(AllowDefault = true)] JoinableTaskContext joinableTaskContext)
3325
{
26+
joinableTaskContext = joinableTaskContext ?? ThreadHelper.JoinableTaskContext;
27+
3428
JoinableTaskCollection = joinableTaskContext.CreateCollection();
3529
JoinableTaskCollection.DisplayName = nameof(TeamExplorerServiceHolder);
3630
JoinableTaskFactory = joinableTaskContext.CreateFactory(JoinableTaskCollection);

src/GitHub.TeamFoundation.14/Services/VSGitExt.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ public class VSGitExt : IVSGitExt
3333
IGitExt gitExt;
3434
IReadOnlyList<LocalRepositoryModel> activeRepositories;
3535

36-
public VSGitExt(IServiceProvider serviceProvider, IGitService gitService)
37-
: this(serviceProvider, new VSUIContextFactory(), gitService, ThreadHelper.JoinableTaskContext)
38-
{
39-
}
40-
4136
public VSGitExt(IServiceProvider serviceProvider, IVSUIContextFactory factory, IGitService gitService,
4237
JoinableTaskContext joinableTaskContext)
4338
{

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ async Task<object> CreateService(IAsyncServiceContainer container, CancellationT
357357
// needs to be refactored. See #1398.
358358
#pragma warning disable VSTHRD011 // Use AsyncLazy<T>
359359
var lazy2Fa = new Lazy<ITwoFactorChallengeHandler>(() =>
360-
ThreadHelper.JoinableTaskFactory.Run(async () =>
360+
JoinableTaskFactory.Run(async () =>
361361
{
362-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
362+
await JoinableTaskFactory.SwitchToMainThreadAsync();
363363
return serviceProvider.GetService<ITwoFactorChallengeHandler>();
364364
}));
365365
#pragma warning restore VSTHRD011 // Use AsyncLazy<T>
@@ -406,7 +406,7 @@ async Task<object> CreateService(IAsyncServiceContainer container, CancellationT
406406
}
407407
else if (serviceType == typeof(IPackageSettings))
408408
{
409-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
409+
await JoinableTaskFactory.SwitchToMainThreadAsync();
410410
var sp = new ServiceProvider(Services.Dte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
411411
return new PackageSettings(sp);
412412
}

test/GitHub.App.UnitTests/Factories/ModelServiceFactoryTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using GitHub.Models;
88
using GitHub.Primitives;
99
using GitHub.Services;
10+
using Microsoft.VisualStudio.Threading;
1011
using NSubstitute;
1112
using NUnit.Framework;
1213

@@ -72,7 +73,8 @@ static ModelServiceFactory CreateTarget(
7273
return new ModelServiceFactory(
7374
apiClientFactory,
7475
hostCacheFactory,
75-
avatarProvider);
76+
avatarProvider,
77+
new JoinableTaskContext());
7678
}
7779

7880
static IConnection CreateConnection(string address, string login = "user")

0 commit comments

Comments
 (0)