Skip to content

Commit

Permalink
combine async calls
Browse files Browse the repository at this point in the history
  • Loading branch information
semd committed Dec 16, 2022
1 parent 7843c3f commit cd1bdbe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { render, unmountComponentAtNode } from 'react-dom';
import { I18nProvider } from '@kbn/i18n-react';
import { css } from '@emotion/react';
import { i18n } from '@kbn/i18n';
import { Datatable, ExpressionRenderDefinition } from '@kbn/expressions-plugin/public';
import type {
Datatable,
ExpressionRenderDefinition,
IInterpreterRenderHandlers,
} from '@kbn/expressions-plugin/public';
import type { PersistedState } from '@kbn/visualizations-plugin/public';
import { withSuspense } from '@kbn/presentation-util-plugin/public';
import { KibanaThemeProvider } from '@kbn/kibana-react-plugin/public';
Expand Down Expand Up @@ -50,16 +54,18 @@ const partitionVisRenderer = css({
export const getColumnCellValueActions = async (
visConfig: PartitionVisParams,
visData: Datatable,
getCompatibleCellValueActions: GetCompatibleCellValueActions | undefined
getCompatibleCellValueActions?: IInterpreterRenderHandlers['getCompatibleCellValueActions']
) => {
if (!Array.isArray(visConfig.dimensions.buckets) || !getCompatibleCellValueActions) {
return [];
}
return Promise.all(
visConfig.dimensions.buckets.reduce<Array<Promise<CellValueAction[]>>>((acc, accessor) => {
const column = getColumnByAccessor(accessor, visData.columns);
if (column) {
acc.push(getCompatibleCellValueActions([{ columnMeta: column.meta }]));
const columnMeta = getColumnByAccessor(accessor, visData.columns)?.meta;
if (columnMeta) {
acc.push(
(getCompatibleCellValueActions as GetCompatibleCellValueActions)([{ columnMeta }])
);
}
return acc;
}, [])
Expand All @@ -84,12 +90,6 @@ export const getPartitionVisRenderer: (
unmountComponentAtNode(domNode);
});

const columnCellValueActions = await getColumnCellValueActions(
visConfig,
visData,
handlers.getCompatibleCellValueActions as GetCompatibleCellValueActions
);

const renderComplete = () => {
const executionContext = handlers.getExecutionContext();
const containerType = extractContainerType(executionContext);
Expand All @@ -106,7 +106,10 @@ export const getPartitionVisRenderer: (
handlers.done();
};

const palettesRegistry = await plugins.charts.palettes.getPalettes();
const [columnCellValueActions, palettesRegistry] = await Promise.all([
getColumnCellValueActions(visConfig, visData, handlers.getCompatibleCellValueActions),
plugins.charts.palettes.getPalettes(),
]);

render(
<I18nProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import { ExtendedDataLayerConfig, XYProps, AnnotationLayerConfigResult } from '.
import { DataLayers } from './data_layers';
import { SplitChart } from './split_chart';
import { LegendSize } from '@kbn/visualizations-plugin/common';
import { LayerCellValueActions } from '../types';
import type { LayerCellValueActions } from '../types';

const onClickValue = jest.fn();
const layerCellValueActions: LayerCellValueActions = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import { PersistedState } from '@kbn/visualizations-plugin/public';
import type { ChartsPluginStart } from '@kbn/charts-plugin/public';
import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
import { EventAnnotationServiceType } from '@kbn/event-annotation-plugin/public';
import { ExpressionRenderDefinition } from '@kbn/expressions-plugin/common';
import type {
ExpressionRenderDefinition,
IInterpreterRenderHandlers,
} from '@kbn/expressions-plugin/common';
import { FormatFactory } from '@kbn/field-formats-plugin/common';
import { KibanaThemeProvider } from '@kbn/kibana-react-plugin/public';
import { UsageCollectionStart } from '@kbn/usage-collection-plugin/public';
Expand Down Expand Up @@ -162,7 +165,7 @@ const extractCounterEvents = (
**/
const getLayerCellValueActions = async (
layers: CommonXYDataLayerConfig[],
getCompatibleCellValueActions?: GetCompatibleCellValueActions
getCompatibleCellValueActions?: IInterpreterRenderHandlers['getCompatibleCellValueActions']
) => {
if (!layers || !getCompatibleCellValueActions) {
return [];
Expand All @@ -174,7 +177,7 @@ const getLayerCellValueActions = async (
const column = layer.table.columns.find(({ id }) => id === accessor);
return { columnMeta: column?.meta };
}) ?? [];
return getCompatibleCellValueActions(data);
return (getCompatibleCellValueActions as GetCompatibleCellValueActions)(data);
})
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import type {
import type { FormatFactory } from '../../../common';
import type { DatatableProps } from '../../../common/expressions';

async function columnsFilterable(table: Datatable, handlers: IInterpreterRenderHandlers) {
async function getColumnsFilterable(table: Datatable, handlers: IInterpreterRenderHandlers) {
if (!table.rows.length) {
return;
}
Expand Down Expand Up @@ -58,15 +58,15 @@ async function columnsFilterable(table: Datatable, handlers: IInterpreterRenderH
**/
export async function getColumnCellValueActions(
config: DatatableProps,
getCompatibleCellValueActions: GetCompatibleCellValueActions | undefined
getCompatibleCellValueActions?: ILensInterpreterRenderHandlers['getCompatibleCellValueActions']
): Promise<LensCellValueAction[][]> {
if (!config.data || !getCompatibleCellValueActions) {
return [];
}
return Promise.all(
config.data.columns.map(({ meta: columnMeta }) => {
try {
return getCompatibleCellValueActions([{ columnMeta }]);
return (getCompatibleCellValueActions as GetCompatibleCellValueActions)([{ columnMeta }]);
} catch {
return [];
}
Expand Down Expand Up @@ -129,10 +129,10 @@ export const getDatatableRenderer = (dependencies: {
}
}

const columnCellValueActions = await getColumnCellValueActions(
config,
getCompatibleCellValueActions as GetCompatibleCellValueActions | undefined
);
const [columnCellValueActions, columnsFilterable] = await Promise.all([
getColumnCellValueActions(config, getCompatibleCellValueActions),
getColumnsFilterable(config.data, handlers),
]);

ReactDOM.render(
<KibanaThemeProvider theme$={dependencies.theme.theme$}>
Expand All @@ -146,7 +146,7 @@ export const getDatatableRenderer = (dependencies: {
getType={resolvedGetType}
rowHasRowClickTriggerActions={rowHasRowClickTriggerActions}
columnCellValueActions={columnCellValueActions}
columnFilterable={await columnsFilterable(config.data, handlers)}
columnFilterable={columnsFilterable}
interactive={isInteractive()}
uiSettings={dependencies.uiSettings}
renderComplete={renderComplete}
Expand Down

0 comments on commit cd1bdbe

Please sign in to comment.