Introducing async/await in TS client + minor clean up

This commit is contained in:
moozzyk 2016-12-21 17:01:36 -08:00 committed by moozzyk
parent b424e5b2b0
commit 3a01d6cff1
4 changed files with 24 additions and 27 deletions

View File

@ -22,7 +22,7 @@ export class Connection {
this.connectionState = ConnectionState.Disconnected; this.connectionState = ConnectionState.Disconnected;
} }
start(transportName: string = 'webSockets'): Promise<void> { async start(transportName: string = 'webSockets'): Promise<void> {
if (this.connectionState != ConnectionState.Disconnected) { if (this.connectionState != ConnectionState.Disconnected) {
throw new Error("Cannot start a connection that is not in the 'Disconnected' state"); throw new Error("Cannot start a connection that is not in the 'Disconnected' state");
} }
@ -31,21 +31,18 @@ export class Connection {
this.transport.onDataReceived = this.dataReceivedCallback; this.transport.onDataReceived = this.dataReceivedCallback;
this.transport.onError = e => this.stopConnection(); this.transport.onError = e => this.stopConnection();
return new HttpClient().get(`${this.url}/getid?${this.queryString}`) try {
.then(connectionId => { this.connectionId = await new HttpClient().get(`${this.url}/getid?${this.queryString}`);
this.connectionId = connectionId; this.queryString = `id=${this.connectionId}`;
this.queryString = `id=${connectionId}&${this.connectionId}`; await this.transport.connect(this.url, this.queryString);
return this.transport.connect(this.url, this.queryString); this.connectionState = ConnectionState.Connected;
}) }
.then(() => { catch(e) {
this.connectionState = ConnectionState.Connected; console.log("Failed to start the connection.")
}) this.connectionState = ConnectionState.Disconnected;
.catch(e => { this.transport = null;
console.log("Failed to start the connection.") throw e;
this.connectionState = ConnectionState.Disconnected; };
this.transport = null;
throw e;
});
} }
private createTransport(transportName: string): ITransport { private createTransport(transportName: string): ITransport {

View File

@ -8,7 +8,7 @@ export class HttpClient {
} }
private xhr(method: string, url: string, content?: string): Promise<string> { private xhr(method: string, url: string, content?: string): Promise<string> {
return new Promise((resolve, reject) => { return new Promise<string>((resolve, reject) => {
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.open(method, url, true); xhr.open(method, url, true);

View File

@ -16,8 +16,8 @@ export { Connection } from "./Connection"
export class HubConnection { export class HubConnection {
private connection: Connection; private connection: Connection;
private callbacks: Map<string, (any) => void>; private callbacks: Map<string, (invocationDescriptor: InvocationResultDescriptor) => void>;
private methods: Map<string, (...args:any[]) => void>; private methods: Map<string, (...args: any[]) => void>;
private id: number; private id: number;
constructor(url: string, queryString?: string) { constructor(url: string, queryString?: string) {
@ -28,13 +28,13 @@ export class HubConnection {
thisHubConnection.dataReceived(data); thisHubConnection.dataReceived(data);
}; };
this.callbacks = new Map<string, (any) => void>(); this.callbacks = new Map<string, (InvocationResultDescriptor) => void>();
this.methods = new Map<string, (...args:any[]) => void>(); this.methods = new Map<string, (...args: any[]) => void>();
this.id = 0; this.id = 0;
} }
private dataReceived(data: any) { private dataReceived(data: any) {
//TODO: separate JSON parsing // TODO: separate JSON parsing
// Can happen if a poll request was cancelled // Can happen if a poll request was cancelled
if (!data) { if (!data) {
return; return;
@ -105,4 +105,4 @@ export class HubConnection {
set connectionClosed(callback: ConnectionClosed) { set connectionClosed(callback: ConnectionClosed) {
this.connection.connectionClosed = callback; this.connection.connectionClosed = callback;
} }
} }

View File

@ -112,8 +112,8 @@ export class ServerSentEventsTransport implements ITransport {
}); });
} }
send(data: any): Promise<void> { async send(data: any): Promise<void> {
return new HttpClient().post(this.url + "/send?" + this.queryString, data); await new HttpClient().post(this.url + "/send?" + this.queryString, data);
} }
stop(): void { stop(): void {
@ -189,8 +189,8 @@ export class LongPollingTransport implements ITransport {
this.pollXhr.send(); this.pollXhr.send();
} }
send(data: any): Promise<void> { async send(data: any): Promise<void> {
return new HttpClient().post(this.url + "/send?" + this.queryString, data); await new HttpClient().post(this.url + "/send?" + this.queryString, data);
} }
stop(): void { stop(): void {