-
Notifications
You must be signed in to change notification settings - Fork 166
Open
Description
Here is a fix. It also performs better. Basically, it effectively does not increment the cache unless that data was actually processed into a MessageEvent.
- replace the
cachevariable withchars_processed - replace the
cache = ''line withchars_processed = 0 - replace the event handling code with:
// process this.responseText
var parts = responseText.substr(chars_processed).split("\n"),
eventType = 'message',
data = [],
i = 0,
line = '';
// TODO handle 'event' (for buffer name), retry
var temp_chars_processed = 0;
for (; i < parts.length; i++) {
line = parts[i].replace(reTrim, '');
temp_chars_processed += line.length + (i + 1 == parts.length ? 0 : 1);
if (line.startsWith('event')) {
eventType = line.replace(/event:?\s*/, '');
} else if (line.startsWith('retry')) {
retry = parseInt(line.replace(/retry:?\s*/, ''));
if(!isNaN(retry)) { interval = retry; }
} else if (line.startsWith('data')) {
data.push(line.replace(/data:?\s*/, ''));
} else if (line.startsWith('id:')) {
lastEventId = line.replace(/id:?\s*/, '');
} else if (line.startsWith('id')) { // this resets the id
lastEventId = null;
} else if (line == '') {
if (data.length) {
var event = new MessageEvent(data.join('\n'), eventsource.url, lastEventId);
eventsource.dispatchEvent(eventType, event);
data = [];
eventType = 'message';
chars_processed += temp_chars_processed;
}
}
}
Metadata
Metadata
Assignees
Labels
No labels