Skip to content

Commit 84a1af1

Browse files
fix dashboard index pattern race condition (#72899) (#73408)
* fix dashboard index pattern race condition * improve Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 66e557a commit 84a1af1

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

src/plugins/dashboard/public/application/dashboard_app_controller.tsx

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import React, { useState, ReactElement } from 'react';
2525
import ReactDOM from 'react-dom';
2626
import angular from 'angular';
2727

28-
import { Subscription } from 'rxjs';
29-
import { map } from 'rxjs/operators';
28+
import { Observable, pipe, Subscription } from 'rxjs';
29+
import { filter, map, mapTo, startWith, switchMap } from 'rxjs/operators';
3030
import { History } from 'history';
3131
import { SavedObjectSaveOpts } from 'src/plugins/saved_objects/public';
3232
import { NavigationPublicPluginStart as NavigationStart } from 'src/plugins/navigation/public';
@@ -253,11 +253,7 @@ export class DashboardAppController {
253253
navActions[TopNavIds.VISUALIZE]();
254254
};
255255

256-
const updateIndexPatterns = (container?: DashboardContainer) => {
257-
if (!container || isErrorEmbeddable(container)) {
258-
return;
259-
}
260-
256+
function getDashboardIndexPatterns(container: DashboardContainer): IndexPattern[] {
261257
let panelIndexPatterns: IndexPattern[] = [];
262258
Object.values(container.getChildIds()).forEach((id) => {
263259
const embeddableInstance = container.getChild(id);
@@ -267,19 +263,34 @@ export class DashboardAppController {
267263
panelIndexPatterns.push(...embeddableIndexPatterns);
268264
});
269265
panelIndexPatterns = uniqBy(panelIndexPatterns, 'id');
266+
return panelIndexPatterns;
267+
}
270268

271-
if (panelIndexPatterns && panelIndexPatterns.length > 0) {
272-
$scope.$evalAsync(() => {
273-
$scope.indexPatterns = panelIndexPatterns;
274-
});
275-
} else {
276-
indexPatterns.getDefault().then((defaultIndexPattern) => {
277-
$scope.$evalAsync(() => {
278-
$scope.indexPatterns = [defaultIndexPattern as IndexPattern];
279-
});
269+
const updateIndexPatternsOperator = pipe(
270+
filter((container: DashboardContainer) => !!container && !isErrorEmbeddable(container)),
271+
map(getDashboardIndexPatterns),
272+
// using switchMap for previous task cancellation
273+
switchMap((panelIndexPatterns: IndexPattern[]) => {
274+
return new Observable((observer) => {
275+
if (panelIndexPatterns && panelIndexPatterns.length > 0) {
276+
$scope.$evalAsync(() => {
277+
if (observer.closed) return;
278+
$scope.indexPatterns = panelIndexPatterns;
279+
observer.complete();
280+
});
281+
} else {
282+
indexPatterns.getDefault().then((defaultIndexPattern) => {
283+
if (observer.closed) return;
284+
$scope.$evalAsync(() => {
285+
if (observer.closed) return;
286+
$scope.indexPatterns = [defaultIndexPattern as IndexPattern];
287+
observer.complete();
288+
});
289+
});
290+
}
280291
});
281-
}
282-
};
292+
})
293+
);
283294

284295
const getEmptyScreenProps = (
285296
shouldShowEditHelp: boolean,
@@ -384,11 +395,17 @@ export class DashboardAppController {
384395
) : null;
385396
};
386397

387-
updateIndexPatterns(dashboardContainer);
388-
389-
outputSubscription = dashboardContainer.getOutput$().subscribe(() => {
390-
updateIndexPatterns(dashboardContainer);
391-
});
398+
outputSubscription = new Subscription();
399+
outputSubscription.add(
400+
dashboardContainer
401+
.getOutput$()
402+
.pipe(
403+
mapTo(dashboardContainer),
404+
startWith(dashboardContainer), // to trigger initial index pattern update
405+
updateIndexPatternsOperator
406+
)
407+
.subscribe()
408+
);
392409

393410
inputSubscription = dashboardContainer.getInput$().subscribe(() => {
394411
let dirty = false;

0 commit comments

Comments
 (0)