From a4aecbcd3578c1cf4ecffb3a58fb6d26e15ee513 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Wed, 27 Nov 2019 08:48:54 -0800 Subject: [PATCH] fix(transport-commons): Allow to properly chain SocketIo client.off (#1706) --- packages/transport-commons/src/client.ts | 4 +++- packages/transport-commons/test/client.test.ts | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/transport-commons/src/client.ts b/packages/transport-commons/src/client.ts index adcdb8cdfa..6aec89790d 100644 --- a/packages/transport-commons/src/client.ts +++ b/packages/transport-commons/src/client.ts @@ -130,7 +130,9 @@ export class Service { // of the emitter-component Socket.io is using off (name: string, ...args: any[]) { if (typeof this.connection.off === 'function') { - return this.connection.off(`${this.path} ${name}`, ...args); + const result = this.connection.off(`${this.path} ${name}`, ...args); + + return result === this.connection ? this : result; } else if (args.length === 0) { // @ts-ignore return this.removeAllListeners(name); diff --git a/packages/transport-commons/test/client.test.ts b/packages/transport-commons/test/client.test.ts index 7a9e21f05f..a5d11af48f 100644 --- a/packages/transport-commons/test/client.test.ts +++ b/packages/transport-commons/test/client.test.ts @@ -204,14 +204,15 @@ describe('client', () => { connection.emit('todos test', testing); }); - it('forwards namespaced call to .off', done => { + it('forwards namespaced call to .off, returns service instance', () => { // Use it's own connection and service so off method gets detected const conn = new EventEmitter(); // @ts-ignore - conn.off = name => { + conn.off = function (name) { assert.strictEqual(name, 'todos test'); - done(); + + return this; }; const client = new Service({ @@ -221,6 +222,6 @@ describe('client', () => { connection: conn }); - client.off('test'); + assert.strictEqual(client.off('test'), client); }); });