2020-03-19 10:28:21 +00:00
|
|
|
import Service, { inject as service } from '@ember/service';
|
|
|
|
import { get } from '@ember/object';
|
|
|
|
|
|
|
|
import { BlockingEventSource as EventSource } from 'consul-ui/utils/dom/event-source';
|
|
|
|
import { ifNotBlocking } from 'consul-ui/services/settings';
|
|
|
|
import { restartWhenAvailable } from 'consul-ui/services/client/http';
|
|
|
|
import maybeCall from 'consul-ui/utils/maybe-call';
|
|
|
|
|
2020-11-09 09:25:35 +00:00
|
|
|
export default class BlockingService extends Service {
|
|
|
|
@service('client/http')
|
|
|
|
client;
|
|
|
|
|
|
|
|
@service('settings')
|
|
|
|
settings;
|
|
|
|
|
|
|
|
source(find, configuration) {
|
2020-03-19 10:28:21 +00:00
|
|
|
return new EventSource((configuration, source) => {
|
|
|
|
const close = source.close.bind(source);
|
|
|
|
const deleteCursor = () => (configuration.cursor = undefined);
|
|
|
|
//
|
|
|
|
return maybeCall(deleteCursor, ifNotBlocking(this.settings))().then(() => {
|
|
|
|
return find(configuration)
|
|
|
|
.then(maybeCall(close, ifNotBlocking(this.settings)))
|
2020-10-06 08:31:01 +00:00
|
|
|
.then(function(res = {}) {
|
|
|
|
const meta = get(res, 'meta') || {};
|
|
|
|
if (typeof meta.cursor === 'undefined' && typeof meta.interval === 'undefined') {
|
2020-03-19 10:28:21 +00:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
})
|
|
|
|
.catch(restartWhenAvailable(this.client));
|
|
|
|
});
|
|
|
|
}, configuration);
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
}
|