open-consul/ui-v2/app/utils/dom/event-source/cache.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

32 lines
1.2 KiB
JavaScript

export default function(source, DefaultEventSource, P = Promise) {
return function(sources) {
return function(cb, configuration) {
const key = configuration.key;
if (typeof sources[key] !== 'undefined' && configuration.settings.enabled) {
if (typeof sources[key].configuration === 'undefined') {
sources[key].configuration = {};
}
sources[key].configuration.settings = configuration.settings;
return source(sources[key]);
} else {
const EventSource = configuration.type || DefaultEventSource;
const eventSource = (sources[key] = new EventSource(cb, configuration));
return source(eventSource)
.catch(function(e) {
// any errors, delete from the cache for next time
delete sources[key];
return P.reject(e);
})
.then(function(eventSource) {
// make sure we cancel everything out if there is no cursor
if (typeof eventSource.configuration.cursor === 'undefined') {
eventSource.close();
delete sources[key];
}
return eventSource;
});
}
};
};
}