reconnecting-eventsource 1.0.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +1 -0
- package/dist/ReconnectingEventSource.js +1 -375
- package/dist/ReconnectingEventSource.min.js +1 -1
- package/lib/index.js +1 -337
- package/package.json +6 -4
- package/reconnecting-eventsource.d.ts +11 -0
- package/src/reconnecting-eventsource.js +13 -5
- package/test.js +8 -1
- package/tsconfig.json +8 -0
- package/webpack.config.js +1 -14
package/.eslintignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
*.d.ts
|
|
@@ -1,375 +1 @@
|
|
|
1
|
-
var ReconnectingEventSource =
|
|
2
|
-
/******/ (function(modules) { // webpackBootstrap
|
|
3
|
-
/******/ // The module cache
|
|
4
|
-
/******/ var installedModules = {};
|
|
5
|
-
/******/
|
|
6
|
-
/******/ // The require function
|
|
7
|
-
/******/ function __webpack_require__(moduleId) {
|
|
8
|
-
/******/
|
|
9
|
-
/******/ // Check if module is in cache
|
|
10
|
-
/******/ if(installedModules[moduleId]) {
|
|
11
|
-
/******/ return installedModules[moduleId].exports;
|
|
12
|
-
/******/ }
|
|
13
|
-
/******/ // Create a new module (and put it into the cache)
|
|
14
|
-
/******/ var module = installedModules[moduleId] = {
|
|
15
|
-
/******/ i: moduleId,
|
|
16
|
-
/******/ l: false,
|
|
17
|
-
/******/ exports: {}
|
|
18
|
-
/******/ };
|
|
19
|
-
/******/
|
|
20
|
-
/******/ // Execute the module function
|
|
21
|
-
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
|
22
|
-
/******/
|
|
23
|
-
/******/ // Flag the module as loaded
|
|
24
|
-
/******/ module.l = true;
|
|
25
|
-
/******/
|
|
26
|
-
/******/ // Return the exports of the module
|
|
27
|
-
/******/ return module.exports;
|
|
28
|
-
/******/ }
|
|
29
|
-
/******/
|
|
30
|
-
/******/
|
|
31
|
-
/******/ // expose the modules object (__webpack_modules__)
|
|
32
|
-
/******/ __webpack_require__.m = modules;
|
|
33
|
-
/******/
|
|
34
|
-
/******/ // expose the module cache
|
|
35
|
-
/******/ __webpack_require__.c = installedModules;
|
|
36
|
-
/******/
|
|
37
|
-
/******/ // define getter function for harmony exports
|
|
38
|
-
/******/ __webpack_require__.d = function(exports, name, getter) {
|
|
39
|
-
/******/ if(!__webpack_require__.o(exports, name)) {
|
|
40
|
-
/******/ Object.defineProperty(exports, name, {
|
|
41
|
-
/******/ configurable: false,
|
|
42
|
-
/******/ enumerable: true,
|
|
43
|
-
/******/ get: getter
|
|
44
|
-
/******/ });
|
|
45
|
-
/******/ }
|
|
46
|
-
/******/ };
|
|
47
|
-
/******/
|
|
48
|
-
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
|
49
|
-
/******/ __webpack_require__.n = function(module) {
|
|
50
|
-
/******/ var getter = module && module.__esModule ?
|
|
51
|
-
/******/ function getDefault() { return module['default']; } :
|
|
52
|
-
/******/ function getModuleExports() { return module; };
|
|
53
|
-
/******/ __webpack_require__.d(getter, 'a', getter);
|
|
54
|
-
/******/ return getter;
|
|
55
|
-
/******/ };
|
|
56
|
-
/******/
|
|
57
|
-
/******/ // Object.prototype.hasOwnProperty.call
|
|
58
|
-
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
|
59
|
-
/******/
|
|
60
|
-
/******/ // __webpack_public_path__
|
|
61
|
-
/******/ __webpack_require__.p = "";
|
|
62
|
-
/******/
|
|
63
|
-
/******/ // Load entry module and return exports
|
|
64
|
-
/******/ return __webpack_require__(__webpack_require__.s = 0);
|
|
65
|
-
/******/ })
|
|
66
|
-
/************************************************************************/
|
|
67
|
-
/******/ ([
|
|
68
|
-
/* 0 */
|
|
69
|
-
/***/ (function(module, exports, __webpack_require__) {
|
|
70
|
-
|
|
71
|
-
var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;(function (global, factory) {
|
|
72
|
-
if (true) {
|
|
73
|
-
!(__WEBPACK_AMD_DEFINE_ARRAY__ = [module, __webpack_require__(1)], __WEBPACK_AMD_DEFINE_FACTORY__ = (factory),
|
|
74
|
-
__WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
|
|
75
|
-
(__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__),
|
|
76
|
-
__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
|
|
77
|
-
} else if (typeof exports !== "undefined") {
|
|
78
|
-
factory(module, require("./reconnecting-eventsource"));
|
|
79
|
-
} else {
|
|
80
|
-
var mod = {
|
|
81
|
-
exports: {}
|
|
82
|
-
};
|
|
83
|
-
factory(mod, global.reconnectingEventsource);
|
|
84
|
-
global.indexUmd = mod.exports;
|
|
85
|
-
}
|
|
86
|
-
})(this, function (module, _reconnectingEventsource) {
|
|
87
|
-
"use strict";
|
|
88
|
-
|
|
89
|
-
var _reconnectingEventsource2 = _interopRequireDefault(_reconnectingEventsource);
|
|
90
|
-
|
|
91
|
-
function _interopRequireDefault(obj) {
|
|
92
|
-
return obj && obj.__esModule ? obj : {
|
|
93
|
-
default: obj
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
module.exports = _reconnectingEventsource2.default;
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
/***/ }),
|
|
101
|
-
/* 1 */
|
|
102
|
-
/***/ (function(module, exports, __webpack_require__) {
|
|
103
|
-
|
|
104
|
-
var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;(function (global, factory) {
|
|
105
|
-
if (true) {
|
|
106
|
-
!(__WEBPACK_AMD_DEFINE_ARRAY__ = [exports], __WEBPACK_AMD_DEFINE_FACTORY__ = (factory),
|
|
107
|
-
__WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
|
|
108
|
-
(__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__),
|
|
109
|
-
__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
|
|
110
|
-
} else if (typeof exports !== "undefined") {
|
|
111
|
-
factory(exports);
|
|
112
|
-
} else {
|
|
113
|
-
var mod = {
|
|
114
|
-
exports: {}
|
|
115
|
-
};
|
|
116
|
-
factory(mod.exports);
|
|
117
|
-
global.reconnectingEventsource = mod.exports;
|
|
118
|
-
}
|
|
119
|
-
})(this, function (exports) {
|
|
120
|
-
'use strict';
|
|
121
|
-
|
|
122
|
-
Object.defineProperty(exports, "__esModule", {
|
|
123
|
-
value: true
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
function _toConsumableArray(arr) {
|
|
127
|
-
if (Array.isArray(arr)) {
|
|
128
|
-
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
|
|
129
|
-
arr2[i] = arr[i];
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return arr2;
|
|
133
|
-
} else {
|
|
134
|
-
return Array.from(arr);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function _classCallCheck(instance, Constructor) {
|
|
139
|
-
if (!(instance instanceof Constructor)) {
|
|
140
|
-
throw new TypeError("Cannot call a class as a function");
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
var _createClass = function () {
|
|
145
|
-
function defineProperties(target, props) {
|
|
146
|
-
for (var i = 0; i < props.length; i++) {
|
|
147
|
-
var descriptor = props[i];
|
|
148
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
149
|
-
descriptor.configurable = true;
|
|
150
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
151
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return function (Constructor, protoProps, staticProps) {
|
|
156
|
-
if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
|
157
|
-
if (staticProps) defineProperties(Constructor, staticProps);
|
|
158
|
-
return Constructor;
|
|
159
|
-
};
|
|
160
|
-
}();
|
|
161
|
-
|
|
162
|
-
var ReconnectingEventSource = function () {
|
|
163
|
-
function ReconnectingEventSource(url, configuration) {
|
|
164
|
-
var _this = this;
|
|
165
|
-
|
|
166
|
-
_classCallCheck(this, ReconnectingEventSource);
|
|
167
|
-
|
|
168
|
-
this._eventSource = null;
|
|
169
|
-
this._lastEventId = null;
|
|
170
|
-
this._timer = null;
|
|
171
|
-
this._listeners = {};
|
|
172
|
-
|
|
173
|
-
this.url = url;
|
|
174
|
-
this.readyState = 0;
|
|
175
|
-
this.max_retry_time = configuration.max_retry_time || 3000;
|
|
176
|
-
|
|
177
|
-
this._configuration = configuration != null ? Object.assign({}, configuration) : null;
|
|
178
|
-
|
|
179
|
-
if (this._configuration != null && this._configuration.lastEventId) {
|
|
180
|
-
this._lastEventId = this._configuration.lastEventId;
|
|
181
|
-
delete this._configuration['lastEventId'];
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
this._onevent_wrapped = function (event) {
|
|
185
|
-
_this._onevent(event);
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
this._start();
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
_createClass(ReconnectingEventSource, [{
|
|
192
|
-
key: '_start',
|
|
193
|
-
value: function _start() {
|
|
194
|
-
var _this2 = this;
|
|
195
|
-
|
|
196
|
-
var url = this.url;
|
|
197
|
-
|
|
198
|
-
if (this._lastEventId) {
|
|
199
|
-
if (url.indexOf('?') === -1) {
|
|
200
|
-
url += '?';
|
|
201
|
-
} else {
|
|
202
|
-
url += '&';
|
|
203
|
-
}
|
|
204
|
-
url += 'lastEventId=' + encodeURIComponent(this._lastEventId);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
this._eventSource = new EventSource(url, this._configuration);
|
|
208
|
-
|
|
209
|
-
this._eventSource.onopen = function (event) {
|
|
210
|
-
_this2._onopen(event);
|
|
211
|
-
};
|
|
212
|
-
this._eventSource.onerror = function (event) {
|
|
213
|
-
_this2._onerror(event);
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
// apply listen types
|
|
217
|
-
var _iteratorNormalCompletion = true;
|
|
218
|
-
var _didIteratorError = false;
|
|
219
|
-
var _iteratorError = undefined;
|
|
220
|
-
|
|
221
|
-
try {
|
|
222
|
-
for (var _iterator = Object.keys(this._listeners)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
223
|
-
var type = _step.value;
|
|
224
|
-
|
|
225
|
-
this._eventSource.addEventListener(type, this._onevent_wrapped);
|
|
226
|
-
}
|
|
227
|
-
} catch (err) {
|
|
228
|
-
_didIteratorError = true;
|
|
229
|
-
_iteratorError = err;
|
|
230
|
-
} finally {
|
|
231
|
-
try {
|
|
232
|
-
if (!_iteratorNormalCompletion && _iterator.return) {
|
|
233
|
-
_iterator.return();
|
|
234
|
-
}
|
|
235
|
-
} finally {
|
|
236
|
-
if (_didIteratorError) {
|
|
237
|
-
throw _iteratorError;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}, {
|
|
243
|
-
key: '_onopen',
|
|
244
|
-
value: function _onopen(event) {
|
|
245
|
-
if (this.readyState === 0) {
|
|
246
|
-
this.readyState = 1;
|
|
247
|
-
this.onopen(event);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}, {
|
|
251
|
-
key: '_onerror',
|
|
252
|
-
value: function _onerror(event) {
|
|
253
|
-
var _this3 = this;
|
|
254
|
-
|
|
255
|
-
if (this.readyState === 1) {
|
|
256
|
-
this.readyState = 0;
|
|
257
|
-
this.onerror(event);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
if (this._eventSource) {
|
|
261
|
-
if (this._eventSource.readyState === 2) {
|
|
262
|
-
// reconnect with new object
|
|
263
|
-
this._eventSource.close();
|
|
264
|
-
this._eventSource = null;
|
|
265
|
-
|
|
266
|
-
// reconnect after random timeout < max_retry_time
|
|
267
|
-
var timeout = Math.round(this.max_retry_time * Math.random());
|
|
268
|
-
this._timer = setTimeout(function () {
|
|
269
|
-
return _this3._start();
|
|
270
|
-
}, timeout);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}, {
|
|
275
|
-
key: '_onevent',
|
|
276
|
-
value: function _onevent(event) {
|
|
277
|
-
if (event.lastEventId) {
|
|
278
|
-
this._lastEventId = event.lastEventId;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
var listenersForType = this._listeners[event.type];
|
|
282
|
-
if (listenersForType != null) {
|
|
283
|
-
var _arr = [].concat(_toConsumableArray(listenersForType));
|
|
284
|
-
|
|
285
|
-
// operate on a copy
|
|
286
|
-
for (var _i = 0; _i < _arr.length; _i++) {
|
|
287
|
-
var listener = _arr[_i];
|
|
288
|
-
listener(event);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
if (event.type === 'message') {
|
|
293
|
-
this.onmessage(event);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
}, {
|
|
297
|
-
key: 'onopen',
|
|
298
|
-
value: function onopen(event) {
|
|
299
|
-
// may be overridden
|
|
300
|
-
}
|
|
301
|
-
}, {
|
|
302
|
-
key: 'onerror',
|
|
303
|
-
value: function onerror(event) {
|
|
304
|
-
// may be overridden
|
|
305
|
-
}
|
|
306
|
-
}, {
|
|
307
|
-
key: 'onmessage',
|
|
308
|
-
value: function onmessage(event) {
|
|
309
|
-
// may be overridden
|
|
310
|
-
}
|
|
311
|
-
}, {
|
|
312
|
-
key: 'close',
|
|
313
|
-
value: function close() {
|
|
314
|
-
if (this._timer) {
|
|
315
|
-
clearTimeout(this._timer);
|
|
316
|
-
this._timer = null;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
if (this._eventSource) {
|
|
320
|
-
this._eventSource.close();
|
|
321
|
-
this._eventSource = null;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
this.readyState = 2;
|
|
325
|
-
}
|
|
326
|
-
}, {
|
|
327
|
-
key: 'addEventListener',
|
|
328
|
-
value: function addEventListener(inType, callback) {
|
|
329
|
-
var type = inType.toString();
|
|
330
|
-
|
|
331
|
-
if (!(type in this._listeners)) {
|
|
332
|
-
this._listeners[type] = [];
|
|
333
|
-
if (this._eventSource) {
|
|
334
|
-
this._eventSource.addEventListener(type, this._onevent_wrapped);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
var listenersForType = this._listeners[type];
|
|
339
|
-
if (!listenersForType.includes(callback)) {
|
|
340
|
-
this._listeners[type] = [].concat(_toConsumableArray(listenersForType), [callback]);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
}, {
|
|
344
|
-
key: 'removeEventListener',
|
|
345
|
-
value: function removeEventListener(inType, callback) {
|
|
346
|
-
var type = inType.toString();
|
|
347
|
-
|
|
348
|
-
if (type in this._listeners) {
|
|
349
|
-
|
|
350
|
-
var listenersForType = this._listeners[type];
|
|
351
|
-
|
|
352
|
-
var updatedListenersForType = listenersForType.filter(function (l) {
|
|
353
|
-
return l !== callback;
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
if (updatedListenersForType.length > 0) {
|
|
357
|
-
this._listeners[type] = updatedListenersForType;
|
|
358
|
-
} else {
|
|
359
|
-
delete this._listeners[type];
|
|
360
|
-
if (this._eventSource) {
|
|
361
|
-
this._eventSource.removeEventListener(type, this._onevent_wrapped);
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
}]);
|
|
367
|
-
|
|
368
|
-
return ReconnectingEventSource;
|
|
369
|
-
}();
|
|
370
|
-
|
|
371
|
-
exports.default = ReconnectingEventSource;
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
/***/ })
|
|
375
|
-
/******/ ]);
|
|
1
|
+
var ReconnectingEventSource=function(e){var t={};function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)n.d(r,i,function(t){return e[t]}.bind(null,i));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){var r,i,o;i=[e,n(1)],void 0===(o="function"==typeof(r=function(e,t){"use strict";var n,r=(n=t)&&n.__esModule?n:{default:n};e.exports=r.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";function t(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)}Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),r=function(){function e(t,n){var r=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this._configuration=null!=n?Object.assign({},n):null,this._eventSource=null,this._lastEventId=null,this._timer=null,this._listeners={},this.url=t,this.readyState=0,this.max_retry_time=3e3,null!=this._configuration&&(this._configuration.lastEventId&&(this._lastEventId=this._configuration.lastEventId,delete this._configuration.lastEventId),this._configuration.max_retry_time&&(this.max_retry_time=this._configuration.max_retry_time,delete this._configuration.max_retry_time)),this._onevent_wrapped=function(e){r._onevent(e)},this._start()}return n(e,[{key:"_start",value:function(){var e=this,t=this.url;this._lastEventId&&(-1===t.indexOf("?")?t+="?":t+="&",t+="lastEventId="+encodeURIComponent(this._lastEventId)),this._eventSource=new EventSource(t,this._configuration),this._eventSource.onopen=function(t){e._onopen(t)},this._eventSource.onerror=function(t){e._onerror(t)},this._eventSource.onmessage=function(t){e.onmessage(t)};var n=!0,r=!1,i=void 0;try{for(var o,s=Object.keys(this._listeners)[Symbol.iterator]();!(n=(o=s.next()).done);n=!0){var u=o.value;this._eventSource.addEventListener(u,this._onevent_wrapped)}}catch(e){r=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(r)throw i}}}},{key:"_onopen",value:function(e){0===this.readyState&&(this.readyState=1,this.onopen(e))}},{key:"_onerror",value:function(e){var t=this;if(1===this.readyState&&(this.readyState=0,this.onerror(e)),this._eventSource&&2===this._eventSource.readyState){this._eventSource.close(),this._eventSource=null;var n=Math.round(this.max_retry_time*Math.random());this._timer=setTimeout((function(){return t._start()}),n)}}},{key:"_onevent",value:function(e){e.lastEventId&&(this._lastEventId=e.lastEventId);var n=this._listeners[e.type];if(null!=n)for(var r=[].concat(t(n)),i=0;i<r.length;i++)(0,r[i])(e);"message"===e.type&&this.onmessage(e)}},{key:"onopen",value:function(e){}},{key:"onerror",value:function(e){}},{key:"onmessage",value:function(e){}},{key:"close",value:function(){this._timer&&(clearTimeout(this._timer),this._timer=null),this._eventSource&&(this._eventSource.close(),this._eventSource=null),this.readyState=2}},{key:"addEventListener",value:function(e,n){var r=e.toString();r in this._listeners||(this._listeners[r]=[],this._eventSource&&this._eventSource.addEventListener(r,this._onevent_wrapped));var i=this._listeners[r];i.includes(n)||(this._listeners[r]=[].concat(t(i),[n]))}},{key:"removeEventListener",value:function(e,t){var n=e.toString();if(n in this._listeners){var r=this._listeners[n].filter((function(e){return e!==t}));r.length>0?this._listeners[n]=r:(delete this._listeners[n],this._eventSource&&this._eventSource.removeEventListener(n,this._onevent_wrapped))}}}]),e}();e.default=r})?r.apply(t,i):r)||(e.exports=o)}]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var ReconnectingEventSource=function(e){function
|
|
1
|
+
var ReconnectingEventSource=function(e){var t={};function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)n.d(r,i,function(t){return e[t]}.bind(null,i));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){var r,i,o;i=[e,n(1)],void 0===(o="function"==typeof(r=function(e,t){"use strict";var n,r=(n=t)&&n.__esModule?n:{default:n};e.exports=r.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";function t(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)}Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),r=function(){function e(t,n){var r=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this._configuration=null!=n?Object.assign({},n):null,this._eventSource=null,this._lastEventId=null,this._timer=null,this._listeners={},this.url=t,this.readyState=0,this.max_retry_time=3e3,null!=this._configuration&&(this._configuration.lastEventId&&(this._lastEventId=this._configuration.lastEventId,delete this._configuration.lastEventId),this._configuration.max_retry_time&&(this.max_retry_time=this._configuration.max_retry_time,delete this._configuration.max_retry_time)),this._onevent_wrapped=function(e){r._onevent(e)},this._start()}return n(e,[{key:"_start",value:function(){var e=this,t=this.url;this._lastEventId&&(-1===t.indexOf("?")?t+="?":t+="&",t+="lastEventId="+encodeURIComponent(this._lastEventId)),this._eventSource=new EventSource(t,this._configuration),this._eventSource.onopen=function(t){e._onopen(t)},this._eventSource.onerror=function(t){e._onerror(t)},this._eventSource.onmessage=function(t){e.onmessage(t)};var n=!0,r=!1,i=void 0;try{for(var o,s=Object.keys(this._listeners)[Symbol.iterator]();!(n=(o=s.next()).done);n=!0){var u=o.value;this._eventSource.addEventListener(u,this._onevent_wrapped)}}catch(e){r=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(r)throw i}}}},{key:"_onopen",value:function(e){0===this.readyState&&(this.readyState=1,this.onopen(e))}},{key:"_onerror",value:function(e){var t=this;if(1===this.readyState&&(this.readyState=0,this.onerror(e)),this._eventSource&&2===this._eventSource.readyState){this._eventSource.close(),this._eventSource=null;var n=Math.round(this.max_retry_time*Math.random());this._timer=setTimeout((function(){return t._start()}),n)}}},{key:"_onevent",value:function(e){e.lastEventId&&(this._lastEventId=e.lastEventId);var n=this._listeners[e.type];if(null!=n)for(var r=[].concat(t(n)),i=0;i<r.length;i++)(0,r[i])(e);"message"===e.type&&this.onmessage(e)}},{key:"onopen",value:function(e){}},{key:"onerror",value:function(e){}},{key:"onmessage",value:function(e){}},{key:"close",value:function(){this._timer&&(clearTimeout(this._timer),this._timer=null),this._eventSource&&(this._eventSource.close(),this._eventSource=null),this.readyState=2}},{key:"addEventListener",value:function(e,n){var r=e.toString();r in this._listeners||(this._listeners[r]=[],this._eventSource&&this._eventSource.addEventListener(r,this._onevent_wrapped));var i=this._listeners[r];i.includes(n)||(this._listeners[r]=[].concat(t(i),[n]))}},{key:"removeEventListener",value:function(e,t){var n=e.toString();if(n in this._listeners){var r=this._listeners[n].filter((function(e){return e!==t}));r.length>0?this._listeners[n]=r:(delete this._listeners[n],this._eventSource&&this._eventSource.removeEventListener(n,this._onevent_wrapped))}}}]),e}();e.default=r})?r.apply(t,i):r)||(e.exports=o)}]);
|
package/lib/index.js
CHANGED
|
@@ -1,337 +1 @@
|
|
|
1
|
-
module.exports =
|
|
2
|
-
/******/ (function(modules) { // webpackBootstrap
|
|
3
|
-
/******/ // The module cache
|
|
4
|
-
/******/ var installedModules = {};
|
|
5
|
-
/******/
|
|
6
|
-
/******/ // The require function
|
|
7
|
-
/******/ function __webpack_require__(moduleId) {
|
|
8
|
-
/******/
|
|
9
|
-
/******/ // Check if module is in cache
|
|
10
|
-
/******/ if(installedModules[moduleId]) {
|
|
11
|
-
/******/ return installedModules[moduleId].exports;
|
|
12
|
-
/******/ }
|
|
13
|
-
/******/ // Create a new module (and put it into the cache)
|
|
14
|
-
/******/ var module = installedModules[moduleId] = {
|
|
15
|
-
/******/ i: moduleId,
|
|
16
|
-
/******/ l: false,
|
|
17
|
-
/******/ exports: {}
|
|
18
|
-
/******/ };
|
|
19
|
-
/******/
|
|
20
|
-
/******/ // Execute the module function
|
|
21
|
-
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
|
22
|
-
/******/
|
|
23
|
-
/******/ // Flag the module as loaded
|
|
24
|
-
/******/ module.l = true;
|
|
25
|
-
/******/
|
|
26
|
-
/******/ // Return the exports of the module
|
|
27
|
-
/******/ return module.exports;
|
|
28
|
-
/******/ }
|
|
29
|
-
/******/
|
|
30
|
-
/******/
|
|
31
|
-
/******/ // expose the modules object (__webpack_modules__)
|
|
32
|
-
/******/ __webpack_require__.m = modules;
|
|
33
|
-
/******/
|
|
34
|
-
/******/ // expose the module cache
|
|
35
|
-
/******/ __webpack_require__.c = installedModules;
|
|
36
|
-
/******/
|
|
37
|
-
/******/ // define getter function for harmony exports
|
|
38
|
-
/******/ __webpack_require__.d = function(exports, name, getter) {
|
|
39
|
-
/******/ if(!__webpack_require__.o(exports, name)) {
|
|
40
|
-
/******/ Object.defineProperty(exports, name, {
|
|
41
|
-
/******/ configurable: false,
|
|
42
|
-
/******/ enumerable: true,
|
|
43
|
-
/******/ get: getter
|
|
44
|
-
/******/ });
|
|
45
|
-
/******/ }
|
|
46
|
-
/******/ };
|
|
47
|
-
/******/
|
|
48
|
-
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
|
49
|
-
/******/ __webpack_require__.n = function(module) {
|
|
50
|
-
/******/ var getter = module && module.__esModule ?
|
|
51
|
-
/******/ function getDefault() { return module['default']; } :
|
|
52
|
-
/******/ function getModuleExports() { return module; };
|
|
53
|
-
/******/ __webpack_require__.d(getter, 'a', getter);
|
|
54
|
-
/******/ return getter;
|
|
55
|
-
/******/ };
|
|
56
|
-
/******/
|
|
57
|
-
/******/ // Object.prototype.hasOwnProperty.call
|
|
58
|
-
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
|
59
|
-
/******/
|
|
60
|
-
/******/ // __webpack_public_path__
|
|
61
|
-
/******/ __webpack_require__.p = "";
|
|
62
|
-
/******/
|
|
63
|
-
/******/ // Load entry module and return exports
|
|
64
|
-
/******/ return __webpack_require__(__webpack_require__.s = 0);
|
|
65
|
-
/******/ })
|
|
66
|
-
/************************************************************************/
|
|
67
|
-
/******/ ([
|
|
68
|
-
/* 0 */
|
|
69
|
-
/***/ (function(module, exports, __webpack_require__) {
|
|
70
|
-
|
|
71
|
-
"use strict";
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
Object.defineProperty(exports, "__esModule", {
|
|
75
|
-
value: true
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
var _reconnectingEventsource = __webpack_require__(1);
|
|
79
|
-
|
|
80
|
-
var _reconnectingEventsource2 = _interopRequireDefault(_reconnectingEventsource);
|
|
81
|
-
|
|
82
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
83
|
-
|
|
84
|
-
exports.default = _reconnectingEventsource2.default;
|
|
85
|
-
|
|
86
|
-
/***/ }),
|
|
87
|
-
/* 1 */
|
|
88
|
-
/***/ (function(module, exports, __webpack_require__) {
|
|
89
|
-
|
|
90
|
-
"use strict";
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
Object.defineProperty(exports, "__esModule", {
|
|
94
|
-
value: true
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
98
|
-
|
|
99
|
-
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
|
100
|
-
|
|
101
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
102
|
-
|
|
103
|
-
// MIT License:
|
|
104
|
-
//
|
|
105
|
-
// Copyright (C) 2017 Fanout, Inc.
|
|
106
|
-
//
|
|
107
|
-
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
108
|
-
// of this software and associated documentation files (the "Software"), to
|
|
109
|
-
// deal in the Software without restriction, including without limitation the
|
|
110
|
-
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
111
|
-
// sell copies of the Software, and to permit persons to whom the Software is
|
|
112
|
-
// furnished to do so, subject to the following conditions:
|
|
113
|
-
//
|
|
114
|
-
// The above copyright notice and this permission notice shall be included in
|
|
115
|
-
// all copies or substantial portions of the Software.
|
|
116
|
-
//
|
|
117
|
-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
118
|
-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
119
|
-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
120
|
-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
121
|
-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
122
|
-
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
123
|
-
// IN THE SOFTWARE.
|
|
124
|
-
|
|
125
|
-
var ReconnectingEventSource = function () {
|
|
126
|
-
function ReconnectingEventSource(url, configuration) {
|
|
127
|
-
var _this = this;
|
|
128
|
-
|
|
129
|
-
_classCallCheck(this, ReconnectingEventSource);
|
|
130
|
-
|
|
131
|
-
this._eventSource = null;
|
|
132
|
-
this._lastEventId = null;
|
|
133
|
-
this._timer = null;
|
|
134
|
-
this._listeners = {};
|
|
135
|
-
|
|
136
|
-
this.url = url;
|
|
137
|
-
this.readyState = 0;
|
|
138
|
-
this.max_retry_time = configuration.max_retry_time || 3000;
|
|
139
|
-
|
|
140
|
-
this._configuration = configuration != null ? Object.assign({}, configuration) : null;
|
|
141
|
-
|
|
142
|
-
if (this._configuration != null && this._configuration.lastEventId) {
|
|
143
|
-
this._lastEventId = this._configuration.lastEventId;
|
|
144
|
-
delete this._configuration['lastEventId'];
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
this._onevent_wrapped = function (event) {
|
|
148
|
-
_this._onevent(event);
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
this._start();
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
_createClass(ReconnectingEventSource, [{
|
|
155
|
-
key: '_start',
|
|
156
|
-
value: function _start() {
|
|
157
|
-
var _this2 = this;
|
|
158
|
-
|
|
159
|
-
var url = this.url;
|
|
160
|
-
|
|
161
|
-
if (this._lastEventId) {
|
|
162
|
-
if (url.indexOf('?') === -1) {
|
|
163
|
-
url += '?';
|
|
164
|
-
} else {
|
|
165
|
-
url += '&';
|
|
166
|
-
}
|
|
167
|
-
url += 'lastEventId=' + encodeURIComponent(this._lastEventId);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
this._eventSource = new EventSource(url, this._configuration);
|
|
171
|
-
|
|
172
|
-
this._eventSource.onopen = function (event) {
|
|
173
|
-
_this2._onopen(event);
|
|
174
|
-
};
|
|
175
|
-
this._eventSource.onerror = function (event) {
|
|
176
|
-
_this2._onerror(event);
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
// apply listen types
|
|
180
|
-
var _iteratorNormalCompletion = true;
|
|
181
|
-
var _didIteratorError = false;
|
|
182
|
-
var _iteratorError = undefined;
|
|
183
|
-
|
|
184
|
-
try {
|
|
185
|
-
for (var _iterator = Object.keys(this._listeners)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
186
|
-
var type = _step.value;
|
|
187
|
-
|
|
188
|
-
this._eventSource.addEventListener(type, this._onevent_wrapped);
|
|
189
|
-
}
|
|
190
|
-
} catch (err) {
|
|
191
|
-
_didIteratorError = true;
|
|
192
|
-
_iteratorError = err;
|
|
193
|
-
} finally {
|
|
194
|
-
try {
|
|
195
|
-
if (!_iteratorNormalCompletion && _iterator.return) {
|
|
196
|
-
_iterator.return();
|
|
197
|
-
}
|
|
198
|
-
} finally {
|
|
199
|
-
if (_didIteratorError) {
|
|
200
|
-
throw _iteratorError;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}, {
|
|
206
|
-
key: '_onopen',
|
|
207
|
-
value: function _onopen(event) {
|
|
208
|
-
if (this.readyState === 0) {
|
|
209
|
-
this.readyState = 1;
|
|
210
|
-
this.onopen(event);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}, {
|
|
214
|
-
key: '_onerror',
|
|
215
|
-
value: function _onerror(event) {
|
|
216
|
-
var _this3 = this;
|
|
217
|
-
|
|
218
|
-
if (this.readyState === 1) {
|
|
219
|
-
this.readyState = 0;
|
|
220
|
-
this.onerror(event);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (this._eventSource) {
|
|
224
|
-
if (this._eventSource.readyState === 2) {
|
|
225
|
-
// reconnect with new object
|
|
226
|
-
this._eventSource.close();
|
|
227
|
-
this._eventSource = null;
|
|
228
|
-
|
|
229
|
-
// reconnect after random timeout < max_retry_time
|
|
230
|
-
var timeout = Math.round(this.max_retry_time * Math.random());
|
|
231
|
-
this._timer = setTimeout(function () {
|
|
232
|
-
return _this3._start();
|
|
233
|
-
}, timeout);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}, {
|
|
238
|
-
key: '_onevent',
|
|
239
|
-
value: function _onevent(event) {
|
|
240
|
-
if (event.lastEventId) {
|
|
241
|
-
this._lastEventId = event.lastEventId;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
var listenersForType = this._listeners[event.type];
|
|
245
|
-
if (listenersForType != null) {
|
|
246
|
-
// operate on a copy
|
|
247
|
-
var _arr = [].concat(_toConsumableArray(listenersForType));
|
|
248
|
-
|
|
249
|
-
for (var _i = 0; _i < _arr.length; _i++) {
|
|
250
|
-
var listener = _arr[_i];
|
|
251
|
-
listener(event);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if (event.type === 'message') {
|
|
256
|
-
this.onmessage(event);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}, {
|
|
260
|
-
key: 'onopen',
|
|
261
|
-
value: function onopen(event) {
|
|
262
|
-
// may be overridden
|
|
263
|
-
}
|
|
264
|
-
}, {
|
|
265
|
-
key: 'onerror',
|
|
266
|
-
value: function onerror(event) {
|
|
267
|
-
// may be overridden
|
|
268
|
-
}
|
|
269
|
-
}, {
|
|
270
|
-
key: 'onmessage',
|
|
271
|
-
value: function onmessage(event) {
|
|
272
|
-
// may be overridden
|
|
273
|
-
}
|
|
274
|
-
}, {
|
|
275
|
-
key: 'close',
|
|
276
|
-
value: function close() {
|
|
277
|
-
if (this._timer) {
|
|
278
|
-
clearTimeout(this._timer);
|
|
279
|
-
this._timer = null;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
if (this._eventSource) {
|
|
283
|
-
this._eventSource.close();
|
|
284
|
-
this._eventSource = null;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
this.readyState = 2;
|
|
288
|
-
}
|
|
289
|
-
}, {
|
|
290
|
-
key: 'addEventListener',
|
|
291
|
-
value: function addEventListener(inType, callback) {
|
|
292
|
-
var type = inType.toString();
|
|
293
|
-
|
|
294
|
-
if (!(type in this._listeners)) {
|
|
295
|
-
this._listeners[type] = [];
|
|
296
|
-
if (this._eventSource) {
|
|
297
|
-
this._eventSource.addEventListener(type, this._onevent_wrapped);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
var listenersForType = this._listeners[type];
|
|
302
|
-
if (!listenersForType.includes(callback)) {
|
|
303
|
-
this._listeners[type] = [].concat(_toConsumableArray(listenersForType), [callback]);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}, {
|
|
307
|
-
key: 'removeEventListener',
|
|
308
|
-
value: function removeEventListener(inType, callback) {
|
|
309
|
-
var type = inType.toString();
|
|
310
|
-
|
|
311
|
-
if (type in this._listeners) {
|
|
312
|
-
|
|
313
|
-
var listenersForType = this._listeners[type];
|
|
314
|
-
|
|
315
|
-
var updatedListenersForType = listenersForType.filter(function (l) {
|
|
316
|
-
return l !== callback;
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
if (updatedListenersForType.length > 0) {
|
|
320
|
-
this._listeners[type] = updatedListenersForType;
|
|
321
|
-
} else {
|
|
322
|
-
delete this._listeners[type];
|
|
323
|
-
if (this._eventSource) {
|
|
324
|
-
this._eventSource.removeEventListener(type, this._onevent_wrapped);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
}]);
|
|
330
|
-
|
|
331
|
-
return ReconnectingEventSource;
|
|
332
|
-
}();
|
|
333
|
-
|
|
334
|
-
exports.default = ReconnectingEventSource;
|
|
335
|
-
|
|
336
|
-
/***/ })
|
|
337
|
-
/******/ ]);
|
|
1
|
+
module.exports=function(e){var t={};function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)n.d(r,i,function(t){return e[t]}.bind(null,i));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r,i=n(1),o=(r=i)&&r.__esModule?r:{default:r};t.default=o.default},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();function i(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)}var o=function(){function e(t,n){var r=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this._configuration=null!=n?Object.assign({},n):null,this._eventSource=null,this._lastEventId=null,this._timer=null,this._listeners={},this.url=t,this.readyState=0,this.max_retry_time=3e3,null!=this._configuration&&(this._configuration.lastEventId&&(this._lastEventId=this._configuration.lastEventId,delete this._configuration.lastEventId),this._configuration.max_retry_time&&(this.max_retry_time=this._configuration.max_retry_time,delete this._configuration.max_retry_time)),this._onevent_wrapped=function(e){r._onevent(e)},this._start()}return r(e,[{key:"_start",value:function(){var e=this,t=this.url;this._lastEventId&&(-1===t.indexOf("?")?t+="?":t+="&",t+="lastEventId="+encodeURIComponent(this._lastEventId)),this._eventSource=new EventSource(t,this._configuration),this._eventSource.onopen=function(t){e._onopen(t)},this._eventSource.onerror=function(t){e._onerror(t)},this._eventSource.onmessage=function(t){e.onmessage(t)};var n=!0,r=!1,i=void 0;try{for(var o,s=Object.keys(this._listeners)[Symbol.iterator]();!(n=(o=s.next()).done);n=!0){var u=o.value;this._eventSource.addEventListener(u,this._onevent_wrapped)}}catch(e){r=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(r)throw i}}}},{key:"_onopen",value:function(e){0===this.readyState&&(this.readyState=1,this.onopen(e))}},{key:"_onerror",value:function(e){var t=this;if(1===this.readyState&&(this.readyState=0,this.onerror(e)),this._eventSource&&2===this._eventSource.readyState){this._eventSource.close(),this._eventSource=null;var n=Math.round(this.max_retry_time*Math.random());this._timer=setTimeout((function(){return t._start()}),n)}}},{key:"_onevent",value:function(e){e.lastEventId&&(this._lastEventId=e.lastEventId);var t=this._listeners[e.type];if(null!=t)for(var n=[].concat(i(t)),r=0;r<n.length;r++){(0,n[r])(e)}"message"===e.type&&this.onmessage(e)}},{key:"onopen",value:function(e){}},{key:"onerror",value:function(e){}},{key:"onmessage",value:function(e){}},{key:"close",value:function(){this._timer&&(clearTimeout(this._timer),this._timer=null),this._eventSource&&(this._eventSource.close(),this._eventSource=null),this.readyState=2}},{key:"addEventListener",value:function(e,t){var n=e.toString();n in this._listeners||(this._listeners[n]=[],this._eventSource&&this._eventSource.addEventListener(n,this._onevent_wrapped));var r=this._listeners[n];r.includes(t)||(this._listeners[n]=[].concat(i(r),[t]))}},{key:"removeEventListener",value:function(e,t){var n=e.toString();if(n in this._listeners){var r=this._listeners[n].filter((function(e){return e!==t}));r.length>0?this._listeners[n]=r:(delete this._listeners[n],this._eventSource&&this._eventSource.removeEventListener(n,this._onevent_wrapped))}}}]),e}();t.default=o}]);
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reconnecting-eventsource",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "wrapper library around the JavaScript EventSource API to ensure it maintains a connection to the server.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
+
"typings": "reconnecting-eventsource.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"clean": "rimraf lib dist",
|
|
8
9
|
"build": "npm run build:commonjs && npm run build:umd && npm run build:umd:min",
|
|
@@ -12,7 +13,7 @@
|
|
|
12
13
|
"check:src": "npm run lint",
|
|
13
14
|
"lint": "eslint src",
|
|
14
15
|
"prepublishOnly": "npm run clean && npm run check:src && npm run build",
|
|
15
|
-
"test": "node test"
|
|
16
|
+
"test": "npm run build && node test"
|
|
16
17
|
},
|
|
17
18
|
"repository": {
|
|
18
19
|
"type": "git",
|
|
@@ -45,11 +46,12 @@
|
|
|
45
46
|
"babel-plugin-transform-es2015-modules-umd": "^6.24.1",
|
|
46
47
|
"babel-preset-env": "^1.7.0",
|
|
47
48
|
"cross-env": "^5.0.1",
|
|
48
|
-
"eslint": "^
|
|
49
|
+
"eslint": "^6.6.0",
|
|
49
50
|
"eventsource": "^1.0.7",
|
|
50
51
|
"rimraf": "^2.6.1",
|
|
51
52
|
"sse": "0.0.8",
|
|
52
53
|
"stoppable": "^1.1.0",
|
|
53
|
-
"webpack": "^
|
|
54
|
+
"webpack": "^4.41.2",
|
|
55
|
+
"webpack-cli": "^3.3.10"
|
|
54
56
|
}
|
|
55
57
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface ReconnectableEventSourceInit extends EventSourceInit {
|
|
2
|
+
// the maximum time to wait before attempting to reconnect in ms, default `3000`
|
|
3
|
+
// note: wait time is randomised to prevent all clients from attempting to reconnect simultaneously
|
|
4
|
+
max_retry_time?: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export default class ReconnectingEventSource extends EventSource {
|
|
8
|
+
constructor(url: string, configuration?: ReconnectableEventSourceInit);
|
|
9
|
+
|
|
10
|
+
readonly max_retry_time: number;
|
|
11
|
+
}
|
|
@@ -23,6 +23,8 @@
|
|
|
23
23
|
export default class ReconnectingEventSource {
|
|
24
24
|
|
|
25
25
|
constructor(url, configuration) {
|
|
26
|
+
this._configuration = configuration != null ? Object.assign({}, configuration) : null;
|
|
27
|
+
|
|
26
28
|
this._eventSource = null;
|
|
27
29
|
this._lastEventId = null;
|
|
28
30
|
this._timer = null;
|
|
@@ -30,13 +32,18 @@ export default class ReconnectingEventSource {
|
|
|
30
32
|
|
|
31
33
|
this.url = url;
|
|
32
34
|
this.readyState = 0;
|
|
33
|
-
this.max_retry_time =
|
|
35
|
+
this.max_retry_time = 3000;
|
|
34
36
|
|
|
35
|
-
this._configuration
|
|
37
|
+
if (this._configuration != null) {
|
|
38
|
+
if (this._configuration.lastEventId) {
|
|
39
|
+
this._lastEventId = this._configuration.lastEventId;
|
|
40
|
+
delete this._configuration['lastEventId'];
|
|
41
|
+
}
|
|
36
42
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
43
|
+
if (this._configuration.max_retry_time) {
|
|
44
|
+
this.max_retry_time = this._configuration.max_retry_time;
|
|
45
|
+
delete this._configuration['max_retry_time'];
|
|
46
|
+
}
|
|
40
47
|
}
|
|
41
48
|
|
|
42
49
|
this._onevent_wrapped = event => { this._onevent(event); };
|
|
@@ -60,6 +67,7 @@ export default class ReconnectingEventSource {
|
|
|
60
67
|
|
|
61
68
|
this._eventSource.onopen = event => { this._onopen(event); };
|
|
62
69
|
this._eventSource.onerror = event => { this._onerror(event); };
|
|
70
|
+
this._eventSource.onmessage = event => { this.onmessage(event); };
|
|
63
71
|
|
|
64
72
|
// apply listen types
|
|
65
73
|
for (const type of Object.keys(this._listeners)) {
|
package/test.js
CHANGED
|
@@ -59,7 +59,7 @@ const main = async () => {
|
|
|
59
59
|
const server1SseUrl = `http://localhost:${server1.address().port}/sse`;
|
|
60
60
|
|
|
61
61
|
// Set up Eventsource
|
|
62
|
-
const eventSource = new ReconnectingEventSource(server1SseUrl
|
|
62
|
+
const eventSource = new ReconnectingEventSource(server1SseUrl);
|
|
63
63
|
eventSource.addEventListener('error', (error) => {
|
|
64
64
|
console.error('eventSource error', error);
|
|
65
65
|
});
|
|
@@ -94,6 +94,13 @@ const main = async () => {
|
|
|
94
94
|
// clean up
|
|
95
95
|
eventSource.close();
|
|
96
96
|
clock.destroy();
|
|
97
|
+
|
|
98
|
+
// test construction with config
|
|
99
|
+
const eventSource2 = new ReconnectingEventSource(server1SseUrl, {
|
|
100
|
+
lastEventId: 'abc',
|
|
101
|
+
max_retry_time: 5000,
|
|
102
|
+
});
|
|
103
|
+
eventSource2.close();
|
|
97
104
|
};
|
|
98
105
|
|
|
99
106
|
if (require.main === module) {
|
package/tsconfig.json
ADDED
package/webpack.config.js
CHANGED
|
@@ -92,33 +92,20 @@ function addBabelRule(rules) {
|
|
|
92
92
|
return [...rules, babelRule];
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
function addMinifyPlugin(plugins) {
|
|
96
|
-
if (BUILD_TARGET === "umd-min") {
|
|
97
|
-
const minifyPlugin = new webpack.optimize.UglifyJsPlugin({
|
|
98
|
-
compress: { warnings: false }
|
|
99
|
-
});
|
|
100
|
-
return [...plugins, minifyPlugin];
|
|
101
|
-
}
|
|
102
|
-
return plugins;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
95
|
const entry = buildEntry();
|
|
106
96
|
const output = buildOutput();
|
|
107
97
|
|
|
108
98
|
let rules = [];
|
|
109
99
|
rules = addBabelRule(rules);
|
|
110
100
|
|
|
111
|
-
let plugins = [];
|
|
112
|
-
plugins = addMinifyPlugin(plugins);
|
|
113
|
-
|
|
114
101
|
const config = {
|
|
102
|
+
mode: 'production',
|
|
115
103
|
context: srcDir,
|
|
116
104
|
entry,
|
|
117
105
|
output,
|
|
118
106
|
module: {
|
|
119
107
|
rules
|
|
120
108
|
},
|
|
121
|
-
plugins
|
|
122
109
|
};
|
|
123
110
|
|
|
124
111
|
module.exports = config;
|