From acf6f0b833c235ae935c990b8a326e8c1ee635e8 Mon Sep 17 00:00:00 2001 From: "T. Thiery" Date: Thu, 8 Apr 2021 22:21:16 +0200 Subject: [PATCH] Add DiscoverAsync with type parameter #161 non-breaking --- src/SharpBrick.PoweredUp/PoweredUpHost.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/SharpBrick.PoweredUp/PoweredUpHost.cs b/src/SharpBrick.PoweredUp/PoweredUpHost.cs index 16a941d..47cae1e 100644 --- a/src/SharpBrick.PoweredUp/PoweredUpHost.cs +++ b/src/SharpBrick.PoweredUp/PoweredUpHost.cs @@ -67,15 +67,23 @@ public void Discover(Func onDiscovery, CancellationToken token = defa }, token); } - public async Task DiscoverAsync(CancellationToken token = default) + public async Task DiscoverAsync(CancellationToken token = default) where THub : class + => await DiscoverInternalAsync(typeof(THub), token) as THub; + + public async Task DiscoverAsync(Type hubType, CancellationToken token = default) + => await DiscoverInternalAsync(hubType, token); + + private async Task DiscoverInternalAsync(Type hubType, CancellationToken token) { - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(); Discover(hub => { - if (hub is THub tHub) + var currentHubType = hub.GetType(); + + if (currentHubType == hubType) { - tcs.SetResult(tHub); + tcs.SetResult(hub); } return Task.CompletedTask; @@ -83,8 +91,7 @@ public async Task DiscoverAsync(CancellationToken token = default) var hub = await tcs.Task; - _logger.LogInformation($"End DiscoveryAsync for {typeof(THub).Name}"); - + _logger.LogInformation($"End DiscoveryAsync for {hubType.Name}"); return hub; }