From 2c31657e7866c3dad4c278285b6131e8d06f8039 Mon Sep 17 00:00:00 2001 From: Clint Andrew Hall Date: Fri, 8 Mar 2019 19:26:37 -0800 Subject: [PATCH] [7.0] [intepreter][Canvas] Dedupe server functions in batched requests (#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. --- .../src/public/batched_fetch.js | 23 +++++++++++++++++-- .../src/public/interpreter.test.js | 17 +++++++------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/packages/kbn-interpreter/src/public/batched_fetch.js b/packages/kbn-interpreter/src/public/batched_fetch.js index 20fd0d37b5ef9..905e05ef7dae6 100644 --- a/packages/kbn-interpreter/src/public/batched_fetch.js +++ b/packages/kbn-interpreter/src/public/batched_fetch.js @@ -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; diff --git a/packages/kbn-interpreter/src/public/interpreter.test.js b/packages/kbn-interpreter/src/public/interpreter.test.js index 34eb3578ec35c..43007a9e9b713 100644 --- a/packages/kbn-interpreter/src/public/interpreter.test.js +++ b/packages/kbn-interpreter/src/public/interpreter.test.js @@ -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, + }, + ], }), }); }); - });