mirror of
https://github.com/status-im/consul.git
synced 2025-01-10 05:45:46 +00:00
62e3c5605c
You can potentially close an EventSource before its first tick by immediately setting the readyState to a non-open state. Therefore it never opens. Calling `open` will then open it. 'Open' fits better than 'reopen' when taking the above into account
25 lines
617 B
JavaScript
25 lines
617 B
JavaScript
/**
|
|
* 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;
|
|
}
|
|
open() {
|
|
switch (this.readyState) {
|
|
case 3: // CLOSING
|
|
this.readyState = 1;
|
|
break;
|
|
case 2: // CLOSED
|
|
eventSource.apply(this, [this.source, this.configuration]);
|
|
break;
|
|
}
|
|
return this;
|
|
}
|
|
};
|
|
}
|