-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
115bd82
commit e180de6
Showing
17 changed files
with
969 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { EventSource, EventSourceInitDict } from '@launchdarkly/js-sdk-common'; | ||
|
||
export class MockEventSource implements EventSource { | ||
eventsByType: Map<string, { data?: any }[]> = new Map<string, { data?: any }[]>(); | ||
|
||
handlers: Record<string, (event?: { data?: any }) => void> = {}; | ||
|
||
closed = false; | ||
|
||
url: string; | ||
|
||
options: EventSourceInitDict; | ||
|
||
constructor(url: string, options: EventSourceInitDict) { | ||
this.url = url; | ||
this.options = options; | ||
} | ||
|
||
onclose: (() => void) | undefined; | ||
|
||
onerror: (() => void) | undefined; | ||
|
||
onopen: (() => void) | undefined; | ||
|
||
onretrying: ((e: { delayMillis: number }) => void) | undefined; | ||
|
||
addEventListener(type: string, listener: (event?: { data?: any }) => void): void { | ||
this.handlers[type] = listener; | ||
|
||
// replay events to listener | ||
(this.eventsByType.get(type) ?? []).forEach((event) => { | ||
listener(event); | ||
}); | ||
} | ||
|
||
close(): void { | ||
this.closed = true; | ||
} | ||
|
||
simulateEvents(type: string, events: { data?: any }[]) { | ||
this.eventsByType.set(type, events); | ||
} | ||
|
||
simulateError(error: { status: number; message: string }) { | ||
const shouldRetry = this.options.errorFilter(error); | ||
if (!shouldRetry) { | ||
this.closed = true; | ||
} | ||
} | ||
} |
Oops, something went wrong.