You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
consul/ui-v2/app/utils/dom/event-source/cache.js

32 lines
1.2 KiB

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