Skip to content

Commit

Permalink
fix(eio-client): only remove the event listener if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Sep 19, 2024
1 parent 043b55c commit 9b3c9ab
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/engine.io-client/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import debugModule from "debug"; // debug()

const debug = debugModule("engine.io-client:socket"); // debug()

const withEventListeners =
typeof addEventListener === "function" &&
typeof removeEventListener === "function";

export interface SocketOptions {
/**
* The host that we're connecting to. Set from the URI passed when connecting
Expand Down Expand Up @@ -425,7 +429,7 @@ export class SocketWithoutUpgrade extends Emitter<
this.opts.query = decode(this.opts.query);
}

if (typeof addEventListener === "function") {
if (withEventListeners) {
if (this.opts.closeOnBeforeunload) {
// Firefox closes the connection when the "beforeunload" event is emitted but not Chrome. This event listener
// ensures every browser behaves the same (no "disconnect" event at the Socket.IO level when the page is
Expand Down Expand Up @@ -903,13 +907,17 @@ export class SocketWithoutUpgrade extends Emitter<
// ignore further transport communication
this.transport.removeAllListeners();

if (typeof removeEventListener === "function") {
removeEventListener(
"beforeunload",
this._beforeunloadEventListener,
false,
);
removeEventListener("offline", this._offlineEventListener, false);
if (withEventListeners) {
if (this._beforeunloadEventListener) {
removeEventListener(
"beforeunload",
this._beforeunloadEventListener,
false,
);
}
if (this._offlineEventListener) {
removeEventListener("offline", this._offlineEventListener, false);
}
}

// set ready state
Expand Down

0 comments on commit 9b3c9ab

Please sign in to comment.