Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 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({ ajaxStream, 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you're using isMatch here instead of isEqual because you're trying to ignore the id param. Is that right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right.

);

// 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 Expand Up @@ -101,7 +120,7 @@ async function processBatch(ajaxStream, batch) {
} else {
future.resolve(result);
}
}
},
});
} catch (err) {
Object.values(batch).forEach(({ future }) => {
Expand Down