Skip to content

Commit 0ad8aa3

Browse files
committed
Display human readable readyState debug log. Display debug log upon connection.
1 parent 801370b commit 0ad8aa3

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/EventSource.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
const XMLReadyStateMap = [
2+
'UNSENT',
3+
'OPENED',
4+
'HEADERS_RECEIVED',
5+
'LOADING',
6+
'DONE',
7+
];
8+
19
class EventSource {
210
ERROR = -1;
311
CONNECTING = 0;
@@ -86,7 +94,7 @@ class EventSource {
8694

8795
const xhr = this._xhr;
8896

89-
this._logDebug(`[EventSource][onreadystatechange] ReadyState: ${xhr.readyState}, status: ${xhr.status}`);
97+
this._logDebug(`[EventSource][onreadystatechange] ReadyState: ${XMLReadyStateMap[xhr.readyState] || 'Unknown'}(${xhr.readyState}), status: ${xhr.status}`);
9098

9199
if (![XMLHttpRequest.DONE, XMLHttpRequest.LOADING].includes(xhr.readyState)) {
92100
return;
@@ -96,6 +104,7 @@ class EventSource {
96104
if (this.status === this.CONNECTING) {
97105
this.status = this.OPEN;
98106
this.dispatch('open', { type: 'open' });
107+
this._logDebug('[EventSource][onreadystatechange][OPEN] Connection opened.');
99108
}
100109

101110
this._handleEvent(xhr.responseText || '');
@@ -166,10 +175,12 @@ class EventSource {
166175

167176
_handleEvent(response) {
168177
const parts = response.substr(this.lastIndexProcessed).split('\n');
169-
let indexOfDoubleNewline = response.lastIndexOf('\n\n');
178+
179+
const indexOfDoubleNewline = response.lastIndexOf('\n\n');
170180
if (indexOfDoubleNewline != -1) {
171181
this.lastIndexProcessed = indexOfDoubleNewline + 2;
172182
}
183+
173184
let data = [];
174185
let retry = 0;
175186
let line = '';

0 commit comments

Comments
 (0)