From e6c7151d1f89d694c6bdb708180b91ef92aa08d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9la=20Endre=20Orosz?= Date: Mon, 21 Oct 2019 12:24:54 +0200 Subject: [PATCH 1/4] unregisterCallbacks method added to template (#22) * unregisterCallbacks method added to template * Replacing tabs with spaces * README updated to reflect changes in generated code --- README.md | 9 +++++++-- .../Templates/Hub.liquid | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2bfbeb9..fecac58 100644 --- a/README.md +++ b/README.md @@ -195,11 +195,16 @@ export class ChatHub { this.connection.on('Welcome', () => implementation.welcome()); this.connection.on('Send', (message) => implementation.send(message)); } + + unregisterCallbacks(implementation: IChatHubCallbacks) { + this.connection.off('Welcome', () => implementation.welcome()); + this.connection.off('Send', (message) => implementation.send(message)); + } } export interface IChatHubCallbacks { - welcome(); - send(message: string); + welcome(): void; + send(message: string): void; } export interface Person { diff --git a/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid b/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid index 1fb7153..c5f8c74 100644 --- a/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid +++ b/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid @@ -21,6 +21,12 @@ registerCallbacks(implementation: I{{ Name }}HubCallbacks) { {% for operation in Callbacks -%} this.connection.on('{{ operation.Name }}', ({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => implementation.{{operation.MethodName}}({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %})); +{% endfor -%} + } + + unregisterCallbacks(implementation: I{{ Name }}HubCallbacks) { +{% for operation in Callbacks -%} + this.connection.off('{{ operation.Name }}', ({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => implementation.{{operation.MethodName}}({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %})); {% endfor -%} } } From 66a6a0443bee565d93fdfc381c046f8e8d062d3c Mon Sep 17 00:00:00 2001 From: Bela Orosz Date: Fri, 14 Feb 2020 20:51:41 +0100 Subject: [PATCH 2/4] HubConnection unregister bugfix --- src/HelloSignalR/Startup.cs | 15 +++++++++++++++ src/HelloSignalR/start_server.cmd | 2 ++ .../Templates/File.liquid | 2 +- .../Templates/Hub.liquid | 9 +++++++-- 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 src/HelloSignalR/start_server.cmd diff --git a/src/HelloSignalR/Startup.cs b/src/HelloSignalR/Startup.cs index f2a238e..b51e118 100644 --- a/src/HelloSignalR/Startup.cs +++ b/src/HelloSignalR/Startup.cs @@ -6,14 +6,29 @@ namespace HelloSignalR { public class Startup { + readonly string AllowAllPolicy = "AllowAllPolicy"; + public void ConfigureServices(IServiceCollection services) { + services.AddCors(options => + { + options.AddPolicy(AllowAllPolicy, + builder => + { + builder + .AllowAnyMethod() + .AllowAnyHeader() + .AllowAnyOrigin() + .AllowCredentials(); + }); + }); services.AddSignalR(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseStaticFiles(); + app.UseCors(AllowAllPolicy); app.UseSignalR(routes => { routes.MapHub("/chat"); diff --git a/src/HelloSignalR/start_server.cmd b/src/HelloSignalR/start_server.cmd new file mode 100644 index 0000000..84475b4 --- /dev/null +++ b/src/HelloSignalR/start_server.cmd @@ -0,0 +1,2 @@ +SET ASPNETCORE_URLS=http://localhost:61327 +dotnet run --no-launch-profile \ No newline at end of file diff --git a/src/SigSpec.CodeGeneration.TypeScript/Templates/File.liquid b/src/SigSpec.CodeGeneration.TypeScript/Templates/File.liquid index 3c75d9a..441aca0 100644 --- a/src/SigSpec.CodeGeneration.TypeScript/Templates/File.liquid +++ b/src/SigSpec.CodeGeneration.TypeScript/Templates/File.liquid @@ -1,4 +1,4 @@ -import { HubConnection, IStreamResult } from "@aspnet/signalr" +import { HubConnection, IStreamResult } from '@aspnet/signalr' {% for hub in Hubs -%} {{ hub }} diff --git a/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid b/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid index c5f8c74..d4f968f 100644 --- a/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid +++ b/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid @@ -1,4 +1,8 @@ export class {{ Name }}Hub { +{% for operation in Callbacks -%} + callbackFor{{ operation.Name }}: ({% for parameter in operation.Parameters %}{{ parameter.Name }}: {{ parameter.Type }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => void; +{% endfor -%} + constructor(private connection: HubConnection) { } {% for operation in Operations -%} @@ -20,13 +24,14 @@ registerCallbacks(implementation: I{{ Name }}HubCallbacks) { {% for operation in Callbacks -%} - this.connection.on('{{ operation.Name }}', ({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => implementation.{{operation.MethodName}}({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %})); + this.callbackFor{{ operation.Name }} = ({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => implementation.{{operation.MethodName}}({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}); + this.connection.on('{{ operation.Name }}', this.callbackFor{{ operation.Name }}); {% endfor -%} } unregisterCallbacks(implementation: I{{ Name }}HubCallbacks) { {% for operation in Callbacks -%} - this.connection.off('{{ operation.Name }}', ({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => implementation.{{operation.MethodName}}({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %})); + this.connection.off('{{ operation.Name }}', this.callbackFor{{ operation.Name }}); {% endfor -%} } } From db1949069dc460a52aa4e0a020fd8da453bf2793 Mon Sep 17 00:00:00 2001 From: Bela Orosz Date: Fri, 14 Feb 2020 20:54:03 +0100 Subject: [PATCH 3/4] Updating README --- README.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fecac58..2be39ea 100644 --- a/README.md +++ b/README.md @@ -173,9 +173,12 @@ Generated spec: Generated TypeScript code: ```typescript -import { HubConnection, IStreamResult } from "@aspnet/signalr" +import { HubConnection, IStreamResult } from '@aspnet/signalr' export class ChatHub { + callbackForWelcome: () => void; + callbackForSend: (message: string) => void; + constructor(private connection: HubConnection) { } @@ -192,13 +195,15 @@ export class ChatHub { } registerCallbacks(implementation: IChatHubCallbacks) { - this.connection.on('Welcome', () => implementation.welcome()); - this.connection.on('Send', (message) => implementation.send(message)); + this.callbackForWelcome = () => implementation.welcome(); + this.connection.on('Welcome', this.callbackForWelcome); + this.callbackForSend = (message) => implementation.send(message); + this.connection.on('Send', this.callbackForSend); } unregisterCallbacks(implementation: IChatHubCallbacks) { - this.connection.off('Welcome', () => implementation.welcome()); - this.connection.off('Send', (message) => implementation.send(message)); + this.connection.off('Welcome', this.callbackForWelcome); + this.connection.off('Send', this.callbackForSend); } } From 45cb161ec66903bf96e20a40333e14ed8f2e4226 Mon Sep 17 00:00:00 2001 From: Bela Orosz Date: Fri, 21 Feb 2020 20:50:47 +0100 Subject: [PATCH 4/4] Resolving PR comments --- README.md | 24 +++++++++++++------ .../Templates/File.liquid | 2 +- .../Templates/Hub.liquid | 16 +++++++++---- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 2be39ea..9ae468f 100644 --- a/README.md +++ b/README.md @@ -173,13 +173,15 @@ Generated spec: Generated TypeScript code: ```typescript -import { HubConnection, IStreamResult } from '@aspnet/signalr' +import { HubConnection, IStreamResult } from '@aspnet/signalr'; export class ChatHub { - callbackForWelcome: () => void; - callbackForSend: (message: string) => void; + private callbackForWelcome: () => void; + private callbackForSend: (message: string) => void; constructor(private connection: HubConnection) { + this.callbackForWelcome = undefined; + this.callbackForSend = undefined; } send(message: string): Promise { @@ -194,16 +196,24 @@ export class ChatHub { return this.connection.stream('GetEvents'); } - registerCallbacks(implementation: IChatHubCallbacks) { + registerCallbacks(implementation: IChatHubCallbacks): void { + this.unregisterCallbacks(); + this.callbackForWelcome = () => implementation.welcome(); this.connection.on('Welcome', this.callbackForWelcome); this.callbackForSend = (message) => implementation.send(message); this.connection.on('Send', this.callbackForSend); } - unregisterCallbacks(implementation: IChatHubCallbacks) { - this.connection.off('Welcome', this.callbackForWelcome); - this.connection.off('Send', this.callbackForSend); + unregisterCallbacks(): void { + if (this.callbackForWelcome !== undefined) { + this.connection.off('Welcome', this.callbackForWelcome); + this.callbackForWelcome = undefined; + } + if (this.callbackForSend !== undefined) { + this.connection.off('Send', this.callbackForSend); + this.callbackForSend = undefined; + } } } diff --git a/src/SigSpec.CodeGeneration.TypeScript/Templates/File.liquid b/src/SigSpec.CodeGeneration.TypeScript/Templates/File.liquid index 441aca0..4f3a06e 100644 --- a/src/SigSpec.CodeGeneration.TypeScript/Templates/File.liquid +++ b/src/SigSpec.CodeGeneration.TypeScript/Templates/File.liquid @@ -1,4 +1,4 @@ -import { HubConnection, IStreamResult } from '@aspnet/signalr' +import { HubConnection, IStreamResult } from '@aspnet/signalr'; {% for hub in Hubs -%} {{ hub }} diff --git a/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid b/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid index d4f968f..6de943a 100644 --- a/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid +++ b/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid @@ -1,9 +1,12 @@ export class {{ Name }}Hub { {% for operation in Callbacks -%} - callbackFor{{ operation.Name }}: ({% for parameter in operation.Parameters %}{{ parameter.Name }}: {{ parameter.Type }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => void; + private callbackFor{{ operation.Name }}: ({% for parameter in operation.Parameters %}{{ parameter.Name }}: {{ parameter.Type }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => void; {% endfor -%} constructor(private connection: HubConnection) { +{% for operation in Callbacks -%} + this.callbackFor{{ operation.Name }} = undefined; +{% endfor -%} } {% for operation in Operations -%} @@ -22,16 +25,21 @@ {% endif -%} {% endfor -%} - registerCallbacks(implementation: I{{ Name }}HubCallbacks) { + registerCallbacks(implementation: I{{ Name }}HubCallbacks): void { + this.unregisterCallbacks(); + {% for operation in Callbacks -%} this.callbackFor{{ operation.Name }} = ({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => implementation.{{operation.MethodName}}({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}); this.connection.on('{{ operation.Name }}', this.callbackFor{{ operation.Name }}); {% endfor -%} } - unregisterCallbacks(implementation: I{{ Name }}HubCallbacks) { + unregisterCallbacks(): void { {% for operation in Callbacks -%} - this.connection.off('{{ operation.Name }}', this.callbackFor{{ operation.Name }}); + if (this.callbackFor{{ operation.Name }} !== undefined) { + this.connection.off('{{ operation.Name }}', this.callbackFor{{ operation.Name }}); + this.callbackFor{{ operation.Name }} = undefined; + } {% endfor -%} } }