Skip to content

Commit

Permalink
FFM-8967 - Emit DISCONNECTED events once per unique disconnection eve…
Browse files Browse the repository at this point in the history
…nt (#104)
  • Loading branch information
erdirowlands authored Sep 22, 2023
1 parent 99faea0 commit c6e1de1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@harnessio/ff-javascript-client-sdk",
"version": "1.19.0",
"version": "1.19.2",
"author": "Harness",
"license": "Apache-2.0",
"main": "dist/sdk.cjs.js",
Expand Down
21 changes: 13 additions & 8 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export class Streamer {
private closed: boolean = false
private readTimeoutCheckerId: any
private fallbackPoller: Poller
private connectionOpened = false;
private connectionOpened = false
private disconnectEventEmitted = false

constructor(eventBus, configurations, url, apiKey, standardHeaders, fallbackPoller, eventCallback) {
this.eventBus = eventBus
Expand Down Expand Up @@ -49,7 +50,10 @@ export class Streamer {
clearInterval(this.readTimeoutCheckerId)
const reconnectDelayMs = getRandom(1000, 10000)
this.logDebug('Stream disconnected, will reconnect in ' + reconnectDelayMs + 'ms')
this.eventBus.emit(Event.DISCONNECTED)
if (!this.disconnectEventEmitted) {
this.eventBus.emit(Event.DISCONNECTED)
this.disconnectEventEmitted = true
}
setTimeout(() => this.start(), reconnectDelayMs)
}

Expand Down Expand Up @@ -82,18 +86,18 @@ export class Streamer {
}
this.xhr.timeout = 24 * 60 * 60 * 1000 // Force SSE to reconnect after 24hrs
this.xhr.onerror = () => {
this.connectionOpened = false;
this.connectionOpened = false
onFailed('XMLHttpRequest error on SSE stream')
}
this.xhr.onabort = () => {
this.connectionOpened = false;
this.connectionOpened = false
this.logDebug('SSE aborted')
if (!this.closed) {
onFailed(null)
}
}
this.xhr.ontimeout = () => {
this.connectionOpened = false;
this.connectionOpened = false
onFailed('SSE timeout')
}

Expand All @@ -107,9 +111,9 @@ export class Streamer {

if (!this.connectionOpened) {
onConnected()
this.connectionOpened = true;
this.connectionOpened = true
this.disconnectEventEmitted = false
}

}

let offset = 0
Expand All @@ -120,7 +124,8 @@ export class Streamer {
// CONNECTED event here if we haven't already done so per unique connection event.
if (!this.connectionOpened) {
onConnected()
this.connectionOpened = true;
this.connectionOpened = true
this.disconnectEventEmitted = false
}
// if we are in polling mode due to a recovered streaming error, then stop polling
this.stopFallBackPolling()
Expand Down

0 comments on commit c6e1de1

Please sign in to comment.