Skip to content

Commit

Permalink
feat: add support for catch-all listeners
Browse files Browse the repository at this point in the history
Inspired from EventEmitter2 [1]

The API is similar to the one on the server-side:

```js
socket.onAny((event, ...args) => {});

socket.prependAny((event, ...args) => {});

socket.offAny(); // remove all listeners

socket.offAny(listener);

const listeners = socket.listenersAny();
```

[1]: https://github.com/EventEmitter2/EventEmitter2
  • Loading branch information
darrachequesne committed Oct 26, 2020
1 parent 71d6048 commit 55f464f
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
75 changes: 73 additions & 2 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class Socket extends Emitter {
private sendBuffer: Array<any> = [];
private flags: Flags = {};
private subs: Array<any>;
private _anyListeners: Array<(...args: any[]) => void>;

/**
* `Socket` constructor.
Expand Down Expand Up @@ -272,12 +273,22 @@ export class Socket extends Emitter {
}

if (this.connected) {
super.emit.apply(this, args);
this.emitEvent(args);
} else {
this.receiveBuffer.push(args);
}
}

private emitEvent(args) {
if (this._anyListeners && this._anyListeners.length) {
const listeners = this._anyListeners.slice();
for (const listener of listeners) {
listener.apply(this, args);
}
}
super.emit.apply(this, args);
}

/**
* Produces an ack callback to emit with an event.
*
Expand Down Expand Up @@ -337,7 +348,7 @@ export class Socket extends Emitter {
*/
private emitBuffered() {
for (let i = 0; i < this.receiveBuffer.length; i++) {
super.emit.apply(this, this.receiveBuffer[i]);
this.emitEvent(this.receiveBuffer[i]);
}
this.receiveBuffer = [];

Expand Down Expand Up @@ -432,4 +443,64 @@ export class Socket extends Emitter {
this.flags.volatile = true;
return this;
}

/**
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
* callback.
*
* @param listener
* @public
*/
public onAny(listener: (...args: any[]) => void): Socket {
this._anyListeners = this._anyListeners || [];
this._anyListeners.push(listener);
return this;
}

/**
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
* callback. The listener is added to the beginning of the listeners array.
*
* @param listener
* @public
*/
public prependAny(listener: (...args: any[]) => void): Socket {
this._anyListeners = this._anyListeners || [];
this._anyListeners.unshift(listener);
return this;
}

/**
* Removes the listener that will be fired when any event is emitted.
*
* @param listener
* @public
*/
public offAny(listener?: (...args: any[]) => void): Socket {
if (!this._anyListeners) {
return this;
}
if (listener) {
const listeners = this._anyListeners;
for (let i = 0; i < listeners.length; i++) {
if (listener === listeners[i]) {
listeners.splice(i, 1);
return this;
}
}
} else {
this._anyListeners = [];
}
return this;
}

/**
* Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
* e.g. to remove listeners.
*
* @public
*/
public listenersAny() {
return this._anyListeners || [];
}
}
47 changes: 47 additions & 0 deletions test/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,51 @@ describe("socket", function () {
}, 200);
});
});

describe("onAny", () => {
it("should call listener", (done) => {
const socket = io("/abc");

socket.onAny((event, arg1) => {
expect(event).to.be("handshake");
expect(arg1).to.be.an(Object);
done();
});
});

it("should prepend listener", (done) => {
const socket = io("/abc");

let count = 0;

socket.onAny((event, arg1) => {
expect(count).to.be(2);
done();
});

socket.prependAny(() => {
expect(count++).to.be(1);
});

socket.prependAny(() => {
expect(count++).to.be(0);
});
});

it("should remove listener", (done) => {
const socket = io("/abc");

let count = 0;

const fail = () => done(new Error("fail"));

socket.onAny(fail);
socket.offAny(fail);
expect(socket.listenersAny.length).to.be(0);

socket.onAny(() => {
done();
});
});
});
});

0 comments on commit 55f464f

Please sign in to comment.