Skip to content

Commit a73a118

Browse files
kinyoklionyusinto
andauthored
fix: Stop reconnect timer when event source is closed. (#29)
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 <[email protected]>
1 parent bcceb35 commit a73a118

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

example/eventsource-polyfill.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -7322,6 +7322,8 @@ function EventSource (url, eventSourceInitDict) {
73227322

73237323
var streamOriginUrl = new URL(url).origin
73247324

7325+
let reconnectTimer
7326+
73257327
function makeRequestUrlAndOptions () {
73267328
// Returns { url, options }; url is null/undefined if the URL properties are in options
73277329
var actualUrl = url
@@ -7430,7 +7432,9 @@ function EventSource (url, eventSourceInitDict) {
74307432
event.delayMillis = delay
74317433
_emit(event)
74327434

7433-
setTimeout(function () {
7435+
clearTimeout(reconnectTimer)
7436+
7437+
reconnectTimer = setTimeout(function () {
74347438
if (readyState !== EventSource.CONNECTING) return
74357439
connect()
74367440
}, delay)
@@ -7605,6 +7609,8 @@ function EventSource (url, eventSourceInitDict) {
76057609
}
76067610

76077611
this._close = function () {
7612+
clearTimeout(reconnectTimer)
7613+
76087614
if (readyState === EventSource.CLOSED) return
76097615
readyState = EventSource.CLOSED
76107616

lib/eventsource.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ function EventSource (url, eventSourceInitDict) {
8585

8686
var streamOriginUrl = new URL(url).origin
8787

88+
let reconnectTimer
89+
8890
function makeRequestUrlAndOptions () {
8991
// Returns { url, options }; url is null/undefined if the URL properties are in options
9092
var actualUrl = url
@@ -193,7 +195,9 @@ function EventSource (url, eventSourceInitDict) {
193195
event.delayMillis = delay
194196
_emit(event)
195197

196-
setTimeout(function () {
198+
clearTimeout(reconnectTimer)
199+
200+
reconnectTimer = setTimeout(function () {
197201
if (readyState !== EventSource.CONNECTING) return
198202
connect()
199203
}, delay)
@@ -368,6 +372,8 @@ function EventSource (url, eventSourceInitDict) {
368372
}
369373

370374
this._close = function () {
375+
clearTimeout(reconnectTimer)
376+
371377
if (readyState === EventSource.CLOSED) return
372378
readyState = EventSource.CLOSED
373379

0 commit comments

Comments
 (0)