[ui/courier] batch fetch requests for all searches and docs

This commit is contained in:
spalger 2017-02-06 15:13:23 -07:00
parent 9a02e5b349
commit 20d55fe602
5 changed files with 56 additions and 24 deletions

View file

@ -10,7 +10,6 @@ export function loadSavedObject(loaders, panel) {
if (!loader) {
throw new Error(`No loader for object of type ${panel.type}`);
}
const isDefered = true;
return loader.get(panel.id, isDefered)
return loader.get(panel.id)
.then(savedObj => ({ savedObj, editUrl: loader.urlFor(panel.id) }));
}

View file

@ -8,23 +8,32 @@ import ReqStatusProvider from './req_status';
export default function fetchService(Private, Promise) {
const requestQueue = Private(RequestQueueProvider);
const fetchThese = Private(FetchTheseProvider);
const immediatelyFetchThese = Private(FetchTheseProvider);
const callResponseHandlers = Private(CallResponseHandlersProvider);
const INCOMPLETE = Private(ReqStatusProvider).INCOMPLETE;
function fetchQueued(strategy) {
const requests = requestQueue.getStartable(strategy);
if (!requests.length) return Promise.resolve();
else return fetchThese(requests);
}
const debouncedFetchThese = _.debounce(() => {
const requests = requestQueue.get().filter(req => req.isFetchRequested());
immediatelyFetchThese(requests);
}, {
wait: 10,
maxWait: 50
});
this.fetchQueued = fetchQueued;
const fetchTheseSoon = (requests) => {
requests.forEach(req => req._setFetchRequested());
debouncedFetchThese();
return Promise.all(requests.map(req => req.defer.promise));
};
this.fetchQueued = (strategy) => {
return fetchTheseSoon(requestQueue.getStartable(strategy));
};
function fetchASource(source, strategy) {
const defer = Promise.defer();
fetchThese([
fetchTheseSoon([
source._createRequest(defer)
]);
@ -50,7 +59,7 @@ export default function fetchService(Private, Promise) {
* @param {array} reqs - the requests to fetch
* @async
*/
this.these = fetchThese;
this.these = fetchTheseSoon;
/**
* Send responses to a list of requests, used when requests

View file

@ -15,12 +15,42 @@ export default function AbstractReqProvider(Private, Promise) {
this.source = source;
this.defer = defer || Promise.defer();
this._whenAbortedHandlers = [];
requestQueue.push(this);
}
/**
* Called by the loopers to find requests that should be sent to the
* fetch() module. When a module is sent to fetch() it's _fetchRequested flag
* is set, and this consults that flag so requests are not send to fetch()
* multiple times.
*
* @return {Boolean}
*/
canStart() {
return Boolean(!this.stopped && !this.source._fetchDisabled);
return !this._fetchRequested && !this.stopped && !this.source._fetchDisabled;
}
/**
* Used to find requests that were previously sent to the fetch() module but
* have not been started yet, so they can be started.
*
* @return {Boolean}
*/
isFetchRequestedAndPending() {
return !!this._fetchRequested && !this.started;
}
/**
* Called by the fetch() module when this request has been sent to
* be fetched. At that point the request is somewhere between `ready-to-start`
* and `started`. The fetch module then waits a short period of time to
* allow requests to build up in the request queue, and then immediately
* fetches all requests that return true from `isFetchRequested()`
*
* @return {undefined}
*/
_setFetchRequested() {
this._fetchRequested = true;
}
start() {

View file

@ -127,7 +127,7 @@ export default function SavedObjectFactory(esAdmin, kbnIndex, Promise, Private,
* @return {Promise}
* @resolved {SavedObject}
*/
this.init = _.once((isDefered) => {
this.init = _.once(() => {
// ensure that the type is defined
if (!type) throw new Error('You must define a type name to use SavedObject objects.');
@ -166,13 +166,7 @@ export default function SavedObjectFactory(esAdmin, kbnIndex, Promise, Private,
}
// fetch the object from ES
if (isDefered) {
const defer = Promise.defer();
docSource._createRequest(defer);
return defer.promise.then(this.applyESResp);
} else {
return docSource.fetch().then(this.applyESResp);
}
return docSource.fetch().then(this.applyESResp);
})
.then(() => {
return customInit.call(this);

View file

@ -29,8 +29,8 @@ export class SavedObjectLoader {
* @param id
* @returns {Promise<SavedObject>}
*/
get(id, isDefered) {
return (new this.Class(id)).init(isDefered);
get(id) {
return (new this.Class(id)).init();
}
urlFor(id) {