[6.7] [intepreter][Canvas] Dedupe server functions in batched requests (#32712) (#32833)

* [intepreter][Canvas] Dedupe server functions in batched requests (#32712)

* [intepreter][Canvas] Dedupe server functions in batched requests

* Add and correct tests

* Update interpreter.test.js

Fix merge error

* Delete batched_fetch.test.js

This file was not present in the branch and is failing.
This commit is contained in:
Clint Andrew Hall 2019-03-08 19:26:18 -08:00 committed by GitHub
parent b0b18bfca6
commit 9ef9ad3cb4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 10 deletions

View file

@ -18,6 +18,7 @@
*/
import { FUNCTIONS_URL } from './consts';
import _ from 'lodash';
/**
* Create a function which executes an Expression function on the
@ -51,12 +52,30 @@ export function batchedFetch({ kfetch, serialize, ms = 10 }) {
timeout = setTimeout(runBatch, ms);
}
const id = nextId();
const request = {
functionName,
args,
context: serialize(context),
};
// Check to see if this is a duplicate server function.
const duplicate = Object.values(batch).find(batchedRequest =>
_.isMatch(batchedRequest.request, request)
);
// If it is, just return the promise of the duplicated request.
if (duplicate) {
return duplicate.future.promise;
}
// If not, create a new promise, id, and add it to the batched collection.
const future = createFuture();
const id = nextId();
request.id = id;
batch[id] = {
future,
request: { id, functionName, args, context: serialize(context) },
request,
};
return future.promise;

View file

@ -67,7 +67,7 @@ describe('kbn-interpreter/interpreter', () => {
expect(register).toHaveBeenCalledTimes(2);
const [ hello, world ] = register.mock.calls.map(([fn]) => fn());
const [hello, world] = register.mock.calls.map(([fn]) => fn());
expect(hello.name).toEqual('hello');
expect(typeof hello.fn).toEqual('function');
@ -85,14 +85,15 @@ describe('kbn-interpreter/interpreter', () => {
pathname: FUNCTIONS_URL,
method: 'POST',
body: JSON.stringify({
functions: [{
id: 1,
functionName: 'hello',
args,
context,
}]
functions: [
{
functionName: 'hello',
args,
context,
id: 1,
},
],
}),
});
});
});