Add ability to remove all handlers with just the method name (#1729)

This commit is contained in:
Mikael Mengistu 2018-03-27 14:06:56 -07:00 committed by GitHub
parent bc91191876
commit 3952eacfaa
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."]); 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 () => { it("callback invoked when servers invokes a method on the client", async () => {
const connection = new TestConnection(); const connection = new TestConnection();
const hubConnection = new HubConnection(connection, commonOptions); const hubConnection = new HubConnection(connection, commonOptions);

View File

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