Add state to TS HubConnection (#2268)

This commit is contained in:
James Newton-King 2018-05-16 08:43:08 +12:00 committed by GitHub
parent 2e0057e477
commit 43aa392dff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 3 deletions

View File

@ -10,6 +10,14 @@ import { Arg, Subject } from "./Utils";
const DEFAULT_TIMEOUT_IN_MS: number = 30 * 1000; const DEFAULT_TIMEOUT_IN_MS: number = 30 * 1000;
/** Describes the current state of the {@link HubConnection} to the server. */
export enum HubConnectionState {
/** The hub connection is disconnected. */
Disconnected,
/** The hub connection is connected. */
Connected,
}
/** Represents a connection to a SignalR Hub. */ /** Represents a connection to a SignalR Hub. */
export class HubConnection { export class HubConnection {
private readonly connection: IConnection; private readonly connection: IConnection;
@ -22,6 +30,7 @@ export class HubConnection {
private closedCallbacks: Array<(error?: Error) => void>; private closedCallbacks: Array<(error?: Error) => void>;
private timeoutHandle: NodeJS.Timer; private timeoutHandle: NodeJS.Timer;
private receivedHandshakeResponse: boolean; private receivedHandshakeResponse: boolean;
private connectionState: HubConnectionState;
/** The server timeout in milliseconds. /** The server timeout in milliseconds.
* *
@ -58,6 +67,12 @@ export class HubConnection {
this.methods = {}; this.methods = {};
this.closedCallbacks = []; this.closedCallbacks = [];
this.id = 0; this.id = 0;
this.connectionState = HubConnectionState.Disconnected;
}
/** Indicates the state of the {@link HubConnection} to the server. */
get state(): HubConnectionState {
return this.connectionState;
} }
/** Starts the connection. /** Starts the connection.
@ -85,6 +100,8 @@ export class HubConnection {
// defensively cleanup timeout in case we receive a message from the server before we finish start // defensively cleanup timeout in case we receive a message from the server before we finish start
this.cleanupTimeout(); this.cleanupTimeout();
this.configureTimeout(); this.configureTimeout();
this.connectionState = HubConnectionState.Connected;
} }
/** Stops the connection. /** Stops the connection.
@ -380,6 +397,8 @@ export class HubConnection {
const callbacks = this.callbacks; const callbacks = this.callbacks;
this.callbacks = {}; this.callbacks = {};
this.connectionState = HubConnectionState.Disconnected;
Object.keys(callbacks) Object.keys(callbacks)
.forEach((key) => { .forEach((key) => {
const callback = callbacks[key]; const callback = callbacks[key];

View File

@ -10,7 +10,7 @@ export { AbortSignal } from "./AbortController";
export { HttpError, TimeoutError } from "./Errors"; export { HttpError, TimeoutError } from "./Errors";
export { DefaultHttpClient, HttpClient, HttpRequest, HttpResponse } from "./HttpClient"; export { DefaultHttpClient, HttpClient, HttpRequest, HttpResponse } from "./HttpClient";
export { IHttpConnectionOptions } from "./IHttpConnectionOptions"; export { IHttpConnectionOptions } from "./IHttpConnectionOptions";
export { HubConnection } from "./HubConnection"; export { HubConnection, HubConnectionState } from "./HubConnection";
export { HubConnectionBuilder } from "./HubConnectionBuilder"; export { HubConnectionBuilder } from "./HubConnectionBuilder";
export { MessageType, MessageHeaders, HubMessage, HubMessageBase, HubInvocationMessage, InvocationMessage, StreamInvocationMessage, StreamItemMessage, CompletionMessage, PingMessage, CloseMessage, CancelInvocationMessage, IHubProtocol } from "./IHubProtocol"; export { MessageType, MessageHeaders, HubMessage, HubMessageBase, HubInvocationMessage, InvocationMessage, StreamInvocationMessage, StreamItemMessage, CompletionMessage, PingMessage, CloseMessage, CancelInvocationMessage, IHubProtocol } from "./IHubProtocol";
export { ILogger, LogLevel } from "./ILogger"; export { ILogger, LogLevel } from "./ILogger";

View File

@ -1,7 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
import { HubConnection } from "../src/HubConnection"; import { HubConnection, HubConnectionState } from "../src/HubConnection";
import { IConnection } from "../src/IConnection"; import { IConnection } from "../src/IConnection";
import { HubMessage, IHubProtocol, MessageType } from "../src/IHubProtocol"; import { HubMessage, IHubProtocol, MessageType } from "../src/IHubProtocol";
import { ILogger, LogLevel } from "../src/ILogger"; import { ILogger, LogLevel } from "../src/ILogger";
@ -34,6 +34,33 @@ describe("HubConnection", () => {
await hubConnection.stop(); await hubConnection.stop();
} }
}); });
it("state connected", async () => {
const connection = new TestConnection();
const hubConnection = createHubConnection(connection);
expect(hubConnection.state).toBe(HubConnectionState.Disconnected);
try {
await hubConnection.start();
expect(hubConnection.state).toBe(HubConnectionState.Connected);
} finally {
await hubConnection.stop();
}
});
});
describe("stop", () => {
it("state disconnected", async () => {
const connection = new TestConnection();
const hubConnection = createHubConnection(connection);
expect(hubConnection.state).toBe(HubConnectionState.Disconnected);
try {
await hubConnection.start();
expect(hubConnection.state).toBe(HubConnectionState.Connected);
} finally {
await hubConnection.stop();
expect(hubConnection.state).toBe(HubConnectionState.Disconnected);
}
});
}); });
describe("send", () => { describe("send", () => {
@ -834,6 +861,21 @@ describe("HubConnection", () => {
hubConnection.stop(); hubConnection.stop();
} }
}); });
it("state disconnected", async () => {
const connection = new TestConnection();
const hubConnection = createHubConnection(connection);
try {
let state: HubConnectionState;
hubConnection.onclose((e) => state = hubConnection.state);
// Typically this would be called by the transport
connection.onclose();
expect(state).toBe(HubConnectionState.Disconnected);
} finally {
hubConnection.stop();
}
});
}); });
describe("keepAlive", () => { describe("keepAlive", () => {

View File

@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.SignalR.Client
/// </summary> /// </summary>
Disconnected, Disconnected,
/// <summary> /// <summary>
/// The hub connection is open. /// The hub connection is connected.
/// </summary> /// </summary>
Connected Connected
} }