Skip to content

Commit 969debe

Browse files
refactor: rework of the Manager events
- 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
1 parent a9127ce commit 969debe

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

lib/manager.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,12 @@ export class Manager extends Emitter {
457457
fn && fn();
458458
});
459459

460-
// emit `connect_error`
460+
// emit `error`
461461
const errorSub = on(socket, "error", (err) => {
462-
debug("connect_error");
462+
debug("error");
463463
self.cleanup();
464464
self._readyState = "closed";
465-
super.emit("connect_error", err);
465+
super.emit("error", err);
466466
if (fn) {
467467
fn(err);
468468
} else {
@@ -471,7 +471,6 @@ export class Manager extends Emitter {
471471
}
472472
});
473473

474-
// emit `connect_timeout`
475474
if (false !== this._timeout) {
476475
const timeout = this._timeout;
477476
debug("connect attempt will timeout after %d", timeout);
@@ -485,8 +484,7 @@ export class Manager extends Emitter {
485484
debug("connect attempt timed out after %d", timeout);
486485
openSub.destroy();
487486
socket.close();
488-
socket.emit("error", "timeout");
489-
super.emit("connect_error", new Error("timeout"));
487+
socket.emit("error", new Error("timeout"));
490488
}, timeout);
491489

492490
this.subs.push({
@@ -720,7 +718,6 @@ export class Manager extends Emitter {
720718

721719
debug("attempting reconnect");
722720
super.emit("reconnect_attempt", self.backoff.attempts);
723-
super.emit("reconnecting", self.backoff.attempts);
724721

725722
// check again for the case socket closed in above events
726723
if (self.skipReconnect) return;

test/connection.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ describe("connection", function () {
208208
let startTime;
209209
let prevDelay = 0;
210210

211-
manager.on("connect_error", () => {
211+
manager.on("error", () => {
212212
startTime = new Date().getTime();
213213
});
214214
manager.on("reconnect_attempt", () => {
@@ -236,7 +236,7 @@ describe("connection", function () {
236236
timeout: 0,
237237
reconnectionDelay: 10,
238238
});
239-
socket.io.once("connect_error", () => {
239+
socket.io.once("error", () => {
240240
socket.io.on("reconnect_attempt", () => {
241241
expect().fail();
242242
});
@@ -370,7 +370,7 @@ describe("connection", function () {
370370
expect(attempts).to.be(reconnects);
371371
};
372372

373-
manager.on("reconnecting", reconnectCb);
373+
manager.on("reconnect_attempt", reconnectCb);
374374
manager.on("reconnect_failed", () => {
375375
expect(reconnects).to.be(2);
376376
socket.close();
@@ -446,7 +446,7 @@ describe("connection", function () {
446446
};
447447
manager.on("reconnect_attempt", cb);
448448

449-
manager.on("connect_error", () => {
449+
manager.on("error", () => {
450450
// set a timeout to let reconnection possibly fire
451451
setTimeout(() => {
452452
socket.disconnect();

test/socket.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ describe("socket", function () {
3939
});
4040
});
4141

42-
it("doesn't fire a connect_error if we force disconnect in opening state", (done) => {
42+
it("doesn't fire an error event if we force disconnect in opening state", (done) => {
4343
const socket = io({ forceNew: true, timeout: 100 });
4444
socket.disconnect();
45-
socket.io.on("connect_error", () => {
45+
socket.io.on("error", () => {
4646
throw new Error("Unexpected");
4747
});
4848
setTimeout(() => {

0 commit comments

Comments
 (0)