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

Bugfix/unregister callbacks #29

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,15 @@ Generated spec:
Generated TypeScript code:

```typescript
import { HubConnection, IStreamResult } from "@aspnet/signalr"
import { HubConnection, IStreamResult } from '@aspnet/signalr';

export class ChatHub {
private callbackForWelcome: () => void;
private callbackForSend: (message: string) => void;

constructor(private connection: HubConnection) {
this.callbackForWelcome = undefined;
this.callbackForSend = undefined;
}

send(message: string): Promise<void> {
Expand All @@ -191,14 +196,24 @@ export class ChatHub {
return this.connection.stream('GetEvents');
}

registerCallbacks(implementation: IChatHubCallbacks) {
this.connection.on('Welcome', () => implementation.welcome());
this.connection.on('Send', (message) => implementation.send(message));
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', () => implementation.welcome());
this.connection.off('Send', (message) => implementation.send(message));
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;
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/HelloSignalR/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChatHub>("/chat");
Expand Down
2 changes: 2 additions & 0 deletions src/HelloSignalR/start_server.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SET ASPNETCORE_URLS=http://localhost:61327
dotnet run --no-launch-profile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HubConnection, IStreamResult } from "@microsoft/signalr"
import { HubConnection, IStreamResult } from '@microsoft/signalr';
{% for hub in Hubs -%}

{{ hub }}
Expand Down
21 changes: 17 additions & 4 deletions src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export class {{ Name }}Hub {
{% for operation in Callbacks -%}
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 -%}

Expand All @@ -18,15 +25,21 @@
{% endif -%}
{% endfor -%}

registerCallbacks(implementation: I{{ Name }}HubCallbacks) {
registerCallbacks(implementation: I{{ Name }}HubCallbacks): void {
this.unregisterCallbacks();

{% 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) {
unregisterCallbacks(): void {
{% 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 %}));
if (this.callbackFor{{ operation.Name }} !== undefined) {
this.connection.off('{{ operation.Name }}', this.callbackFor{{ operation.Name }});
this.callbackFor{{ operation.Name }} = undefined;
}
{% endfor -%}
}
}
Expand Down