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;
}
start(transportName: string = 'webSockets'): Promise<void> {
async start(transportName: string = 'webSockets'): Promise<void> {
if (this.connectionState != ConnectionState.Disconnected) {
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.onError = e => this.stopConnection();
return new HttpClient().get(`${this.url}/getid?${this.queryString}`)
.then(connectionId => {
this.connectionId = connectionId;
this.queryString = `id=${connectionId}&${this.connectionId}`;
return this.transport.connect(this.url, this.queryString);
})
.then(() => {
this.connectionState = ConnectionState.Connected;
})
.catch(e => {
console.log("Failed to start the connection.")
this.connectionState = ConnectionState.Disconnected;
this.transport = null;
throw e;
});
try {
this.connectionId = await new HttpClient().get(`${this.url}/getid?${this.queryString}`);
this.queryString = `id=${this.connectionId}`;
await this.transport.connect(this.url, this.queryString);
this.connectionState = ConnectionState.Connected;
}
catch(e) {
console.log("Failed to start the connection.")
this.connectionState = ConnectionState.Disconnected;
this.transport = null;
throw e;
};
}
private createTransport(transportName: string): ITransport {

View File

@ -8,7 +8,7 @@ export class HttpClient {
}
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();
xhr.open(method, url, true);

View File

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

View File

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