Skip to content

Commit 132f8ec

Browse files
feat: split the events of the Manager and Socket
Previously, most of the events emitted by the Manager were also emitted by the Socket instances, but it was somehow misleading for the end users because they don't have the same meaning: - Manager: the state of the low-level connection (with connection and reconnection events) - Socket: the state of the connection to the Namespace (only 'connect', 'disconnect' and 'error') For example, the `reconnect` event: ```js socket.on("reconnect", () => { console.log(socket.connected); // might be false, which is a bit surprising }); ``` Breaking change: the Socket instance will no longer forward the events of its Manager Those events can still be accessed on the Manager instance though: ```js socket.io.on("reconnect", () => { // ... }); ```
1 parent 6cd2e4e commit 132f8ec

File tree

5 files changed

+52
-227
lines changed

5 files changed

+52
-227
lines changed

build/manager.d.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ export declare class Manager extends Emitter {
2727
* @api public
2828
*/
2929
constructor(uri: any, opts: any);
30-
/**
31-
* Propagate given event to sockets and emit on `this`
32-
*
33-
* @api private
34-
*/
35-
emitAll(event: string, arg?: any): void;
3630
/**
3731
* Sets the `reconnection` config.
3832
*
@@ -87,8 +81,8 @@ export declare class Manager extends Emitter {
8781
* @return {Manager} self
8882
* @api public
8983
*/
90-
open(fn?: any, opts?: any): this;
91-
connect(fn: any, opts: any): this;
84+
open(fn?: any, opts?: any): Manager;
85+
connect(fn: any, opts: any): Manager;
9286
/**
9387
* Called upon transport open.
9488
*

build/manager.js

Lines changed: 14 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,6 @@ class Manager extends component_emitter_1.default {
7676
if (this.autoConnect)
7777
this.open();
7878
}
79-
/**
80-
* Propagate given event to sockets and emit on `this`
81-
*
82-
* @api private
83-
*/
84-
emitAll(event, arg) {
85-
super.emit(event, arg);
86-
for (let nsp in this.nsps) {
87-
if (has.call(this.nsps, nsp)) {
88-
this.nsps[nsp].emit(event, arg);
89-
}
90-
}
91-
}
9279
/**
9380
* Sets the `reconnection` config.
9481
*
@@ -200,11 +187,11 @@ class Manager extends component_emitter_1.default {
200187
fn && fn();
201188
});
202189
// emit `connect_error`
203-
const errorSub = on_1.on(socket, "error", function (data) {
190+
const errorSub = on_1.on(socket, "error", (data) => {
204191
debug("connect_error");
205192
self.cleanup();
206193
self.readyState = "closed";
207-
self.emitAll("connect_error", data);
194+
super.emit("connect_error", data);
208195
if (fn) {
209196
const err = new Error("Connection error");
210197
// err.data = data;
@@ -223,12 +210,12 @@ class Manager extends component_emitter_1.default {
223210
openSub.destroy(); // prevents a race condition with the 'open' event
224211
}
225212
// set timer
226-
const timer = setTimeout(function () {
213+
const timer = setTimeout(() => {
227214
debug("connect attempt timed out after %d", timeout);
228215
openSub.destroy();
229216
socket.close();
230217
socket.emit("error", "timeout");
231-
self.emitAll("connect_timeout", timeout);
218+
super.emit("connect_timeout", "timeout");
232219
}, timeout);
233220
this.subs.push({
234221
destroy: function () {
@@ -241,60 +228,7 @@ class Manager extends component_emitter_1.default {
241228
return this;
242229
}
243230
connect(fn, opts) {
244-
debug("readyState %s", this.readyState);
245-
if (~this.readyState.indexOf("open"))
246-
return this;
247-
debug("opening %s", this.uri);
248-
this.engine = engine_io_client_1.default(this.uri, this.opts);
249-
const socket = this.engine;
250-
const self = this;
251-
this.readyState = "opening";
252-
this.skipReconnect = false;
253-
// emit `open`
254-
const openSub = on_1.on(socket, "open", function () {
255-
self.onopen();
256-
fn && fn();
257-
});
258-
// emit `connect_error`
259-
const errorSub = on_1.on(socket, "error", function (data) {
260-
debug("connect_error");
261-
self.cleanup();
262-
self.readyState = "closed";
263-
self.emitAll("connect_error", data);
264-
if (fn) {
265-
const err = new Error("Connection error");
266-
// err.data = data;
267-
fn(err);
268-
}
269-
else {
270-
// Only do this if there is no fn to handle the error
271-
self.maybeReconnectOnOpen();
272-
}
273-
});
274-
// emit `connect_timeout`
275-
if (false !== this._timeout) {
276-
const timeout = this._timeout;
277-
debug("connect attempt will timeout after %d", timeout);
278-
if (timeout === 0) {
279-
openSub.destroy(); // prevents a race condition with the 'open' event
280-
}
281-
// set timer
282-
const timer = setTimeout(function () {
283-
debug("connect attempt timed out after %d", timeout);
284-
openSub.destroy();
285-
socket.close();
286-
socket.emit("error", "timeout");
287-
self.emitAll("connect_timeout", timeout);
288-
}, timeout);
289-
this.subs.push({
290-
destroy: function () {
291-
clearTimeout(timer);
292-
},
293-
});
294-
}
295-
this.subs.push(openSub);
296-
this.subs.push(errorSub);
297-
return this;
231+
return this.open(fn, opts);
298232
}
299233
/**
300234
* Called upon transport open.
@@ -322,7 +256,7 @@ class Manager extends component_emitter_1.default {
322256
* @api private
323257
*/
324258
onping() {
325-
this.emitAll("ping");
259+
super.emit("ping");
326260
}
327261
/**
328262
* Called with data.
@@ -347,7 +281,7 @@ class Manager extends component_emitter_1.default {
347281
*/
348282
onerror(err) {
349283
debug("error", err);
350-
this.emitAll("error", err);
284+
super.emit("error", err);
351285
}
352286
/**
353287
* Creates a new socket for the given `nsp`.
@@ -476,28 +410,28 @@ class Manager extends component_emitter_1.default {
476410
if (this.backoff.attempts >= this._reconnectionAttempts) {
477411
debug("reconnect failed");
478412
this.backoff.reset();
479-
this.emitAll("reconnect_failed");
413+
super.emit("reconnect_failed");
480414
this.reconnecting = false;
481415
}
482416
else {
483417
const delay = this.backoff.duration();
484418
debug("will wait %dms before reconnect attempt", delay);
485419
this.reconnecting = true;
486-
const timer = setTimeout(function () {
420+
const timer = setTimeout(() => {
487421
if (self.skipReconnect)
488422
return;
489423
debug("attempting reconnect");
490-
self.emitAll("reconnect_attempt", self.backoff.attempts);
491-
self.emitAll("reconnecting", self.backoff.attempts);
424+
super.emit("reconnect_attempt", self.backoff.attempts);
425+
super.emit("reconnecting", self.backoff.attempts);
492426
// check again for the case socket closed in above events
493427
if (self.skipReconnect)
494428
return;
495-
self.open(function (err) {
429+
self.open((err) => {
496430
if (err) {
497431
debug("reconnect attempt error");
498432
self.reconnecting = false;
499433
self.reconnect();
500-
self.emitAll("reconnect_error", err.data);
434+
super.emit("reconnect_error", err.data);
501435
}
502436
else {
503437
debug("reconnect success");
@@ -521,7 +455,7 @@ class Manager extends component_emitter_1.default {
521455
const attempt = this.backoff.attempts;
522456
this.reconnecting = false;
523457
this.backoff.reset();
524-
this.emitAll("reconnect", attempt);
458+
super.emit("reconnect", attempt);
525459
}
526460
}
527461
exports.Manager = Manager;

lib/manager.ts

Lines changed: 16 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,6 @@ export class Manager extends Emitter {
7676
if (this.autoConnect) this.open();
7777
}
7878

79-
/**
80-
* Propagate given event to sockets and emit on `this`
81-
*
82-
* @api private
83-
*/
84-
emitAll(event: string, arg?) {
85-
super.emit(event, arg);
86-
for (let nsp in this.nsps) {
87-
if (has.call(this.nsps, nsp)) {
88-
this.nsps[nsp].emit(event, arg);
89-
}
90-
}
91-
}
92-
9379
/**
9480
* Sets the `reconnection` config.
9581
*
@@ -188,7 +174,7 @@ export class Manager extends Emitter {
188174
* @return {Manager} self
189175
* @api public
190176
*/
191-
open(fn?, opts?) {
177+
open(fn?, opts?): Manager {
192178
debug("readyState %s", this.readyState);
193179
if (~this.readyState.indexOf("open")) return this;
194180

@@ -206,11 +192,11 @@ export class Manager extends Emitter {
206192
});
207193

208194
// emit `connect_error`
209-
const errorSub = on(socket, "error", function (data) {
195+
const errorSub = on(socket, "error", (data) => {
210196
debug("connect_error");
211197
self.cleanup();
212198
self.readyState = "closed";
213-
self.emitAll("connect_error", data);
199+
super.emit("connect_error", data);
214200
if (fn) {
215201
const err = new Error("Connection error");
216202
// err.data = data;
@@ -231,12 +217,12 @@ export class Manager extends Emitter {
231217
}
232218

233219
// set timer
234-
const timer = setTimeout(function () {
220+
const timer = setTimeout(() => {
235221
debug("connect attempt timed out after %d", timeout);
236222
openSub.destroy();
237223
socket.close();
238224
socket.emit("error", "timeout");
239-
self.emitAll("connect_timeout", timeout);
225+
super.emit("connect_timeout", "timeout");
240226
}, timeout);
241227

242228
this.subs.push({
@@ -252,68 +238,8 @@ export class Manager extends Emitter {
252238
return this;
253239
}
254240

255-
connect(fn, opts) {
256-
debug("readyState %s", this.readyState);
257-
if (~this.readyState.indexOf("open")) return this;
258-
259-
debug("opening %s", this.uri);
260-
this.engine = eio(this.uri, this.opts);
261-
const socket = this.engine;
262-
const self = this;
263-
this.readyState = "opening";
264-
this.skipReconnect = false;
265-
266-
// emit `open`
267-
const openSub = on(socket, "open", function () {
268-
self.onopen();
269-
fn && fn();
270-
});
271-
272-
// emit `connect_error`
273-
const errorSub = on(socket, "error", function (data) {
274-
debug("connect_error");
275-
self.cleanup();
276-
self.readyState = "closed";
277-
self.emitAll("connect_error", data);
278-
if (fn) {
279-
const err = new Error("Connection error");
280-
// err.data = data;
281-
fn(err);
282-
} else {
283-
// Only do this if there is no fn to handle the error
284-
self.maybeReconnectOnOpen();
285-
}
286-
});
287-
288-
// emit `connect_timeout`
289-
if (false !== this._timeout) {
290-
const timeout = this._timeout;
291-
debug("connect attempt will timeout after %d", timeout);
292-
293-
if (timeout === 0) {
294-
openSub.destroy(); // prevents a race condition with the 'open' event
295-
}
296-
297-
// set timer
298-
const timer = setTimeout(function () {
299-
debug("connect attempt timed out after %d", timeout);
300-
openSub.destroy();
301-
socket.close();
302-
socket.emit("error", "timeout");
303-
self.emitAll("connect_timeout", timeout);
304-
}, timeout);
305-
306-
this.subs.push({
307-
destroy: function () {
308-
clearTimeout(timer);
309-
},
310-
});
311-
}
312-
313-
this.subs.push(openSub);
314-
this.subs.push(errorSub);
315-
316-
return this;
241+
connect(fn, opts): Manager {
242+
return this.open(fn, opts);
317243
}
318244

319245
/**
@@ -346,7 +272,7 @@ export class Manager extends Emitter {
346272
* @api private
347273
*/
348274
onping() {
349-
this.emitAll("ping");
275+
super.emit("ping");
350276
}
351277

352278
/**
@@ -374,7 +300,7 @@ export class Manager extends Emitter {
374300
*/
375301
onerror(err) {
376302
debug("error", err);
377-
this.emitAll("error", err);
303+
super.emit("error", err);
378304
}
379305

380306
/**
@@ -516,29 +442,29 @@ export class Manager extends Emitter {
516442
if (this.backoff.attempts >= this._reconnectionAttempts) {
517443
debug("reconnect failed");
518444
this.backoff.reset();
519-
this.emitAll("reconnect_failed");
445+
super.emit("reconnect_failed");
520446
this.reconnecting = false;
521447
} else {
522448
const delay = this.backoff.duration();
523449
debug("will wait %dms before reconnect attempt", delay);
524450

525451
this.reconnecting = true;
526-
const timer = setTimeout(function () {
452+
const timer = setTimeout(() => {
527453
if (self.skipReconnect) return;
528454

529455
debug("attempting reconnect");
530-
self.emitAll("reconnect_attempt", self.backoff.attempts);
531-
self.emitAll("reconnecting", self.backoff.attempts);
456+
super.emit("reconnect_attempt", self.backoff.attempts);
457+
super.emit("reconnecting", self.backoff.attempts);
532458

533459
// check again for the case socket closed in above events
534460
if (self.skipReconnect) return;
535461

536-
self.open(function (err) {
462+
self.open((err) => {
537463
if (err) {
538464
debug("reconnect attempt error");
539465
self.reconnecting = false;
540466
self.reconnect();
541-
self.emitAll("reconnect_error", err.data);
467+
super.emit("reconnect_error", err.data);
542468
} else {
543469
debug("reconnect success");
544470
self.onreconnect();
@@ -563,6 +489,6 @@ export class Manager extends Emitter {
563489
const attempt = this.backoff.attempts;
564490
this.reconnecting = false;
565491
this.backoff.reset();
566-
this.emitAll("reconnect", attempt);
492+
super.emit("reconnect", attempt);
567493
}
568494
}

0 commit comments

Comments
 (0)