|
5 | 5 | * 2. added onopen, onclose, onerror, onretrying functions. |
6 | 6 | * 3. modified dispatch to work with functions added in 2. |
7 | 7 | * 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. |
8 | 14 | */ |
9 | 15 | import type { EventSourceEvent, EventSourceListener, EventSourceOptions, EventType } from './types'; |
10 | 16 |
|
@@ -132,8 +138,34 @@ export default class EventSource<E extends string = never> { |
132 | 138 |
|
133 | 139 | this._xhr.timeout = this._timeout; |
134 | 140 |
|
| 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 | + |
135 | 166 | 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) { |
137 | 169 | return; |
138 | 170 | } |
139 | 171 |
|
|
0 commit comments