Skip to content

Commit

Permalink
refactor: rework of the Manager events
Browse files Browse the repository at this point in the history
- rename "connect_error" to "error"
- remove "reconnecting" (duplicate of "reconnect_attempt")

The updated list of events emitted by the Manager:

- open:                successful (re)connection
- error:               (re)connection failure (previously: "connect_error") or error after a successful connection
- close:               disconnection

- ping:                ping packet
- packet:              data packet

- reconnect_attempt:   reconnection attempt (previously: "reconnect_attempt" & "reconnecting")
- reconnect:           successful reconnection
- reconnect_error:     reconnection failure
- reconnect_failed:    reconnection failure after all attempts

For reference, the Socket instance emits the following events:

- connect:             successful connection to a Namespace
- connect_error:       connection failure
- disconnect:          disconnection
  • Loading branch information
darrachequesne committed Oct 27, 2020
1 parent a9127ce commit 969debe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
11 changes: 4 additions & 7 deletions lib/manager.ts
Expand Up @@ -457,12 +457,12 @@ export class Manager extends Emitter {
fn && fn();
});

// emit `connect_error`
// emit `error`
const errorSub = on(socket, "error", (err) => {
debug("connect_error");
debug("error");
self.cleanup();
self._readyState = "closed";
super.emit("connect_error", err);
super.emit("error", err);
if (fn) {
fn(err);
} else {
Expand All @@ -471,7 +471,6 @@ export class Manager extends Emitter {
}
});

// emit `connect_timeout`
if (false !== this._timeout) {
const timeout = this._timeout;
debug("connect attempt will timeout after %d", timeout);
Expand All @@ -485,8 +484,7 @@ export class Manager extends Emitter {
debug("connect attempt timed out after %d", timeout);
openSub.destroy();
socket.close();
socket.emit("error", "timeout");
super.emit("connect_error", new Error("timeout"));
socket.emit("error", new Error("timeout"));
}, timeout);

this.subs.push({
Expand Down Expand Up @@ -720,7 +718,6 @@ export class Manager extends Emitter {

debug("attempting reconnect");
super.emit("reconnect_attempt", self.backoff.attempts);
super.emit("reconnecting", self.backoff.attempts);

// check again for the case socket closed in above events
if (self.skipReconnect) return;
Expand Down
8 changes: 4 additions & 4 deletions test/connection.ts
Expand Up @@ -208,7 +208,7 @@ describe("connection", function () {
let startTime;
let prevDelay = 0;

manager.on("connect_error", () => {
manager.on("error", () => {
startTime = new Date().getTime();
});
manager.on("reconnect_attempt", () => {
Expand Down Expand Up @@ -236,7 +236,7 @@ describe("connection", function () {
timeout: 0,
reconnectionDelay: 10,
});
socket.io.once("connect_error", () => {
socket.io.once("error", () => {
socket.io.on("reconnect_attempt", () => {
expect().fail();
});
Expand Down Expand Up @@ -370,7 +370,7 @@ describe("connection", function () {
expect(attempts).to.be(reconnects);
};

manager.on("reconnecting", reconnectCb);
manager.on("reconnect_attempt", reconnectCb);
manager.on("reconnect_failed", () => {
expect(reconnects).to.be(2);
socket.close();
Expand Down Expand Up @@ -446,7 +446,7 @@ describe("connection", function () {
};
manager.on("reconnect_attempt", cb);

manager.on("connect_error", () => {
manager.on("error", () => {
// set a timeout to let reconnection possibly fire
setTimeout(() => {
socket.disconnect();
Expand Down
4 changes: 2 additions & 2 deletions test/socket.ts
Expand Up @@ -39,10 +39,10 @@ describe("socket", function () {
});
});

it("doesn't fire a connect_error if we force disconnect in opening state", (done) => {
it("doesn't fire an error event if we force disconnect in opening state", (done) => {
const socket = io({ forceNew: true, timeout: 100 });
socket.disconnect();
socket.io.on("connect_error", () => {
socket.io.on("error", () => {
throw new Error("Unexpected");
});
setTimeout(() => {
Expand Down

0 comments on commit 969debe

Please sign in to comment.