Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
* 2. added onopen, onclose, onerror, onretrying functions.
* 3. modified dispatch to work with functions added in 2.
* 4. replaced all for of loops with foreach
*
* Additional changes:
* 1. separated event handling to use onprogress for data changes
* and onreadystatechange for status changes. This is to address
* an issue with Vega OS where they do not fire a readyStatechange
* event when the response is received.
*/
import type { EventSourceEvent, EventSourceListener, EventSourceOptions, EventType } from './types';

Expand Down Expand Up @@ -132,8 +138,34 @@ export default class EventSource<E extends string = never> {

this._xhr.timeout = this._timeout;

this._xhr.onprogress = () => {
if (this._status === this.CLOSED || this._xhr.readyState !== XMLHttpRequest.LOADING) {
return;
}

this._logger?.debug(
`[EventSource][onprogress] ReadyState: ${
XMLReadyStateMap[this._xhr.readyState] || 'Unknown'
}(${this._xhr.readyState}), status: ${this._xhr.status}`,
);

if (this._xhr.status >= 200 && this._xhr.status < 400) {
this._handleEvent(this._xhr.responseText || '');
} else {
this._status = this.ERROR;

this.dispatch('error', {
type: 'error',
message: this._xhr.responseText,
xhrStatus: this._xhr.status,
xhrState: this._xhr.readyState,
});
}
};

this._xhr.onreadystatechange = () => {
if (this._status === this.CLOSED) {
// Do not handle this state if the status is loading as we will delegate this to the onprogress listener.
if (this._status === this.CLOSED || this._xhr.readyState === XMLHttpRequest.LOADING) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything in the code that follows that should be cleaned up?
Is there any potential for overlapping situations where the responseText could be appended during another state?

Copy link
Contributor Author

@joker23 joker23 Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per https://xhr.spec.whatwg.org/ it looks like progress event will fire on events that have data which I think means that only https://github.com/launchdarkly/js-core/pull/974/files#diff-1eb9eb87df77c7a5d195bc4e8d8d0a5d5c896fb4b4edf90b69d4e3b4ca5c9198R194 could be cleaned up. I left this in there in case we do run into the situation that data is sent on a status update which shouldn't happen so should pass through in all cases.

}

Expand Down