Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for interface inheritance on StreamingHub #722

Merged
merged 2 commits into from
Dec 13, 2023

Conversation

mayuki
Copy link
Member

@mayuki mayuki commented Dec 13, 2023

This PR enables inheritance in StreamingHub interface definitions.

This allows for the consolidation of methods when multiple StreamingHub implementations need to have the same methods.

public interface IHeartbeatable
{
    Task PingAsync();
}

public interface IHeartbeatableReceiver
{
    void Pong();
}

public interface IChatHub : IStreamingHub<IChatHub, IChatHubReceiver>, IHeartbeatable
{
    Task SendAsync(string message);
}

public interface IChatHubReceiver : IHeartbeatableReceiver
{
    void OnMessage(string name, string message);
    void OnJoin(string name);
}

public interface IPollHub : IStreamingHub<IPollHub, IPollHubReceiver>, IHeartbeatable
{
    Task<(int Id, string Name)> GetSelectionsAsync();
    Task VoteAsync(int id);
}

public interface IPollHubReceiver : IHeartbeatableReceiver
{
    void OnCountUpdated(int id, int count);
}
public abstract class MyAppStreamingHubBase<THubInterface, TReceiver> : StreamingHubBase<THubInterface, TReceiver>, IHeartbeatable
    where THubInterface : IStreamingHub<THubInterface, TReceiver>
    where TReceiver : IHeartbeatableReceiver
{
    IGroup groupHeartbeatForSelf = default!;

    protected sealed override async ValueTask OnConnecting()
    {
        groupHeartbeatForSelf = await Group.AddAsync($"Heartbeat-{Context.ContextId}");
        await OnConnectingCore();
    }

    protected virtual ValueTask OnConnectingCore() => default;

    public Task PingAsync()
    {
        BroadcastToSelf(groupHeartbeatForSelf).Pong();
        return Task.CompletedTask;
    }
}
var chatHub = await StreamingHubClient.ConnectAsync<IChatHub, IChatHubReceiver>(channel, chatReceiver);
var pollHub = await StreamingHubClient.ConnectAsync<IPollHub, IPollHubReceiver>(channel, pollReceiver);

await chatHub.PingAsync(); // IHeartbeatable.PingAsync
await pollHub.PingAsync(); // IHeartbeatable.PingAsync

@mayuki mayuki merged commit 780c861 into main Dec 13, 2023
3 checks passed
@mayuki mayuki deleted the feature/StreamingHubInterfaceInheritance branch December 13, 2023 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant