mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
* [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:
parent
b0b18bfca6
commit
9ef9ad3cb4
2 changed files with 30 additions and 10 deletions
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue