Skip to content

Commit

Permalink
Add memoizaion for getConfig (#679)
Browse files Browse the repository at this point in the history
Signed-off-by: Mykhailo Semenchenko <mykhailo.semenchenko@logz.io>
  • Loading branch information
th3M1ke authored Jan 15, 2021
1 parent e0ee8eb commit 48956d5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -402,6 +403,7 @@ describe('<SearchForm>', () => {

it('uses config.search.maxLimit', () => {
const maxLimit = 6789;
getConfig.apply({}, []);
const config = {
search: {
maxLimit,
Expand Down
5 changes: 3 additions & 2 deletions packages/jaeger-ui/src/utils/config/get-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('getConfig()', () => {
let getJaegerUiConfig;

beforeEach(() => {
getConfig.apply({}, []);
embedded = {};
getJaegerUiConfig = jest.fn(() => embedded);
window.getJaegerUiConfig = getJaegerUiConfig;
Expand Down Expand Up @@ -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);
});
});
});
Expand Down
7 changes: 5 additions & 2 deletions packages/jaeger-ui/src/utils/config/get-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand Down Expand Up @@ -53,7 +54,9 @@ export default function getConfig() {
}
}
return rv;
}
});

export default getConfig;

export function getConfigValue(path: string) {
return _get(getConfig(), path);
Expand Down

0 comments on commit 48956d5

Please sign in to comment.