2022-09-15 08:43:17 +00:00
|
|
|
export default function (source, DefaultEventSource, P = Promise) {
|
|
|
|
return function (sources) {
|
|
|
|
return function (cb, configuration) {
|
2019-02-21 10:36:15 +00:00
|
|
|
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)
|
2022-09-15 08:43:17 +00:00
|
|
|
.catch(function (e) {
|
2019-02-21 10:36:15 +00:00
|
|
|
// any errors, delete from the cache for next time
|
|
|
|
delete sources[key];
|
|
|
|
return P.reject(e);
|
|
|
|
})
|
2022-09-15 08:43:17 +00:00
|
|
|
.then(function (eventSource) {
|
2019-02-21 10:36:15 +00:00
|
|
|
// make sure we cancel everything out if there is no cursor
|
|
|
|
if (typeof eventSource.configuration.cursor === 'undefined') {
|
|
|
|
eventSource.close();
|
|
|
|
delete sources[key];
|
|
|
|
}
|
|
|
|
return eventSource;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|