From cb3cad9c30a903c4735dd91bcd87dd582b7de094 Mon Sep 17 00:00:00 2001 From: Mykhailo Semenchenko Date: Fri, 15 Jan 2021 20:09:04 +0200 Subject: [PATCH] Add memoizaion for getConfig Signed-off-by: Mykhailo Semenchenko --- .../src/components/SearchTracePage/SearchForm.test.js | 2 ++ packages/jaeger-ui/src/utils/config/get-config.test.js | 5 +++-- packages/jaeger-ui/src/utils/config/get-config.tsx | 7 +++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/jaeger-ui/src/components/SearchTracePage/SearchForm.test.js b/packages/jaeger-ui/src/components/SearchTracePage/SearchForm.test.js index c5c5317235..8969dc6de7 100644 --- a/packages/jaeger-ui/src/components/SearchTracePage/SearchForm.test.js +++ b/packages/jaeger-ui/src/components/SearchTracePage/SearchForm.test.js @@ -34,6 +34,7 @@ import { validateDurationFields, } from './SearchForm'; import * as markers from './SearchForm.markers'; +import getConfig from '../../utils/config/get-config'; function makeDateParams(dateOffset = 0) { const date = new Date(); @@ -402,6 +403,7 @@ describe('', () => { it('uses config.search.maxLimit', () => { const maxLimit = 6789; + getConfig.apply({}, []); const config = { search: { maxLimit, diff --git a/packages/jaeger-ui/src/utils/config/get-config.test.js b/packages/jaeger-ui/src/utils/config/get-config.test.js index d152469344..26d35f1315 100644 --- a/packages/jaeger-ui/src/utils/config/get-config.test.js +++ b/packages/jaeger-ui/src/utils/config/get-config.test.js @@ -59,6 +59,7 @@ describe('getConfig()', () => { let getJaegerUiConfig; beforeEach(() => { + getConfig.apply({}, []); embedded = {}; getJaegerUiConfig = jest.fn(() => embedded); window.getJaegerUiConfig = getJaegerUiConfig; @@ -110,12 +111,12 @@ describe('getConfig()', () => { }); }); - it('processes deprecations every time `getConfig` is invoked', () => { + it('processes deprecations in `getConfig` is invoked only once', () => { processDeprecation.mockClear(); getConfig(); expect(processDeprecation.mock.calls.length).toBe(deprecations.length); getConfig(); - expect(processDeprecation.mock.calls.length).toBe(2 * deprecations.length); + expect(processDeprecation.mock.calls.length).toBe(deprecations.length); }); }); }); diff --git a/packages/jaeger-ui/src/utils/config/get-config.tsx b/packages/jaeger-ui/src/utils/config/get-config.tsx index 458ba0cd29..c56d1b9bc8 100644 --- a/packages/jaeger-ui/src/utils/config/get-config.tsx +++ b/packages/jaeger-ui/src/utils/config/get-config.tsx @@ -13,6 +13,7 @@ // limitations under the License. import _get from 'lodash/get'; +import memoizeOne from 'memoize-one'; import processDeprecation from './process-deprecation'; import defaultConfig, { deprecations } from '../../constants/default-config'; @@ -24,7 +25,7 @@ let haveWarnedDeprecations = false; * Merge the embedded config from the query service (if present) with the * default config from `../../constants/default-config`. */ -export default function getConfig() { +const getConfig = memoizeOne(function getConfig() { const getJaegerUiConfig = window.getJaegerUiConfig; if (typeof getJaegerUiConfig !== 'function') { if (!haveWarnedFactoryFn) { @@ -53,7 +54,9 @@ export default function getConfig() { } } return rv; -} +}); + +export default getConfig; export function getConfigValue(path: string) { return _get(getConfig(), path);