reconnecting-eventsource 1.0.4 → 1.3.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 ADDED
@@ -0,0 +1 @@
1
+ *.d.ts
@@ -0,0 +1,6 @@
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ <inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
5
+ </profile>
6
+ </component>
package/.idea/misc.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager">
4
+ <output url="file://$PROJECT_DIR$/out" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/reconnecting-eventsource.iml" filepath="$PROJECT_DIR$/.idea/reconnecting-eventsource.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="JAVA_MODULE" version="4">
3
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+ <exclude-output />
5
+ <content url="file://$MODULE_DIR$" />
6
+ <orderEntry type="inheritedJdk" />
7
+ <orderEntry type="sourceFolder" forTests="false" />
8
+ </component>
9
+ </module>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
package/README.md CHANGED
@@ -48,11 +48,13 @@ Like the [`EventSource`](https://developer.mozilla.org/en-US/docs/Web/API/EventS
48
48
  // the maximum time to wait before attempting to reconnect in ms, default `3000`
49
49
  // note: wait time is randomised to prevent all clients from attempting to reconnect simultaneously
50
50
  max_retry_time: 3000,
51
+
52
+ // underlying EventSource class, default `EventSource`
53
+ eventSourceClass: EventSource,
51
54
  }
52
55
 
53
56
  ```
54
57
 
55
-
56
58
  ## Building from source
57
59
 
58
60
  If you wish to build this project, check out this repository and modify the source files in `src/`. Then, run the following command:
@@ -1,387 +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, { enumerable: true, get: getter });
41
- /******/ }
42
- /******/ };
43
- /******/
44
- /******/ // define __esModule on exports
45
- /******/ __webpack_require__.r = function(exports) {
46
- /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
47
- /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
48
- /******/ }
49
- /******/ Object.defineProperty(exports, '__esModule', { value: true });
50
- /******/ };
51
- /******/
52
- /******/ // create a fake namespace object
53
- /******/ // mode & 1: value is a module id, require it
54
- /******/ // mode & 2: merge all properties of value into the ns
55
- /******/ // mode & 4: return value when already ns object
56
- /******/ // mode & 8|1: behave like require
57
- /******/ __webpack_require__.t = function(value, mode) {
58
- /******/ if(mode & 1) value = __webpack_require__(value);
59
- /******/ if(mode & 8) return value;
60
- /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
61
- /******/ var ns = Object.create(null);
62
- /******/ __webpack_require__.r(ns);
63
- /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
64
- /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
65
- /******/ return ns;
66
- /******/ };
67
- /******/
68
- /******/ // getDefaultExport function for compatibility with non-harmony modules
69
- /******/ __webpack_require__.n = function(module) {
70
- /******/ var getter = module && module.__esModule ?
71
- /******/ function getDefault() { return module['default']; } :
72
- /******/ function getModuleExports() { return module; };
73
- /******/ __webpack_require__.d(getter, 'a', getter);
74
- /******/ return getter;
75
- /******/ };
76
- /******/
77
- /******/ // Object.prototype.hasOwnProperty.call
78
- /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
79
- /******/
80
- /******/ // __webpack_public_path__
81
- /******/ __webpack_require__.p = "";
82
- /******/
83
- /******/
84
- /******/ // Load entry module and return exports
85
- /******/ return __webpack_require__(__webpack_require__.s = 0);
86
- /******/ })
87
- /************************************************************************/
88
- /******/ ([
89
- /* 0 */
90
- /***/ (function(module, exports, __webpack_require__) {
91
-
92
- var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;(function (global, factory) {
93
- if (true) {
94
- !(__WEBPACK_AMD_DEFINE_ARRAY__ = [module, __webpack_require__(1)], __WEBPACK_AMD_DEFINE_FACTORY__ = (factory),
95
- __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
96
- (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__),
97
- __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
98
- } else { var mod; }
99
- })(this, function (module, _reconnectingEventsource) {
100
- "use strict";
101
-
102
- var _reconnectingEventsource2 = _interopRequireDefault(_reconnectingEventsource);
103
-
104
- function _interopRequireDefault(obj) {
105
- return obj && obj.__esModule ? obj : {
106
- default: obj
107
- };
108
- }
109
-
110
- module.exports = _reconnectingEventsource2.default;
111
- });
112
-
113
- /***/ }),
114
- /* 1 */
115
- /***/ (function(module, exports, __webpack_require__) {
116
-
117
- var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;(function (global, factory) {
118
- if (true) {
119
- !(__WEBPACK_AMD_DEFINE_ARRAY__ = [exports], __WEBPACK_AMD_DEFINE_FACTORY__ = (factory),
120
- __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
121
- (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__),
122
- __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
123
- } else { var mod; }
124
- })(this, function (exports) {
125
- 'use strict';
126
-
127
- Object.defineProperty(exports, "__esModule", {
128
- value: true
129
- });
130
-
131
- function _toConsumableArray(arr) {
132
- if (Array.isArray(arr)) {
133
- for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
134
- arr2[i] = arr[i];
135
- }
136
-
137
- return arr2;
138
- } else {
139
- return Array.from(arr);
140
- }
141
- }
142
-
143
- function _classCallCheck(instance, Constructor) {
144
- if (!(instance instanceof Constructor)) {
145
- throw new TypeError("Cannot call a class as a function");
146
- }
147
- }
148
-
149
- var _createClass = function () {
150
- function defineProperties(target, props) {
151
- for (var i = 0; i < props.length; i++) {
152
- var descriptor = props[i];
153
- descriptor.enumerable = descriptor.enumerable || false;
154
- descriptor.configurable = true;
155
- if ("value" in descriptor) descriptor.writable = true;
156
- Object.defineProperty(target, descriptor.key, descriptor);
157
- }
158
- }
159
-
160
- return function (Constructor, protoProps, staticProps) {
161
- if (protoProps) defineProperties(Constructor.prototype, protoProps);
162
- if (staticProps) defineProperties(Constructor, staticProps);
163
- return Constructor;
164
- };
165
- }();
166
-
167
- var ReconnectingEventSource = function () {
168
- function ReconnectingEventSource(url, configuration) {
169
- var _this = this;
170
-
171
- _classCallCheck(this, ReconnectingEventSource);
172
-
173
- this._configuration = configuration != null ? Object.assign({}, configuration) : null;
174
-
175
- this._eventSource = null;
176
- this._lastEventId = null;
177
- this._timer = null;
178
- this._listeners = {};
179
-
180
- this.url = url;
181
- this.readyState = 0;
182
- this.max_retry_time = 3000;
183
-
184
- if (this._configuration != null) {
185
- if (this._configuration.lastEventId) {
186
- this._lastEventId = this._configuration.lastEventId;
187
- delete this._configuration['lastEventId'];
188
- }
189
-
190
- if (this._configuration.max_retry_time) {
191
- this.max_retry_time = this._configuration.max_retry_time;
192
- delete this._configuration['max_retry_time'];
193
- }
194
- }
195
-
196
- this._onevent_wrapped = function (event) {
197
- _this._onevent(event);
198
- };
199
-
200
- this._start();
201
- }
202
-
203
- _createClass(ReconnectingEventSource, [{
204
- key: '_start',
205
- value: function _start() {
206
- var _this2 = this;
207
-
208
- var url = this.url;
209
-
210
- if (this._lastEventId) {
211
- if (url.indexOf('?') === -1) {
212
- url += '?';
213
- } else {
214
- url += '&';
215
- }
216
- url += 'lastEventId=' + encodeURIComponent(this._lastEventId);
217
- }
218
-
219
- this._eventSource = new EventSource(url, this._configuration);
220
-
221
- this._eventSource.onopen = function (event) {
222
- _this2._onopen(event);
223
- };
224
- this._eventSource.onerror = function (event) {
225
- _this2._onerror(event);
226
- };
227
-
228
- // apply listen types
229
- var _iteratorNormalCompletion = true;
230
- var _didIteratorError = false;
231
- var _iteratorError = undefined;
232
-
233
- try {
234
- for (var _iterator = Object.keys(this._listeners)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
235
- var type = _step.value;
236
-
237
- this._eventSource.addEventListener(type, this._onevent_wrapped);
238
- }
239
- } catch (err) {
240
- _didIteratorError = true;
241
- _iteratorError = err;
242
- } finally {
243
- try {
244
- if (!_iteratorNormalCompletion && _iterator.return) {
245
- _iterator.return();
246
- }
247
- } finally {
248
- if (_didIteratorError) {
249
- throw _iteratorError;
250
- }
251
- }
252
- }
253
- }
254
- }, {
255
- key: '_onopen',
256
- value: function _onopen(event) {
257
- if (this.readyState === 0) {
258
- this.readyState = 1;
259
- this.onopen(event);
260
- }
261
- }
262
- }, {
263
- key: '_onerror',
264
- value: function _onerror(event) {
265
- var _this3 = this;
266
-
267
- if (this.readyState === 1) {
268
- this.readyState = 0;
269
- this.onerror(event);
270
- }
271
-
272
- if (this._eventSource) {
273
- if (this._eventSource.readyState === 2) {
274
- // reconnect with new object
275
- this._eventSource.close();
276
- this._eventSource = null;
277
-
278
- // reconnect after random timeout < max_retry_time
279
- var timeout = Math.round(this.max_retry_time * Math.random());
280
- this._timer = setTimeout(function () {
281
- return _this3._start();
282
- }, timeout);
283
- }
284
- }
285
- }
286
- }, {
287
- key: '_onevent',
288
- value: function _onevent(event) {
289
- if (event.lastEventId) {
290
- this._lastEventId = event.lastEventId;
291
- }
292
-
293
- var listenersForType = this._listeners[event.type];
294
- if (listenersForType != null) {
295
- var _arr = [].concat(_toConsumableArray(listenersForType));
296
-
297
- // operate on a copy
298
- for (var _i = 0; _i < _arr.length; _i++) {
299
- var listener = _arr[_i];
300
- listener(event);
301
- }
302
- }
303
-
304
- if (event.type === 'message') {
305
- this.onmessage(event);
306
- }
307
- }
308
- }, {
309
- key: 'onopen',
310
- value: function onopen(event) {
311
- // may be overridden
312
- }
313
- }, {
314
- key: 'onerror',
315
- value: function onerror(event) {
316
- // may be overridden
317
- }
318
- }, {
319
- key: 'onmessage',
320
- value: function onmessage(event) {
321
- // may be overridden
322
- }
323
- }, {
324
- key: 'close',
325
- value: function close() {
326
- if (this._timer) {
327
- clearTimeout(this._timer);
328
- this._timer = null;
329
- }
330
-
331
- if (this._eventSource) {
332
- this._eventSource.close();
333
- this._eventSource = null;
334
- }
335
-
336
- this.readyState = 2;
337
- }
338
- }, {
339
- key: 'addEventListener',
340
- value: function addEventListener(inType, callback) {
341
- var type = inType.toString();
342
-
343
- if (!(type in this._listeners)) {
344
- this._listeners[type] = [];
345
- if (this._eventSource) {
346
- this._eventSource.addEventListener(type, this._onevent_wrapped);
347
- }
348
- }
349
-
350
- var listenersForType = this._listeners[type];
351
- if (!listenersForType.includes(callback)) {
352
- this._listeners[type] = [].concat(_toConsumableArray(listenersForType), [callback]);
353
- }
354
- }
355
- }, {
356
- key: 'removeEventListener',
357
- value: function removeEventListener(inType, callback) {
358
- var type = inType.toString();
359
-
360
- if (type in this._listeners) {
361
-
362
- var listenersForType = this._listeners[type];
363
-
364
- var updatedListenersForType = listenersForType.filter(function (l) {
365
- return l !== callback;
366
- });
367
-
368
- if (updatedListenersForType.length > 0) {
369
- this._listeners[type] = updatedListenersForType;
370
- } else {
371
- delete this._listeners[type];
372
- if (this._eventSource) {
373
- this._eventSource.removeEventListener(type, this._onevent_wrapped);
374
- }
375
- }
376
- }
377
- }
378
- }]);
379
-
380
- return ReconnectingEventSource;
381
- }();
382
-
383
- exports.default = ReconnectingEventSource;
384
- });
385
-
386
- /***/ })
387
- /******/ ]);
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,this.eventSourceClass=EventSource,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._configuration.eventSourceClass&&(this.eventSourceClass=this._configuration.eventSourceClass,delete this._configuration.eventSourceClass)),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 this.eventSourceClass(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(n){var r={};function i(e){if(r[e])return r[e].exports;var t=r[e]={i:e,l:!1,exports:{}};return n[e].call(t.exports,t,t.exports,i),t.l=!0,t.exports}return i.m=n,i.c=r,i.d=function(e,t,n){i.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(i.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)i.d(n,r,function(e){return t[e]}.bind(null,r));return n},i.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(t,"a",t),t},i.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i.p="",i(i.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(t);function r(e){return e&&e.__esModule?e:{default:e}}e.exports=n.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 s(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++){n[t]=e[t]}return n}else{return Array.from(e)}}function i(e,t){if(!(e instanceof t)){throw new TypeError("Cannot call a class as a function")}}Object.defineProperty(e,"__esModule",{value:true});var t=function(){function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||false;r.configurable=true;if("value"in r)r.writable=true;Object.defineProperty(e,r.key,r)}}return function(e,t,n){if(t)r(e.prototype,t);if(n)r(e,n);return e}}(),n=function(){function r(e,t){var n=this;i(this,r);this._configuration=t!=null?Object.assign({},t):null;this._eventSource=null;this._lastEventId=null;this._timer=null;this._listeners={};this.url=e;this.readyState=0;this.max_retry_time=3e3;if(this._configuration!=null){if(this._configuration.lastEventId){this._lastEventId=this._configuration.lastEventId;delete this._configuration["lastEventId"]}if(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){n._onevent(e)};this._start()}t(r,[{key:"_start",value:function e(){var t=this;var n=this.url;if(this._lastEventId){if(n.indexOf("?")===-1){n+="?"}else{n+="&"}n+="lastEventId="+encodeURIComponent(this._lastEventId)}this._eventSource=new EventSource(n,this._configuration);this._eventSource.onopen=function(e){t._onopen(e)};this._eventSource.onerror=function(e){t._onerror(e)};var r=true;var i=false;var o=undefined;try{for(var s=Object.keys(this._listeners)[Symbol.iterator](),u;!(r=(u=s.next()).done);r=true){var a=u.value;this._eventSource.addEventListener(a,this._onevent_wrapped)}}catch(e){i=true;o=e}finally{try{if(!r&&s.return){s.return()}}finally{if(i){throw o}}}}},{key:"_onopen",value:function e(t){if(this.readyState===0){this.readyState=1;this.onopen(t)}}},{key:"_onerror",value:function e(t){var n=this;if(this.readyState===1){this.readyState=0;this.onerror(t)}if(this._eventSource){if(this._eventSource.readyState===2){this._eventSource.close();this._eventSource=null;var r=Math.round(this.max_retry_time*Math.random());this._timer=setTimeout(function(){return n._start()},r)}}}},{key:"_onevent",value:function e(t){if(t.lastEventId){this._lastEventId=t.lastEventId}var n=this._listeners[t.type];if(n!=null){var r=[].concat(s(n));for(var i=0;i<r.length;i++){var o=r[i];o(t)}}if(t.type==="message"){this.onmessage(t)}}},{key:"onopen",value:function e(t){}},{key:"onerror",value:function e(t){}},{key:"onmessage",value:function e(t){}},{key:"close",value:function e(){if(this._timer){clearTimeout(this._timer);this._timer=null}if(this._eventSource){this._eventSource.close();this._eventSource=null}this.readyState=2}},{key:"addEventListener",value:function e(t,n){var r=t.toString();if(!(r in this._listeners)){this._listeners[r]=[];if(this._eventSource){this._eventSource.addEventListener(r,this._onevent_wrapped)}}var i=this._listeners[r];if(!i.includes(n)){this._listeners[r]=[].concat(s(i),[n])}}},{key:"removeEventListener",value:function e(t,n){var r=t.toString();if(r in this._listeners){var i=this._listeners[r];var o=i.filter(function(e){return e!==n});if(o.length>0){this._listeners[r]=o}else{delete this._listeners[r];if(this._eventSource){this._eventSource.removeEventListener(r,this._onevent_wrapped)}}}}}]);return r}();e.default=n})?r.apply(t,i):r)||(e.exports=o)}]);
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,this.eventSourceClass=EventSource,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._configuration.eventSourceClass&&(this.eventSourceClass=this._configuration.eventSourceClass,delete this._configuration.eventSourceClass)),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 this.eventSourceClass(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,365 +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, { enumerable: true, get: getter });
41
- /******/ }
42
- /******/ };
43
- /******/
44
- /******/ // define __esModule on exports
45
- /******/ __webpack_require__.r = function(exports) {
46
- /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
47
- /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
48
- /******/ }
49
- /******/ Object.defineProperty(exports, '__esModule', { value: true });
50
- /******/ };
51
- /******/
52
- /******/ // create a fake namespace object
53
- /******/ // mode & 1: value is a module id, require it
54
- /******/ // mode & 2: merge all properties of value into the ns
55
- /******/ // mode & 4: return value when already ns object
56
- /******/ // mode & 8|1: behave like require
57
- /******/ __webpack_require__.t = function(value, mode) {
58
- /******/ if(mode & 1) value = __webpack_require__(value);
59
- /******/ if(mode & 8) return value;
60
- /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
61
- /******/ var ns = Object.create(null);
62
- /******/ __webpack_require__.r(ns);
63
- /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
64
- /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
65
- /******/ return ns;
66
- /******/ };
67
- /******/
68
- /******/ // getDefaultExport function for compatibility with non-harmony modules
69
- /******/ __webpack_require__.n = function(module) {
70
- /******/ var getter = module && module.__esModule ?
71
- /******/ function getDefault() { return module['default']; } :
72
- /******/ function getModuleExports() { return module; };
73
- /******/ __webpack_require__.d(getter, 'a', getter);
74
- /******/ return getter;
75
- /******/ };
76
- /******/
77
- /******/ // Object.prototype.hasOwnProperty.call
78
- /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
79
- /******/
80
- /******/ // __webpack_public_path__
81
- /******/ __webpack_require__.p = "";
82
- /******/
83
- /******/
84
- /******/ // Load entry module and return exports
85
- /******/ return __webpack_require__(__webpack_require__.s = 0);
86
- /******/ })
87
- /************************************************************************/
88
- /******/ ([
89
- /* 0 */
90
- /***/ (function(module, exports, __webpack_require__) {
91
-
92
- "use strict";
93
-
94
-
95
- Object.defineProperty(exports, "__esModule", {
96
- value: true
97
- });
98
-
99
- var _reconnectingEventsource = __webpack_require__(1);
100
-
101
- var _reconnectingEventsource2 = _interopRequireDefault(_reconnectingEventsource);
102
-
103
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
104
-
105
- exports.default = _reconnectingEventsource2.default;
106
-
107
- /***/ }),
108
- /* 1 */
109
- /***/ (function(module, exports, __webpack_require__) {
110
-
111
- "use strict";
112
-
113
-
114
- Object.defineProperty(exports, "__esModule", {
115
- value: true
116
- });
117
-
118
- 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; }; }();
119
-
120
- 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); } }
121
-
122
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
123
-
124
- // MIT License:
125
- //
126
- // Copyright (C) 2017 Fanout, Inc.
127
- //
128
- // Permission is hereby granted, free of charge, to any person obtaining a copy
129
- // of this software and associated documentation files (the "Software"), to
130
- // deal in the Software without restriction, including without limitation the
131
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
132
- // sell copies of the Software, and to permit persons to whom the Software is
133
- // furnished to do so, subject to the following conditions:
134
- //
135
- // The above copyright notice and this permission notice shall be included in
136
- // all copies or substantial portions of the Software.
137
- //
138
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
139
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
140
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
141
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
142
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
143
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
144
- // IN THE SOFTWARE.
145
-
146
- var ReconnectingEventSource = function () {
147
- function ReconnectingEventSource(url, configuration) {
148
- var _this = this;
149
-
150
- _classCallCheck(this, ReconnectingEventSource);
151
-
152
- this._configuration = configuration != null ? Object.assign({}, configuration) : null;
153
-
154
- this._eventSource = null;
155
- this._lastEventId = null;
156
- this._timer = null;
157
- this._listeners = {};
158
-
159
- this.url = url;
160
- this.readyState = 0;
161
- this.max_retry_time = 3000;
162
-
163
- if (this._configuration != null) {
164
- if (this._configuration.lastEventId) {
165
- this._lastEventId = this._configuration.lastEventId;
166
- delete this._configuration['lastEventId'];
167
- }
168
-
169
- if (this._configuration.max_retry_time) {
170
- this.max_retry_time = this._configuration.max_retry_time;
171
- delete this._configuration['max_retry_time'];
172
- }
173
- }
174
-
175
- this._onevent_wrapped = function (event) {
176
- _this._onevent(event);
177
- };
178
-
179
- this._start();
180
- }
181
-
182
- _createClass(ReconnectingEventSource, [{
183
- key: '_start',
184
- value: function _start() {
185
- var _this2 = this;
186
-
187
- var url = this.url;
188
-
189
- if (this._lastEventId) {
190
- if (url.indexOf('?') === -1) {
191
- url += '?';
192
- } else {
193
- url += '&';
194
- }
195
- url += 'lastEventId=' + encodeURIComponent(this._lastEventId);
196
- }
197
-
198
- this._eventSource = new EventSource(url, this._configuration);
199
-
200
- this._eventSource.onopen = function (event) {
201
- _this2._onopen(event);
202
- };
203
- this._eventSource.onerror = function (event) {
204
- _this2._onerror(event);
205
- };
206
-
207
- // apply listen types
208
- var _iteratorNormalCompletion = true;
209
- var _didIteratorError = false;
210
- var _iteratorError = undefined;
211
-
212
- try {
213
- for (var _iterator = Object.keys(this._listeners)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
214
- var type = _step.value;
215
-
216
- this._eventSource.addEventListener(type, this._onevent_wrapped);
217
- }
218
- } catch (err) {
219
- _didIteratorError = true;
220
- _iteratorError = err;
221
- } finally {
222
- try {
223
- if (!_iteratorNormalCompletion && _iterator.return) {
224
- _iterator.return();
225
- }
226
- } finally {
227
- if (_didIteratorError) {
228
- throw _iteratorError;
229
- }
230
- }
231
- }
232
- }
233
- }, {
234
- key: '_onopen',
235
- value: function _onopen(event) {
236
- if (this.readyState === 0) {
237
- this.readyState = 1;
238
- this.onopen(event);
239
- }
240
- }
241
- }, {
242
- key: '_onerror',
243
- value: function _onerror(event) {
244
- var _this3 = this;
245
-
246
- if (this.readyState === 1) {
247
- this.readyState = 0;
248
- this.onerror(event);
249
- }
250
-
251
- if (this._eventSource) {
252
- if (this._eventSource.readyState === 2) {
253
- // reconnect with new object
254
- this._eventSource.close();
255
- this._eventSource = null;
256
-
257
- // reconnect after random timeout < max_retry_time
258
- var timeout = Math.round(this.max_retry_time * Math.random());
259
- this._timer = setTimeout(function () {
260
- return _this3._start();
261
- }, timeout);
262
- }
263
- }
264
- }
265
- }, {
266
- key: '_onevent',
267
- value: function _onevent(event) {
268
- if (event.lastEventId) {
269
- this._lastEventId = event.lastEventId;
270
- }
271
-
272
- var listenersForType = this._listeners[event.type];
273
- if (listenersForType != null) {
274
- // operate on a copy
275
- var _arr = [].concat(_toConsumableArray(listenersForType));
276
-
277
- for (var _i = 0; _i < _arr.length; _i++) {
278
- var listener = _arr[_i];
279
- listener(event);
280
- }
281
- }
282
-
283
- if (event.type === 'message') {
284
- this.onmessage(event);
285
- }
286
- }
287
- }, {
288
- key: 'onopen',
289
- value: function onopen(event) {
290
- // may be overridden
291
- }
292
- }, {
293
- key: 'onerror',
294
- value: function onerror(event) {
295
- // may be overridden
296
- }
297
- }, {
298
- key: 'onmessage',
299
- value: function onmessage(event) {
300
- // may be overridden
301
- }
302
- }, {
303
- key: 'close',
304
- value: function close() {
305
- if (this._timer) {
306
- clearTimeout(this._timer);
307
- this._timer = null;
308
- }
309
-
310
- if (this._eventSource) {
311
- this._eventSource.close();
312
- this._eventSource = null;
313
- }
314
-
315
- this.readyState = 2;
316
- }
317
- }, {
318
- key: 'addEventListener',
319
- value: function addEventListener(inType, callback) {
320
- var type = inType.toString();
321
-
322
- if (!(type in this._listeners)) {
323
- this._listeners[type] = [];
324
- if (this._eventSource) {
325
- this._eventSource.addEventListener(type, this._onevent_wrapped);
326
- }
327
- }
328
-
329
- var listenersForType = this._listeners[type];
330
- if (!listenersForType.includes(callback)) {
331
- this._listeners[type] = [].concat(_toConsumableArray(listenersForType), [callback]);
332
- }
333
- }
334
- }, {
335
- key: 'removeEventListener',
336
- value: function removeEventListener(inType, callback) {
337
- var type = inType.toString();
338
-
339
- if (type in this._listeners) {
340
-
341
- var listenersForType = this._listeners[type];
342
-
343
- var updatedListenersForType = listenersForType.filter(function (l) {
344
- return l !== callback;
345
- });
346
-
347
- if (updatedListenersForType.length > 0) {
348
- this._listeners[type] = updatedListenersForType;
349
- } else {
350
- delete this._listeners[type];
351
- if (this._eventSource) {
352
- this._eventSource.removeEventListener(type, this._onevent_wrapped);
353
- }
354
- }
355
- }
356
- }
357
- }]);
358
-
359
- return ReconnectingEventSource;
360
- }();
361
-
362
- exports.default = ReconnectingEventSource;
363
-
364
- /***/ })
365
- /******/ ]);
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,this.eventSourceClass=EventSource,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._configuration.eventSourceClass&&(this.eventSourceClass=this._configuration.eventSourceClass,delete this._configuration.eventSourceClass)),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 this.eventSourceClass(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.4",
3
+ "version": "1.3.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",
@@ -45,12 +46,11 @@
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": "^4.18.2",
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
- "uglifyjs-webpack-plugin": "^2.2.0",
54
54
  "webpack": "^4.41.2",
55
55
  "webpack-cli": "^3.3.10"
56
56
  }
@@ -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
+ }
@@ -33,6 +33,7 @@ export default class ReconnectingEventSource {
33
33
  this.url = url;
34
34
  this.readyState = 0;
35
35
  this.max_retry_time = 3000;
36
+ this.eventSourceClass = EventSource;
36
37
 
37
38
  if (this._configuration != null) {
38
39
  if (this._configuration.lastEventId) {
@@ -44,6 +45,11 @@ export default class ReconnectingEventSource {
44
45
  this.max_retry_time = this._configuration.max_retry_time;
45
46
  delete this._configuration['max_retry_time'];
46
47
  }
48
+
49
+ if (this._configuration.eventSourceClass) {
50
+ this.eventSourceClass = this._configuration.eventSourceClass;
51
+ delete this._configuration['eventSourceClass'];
52
+ }
47
53
  }
48
54
 
49
55
  this._onevent_wrapped = event => { this._onevent(event); };
@@ -63,10 +69,11 @@ export default class ReconnectingEventSource {
63
69
  url += 'lastEventId=' + encodeURIComponent(this._lastEventId);
64
70
  }
65
71
 
66
- this._eventSource = new EventSource(url, this._configuration);
72
+ this._eventSource = new this.eventSourceClass(url, this._configuration);
67
73
 
68
74
  this._eventSource.onopen = event => { this._onopen(event); };
69
75
  this._eventSource.onerror = event => { this._onerror(event); };
76
+ this._eventSource.onmessage = event => { this.onmessage(event); };
70
77
 
71
78
  // apply listen types
72
79
  for (const type of Object.keys(this._listeners)) {
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "compilerOptions": {
3
+ "lib": [
4
+ "esnext",
5
+ "dom"
6
+ ]
7
+ }
8
+ }
package/webpack.config.js CHANGED
@@ -1,6 +1,5 @@
1
1
  const path = require('path');
2
2
  const webpack = require('webpack');
3
- const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
4
3
 
5
4
  const globalName = "ReconnectingEventSource";
6
5
 
@@ -93,27 +92,12 @@ function addBabelRule(rules) {
93
92
  return [...rules, babelRule];
94
93
  }
95
94
 
96
- function addMinifyPlugin(minimizer) {
97
- if (BUILD_TARGET === "umd-min") {
98
- const minifyPlugin = new UglifyJsPlugin({
99
- uglifyOptions: {
100
- compress: {}
101
- }
102
- });
103
- return [...minimizer, minifyPlugin];
104
- }
105
- return minimizer;
106
- }
107
-
108
95
  const entry = buildEntry();
109
96
  const output = buildOutput();
110
97
 
111
98
  let rules = [];
112
99
  rules = addBabelRule(rules);
113
100
 
114
- let minimizer = [];
115
- minimizer = addMinifyPlugin(minimizer);
116
-
117
101
  const config = {
118
102
  mode: 'production',
119
103
  context: srcDir,
@@ -122,9 +106,6 @@ const config = {
122
106
  module: {
123
107
  rules
124
108
  },
125
- optimization: {
126
- minimizer
127
- }
128
109
  };
129
110
 
130
111
  module.exports = config;