From b32d3ad4e18dd314aca32bbb53b928e298f3cf04 Mon Sep 17 00:00:00 2001 From: Dan Kirkham Date: Tue, 31 Jan 2017 21:47:14 +0000 Subject: [PATCH] Removed explicit capturing of "this" for lamdas (#167) * Removed explicit capturing of "this" for lamdas * Capture current Id when invoking connection --- .../HubConnection.ts | 4 +- .../Transports.ts | 41 +++++++++---------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts b/src/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts index c637efd54f..8aecca3e28 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts +++ b/src/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts @@ -24,9 +24,8 @@ export class HubConnection { constructor(url: string, queryString?: string) { this.connection = new Connection(url, queryString); - let thisHubConnection = this; this.connection.dataReceived = data => { - thisHubConnection.dataReceived(data); + this.dataReceived(data); }; this.callbacks = new Map void>(); @@ -68,7 +67,6 @@ export class HubConnection { } invoke(methodName: string, ...args: any[]): Promise { - let id = this.id; this.id++; diff --git a/src/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts b/src/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts index 6cfd03d76f..56b2d767e3 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts +++ b/src/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts @@ -18,11 +18,10 @@ export class WebSocketTransport implements ITransport { let connectUrl = url + "/ws?" + queryString; let webSocket = new WebSocket(connectUrl); - let thisWebSocketTransport = this; webSocket.onopen = (event: Event) => { console.log(`WebSocket connected to ${connectUrl}`); - thisWebSocketTransport.webSocket = webSocket; + this.webSocket = webSocket; resolve(); }; @@ -32,16 +31,16 @@ export class WebSocketTransport implements ITransport { webSocket.onmessage = (message: MessageEvent) => { console.log(`(WebSockets transport) data received: ${message.data}`); - if (thisWebSocketTransport.onDataReceived) { - thisWebSocketTransport.onDataReceived(message.data); + if (this.onDataReceived) { + this.onDataReceived(message.data); } } webSocket.onclose = (event: CloseEvent) => { // webSocket will be null if the transport did not start successfully - if (thisWebSocketTransport.webSocket && (event.wasClean === false || event.code !== 1000)) { - if (thisWebSocketTransport.onError) { - thisWebSocketTransport.onError(event); + if (this.webSocket && (event.wasClean === false || event.code !== 1000)) { + if (this.onError) { + this.onError(event); } } } @@ -91,10 +90,9 @@ export class ServerSentEventsTransport implements ITransport { let eventSource = new EventSource(`${this.url}/sse?${this.queryString}`); try { - let thisEventSourceTransport = this; eventSource.onmessage = (e: MessageEvent) => { - if (thisEventSourceTransport.onDataReceived) { - thisEventSourceTransport.onDataReceived(e.data); + if (this.onDataReceived) { + this.onDataReceived(e.data); } }; @@ -102,13 +100,13 @@ export class ServerSentEventsTransport implements ITransport { reject(); // don't report an error if the transport did not start successfully - if (thisEventSourceTransport.eventSource && thisEventSourceTransport.onError) { - thisEventSourceTransport.onError(e); + if (this.eventSource && this.onError) { + this.onError(e); } } eventSource.onopen = () => { - thisEventSourceTransport.eventSource = eventSource; + this.eventSource = eventSource; resolve(); } } @@ -157,22 +155,21 @@ export class LongPollingTransport implements ITransport { return; } - let thisLongPollingTransport = this; let pollXhr = new XMLHttpRequest(); pollXhr.onload = () => { if (pollXhr.status == 200) { - if (thisLongPollingTransport.onDataReceived) { - thisLongPollingTransport.onDataReceived(pollXhr.response); + if (this.onDataReceived) { + this.onDataReceived(pollXhr.response); } - thisLongPollingTransport.poll(url); + this.poll(url); } else if (this.pollXhr.status == 204) { // TODO: closed event? } else { - if (thisLongPollingTransport.onError) { - thisLongPollingTransport.onError({ + if (this.onError) { + this.onError({ status: pollXhr.status, statusText: pollXhr.statusText }); @@ -181,8 +178,8 @@ export class LongPollingTransport implements ITransport { }; pollXhr.onerror = () => { - if (thisLongPollingTransport.onError) { - thisLongPollingTransport.onError({ + if (this.onError) { + this.onError({ status: pollXhr.status, statusText: pollXhr.statusText }); @@ -190,7 +187,7 @@ export class LongPollingTransport implements ITransport { }; pollXhr.ontimeout = () => { - thisLongPollingTransport.poll(url); + this.poll(url); } this.pollXhr = pollXhr;