Skip to content

Commit 097ce65

Browse files
committed
feat: add support for steaming in Vega OS
1 parent 8d57904 commit 097ce65

File tree

1 file changed

+33
-1
lines changed
  • packages/sdk/react-native/src/fromExternal/react-native-sse

1 file changed

+33
-1
lines changed

packages/sdk/react-native/src/fromExternal/react-native-sse/EventSource.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
* 2. added onopen, onclose, onerror, onretrying functions.
66
* 3. modified dispatch to work with functions added in 2.
77
* 4. replaced all for of loops with foreach
8+
*
9+
* Additional changes:
10+
* 1. separated event handling to use onprogress for data changes
11+
* and onreadystatechange for status changes. This is to address
12+
* an issue with Vega OS where they do not fire a readyStatechange
13+
* event when the response is received.
814
*/
915
import type { EventSourceEvent, EventSourceListener, EventSourceOptions, EventType } from './types';
1016

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

133139
this._xhr.timeout = this._timeout;
134140

141+
this._xhr.onprogress = () => {
142+
if (this._status === this.CLOSED || this._xhr.readyState !== XMLHttpRequest.LOADING) {
143+
return;
144+
}
145+
146+
this._logger?.debug(
147+
`[EventSource][onprogress] ReadyState: ${
148+
XMLReadyStateMap[this._xhr.readyState] || 'Unknown'
149+
}(${this._xhr.readyState}), status: ${this._xhr.status}`,
150+
);
151+
152+
if (this._xhr.status >= 200 && this._xhr.status < 400) {
153+
this._handleEvent(this._xhr.responseText || '');
154+
} else {
155+
this._status = this.ERROR;
156+
157+
this.dispatch('error', {
158+
type: 'error',
159+
message: this._xhr.responseText,
160+
xhrStatus: this._xhr.status,
161+
xhrState: this._xhr.readyState,
162+
});
163+
}
164+
};
165+
135166
this._xhr.onreadystatechange = () => {
136-
if (this._status === this.CLOSED) {
167+
// Do not handle this state if the status is loading as we will delegate this to the onprogress listener.
168+
if (this._status === this.CLOSED || this._xhr.readyState === XMLHttpRequest.LOADING) {
137169
return;
138170
}
139171

0 commit comments

Comments
 (0)