2019-02-21 10:36:15 +00:00
|
|
|
/**
|
|
|
|
* Wraps an EventSource so that you can `close` and `reopen`
|
|
|
|
*
|
|
|
|
* @param {Class} eventSource - EventSource class to extend from
|
|
|
|
*/
|
|
|
|
export default function(eventSource = EventSource) {
|
|
|
|
return class extends eventSource {
|
|
|
|
constructor(source, configuration) {
|
|
|
|
super(...arguments);
|
|
|
|
this.configuration = configuration;
|
|
|
|
}
|
2019-06-20 08:35:30 +00:00
|
|
|
open() {
|
2019-02-21 10:36:15 +00:00
|
|
|
switch (this.readyState) {
|
|
|
|
case 3: // CLOSING
|
|
|
|
this.readyState = 1;
|
|
|
|
break;
|
|
|
|
case 2: // CLOSED
|
|
|
|
eventSource.apply(this, [this.source, this.configuration]);
|
|
|
|
break;
|
|
|
|
}
|
2019-06-20 08:35:30 +00:00
|
|
|
return this;
|
2019-02-21 10:36:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|