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 18, 2024
1 parent 32c761f commit 6c68608
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions packages/engine.io-client/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
import debugModule from "debug"; // debug()

const debug = debugModule("engine.io-client:socket"); // debug()
const withEventListeners =
typeof addEventListener === "function" &&
typeof removeEventListener === "function";

export interface SocketOptions {
/**
Expand Down Expand Up @@ -425,7 +428,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 +906,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 6c68608

Please sign in to comment.