@@ -15,6 +15,9 @@ import {
15
15
import debugModule from "debug" ; // debug()
16
16
17
17
const debug = debugModule ( "engine.io-client:socket" ) ; // debug()
18
+ const withEventListeners =
19
+ typeof addEventListener === "function" &&
20
+ typeof removeEventListener === "function" ;
18
21
19
22
export interface SocketOptions {
20
23
/**
@@ -425,7 +428,7 @@ export class SocketWithoutUpgrade extends Emitter<
425
428
this . opts . query = decode ( this . opts . query ) ;
426
429
}
427
430
428
- if ( typeof addEventListener === "function" ) {
431
+ if ( withEventListeners ) {
429
432
if ( this . opts . closeOnBeforeunload ) {
430
433
// Firefox closes the connection when the "beforeunload" event is emitted but not Chrome. This event listener
431
434
// 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<
903
906
// ignore further transport communication
904
907
this . transport . removeAllListeners ( ) ;
905
908
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
+ }
913
920
}
914
921
915
922
// set ready state
0 commit comments