Skip to content

Commit

Permalink
fix: Stop reconnect timer when event source is closed. (#29)
Browse files Browse the repository at this point in the history
Currently the reconnect timer would elapse, discover the event source
was closed, and then do nothing. This is fine, but leaving the timer
open means that it can keep user applications running when they are
trying to exit. Stopping the timer will ensure we don't have any
outstanding async tasks that could prevent node from shutting down.

---------

Co-authored-by: Yusinto Ngadiman <yusinto@gmail.com>
  • Loading branch information
kinyoklion and yusinto authored May 22, 2024
1 parent bcceb35 commit a73a118
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion example/eventsource-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -7322,6 +7322,8 @@ function EventSource (url, eventSourceInitDict) {

var streamOriginUrl = new URL(url).origin

let reconnectTimer

function makeRequestUrlAndOptions () {
// Returns { url, options }; url is null/undefined if the URL properties are in options
var actualUrl = url
Expand Down Expand Up @@ -7430,7 +7432,9 @@ function EventSource (url, eventSourceInitDict) {
event.delayMillis = delay
_emit(event)

setTimeout(function () {
clearTimeout(reconnectTimer)

reconnectTimer = setTimeout(function () {
if (readyState !== EventSource.CONNECTING) return
connect()
}, delay)
Expand Down Expand Up @@ -7605,6 +7609,8 @@ function EventSource (url, eventSourceInitDict) {
}

this._close = function () {
clearTimeout(reconnectTimer)

if (readyState === EventSource.CLOSED) return
readyState = EventSource.CLOSED

Expand Down
8 changes: 7 additions & 1 deletion lib/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ function EventSource (url, eventSourceInitDict) {

var streamOriginUrl = new URL(url).origin

let reconnectTimer

function makeRequestUrlAndOptions () {
// Returns { url, options }; url is null/undefined if the URL properties are in options
var actualUrl = url
Expand Down Expand Up @@ -193,7 +195,9 @@ function EventSource (url, eventSourceInitDict) {
event.delayMillis = delay
_emit(event)

setTimeout(function () {
clearTimeout(reconnectTimer)

reconnectTimer = setTimeout(function () {
if (readyState !== EventSource.CONNECTING) return
connect()
}, delay)
Expand Down Expand Up @@ -368,6 +372,8 @@ function EventSource (url, eventSourceInitDict) {
}

this._close = function () {
clearTimeout(reconnectTimer)

if (readyState === EventSource.CLOSED) return
readyState = EventSource.CLOSED

Expand Down

0 comments on commit a73a118

Please sign in to comment.