-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Javascript warnings and type definition gaps
- Loading branch information
1 parent
57b6e4c
commit 4cf1158
Showing
1 changed file
with
21 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* Copyright (C) 2016-2023 Maxime Petazzoni <[email protected]>. | ||
* Copyright (C) 2016-2024 Maxime Petazzoni <[email protected]>. | ||
* All rights reserved. | ||
*/ | ||
|
||
|
@@ -37,7 +37,7 @@ var SSE = function (url, options) { | |
/** @type {string} */ | ||
this.FIELD_SEPARATOR = ':'; | ||
|
||
/** @type { {[key: string]: EventListener} } */ | ||
/** @type { {[key: string]: [EventListener]} } */ | ||
this.listeners = {}; | ||
|
||
/** @type {XMLHttpRequest} */ | ||
|
@@ -70,7 +70,7 @@ var SSE = function (url, options) { | |
return; | ||
} | ||
|
||
var filtered = []; | ||
const filtered = []; | ||
this.listeners[type].forEach(function(element) { | ||
if (element !== listener) { | ||
filtered.push(element); | ||
|
@@ -97,7 +97,7 @@ var SSE = function (url, options) { | |
|
||
e.source = this; | ||
|
||
var onHandler = 'on' + e.type; | ||
const onHandler = 'on' + e.type; | ||
if (this.hasOwnProperty(onHandler)) { | ||
this[onHandler].call(this, e); | ||
if (e.defaultPrevented) { | ||
|
@@ -117,20 +117,20 @@ var SSE = function (url, options) { | |
|
||
/** @private */ | ||
this._setReadyState = function(state) { | ||
var event = new CustomEvent('readystatechange'); | ||
const event = new CustomEvent('readystatechange'); | ||
event.readyState = state; | ||
this.readyState = state; | ||
this.dispatchEvent(event); | ||
}; | ||
|
||
this._onStreamFailure = function(e) { | ||
var event = new CustomEvent('error'); | ||
const event = new CustomEvent('error'); | ||
event.data = e.currentTarget.response; | ||
this.dispatchEvent(event); | ||
this.close(); | ||
} | ||
|
||
this._onStreamAbort = function(e) { | ||
this._onStreamAbort = function() { | ||
this.dispatchEvent(new CustomEvent('abort')); | ||
this.close(); | ||
} | ||
|
@@ -146,19 +146,21 @@ var SSE = function (url, options) { | |
return; | ||
} | ||
|
||
if (this.readyState == this.CONNECTING) { | ||
if (this.readyState === this.CONNECTING) { | ||
this.dispatchEvent(new CustomEvent('open')); | ||
this._setReadyState(this.OPEN); | ||
} | ||
|
||
var data = this.xhr.responseText.substring(this.progress); | ||
const data = this.xhr.responseText.substring(this.progress); | ||
|
||
this.progress += data.length; | ||
var parts = (this.chunk + data).split(/(\r\n\r\n|\r\r|\n\n)/g); | ||
const parts = (this.chunk + data).split(/(\r\n\r\n|\r\r|\n\n)/g); | ||
|
||
// we assume that the last chunk can be incomplete because of buffering or other network effects | ||
// so we always save the last part to merge it with the next incoming packet | ||
var lastPart = parts.pop(); | ||
/* | ||
* We assume that the last chunk can be incomplete because of buffering or other network effects, | ||
* so we always save the last part to merge it with the next incoming packet | ||
*/ | ||
const lastPart = parts.pop(); | ||
parts.forEach(function(part) { | ||
if (part.trim().length > 0) { | ||
this.dispatchEvent(this._parseEventChunk(part)); | ||
|
@@ -190,13 +192,13 @@ var SSE = function (url, options) { | |
console.debug(chunk); | ||
} | ||
|
||
var e = {'id': null, 'retry': null, 'data': null, 'event': null}; | ||
const e = {'id': null, 'retry': null, 'data': null, 'event': null}; | ||
chunk.split(/\n|\r\n|\r/).forEach(function(line) { | ||
var index = line.indexOf(this.FIELD_SEPARATOR); | ||
var field, value; | ||
const index = line.indexOf(this.FIELD_SEPARATOR); | ||
let field, value; | ||
if (index > 0) { | ||
// only first whitespace should be trimmed | ||
var skip = (line[index+1] === ' ') ? 2 : 1; | ||
const skip = (line[index + 1] === ' ') ? 2 : 1; | ||
field = line.substring(0, index); | ||
value = line.substring(index + skip); | ||
} else if (index < 0) { | ||
|
@@ -256,7 +258,7 @@ var SSE = function (url, options) { | |
this.xhr.addEventListener('error', this._onStreamFailure.bind(this)); | ||
this.xhr.addEventListener('abort', this._onStreamAbort.bind(this)); | ||
this.xhr.open(this.method, this.url); | ||
for (var header in this.headers) { | ||
for (let header in this.headers) { | ||
this.xhr.setRequestHeader(header, this.headers[header]); | ||
} | ||
this.xhr.withCredentials = this.withCredentials; | ||
|
@@ -300,6 +302,7 @@ export { SSE }; | |
* @property {string} [payload] - payload as a string | ||
* @property {string} [method] - HTTP Method | ||
* @property {boolean} [withCredentials] - flag, if credentials needed | ||
* @property {boolean} [start] - flag, if streaming should start automatically | ||
* @property {boolean} [debug] - debugging flag | ||
*/ | ||
/** | ||
|