da36c2a0f9
- Maintain http headers as JSON-API meta for all API requests (#4946) - Add EventSource ready for implementing blocking queries - EventSource project implementation to enable blocking queries for service and node listings (#5267) - Add setting to enable/disable blocking queries (#5352)
41 lines
1.1 KiB
JavaScript
41 lines
1.1 KiB
JavaScript
export default function(EventTarget, P = Promise) {
|
|
const handler = function(e) {
|
|
if (e.key === this.configuration.key) {
|
|
P.resolve(this.getCurrentEvent()).then(event => {
|
|
this.configuration.cursor++;
|
|
this.dispatchEvent(event);
|
|
});
|
|
}
|
|
};
|
|
return class extends EventTarget {
|
|
constructor(cb, configuration) {
|
|
super(...arguments);
|
|
this.source = cb;
|
|
this.handler = handler.bind(this);
|
|
this.configuration = configuration;
|
|
this.configuration.cursor = 1;
|
|
this.dispatcher = configuration.dispatcher;
|
|
this.reopen();
|
|
}
|
|
dispatchEvent() {
|
|
if (this.readyState === 1) {
|
|
return super.dispatchEvent(...arguments);
|
|
}
|
|
}
|
|
close() {
|
|
this.dispatcher.removeEventListener('storage', this.handler);
|
|
this.readyState = 2;
|
|
}
|
|
reopen() {
|
|
this.dispatcher.addEventListener('storage', this.handler);
|
|
this.readyState = 1;
|
|
}
|
|
getCurrentEvent() {
|
|
return {
|
|
type: 'message',
|
|
data: this.source(this.configuration),
|
|
};
|
|
}
|
|
};
|
|
}
|