Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Controls] Fix Initialization Race Condition #143220

Merged
merged 4 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { skip, debounceTime, distinctUntilChanged } from 'rxjs/operators';
import React from 'react';
import ReactDOM from 'react-dom';
import { Filter, uniqFilters } from '@kbn/es-query';
import { merge, Subject, Subscription } from 'rxjs';
import { BehaviorSubject, merge, Subject, Subscription } from 'rxjs';
import { EuiContextMenuPanel } from '@elastic/eui';

import {
Expand Down Expand Up @@ -57,6 +57,8 @@ export class ControlGroupContainer extends Container<
public readonly type = CONTROL_GROUP_TYPE;
public readonly anyControlOutputConsumerLoading$: Subject<boolean> = new Subject();

private initialized$ = new BehaviorSubject(false);

private subscriptions: Subscription = new Subscription();
private domNode?: HTMLElement;
private recalculateFilters$: Subject<null>;
Expand Down Expand Up @@ -194,10 +196,11 @@ export class ControlGroupContainer extends Container<
});

// when all children are ready setup subscriptions
this.untilReady().then(() => {
this.untilAllChildrenReady().then(() => {
this.recalculateDataViews();
this.recalculateFilters();
this.setupSubscriptions();
this.initialized$.next(true);
});
}

Expand Down Expand Up @@ -327,7 +330,7 @@ export class ControlGroupContainer extends Container<
};
}

public untilReady = () => {
public untilAllChildrenReady = () => {
const panelsLoading = () =>
Object.keys(this.getInput().panels).some(
(panelId) => !this.getOutput().embeddableLoaded[panelId]
Expand All @@ -349,6 +352,24 @@ export class ControlGroupContainer extends Container<
return Promise.resolve();
};

public untilInitialized = () => {
if (this.initialized$.value === false) {
return new Promise<void>((resolve, reject) => {
const subscription = this.initialized$.subscribe((isInitialized) => {
if (this.destroyed) {
subscription.unsubscribe();
reject();
}
if (isInitialized) {
subscription.unsubscribe();
resolve();
}
});
});
}
return Promise.resolve();
};

public render(dom: HTMLElement) {
if (this.domNode) {
ReactDOM.unmountComponentAtNode(this.domNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,13 @@ export class DashboardContainer extends Container<InheritedChildInput, Dashboard
isProjectEnabledInLabs('labs:dashboard:dashboardControls')
) {
this.controlGroup = controlGroup;
this.controlGroup.untilReady().then(() => {
if (!this.controlGroup || isErrorEmbeddable(this.controlGroup)) return;
syncDashboardControlGroup({
dashboardContainer: this,
controlGroup: this.controlGroup,
}).then((result) => {
if (!result) return;
const { onDestroyControlGroup } = result;
this.onDestroyControlGroup = onDestroyControlGroup;
});
syncDashboardControlGroup({
dashboardContainer: this,
controlGroup: this.controlGroup,
}).then((result) => {
if (!result) return;
const { onDestroyControlGroup } = result;
this.onDestroyControlGroup = onDestroyControlGroup;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ContainerOutput,
EmbeddableFactory,
EmbeddableFactoryDefinition,
isErrorEmbeddable,
} from '@kbn/embeddable-plugin/public';

import { getDefaultControlGroupInput } from '@kbn/controls-plugin/common';
Expand Down Expand Up @@ -73,15 +74,13 @@ export class DashboardContainerFactoryDefinition
};
}

public create = async (
initialInput: DashboardContainerInput,
parent?: Container
): Promise<DashboardContainer | ErrorEmbeddable> => {
private buildControlGroup = async (
initialInput: DashboardContainerInput
): Promise<ControlGroupContainer | ErrorEmbeddable | undefined> => {
const { pluginServices } = await import('../../services/plugin_services');
const {
embeddable: { getEmbeddableFactory },
} = pluginServices.getServices();

const controlsGroupFactory = getEmbeddableFactory<
ControlGroupInput,
ControlGroupOutput,
Expand All @@ -97,10 +96,23 @@ export class DashboardContainerFactoryDefinition
filters,
query,
});
if (controlGroup && !isErrorEmbeddable(controlGroup)) {
await controlGroup.untilInitialized();
}
return controlGroup;
};

public create = async (
initialInput: DashboardContainerInput,
parent?: Container
): Promise<DashboardContainer | ErrorEmbeddable> => {
const controlGroupPromise = this.buildControlGroup(initialInput);
const dashboardContainerPromise = import('./dashboard_container');

const { DashboardContainer: DashboardContainerEmbeddable } = await import(
'./dashboard_container'
);
const [controlGroup, { DashboardContainer: DashboardContainerEmbeddable }] = await Promise.all([
controlGroupPromise,
dashboardContainerPromise,
]);

return Promise.resolve(new DashboardContainerEmbeddable(initialInput, parent, controlGroup));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export interface DashboardViewportProps {

interface State {
isFullScreenMode: boolean;
controlGroupReady: boolean;
useMargins: boolean;
title: string;
description?: string;
Expand All @@ -57,7 +56,6 @@ export class DashboardViewport extends React.Component<DashboardViewportProps, S
this.controlsRoot = React.createRef();

this.state = {
controlGroupReady: !this.props.controlGroup,
isFullScreenMode,
panelCount: Object.values(panels).length,
useMargins,
Expand Down Expand Up @@ -85,9 +83,6 @@ export class DashboardViewport extends React.Component<DashboardViewportProps, S
if (this.props.controlGroup && this.controlsRoot.current) {
this.props.controlGroup.render(this.controlsRoot.current);
}
if (this.props.controlGroup) {
this.props.controlGroup?.untilReady().then(() => this.setState({ controlGroupReady: true }));
}
}

public componentWillUnmount() {
Expand Down Expand Up @@ -164,9 +159,7 @@ export class DashboardViewport extends React.Component<DashboardViewportProps, S
<DashboardEmptyScreen isEditMode={isEditMode} />
</div>
)}
{this.state.controlGroupReady && (
<DashboardGrid container={container} onDataLoaded={this.props.onDataLoaded} />
)}
<DashboardGrid container={container} onDataLoaded={this.props.onDataLoaded} />
</div>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
const spaces = getService('spaces');
const elasticChart = getService('elasticChart');

// Failing: See https://github.com/elastic/kibana/issues/142913
// Failing: See https://github.com/elastic/kibana/issues/142912
describe.skip('Dashboard to dashboard drilldown', function () {
describe('Dashboard to dashboard drilldown', function () {
describe('Create & use drilldowns', () => {
before(async () => {
log.debug('Dashboard Drilldowns:initTests');
Expand Down