Merge pull request #1738 from aspnet/release/2.1

This commit is contained in:
Mikael Mengistu 2018-03-27 14:08:55 -07:00 committed by GitHub
commit 1fdd6511fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 108 additions and 10 deletions

View File

@ -271,6 +271,94 @@ describe("HubConnection", () => {
expect(warnings).toEqual(["No client method with the name 'message' found."]);
});
it("all handlers can be unregistered with just the method name", async () => {
const connection = new TestConnection();
const hubConnection = new HubConnection(connection);
connection.receiveHandshakeResponse();
let count = 0;
const handler = () => { count++; };
const secondHandler = () => { count++; };
hubConnection.on("inc", handler);
hubConnection.on("inc", secondHandler);
connection.receive({
arguments: [],
invocationId: "0",
nonblocking: true,
target: "inc",
type: MessageType.Invocation,
});
hubConnection.off("inc");
connection.receive({
arguments: [],
invocationId: "0",
nonblocking: true,
target: "inc",
type: MessageType.Invocation,
});
expect(count).toBe(2);
});
it("a single handler can be unregistered with the method name and handler", async () => {
const connection = new TestConnection();
const hubConnection = new HubConnection(connection);
connection.receiveHandshakeResponse();
let count = 0;
const handler = () => { count++; };
const secondHandler = () => { count++; };
hubConnection.on("inc", handler);
hubConnection.on("inc", secondHandler);
connection.receive({
arguments: [],
invocationId: "0",
nonblocking: true,
target: "inc",
type: MessageType.Invocation,
});
hubConnection.off("inc", handler);
connection.receive({
arguments: [],
invocationId: "0",
nonblocking: true,
target: "inc",
type: MessageType.Invocation,
});
expect(count).toBe(3);
});
it("can't register the same handler multiple times", async () => {
const connection = new TestConnection();
const hubConnection = new HubConnection(connection);
connection.receiveHandshakeResponse();
let count = 0;
const handler = () => { count++; };
hubConnection.on("inc", handler);
hubConnection.on("inc", handler);
connection.receive({
arguments: [],
invocationId: "0",
nonblocking: true,
target: "inc",
type: MessageType.Invocation,
});
expect(count).toBe(1);
});
it("callback invoked when servers invokes a method on the client", async () => {
const connection = new TestConnection();
const hubConnection = new HubConnection(connection, commonOptions);

View File

@ -306,8 +306,8 @@ export class HubConnection {
return p;
}
public on(methodName: string, method: (...args: any[]) => void) {
if (!methodName || !method) {
public on(methodName: string, newMethod: (...args: any[]) => void) {
if (!methodName || !newMethod) {
return;
}
@ -316,11 +316,16 @@ export class HubConnection {
this.methods[methodName] = [];
}
this.methods[methodName].push(method);
// Preventing adding the same handler multiple times.
if (this.methods[methodName].indexOf(newMethod) !== -1) {
return;
}
this.methods[methodName].push(newMethod);
}
public off(methodName: string, method: (...args: any[]) => void) {
if (!methodName || !method) {
public off(methodName: string, method?: (...args: any[]) => void) {
if (!methodName) {
return;
}
@ -329,13 +334,18 @@ export class HubConnection {
if (!handlers) {
return;
}
const removeIdx = handlers.indexOf(method);
if (removeIdx !== -1) {
handlers.splice(removeIdx, 1);
if (handlers.length === 0) {
delete this.methods[methodName];
if (method) {
const removeIdx = handlers.indexOf(method);
if (removeIdx !== -1) {
handlers.splice(removeIdx, 1);
if (handlers.length === 0) {
delete this.methods[methodName];
}
}
} else {
delete this.methods[methodName];
}
}
public onclose(callback: ConnectionClosed) {