@@ -16,6 +16,10 @@ import debugModule from "debug"; // debug()
16
16
17
17
const debug = debugModule ( "engine.io-client:socket" ) ; // debug()
18
18
19
+ const withEventListeners =
20
+ typeof addEventListener === "function" &&
21
+ typeof removeEventListener === "function" ;
22
+
19
23
export interface SocketOptions {
20
24
/**
21
25
* The host that we're connecting to. Set from the URI passed when connecting
@@ -425,7 +429,7 @@ export class SocketWithoutUpgrade extends Emitter<
425
429
this . opts . query = decode ( this . opts . query ) ;
426
430
}
427
431
428
- if ( typeof addEventListener === "function" ) {
432
+ if ( withEventListeners ) {
429
433
if ( this . opts . closeOnBeforeunload ) {
430
434
// Firefox closes the connection when the "beforeunload" event is emitted but not Chrome. This event listener
431
435
// ensures every browser behaves the same (no "disconnect" event at the Socket.IO level when the page is
@@ -903,13 +907,17 @@ export class SocketWithoutUpgrade extends Emitter<
903
907
// ignore further transport communication
904
908
this . transport . removeAllListeners ( ) ;
905
909
906
- if ( typeof removeEventListener === "function" ) {
907
- removeEventListener (
908
- "beforeunload" ,
909
- this . _beforeunloadEventListener ,
910
- false ,
911
- ) ;
912
- removeEventListener ( "offline" , this . _offlineEventListener , false ) ;
910
+ if ( withEventListeners ) {
911
+ if ( this . _beforeunloadEventListener ) {
912
+ removeEventListener (
913
+ "beforeunload" ,
914
+ this . _beforeunloadEventListener ,
915
+ false ,
916
+ ) ;
917
+ }
918
+ if ( this . _offlineEventListener ) {
919
+ removeEventListener ( "offline" , this . _offlineEventListener , false ) ;
920
+ }
913
921
}
914
922
915
923
// set ready state
0 commit comments