Refactor HubContext to not Implement IHubClients (#1262)

This commit is contained in:
Mikael Mengistu 2018-01-02 17:26:10 -08:00 committed by GitHub
parent 44052ffcf6
commit 6baee8b7a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 111 additions and 73 deletions

View File

@ -42,7 +42,7 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks
[Benchmark]
public Task InvokeAsyncAll()
{
return _hubContext.All.InvokeAsync("Method");
return _hubContext.Clients.All.InvokeAsync("Method");
}
}
}

View File

@ -0,0 +1,50 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
namespace Microsoft.AspNetCore.SignalR
{
public class HubClients<THub> : IHubClients
{
private readonly HubLifetimeManager<THub> _lifetimeManager;
public HubClients(HubLifetimeManager<THub> lifetimeManager)
{
_lifetimeManager = lifetimeManager;
All = new AllClientProxy<THub>(_lifetimeManager);
}
public IClientProxy All { get; }
public IClientProxy AllExcept(IReadOnlyList<string> excludedIds)
{
return new AllClientsExceptProxy<THub>(_lifetimeManager, excludedIds);
}
public IClientProxy Client(string connectionId)
{
return new SingleClientProxy<THub>(_lifetimeManager, connectionId);
}
public IClientProxy Group(string groupName)
{
return new GroupProxy<THub>(_lifetimeManager, groupName);
}
public IClientProxy GroupExcept(string groupName, IReadOnlyList<string> excludeIds)
{
return new GroupExceptProxy<THub>(_lifetimeManager, groupName, excludeIds);
}
public IClientProxy MultipleClients(IReadOnlyList<string> connectionIds)
{
return new MultipleClientProxy<THub>(_lifetimeManager, connectionIds);
}
public IClientProxy User(string userId)
{
return new UserProxy<THub>(_lifetimeManager, userId);
}
}
}

View File

@ -0,0 +1,52 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.AspNetCore.SignalR
{
public class HubClients<THub, T> : IHubClients<T>
{
private readonly HubLifetimeManager<THub> _lifetimeManager;
public HubClients(HubLifetimeManager<THub> lifetimeManager)
{
_lifetimeManager = lifetimeManager;
All = TypedClientBuilder<T>.Build(new AllClientProxy<THub>(_lifetimeManager));
}
public T All { get; }
public T AllExcept(IReadOnlyList<string> excludedIds)
{
return TypedClientBuilder<T>.Build(new AllClientsExceptProxy<THub>(_lifetimeManager, excludedIds));
}
public virtual T Client(string connectionId)
{
return TypedClientBuilder<T>.Build(new SingleClientProxy<THub>(_lifetimeManager, connectionId));
}
public T MultipleClients(IReadOnlyList<string> connectionIds)
{
return TypedClientBuilder<T>.Build(new MultipleClientProxy<THub>(_lifetimeManager, connectionIds));
}
public virtual T Group(string groupName)
{
return TypedClientBuilder<T>.Build(new GroupProxy<THub>(_lifetimeManager, groupName));
}
public T GroupExcept(string groupName, IReadOnlyList<string> excludeIds)
{
return TypedClientBuilder<T>.Build(new GroupExceptProxy<THub>(_lifetimeManager, groupName, excludeIds));
}
public virtual T User(string userId)
{
return TypedClientBuilder<T>.Build(new UserProxy<THub>(_lifetimeManager, userId));
}
}
}

View File

@ -1,55 +1,22 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
namespace Microsoft.AspNetCore.SignalR
{
public class HubContext<THub> : IHubContext<THub>, IHubClients where THub : Hub
public class HubContext<THub> : IHubContext<THub> where THub : Hub
{
private readonly HubLifetimeManager<THub> _lifetimeManager;
private readonly IHubClients _clients;
public HubContext(HubLifetimeManager<THub> lifetimeManager)
{
_lifetimeManager = lifetimeManager;
All = new AllClientProxy<THub>(_lifetimeManager);
_clients = new HubClients<THub>(_lifetimeManager);
Groups = new GroupManager<THub>(lifetimeManager);
}
public IHubClients Clients => this;
public virtual IClientProxy All { get; }
public IHubClients Clients => _clients;
public virtual IGroupManager Groups { get; }
public IClientProxy AllExcept(IReadOnlyList<string> excludedIds)
{
return new AllClientsExceptProxy<THub>(_lifetimeManager, excludedIds);
}
public virtual IClientProxy Client(string connectionId)
{
return new SingleClientProxy<THub>(_lifetimeManager, connectionId);
}
public virtual IClientProxy Group(string groupName)
{
return new GroupProxy<THub>(_lifetimeManager, groupName);
}
public IClientProxy GroupExcept(string groupName, IReadOnlyList<string> excludeIds)
{
return new GroupExceptProxy<THub>(_lifetimeManager, groupName, excludeIds);
}
public virtual IClientProxy User(string userId)
{
return new UserProxy<THub>(_lifetimeManager, userId);
}
public virtual IClientProxy MultipleClients(IReadOnlyList<string> connectionIds)
{
return new MultipleClientProxy<THub>(_lifetimeManager, connectionIds);
}
}
}

View File

@ -5,53 +5,22 @@ using System.Collections.Generic;
namespace Microsoft.AspNetCore.SignalR
{
public class HubContext<THub, T> : IHubContext<THub, T>, IHubClients<T>
public class HubContext<THub, T> : IHubContext<THub, T>
where THub : Hub<T>
where T : class
{
private readonly HubLifetimeManager<THub> _lifetimeManager;
private readonly IHubClients<T> _clients;
public HubContext(HubLifetimeManager<THub> lifetimeManager)
{
_lifetimeManager = lifetimeManager;
All = TypedClientBuilder<T>.Build(new AllClientProxy<THub>(_lifetimeManager));
_clients = new HubClients<THub, T>(_lifetimeManager);
Groups = new GroupManager<THub>(lifetimeManager);
}
public IHubClients<T> Clients => this;
public virtual T All { get; }
public IHubClients<T> Clients => _clients;
public virtual IGroupManager Groups { get; }
public T AllExcept(IReadOnlyList<string> excludedIds)
{
return TypedClientBuilder<T>.Build(new AllClientsExceptProxy<THub>(_lifetimeManager, excludedIds));
}
public virtual T Client(string connectionId)
{
return TypedClientBuilder<T>.Build(new SingleClientProxy<THub>(_lifetimeManager, connectionId));
}
public T MultipleClients(IReadOnlyList<string> connectionIds)
{
return TypedClientBuilder<T>.Build(new MultipleClientProxy<THub>(_lifetimeManager, connectionIds));
}
public virtual T Group(string groupName)
{
return TypedClientBuilder<T>.Build(new GroupProxy<THub>(_lifetimeManager, groupName));
}
public T GroupExcept(string groupName, IReadOnlyList<string> excludeIds)
{
return TypedClientBuilder<T>.Build(new GroupExceptProxy<THub>(_lifetimeManager, groupName, excludeIds));
}
public virtual T User(string userId)
{
return TypedClientBuilder<T>.Build(new UserProxy<THub>(_lifetimeManager, userId));
}
}
}