Skip to content

Commit

Permalink
[Lens] Cache avilable operations meta data (#95034)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored Mar 24, 2021
1 parent 326abc2 commit 6295ae7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import _ from 'lodash';
import { DatasourceDimensionDropProps } from '../../types';
import { OperationType } from '../indexpattern';
import { getAvailableOperationsByMetadata } from '../operations';
import { memoizedGetAvailableOperationsByMetadata } from '../operations';
import { IndexPatternPrivateState } from '../types';

export interface OperationSupportMatrix {
Expand All @@ -30,7 +30,7 @@ export const getOperationSupportMatrix = (props: Props): OperationSupportMatrix
const layerId = props.layerId;
const currentIndexPattern = props.state.indexPatterns[props.state.layers[layerId].indexPatternId];

const filteredOperationsByMetadata = getAvailableOperationsByMetadata(
const filteredOperationsByMetadata = memoizedGetAvailableOperationsByMetadata(
currentIndexPattern
).filter((operation) => props.filterOperations(operation.operationMetaData));

Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/lens/public/indexpattern_datasource/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { VisualizeFieldContext } from '../../../../../src/plugins/ui_actions/pub
import { documentField } from './document_field';
import { readFromStorage, writeToStorage } from '../settings_storage';
import { getFieldByNameFactory } from './pure_helpers';
import { memoizedGetAvailableOperationsByMetadata } from './operations';

type SetState = StateSetter<IndexPatternPrivateState>;
type IndexPatternsService = Pick<IndexPatternsContract, 'get' | 'getIdsWithTitle'>;
Expand All @@ -49,6 +50,11 @@ export async function loadIndexPatterns({
return cache;
}

if (memoizedGetAvailableOperationsByMetadata.cache.clear) {
// clear operations meta data cache because index pattern reference may change
memoizedGetAvailableOperationsByMetadata.cache.clear();
}

const allIndexPatterns = await Promise.allSettled(
missingIds.map((id) => indexPatternsService.get(id))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jest.spyOn(actualHelpers, 'getErrorMessages');

export const {
getAvailableOperationsByMetadata,
memoizedGetAvailableOperationsByMetadata,
getOperations,
getOperationDisplay,
getOperationTypesForField,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import _ from 'lodash';
import { memoize } from 'lodash';
import { OperationMetadata } from '../../types';
import {
operationDefinitionMap,
Expand Down Expand Up @@ -187,3 +187,5 @@ export function getAvailableOperationsByMetadata(indexPattern: IndexPattern) {

return Object.values(operationByMetadata);
}

export const memoizedGetAvailableOperationsByMetadata = memoize(getAvailableOperationsByMetadata);

0 comments on commit 6295ae7

Please sign in to comment.