open-consul/ui-v2/app/utils/dom/event-source/storage.js
John Cowen da36c2a0f9 UI: Add EventSource ready for implementing blocking queries (#5070)
- 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)
2019-05-01 18:22:06 +00:00

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),
};
}
};
}