Skip to content

Commit 4cf1158

Browse files
committed
Fix Javascript warnings and type definition gaps
1 parent 57b6e4c commit 4cf1158

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

Diff for: lib/sse.js

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (C) 2016-2023 Maxime Petazzoni <[email protected]>.
2+
* Copyright (C) 2016-2024 Maxime Petazzoni <[email protected]>.
33
* All rights reserved.
44
*/
55

@@ -37,7 +37,7 @@ var SSE = function (url, options) {
3737
/** @type {string} */
3838
this.FIELD_SEPARATOR = ':';
3939

40-
/** @type { {[key: string]: EventListener} } */
40+
/** @type { {[key: string]: [EventListener]} } */
4141
this.listeners = {};
4242

4343
/** @type {XMLHttpRequest} */
@@ -70,7 +70,7 @@ var SSE = function (url, options) {
7070
return;
7171
}
7272

73-
var filtered = [];
73+
const filtered = [];
7474
this.listeners[type].forEach(function(element) {
7575
if (element !== listener) {
7676
filtered.push(element);
@@ -97,7 +97,7 @@ var SSE = function (url, options) {
9797

9898
e.source = this;
9999

100-
var onHandler = 'on' + e.type;
100+
const onHandler = 'on' + e.type;
101101
if (this.hasOwnProperty(onHandler)) {
102102
this[onHandler].call(this, e);
103103
if (e.defaultPrevented) {
@@ -117,20 +117,20 @@ var SSE = function (url, options) {
117117

118118
/** @private */
119119
this._setReadyState = function(state) {
120-
var event = new CustomEvent('readystatechange');
120+
const event = new CustomEvent('readystatechange');
121121
event.readyState = state;
122122
this.readyState = state;
123123
this.dispatchEvent(event);
124124
};
125125

126126
this._onStreamFailure = function(e) {
127-
var event = new CustomEvent('error');
127+
const event = new CustomEvent('error');
128128
event.data = e.currentTarget.response;
129129
this.dispatchEvent(event);
130130
this.close();
131131
}
132132

133-
this._onStreamAbort = function(e) {
133+
this._onStreamAbort = function() {
134134
this.dispatchEvent(new CustomEvent('abort'));
135135
this.close();
136136
}
@@ -146,19 +146,21 @@ var SSE = function (url, options) {
146146
return;
147147
}
148148

149-
if (this.readyState == this.CONNECTING) {
149+
if (this.readyState === this.CONNECTING) {
150150
this.dispatchEvent(new CustomEvent('open'));
151151
this._setReadyState(this.OPEN);
152152
}
153153

154-
var data = this.xhr.responseText.substring(this.progress);
154+
const data = this.xhr.responseText.substring(this.progress);
155155

156156
this.progress += data.length;
157-
var parts = (this.chunk + data).split(/(\r\n\r\n|\r\r|\n\n)/g);
157+
const parts = (this.chunk + data).split(/(\r\n\r\n|\r\r|\n\n)/g);
158158

159-
// we assume that the last chunk can be incomplete because of buffering or other network effects
160-
// so we always save the last part to merge it with the next incoming packet
161-
var lastPart = parts.pop();
159+
/*
160+
* We assume that the last chunk can be incomplete because of buffering or other network effects,
161+
* so we always save the last part to merge it with the next incoming packet
162+
*/
163+
const lastPart = parts.pop();
162164
parts.forEach(function(part) {
163165
if (part.trim().length > 0) {
164166
this.dispatchEvent(this._parseEventChunk(part));
@@ -190,13 +192,13 @@ var SSE = function (url, options) {
190192
console.debug(chunk);
191193
}
192194

193-
var e = {'id': null, 'retry': null, 'data': null, 'event': null};
195+
const e = {'id': null, 'retry': null, 'data': null, 'event': null};
194196
chunk.split(/\n|\r\n|\r/).forEach(function(line) {
195-
var index = line.indexOf(this.FIELD_SEPARATOR);
196-
var field, value;
197+
const index = line.indexOf(this.FIELD_SEPARATOR);
198+
let field, value;
197199
if (index > 0) {
198200
// only first whitespace should be trimmed
199-
var skip = (line[index+1] === ' ') ? 2 : 1;
201+
const skip = (line[index + 1] === ' ') ? 2 : 1;
200202
field = line.substring(0, index);
201203
value = line.substring(index + skip);
202204
} else if (index < 0) {
@@ -256,7 +258,7 @@ var SSE = function (url, options) {
256258
this.xhr.addEventListener('error', this._onStreamFailure.bind(this));
257259
this.xhr.addEventListener('abort', this._onStreamAbort.bind(this));
258260
this.xhr.open(this.method, this.url);
259-
for (var header in this.headers) {
261+
for (let header in this.headers) {
260262
this.xhr.setRequestHeader(header, this.headers[header]);
261263
}
262264
this.xhr.withCredentials = this.withCredentials;
@@ -300,6 +302,7 @@ export { SSE };
300302
* @property {string} [payload] - payload as a string
301303
* @property {string} [method] - HTTP Method
302304
* @property {boolean} [withCredentials] - flag, if credentials needed
305+
* @property {boolean} [start] - flag, if streaming should start automatically
303306
* @property {boolean} [debug] - debugging flag
304307
*/
305308
/**

0 commit comments

Comments
 (0)