Skip to content

Commit

Permalink
[7.0] [intepreter][Canvas] Dedupe server functions in batched requests (
Browse files Browse the repository at this point in the history
#32712) (#32832)

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

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

* Add and correct tests

* Delete batched_fetch.test.js

This file was not present in the branch and is failing.
  • Loading branch information
clintandrewhall authored Mar 9, 2019
1 parent 1e7262f commit 2c31657
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
23 changes: 21 additions & 2 deletions packages/kbn-interpreter/src/public/batched_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { FUNCTIONS_URL } from './consts';
import _ from 'lodash';

/**
* Create a function which executes an Expression function on the
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 9 additions & 8 deletions packages/kbn-interpreter/src/public/interpreter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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,
},
],
}),
});
});

});

0 comments on commit 2c31657

Please sign in to comment.