@harnessio/eventsource
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -83,10 +83,2 @@ var parse = require('url').parse | ||
shortTermRetryStrategy.attempt(function (currentAttempt) { | ||
_emit('retrying', new Event('retrying', { shortTermRetryAttempt: currentAttempt })) | ||
if (readyState !== EventSource.RETRYING) return | ||
connect() | ||
shortTermRetryStrategy.retry(readyState === EventSource.RETRYING) | ||
}) | ||
// Second retry scenario covering two minutes worth of retries. | ||
@@ -103,9 +95,2 @@ // Wait 10 seconds before the first retry. | ||
mediumTermStrategy.attempt(function (currentAttempt) { | ||
_emit('retrying', new Event('retrying', { mediumTermRetryAttempt: currentAttempt })) | ||
if (readyState !== EventSource.RETRYING) return | ||
connect() | ||
mediumTermStrategy.retry(readyState === EventSource.RETRYING) | ||
}) | ||
// This scenario is for long term retries, which tries to connect forever, as long as the error | ||
@@ -121,14 +106,63 @@ // is retryable. Uses an exponential backoff and randomizes the timeouts. | ||
longTermStrategy.attempt(function (currentAttempt) { | ||
_emit('retrying', new Event('retrying', { longTermRetryAttempt: currentAttempt })) | ||
if (readyState !== EventSource.RETRYING) return | ||
connect() | ||
longTermStrategy.retry(readyState === EventSource.RETRYING) | ||
}) | ||
function attemptRetries () { | ||
shortTermRetryStrategy.attempt(function (currentAttempt) { | ||
// Check if we're in a closed state before proceeding with any retries. | ||
if (readyState === EventSource.CLOSED) return | ||
// Unlike the medium and long term strategies, we emit the retrying event before | ||
// checking if we're in a retrying state, which ensures we always fire off the initial retrying event. | ||
// The check for if we're in a closed state covers us for subsequent retries. | ||
_emit('retrying', new Event('retrying', { shortTermRetryAttempt: currentAttempt })) | ||
if (readyState !== EventSource.RETRYING) return | ||
connect() | ||
if (shortTermRetryStrategy.retry(readyState === EventSource.RETRYING)) { | ||
return | ||
} | ||
if (readyState !== EventSource.RETRYING) { | ||
return | ||
} | ||
mediumAndLongTermAttempt() | ||
}) | ||
} | ||
function mediumAndLongTermAttempt () { | ||
mediumTermStrategy.attempt(function (currentAttempt) { | ||
// It's feasible, if unlikely, a user can close a connection themselves after the library starts retrying. | ||
// So unlike the short term strategy, only emit the retrying event after this check. | ||
if (readyState !== EventSource.RETRYING) return | ||
_emit('retrying', new Event('retrying', { mediumTermRetryAttempt: currentAttempt })) | ||
connect() | ||
if (mediumTermStrategy.retry(readyState === EventSource.RETRYING)) { | ||
return | ||
} | ||
if (readyState !== EventSource.RETRYING) { | ||
return | ||
} | ||
longTermAttempt() | ||
}) | ||
} | ||
function longTermAttempt () { | ||
longTermStrategy.attempt(function (currentAttempt) { | ||
// It's feasible, if unlikely, a user can close a connection themselves after the library starts retrying. | ||
// So unlike the short term strategy, only emit the retrying event after this check. | ||
if (readyState !== EventSource.RETRYING) return | ||
_emit('retrying', new Event('retrying', { longTermRetryAttempt: currentAttempt })) | ||
connect() | ||
longTermStrategy.retry(readyState === EventSource.RETRYING) | ||
}) | ||
} | ||
attemptRetries() | ||
} | ||
function isRetryableError (error) { | ||
if (error.status) { | ||
var status = error.status | ||
return status === 500 || status === 502 || status === 503 || status === 504 | ||
if (error) { | ||
if (error.status) { | ||
var status = error.status | ||
return status === 500 || status === 502 || status === 503 || status === 504 | ||
} | ||
} | ||
@@ -151,5 +185,4 @@ // Always retry on IO Errors | ||
var errorEvent = new Event('error', { error: error }) | ||
if (isRetryableError(errorEvent)) { | ||
if (isRetryableError(error)) { | ||
readyState = EventSource.RETRYING | ||
_emit('error', errorEvent) | ||
scheduleReconnect() | ||
@@ -162,3 +195,3 @@ } else { | ||
// place we emit `closed` and it's emitted for the same reason as an error. | ||
_emit(new Event('closed')) | ||
_emit('closed', new Event('closed')) | ||
} | ||
@@ -393,2 +426,3 @@ } | ||
if (req.xhr && req.xhr.abort) req.xhr.abort() | ||
_emit('closed', new Event('closed')) | ||
} | ||
@@ -395,0 +429,0 @@ |
{ | ||
"name": "@harnessio/eventsource", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Fork of eventsource package: W3C compliant EventSource client for Node.js and browser (polyfill)", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
324165
9038