Skip to content

Commit

Permalink
Add DiscoverAsync with type parameter
Browse files Browse the repository at this point in the history
#161 non-breaking
  • Loading branch information
tthiery committed Apr 8, 2021
1 parent 0b4ae53 commit acf6f0b
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/SharpBrick.PoweredUp/PoweredUpHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,31 @@ public void Discover(Func<Hub, Task> onDiscovery, CancellationToken token = defa
}, token);
}

public async Task<THub> DiscoverAsync<THub>(CancellationToken token = default)
public async Task<THub> DiscoverAsync<THub>(CancellationToken token = default) where THub : class
=> await DiscoverInternalAsync(typeof(THub), token) as THub;

public async Task<Hub> DiscoverAsync(Type hubType, CancellationToken token = default)
=> await DiscoverInternalAsync(hubType, token);

private async Task<Hub> DiscoverInternalAsync(Type hubType, CancellationToken token)
{
var tcs = new TaskCompletionSource<THub>();
var tcs = new TaskCompletionSource<Hub>();

Discover(hub =>
{
if (hub is THub tHub)
var currentHubType = hub.GetType();
if (currentHubType == hubType)
{
tcs.SetResult(tHub);
tcs.SetResult(hub);
}
return Task.CompletedTask;
}, token);

var hub = await tcs.Task;

_logger.LogInformation($"End DiscoveryAsync for {typeof(THub).Name}");

_logger.LogInformation($"End DiscoveryAsync for {hubType.Name}");
return hub;
}

Expand Down

0 comments on commit acf6f0b

Please sign in to comment.