Skip to content

Commit 6c68608

Browse files
fix(eio-client): only remove the event listener if it exists
1 parent 32c761f commit 6c68608

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

packages/engine.io-client/lib/socket.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import {
1515
import debugModule from "debug"; // debug()
1616

1717
const debug = debugModule("engine.io-client:socket"); // debug()
18+
const withEventListeners =
19+
typeof addEventListener === "function" &&
20+
typeof removeEventListener === "function";
1821

1922
export interface SocketOptions {
2023
/**
@@ -425,7 +428,7 @@ export class SocketWithoutUpgrade extends Emitter<
425428
this.opts.query = decode(this.opts.query);
426429
}
427430

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

906-
if (typeof removeEventListener === "function") {
907-
removeEventListener(
908-
"beforeunload",
909-
this._beforeunloadEventListener,
910-
false,
911-
);
912-
removeEventListener("offline", this._offlineEventListener, false);
909+
if (withEventListeners) {
910+
if (this._beforeunloadEventListener) {
911+
removeEventListener(
912+
"beforeunload",
913+
this._beforeunloadEventListener,
914+
false,
915+
);
916+
}
917+
if (this._offlineEventListener) {
918+
removeEventListener("offline", this._offlineEventListener, false);
919+
}
913920
}
914921

915922
// set ready state

0 commit comments

Comments
 (0)