Skip to content

Commit

Permalink
refactor: remove uneeded hub context
Browse files Browse the repository at this point in the history
  • Loading branch information
aguacongas committed Oct 8, 2020
1 parent 25c8d23 commit f263bdb
Showing 1 changed file with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2020 @Olivier Lefebvre
using Aguacongas.IdentityServer.Abstractions;
using Aguacongas.TheIdServer.Admin.Hubs;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Threading;
Expand All @@ -16,22 +14,19 @@ namespace Aguacongas.IdentityServer.Admin.Services
/// <seealso cref="IProviderClient" />
public class ProviderClient : IProviderClient
{
private readonly IHubContext<ProviderHub> _context;
private readonly HubConnectionFactory _hubConnectionFactory;

/// <summary>
/// Initializes a new instance of the <see cref="ProviderClient"/> class.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="hubConnectionFactory">The hub connection factory.</param>
/// <exception cref="ArgumentNullException">
/// context
/// or
/// hubConnectionFactory
/// </exception>
public ProviderClient(IHubContext<ProviderHub> context, HubConnectionFactory hubConnectionFactory)
public ProviderClient(HubConnectionFactory hubConnectionFactory)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_hubConnectionFactory = hubConnectionFactory ?? throw new ArgumentNullException(nameof(hubConnectionFactory));
}

Expand All @@ -44,7 +39,7 @@ public ProviderClient(IHubContext<ProviderHub> context, HubConnectionFactory hub
/// <returns></returns>
public Task KeyRevokedAsync(string kind, Guid id, CancellationToken cancellationToken = default)
{
return GetClientProxy(cancellationToken).SendAsync(nameof(IProviderHub.KeyRevoked), kind, id, cancellationToken);
return GetConnection(cancellationToken).SendAsync(nameof(IProviderHub.KeyRevoked), kind, id, cancellationToken);
}

/// <summary>
Expand All @@ -55,7 +50,7 @@ public Task KeyRevokedAsync(string kind, Guid id, CancellationToken cancellation
/// <returns></returns>
public Task ProviderAddedAsync(string scheme, CancellationToken cancellationToken = default)
{
return GetClientProxy(cancellationToken).SendAsync(nameof(IProviderHub.ProviderAdded), scheme, cancellationToken);
return GetConnection(cancellationToken).SendAsync(nameof(IProviderHub.ProviderAdded), scheme, cancellationToken);
}

/// <summary>
Expand All @@ -66,7 +61,7 @@ public Task ProviderAddedAsync(string scheme, CancellationToken cancellationToke
/// <returns></returns>
public Task ProviderRemovedAsync(string scheme, CancellationToken cancellationToken = default)
{
return GetClientProxy(cancellationToken).SendAsync(nameof(IProviderHub.ProviderRemoved), scheme, cancellationToken);
return GetConnection(cancellationToken).SendAsync(nameof(IProviderHub.ProviderRemoved), scheme, cancellationToken);
}

/// <summary>
Expand All @@ -77,13 +72,12 @@ public Task ProviderRemovedAsync(string scheme, CancellationToken cancellationTo
/// <returns></returns>
public Task ProviderUpdatedAsync(string scheme, CancellationToken cancellationToken = default)
{
return GetClientProxy(cancellationToken).SendAsync(nameof(IProviderHub.ProviderUpdated), scheme, cancellationToken);
return GetConnection(cancellationToken).SendAsync(nameof(IProviderHub.ProviderUpdated), scheme, cancellationToken);
}

private IClientProxy GetClientProxy(CancellationToken cancellationToken)
private HubConnection GetConnection(CancellationToken cancellationToken)
{
var connection = _hubConnectionFactory.GetConnection(cancellationToken);
return _context.Clients.AllExcept(connection?.ConnectionId);
return _hubConnectionFactory.GetConnection(cancellationToken);
}
}
}

0 comments on commit f263bdb

Please sign in to comment.