Removed explicit capturing of "this" for lamdas (#167)
* Removed explicit capturing of "this" for lamdas * Capture current Id when invoking connection
This commit is contained in:
parent
eafbe74160
commit
b32d3ad4e1
|
|
@ -24,9 +24,8 @@ export class HubConnection {
|
||||||
constructor(url: string, queryString?: string) {
|
constructor(url: string, queryString?: string) {
|
||||||
this.connection = new Connection(url, queryString);
|
this.connection = new Connection(url, queryString);
|
||||||
|
|
||||||
let thisHubConnection = this;
|
|
||||||
this.connection.dataReceived = data => {
|
this.connection.dataReceived = data => {
|
||||||
thisHubConnection.dataReceived(data);
|
this.dataReceived(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.callbacks = new Map<string, (invocationDescriptor: InvocationResultDescriptor) => void>();
|
this.callbacks = new Map<string, (invocationDescriptor: InvocationResultDescriptor) => void>();
|
||||||
|
|
@ -68,7 +67,6 @@ export class HubConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
invoke(methodName: string, ...args: any[]): Promise<any> {
|
invoke(methodName: string, ...args: any[]): Promise<any> {
|
||||||
|
|
||||||
let id = this.id;
|
let id = this.id;
|
||||||
this.id++;
|
this.id++;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,10 @@ export class WebSocketTransport implements ITransport {
|
||||||
let connectUrl = url + "/ws?" + queryString;
|
let connectUrl = url + "/ws?" + queryString;
|
||||||
|
|
||||||
let webSocket = new WebSocket(connectUrl);
|
let webSocket = new WebSocket(connectUrl);
|
||||||
let thisWebSocketTransport = this;
|
|
||||||
|
|
||||||
webSocket.onopen = (event: Event) => {
|
webSocket.onopen = (event: Event) => {
|
||||||
console.log(`WebSocket connected to ${connectUrl}`);
|
console.log(`WebSocket connected to ${connectUrl}`);
|
||||||
thisWebSocketTransport.webSocket = webSocket;
|
this.webSocket = webSocket;
|
||||||
resolve();
|
resolve();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -32,16 +31,16 @@ export class WebSocketTransport implements ITransport {
|
||||||
|
|
||||||
webSocket.onmessage = (message: MessageEvent) => {
|
webSocket.onmessage = (message: MessageEvent) => {
|
||||||
console.log(`(WebSockets transport) data received: ${message.data}`);
|
console.log(`(WebSockets transport) data received: ${message.data}`);
|
||||||
if (thisWebSocketTransport.onDataReceived) {
|
if (this.onDataReceived) {
|
||||||
thisWebSocketTransport.onDataReceived(message.data);
|
this.onDataReceived(message.data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
webSocket.onclose = (event: CloseEvent) => {
|
webSocket.onclose = (event: CloseEvent) => {
|
||||||
// webSocket will be null if the transport did not start successfully
|
// webSocket will be null if the transport did not start successfully
|
||||||
if (thisWebSocketTransport.webSocket && (event.wasClean === false || event.code !== 1000)) {
|
if (this.webSocket && (event.wasClean === false || event.code !== 1000)) {
|
||||||
if (thisWebSocketTransport.onError) {
|
if (this.onError) {
|
||||||
thisWebSocketTransport.onError(event);
|
this.onError(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -91,10 +90,9 @@ export class ServerSentEventsTransport implements ITransport {
|
||||||
let eventSource = new EventSource(`${this.url}/sse?${this.queryString}`);
|
let eventSource = new EventSource(`${this.url}/sse?${this.queryString}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let thisEventSourceTransport = this;
|
|
||||||
eventSource.onmessage = (e: MessageEvent) => {
|
eventSource.onmessage = (e: MessageEvent) => {
|
||||||
if (thisEventSourceTransport.onDataReceived) {
|
if (this.onDataReceived) {
|
||||||
thisEventSourceTransport.onDataReceived(e.data);
|
this.onDataReceived(e.data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -102,13 +100,13 @@ export class ServerSentEventsTransport implements ITransport {
|
||||||
reject();
|
reject();
|
||||||
|
|
||||||
// don't report an error if the transport did not start successfully
|
// don't report an error if the transport did not start successfully
|
||||||
if (thisEventSourceTransport.eventSource && thisEventSourceTransport.onError) {
|
if (this.eventSource && this.onError) {
|
||||||
thisEventSourceTransport.onError(e);
|
this.onError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
eventSource.onopen = () => {
|
eventSource.onopen = () => {
|
||||||
thisEventSourceTransport.eventSource = eventSource;
|
this.eventSource = eventSource;
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -157,22 +155,21 @@ export class LongPollingTransport implements ITransport {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let thisLongPollingTransport = this;
|
|
||||||
let pollXhr = new XMLHttpRequest();
|
let pollXhr = new XMLHttpRequest();
|
||||||
|
|
||||||
pollXhr.onload = () => {
|
pollXhr.onload = () => {
|
||||||
if (pollXhr.status == 200) {
|
if (pollXhr.status == 200) {
|
||||||
if (thisLongPollingTransport.onDataReceived) {
|
if (this.onDataReceived) {
|
||||||
thisLongPollingTransport.onDataReceived(pollXhr.response);
|
this.onDataReceived(pollXhr.response);
|
||||||
}
|
}
|
||||||
thisLongPollingTransport.poll(url);
|
this.poll(url);
|
||||||
}
|
}
|
||||||
else if (this.pollXhr.status == 204) {
|
else if (this.pollXhr.status == 204) {
|
||||||
// TODO: closed event?
|
// TODO: closed event?
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (thisLongPollingTransport.onError) {
|
if (this.onError) {
|
||||||
thisLongPollingTransport.onError({
|
this.onError({
|
||||||
status: pollXhr.status,
|
status: pollXhr.status,
|
||||||
statusText: pollXhr.statusText
|
statusText: pollXhr.statusText
|
||||||
});
|
});
|
||||||
|
|
@ -181,8 +178,8 @@ export class LongPollingTransport implements ITransport {
|
||||||
};
|
};
|
||||||
|
|
||||||
pollXhr.onerror = () => {
|
pollXhr.onerror = () => {
|
||||||
if (thisLongPollingTransport.onError) {
|
if (this.onError) {
|
||||||
thisLongPollingTransport.onError({
|
this.onError({
|
||||||
status: pollXhr.status,
|
status: pollXhr.status,
|
||||||
statusText: pollXhr.statusText
|
statusText: pollXhr.statusText
|
||||||
});
|
});
|
||||||
|
|
@ -190,7 +187,7 @@ export class LongPollingTransport implements ITransport {
|
||||||
};
|
};
|
||||||
|
|
||||||
pollXhr.ontimeout = () => {
|
pollXhr.ontimeout = () => {
|
||||||
thisLongPollingTransport.poll(url);
|
this.poll(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.pollXhr = pollXhr;
|
this.pollXhr = pollXhr;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue