Skip to content

Commit 5b541d5

Browse files
authored
Merge pull request #163 from nenoNaninu/support_inheritance
Support inheritance
2 parents dff2fef + e80ab1a commit 5b541d5

File tree

9 files changed

+239
-5
lines changed

9 files changed

+239
-5
lines changed

src/TypedSignalR.Client.TypeScript/RoslynExtensions.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,24 @@ public static IEnumerable<ISymbol> GetPublicFieldsAndProperties(this INamedTypeS
2828

2929
public static IEnumerable<IMethodSymbol> GetMethods(this INamedTypeSymbol source)
3030
{
31-
return source.GetMembers()
31+
var methods = source.GetMembers()
3232
.OfType<IMethodSymbol>()
3333
.Where(static x => x.MethodKind == MethodKind.Ordinary);
34+
35+
var allInterfaces = source.AllInterfaces;
36+
37+
if (allInterfaces.IsEmpty)
38+
{
39+
return methods;
40+
}
41+
42+
var allMethods = allInterfaces
43+
.SelectMany(static x => x.GetMembers())
44+
.OfType<IMethodSymbol>()
45+
.Where(static x => x.MethodKind is MethodKind.Ordinary)
46+
.Concat(methods);
47+
48+
return allMethods;
3449
}
3550

3651
public static IEnumerable<ISymbol> IgnoreStatic(this IEnumerable<ISymbol> source)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { HubConnectionBuilder } from '@microsoft/signalr'
2+
import { getHubProxyFactory } from '../generated/json/TypedSignalR.Client'
3+
import { MyEnum, MyRequestItem, MyRequestItem2, UserDefinedType } from '../generated/json/TypedSignalR.Client.TypeScript.Tests.Shared';
4+
import crypto from 'crypto'
5+
6+
const getRandomInt = (max: number) => {
7+
return Math.floor(Math.random() * max);
8+
}
9+
10+
const toUTCString = (date: string | Date): string => {
11+
if (typeof date === 'string') {
12+
const d = new Date(date);
13+
return d.toUTCString();
14+
}
15+
16+
return date.toUTCString();
17+
}
18+
19+
const testMethod = async () => {
20+
const connection = new HubConnectionBuilder()
21+
.withUrl("http://localhost:5000/hubs/InheritHub")
22+
.build();
23+
24+
const hubProxy = getHubProxyFactory("IInheritHub")
25+
.createHubProxy(connection);
26+
27+
try {
28+
await connection.start();
29+
30+
const r1 = await hubProxy.get();
31+
expect(r1).toEqual("TypedSignalR.Client.TypeScript");
32+
33+
const x = getRandomInt(1000);
34+
const y = getRandomInt(1000);
35+
36+
const r2 = await hubProxy.add(x, y);
37+
expect(r2).toEqual(x + y);
38+
39+
const s1 = "revue";
40+
const s2 = "starlight";
41+
42+
const r3 = await hubProxy.cat(s1, s2);;
43+
44+
expect(r3).toEqual(s1 + s2);
45+
46+
const instance: UserDefinedType = {
47+
dateTime: new Date(),
48+
guid: crypto.randomUUID()
49+
}
50+
51+
const r4 = await hubProxy.echo(instance);
52+
53+
instance.dateTime = toUTCString(instance.dateTime)
54+
r4.dateTime = toUTCString(r4.dateTime)
55+
56+
expect(r4).toEqual(instance)
57+
}
58+
finally {
59+
await connection.stop();
60+
}
61+
}
62+
63+
test('unary.test', testMethod);

tests/TypeScriptTests/src/json/unary.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const testMethod = async () => {
5050

5151
const r4 = await hubProxy.echo(instance);
5252

53-
instance.dateTime = toUTCString(r4.dateTime)
53+
instance.dateTime = toUTCString(instance.dateTime)
5454
r4.dateTime = toUTCString(r4.dateTime)
5555

5656
expect(r4).toEqual(instance)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { HubConnectionBuilder } from '@microsoft/signalr'
2+
import { getHubProxyFactory } from '../generated/msgpack/TypedSignalR.Client'
3+
import { UserDefinedType } from '../generated/msgpack/TypedSignalR.Client.TypeScript.Tests.Shared';
4+
import crypto from 'crypto'
5+
import { MessagePackHubProtocol } from '@microsoft/signalr-protocol-msgpack';
6+
7+
const getRandomInt = (max: number) => {
8+
return Math.floor(Math.random() * max);
9+
}
10+
11+
const toUTCString = (date: string | Date): string => {
12+
if (typeof date === 'string') {
13+
const d = new Date(date);
14+
return d.toUTCString();
15+
}
16+
17+
return date.toUTCString();
18+
}
19+
20+
const testMethod = async () => {
21+
const connection = new HubConnectionBuilder()
22+
.withUrl("http://localhost:5000/hubs/InheritHub")
23+
.withHubProtocol(new MessagePackHubProtocol())
24+
.build();
25+
26+
const hubProxy = getHubProxyFactory("IInheritHub")
27+
.createHubProxy(connection);
28+
29+
try {
30+
await connection.start();
31+
32+
const r1 = await hubProxy.get();
33+
expect(r1).toEqual("TypedSignalR.Client.TypeScript");
34+
35+
const x = getRandomInt(1000);
36+
const y = getRandomInt(1000);
37+
38+
const r2 = await hubProxy.add(x, y);
39+
expect(r2).toEqual(x + y);
40+
41+
const s1 = "revue";
42+
const s2 = "starlight";
43+
44+
const r3 = await hubProxy.cat(s1, s2);;
45+
46+
expect(r3).toEqual(s1 + s2);
47+
48+
const instance: UserDefinedType = {
49+
DateTime: new Date(),
50+
Guid: crypto.randomUUID()
51+
}
52+
53+
const r4 = await hubProxy.echo(instance);
54+
55+
expect(r4).toEqual(instance)
56+
}
57+
finally {
58+
await connection.stop();
59+
}
60+
}
61+
62+
test('unary.test', testMethod);

tests/TypeScriptTests/src/msgpack/unary.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ const testMethod = async () => {
4444

4545
const r4 = await hubProxy.echo(instance);
4646

47-
instance.DateTime = r4.DateTime
48-
r4.DateTime = r4.DateTime
49-
5047
expect(r4).toEqual(instance)
5148

5249
const r5 = await hubProxy.echoMyEnum(MyEnum.Four);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
using TypedSignalR.Client.TypeScript.Tests.Shared;
3+
4+
namespace TypedSignalR.Client.TypeScript.Tests.Server.Hubs;
5+
6+
public class InheritHub : Hub<IInheritHubReceiver>, IInheritHub
7+
{
8+
public Task<int> Add(int x, int y)
9+
{
10+
return Task.FromResult(x + y);
11+
}
12+
13+
public Task<string> Cat(string x, string y)
14+
{
15+
return Task.FromResult(x + y);
16+
}
17+
18+
public Task<UserDefinedType> Echo(UserDefinedType instance)
19+
{
20+
return Task.FromResult(instance);
21+
}
22+
23+
public Task<string> Get()
24+
{
25+
return Task.FromResult("TypedSignalR.Client.TypeScript");
26+
}
27+
}

tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/UnaryHub.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.SignalR;
2+
using Microsoft.Extensions.Logging;
23
using TypedSignalR.Client.TypeScript.Tests.Shared;
34

45
namespace TypedSignalR.Client.TypeScript.Tests.Server.Hubs;
@@ -75,4 +76,22 @@ public Task<List<MyResponseItem2>> RequestList(List<MyRequestItem2> list)
7576

7677
return Task.FromResult(buffer);
7778
}
79+
80+
public override Task OnConnectedAsync()
81+
{
82+
_logger.Log(LogLevel.Information, "UnaryHub.OnConnectedAsync");
83+
84+
return base.OnConnectedAsync();
85+
}
86+
87+
public override Task OnDisconnectedAsync(Exception? exception)
88+
{
89+
//_logger.Log(LogLevel.Information, "UnaryHub.OnDisconnectedAsync");
90+
if (exception is not null)
91+
{
92+
_logger.LogError(exception, "UnaryHub.OnDisconnectedAsync");
93+
}
94+
95+
return base.OnDisconnectedAsync(exception);
96+
}
7897
}

tests/TypedSignalR.Client.TypeScript.Tests.Server/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@
4444
app.MapHub<StreamingHub>("/hubs/StreamingHub");
4545
app.MapHub<ClientResultsTestHub>("/hubs/ClientResultsTestHub");
4646
app.MapHub<NestedTypeHub>("/hubs/NestedTypeHub");
47+
app.MapHub<InheritHub>("/hubs/InheritHub");
4748

4849
app.Run();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace TypedSignalR.Client.TypeScript.Tests.Shared;
8+
9+
public interface IHubBaseBase
10+
{
11+
Task<string> Get();
12+
}
13+
14+
public interface IHubBase1 : IHubBaseBase
15+
{
16+
Task<int> Add(int x, int y);
17+
}
18+
19+
public interface IHubBase2 : IHubBaseBase
20+
{
21+
Task<string> Cat(string x, string y);
22+
}
23+
24+
[Hub]
25+
public interface IInheritHub : IHubBase1, IHubBase2
26+
{
27+
Task<UserDefinedType> Echo(UserDefinedType instance);
28+
}
29+
30+
31+
public interface IReceiverBaseBase
32+
{
33+
Task ReceiveMessage(string message, int value);
34+
}
35+
36+
public interface IReceiverBase1 : IReceiverBaseBase
37+
{
38+
Task ReceiveCustomMessage(UserDefinedType userDefined);
39+
}
40+
41+
public interface IReceiverBase2 : IReceiverBaseBase
42+
{
43+
Task Notify();
44+
}
45+
46+
[Receiver]
47+
public interface IInheritHubReceiver : IReceiverBase1, IReceiverBase2
48+
{
49+
Task ReceiveMessage2(string message, int value);
50+
}

0 commit comments

Comments
 (0)