Skip to content

Commit

Permalink
fix: silently close the transport in the beforeunload hook
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Feb 24, 2021
1 parent 76338d7 commit ed48b5d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
14 changes: 14 additions & 0 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ class Socket extends Emitter {
// set on heartbeat
this.pingTimeoutTimer = null;

if (typeof addEventListener === "function") {
addEventListener(
"beforeunload",
() => {
if (this.transport) {
// silently close the transport
this.transport.removeAllListeners();
this.transport.close();
}
},
false
);
}

this.open();
}

Expand Down
19 changes: 2 additions & 17 deletions lib/transports/polling-jsonp.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ const rEscapedNewline = /\\n/g;

let callbacks;

/**
* Noop.
*/

function empty() {}

class JSONPPolling extends Polling {
/**
* JSONP Polling constructor.
Expand Down Expand Up @@ -46,17 +40,6 @@ class JSONPPolling extends Polling {

// append to query string
this.query.j = this.index;

// prevent spurious errors from being emitted when the window is unloaded
if (typeof addEventListener === "function") {
addEventListener(
"beforeunload",
function() {
if (self.script) self.script.onerror = empty;
},
false
);
}
}

/**
Expand All @@ -73,6 +56,8 @@ class JSONPPolling extends Polling {
*/
doClose() {
if (this.script) {
// prevent spurious errors from being emitted when the window is unloaded
this.script.onerror = () => {};
this.script.parentNode.removeChild(this.script);
this.script = null;
}
Expand Down
1 change: 1 addition & 0 deletions lib/transports/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class WS extends Transport {
doClose() {
if (typeof this.ws !== "undefined") {
this.ws.close();
this.ws = null;
}
}

Expand Down

0 comments on commit ed48b5d

Please sign in to comment.