Skip to content

Commit

Permalink
refactor: simplify transport creation
Browse files Browse the repository at this point in the history
The try/catch clause was needed for the JSONP transport, which was
removed in [1].

[1]: b2c7381
  • Loading branch information
darrachequesne committed May 20, 2024
1 parent 68f9e0d commit 2c1851d
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,34 +448,23 @@ export class Socket extends Emitter<
* @private
*/
private open() {
let transport;
if (
this.opts.rememberUpgrade &&
Socket.priorWebsocketSuccess &&
this.transports.indexOf("websocket") !== -1
) {
transport = "websocket";
} else if (0 === this.transports.length) {
if (this.transports.length === 0) {
// Emit error on next tick so it can be listened to
this.setTimeoutFn(() => {
this.emitReserved("error", "No transports available");
}, 0);
return;
} else {
transport = this.transports[0];
}
this.readyState = "opening";

// Retry with the next transport if the transport is disabled (jsonp: false)
try {
transport = this.createTransport(transport);
} catch (e) {
debug("error while creating transport: %s", e);
this.transports.shift();
this.open();
return;
}
const transportName =
this.opts.rememberUpgrade &&
Socket.priorWebsocketSuccess &&
this.transports.indexOf("websocket") !== -1
? "websocket"
: this.transports[0];
this.readyState = "opening";

const transport = this.createTransport(transportName);
transport.open();
this.setTransport(transport);
}
Expand Down

0 comments on commit 2c1851d

Please sign in to comment.