From 88f485ae207ba63a448bcfa31e8a9e944981ccd9 Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Fri, 1 Nov 2024 17:44:02 +0800 Subject: [PATCH] feat: optimize code Signed-off-by: SuZhou-Joe --- src/core/public/chrome/chrome_service.test.ts | 1 - src/core/public/chrome/chrome_service.tsx | 39 ++++++++----------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/core/public/chrome/chrome_service.test.ts b/src/core/public/chrome/chrome_service.test.ts index 25fb9d35b54b..e930aa8931ce 100644 --- a/src/core/public/chrome/chrome_service.test.ts +++ b/src/core/public/chrome/chrome_service.test.ts @@ -314,7 +314,6 @@ describe('start', () => { false, true, true, - true, ] `); }); diff --git a/src/core/public/chrome/chrome_service.tsx b/src/core/public/chrome/chrome_service.tsx index 10613b9a2f04..b890fdfb8801 100644 --- a/src/core/public/chrome/chrome_service.tsx +++ b/src/core/public/chrome/chrome_service.tsx @@ -41,7 +41,7 @@ import { ReplaySubject, Subscription, } from 'rxjs'; -import { flatMap, map, takeUntil } from 'rxjs/operators'; +import { map, switchMap, takeUntil } from 'rxjs/operators'; import { EuiLink } from '@elastic/eui'; import { mountReactNode } from '../utils/mount'; import { InternalApplicationStart } from '../application'; @@ -160,32 +160,25 @@ export class ChromeService { const isEmbedded = new URL(location.hash.slice(1), location.origin).searchParams.has('embed'); this.isForceHidden$ = new BehaviorSubject(isEmbedded); - /** - * There is a staled closure issue here. - * For example, when currentAppId$ is going through A -> B -> C and - * the application.applications$ just get changed in B, then it will always use B as the currentAppId - * even though the latest appId now is C. - */ - let currentAppId: string | undefined; - const appHidden$ = merge( // For the isVisible$ logic, having no mounted app is equivalent to having a hidden app // in the sense that the chrome UI should not be displayed until a non-chromeless app is mounting or mounted of(true), application.currentAppId$.pipe( - flatMap((appId) => { - // Update the currentAppId to latest - currentAppId = appId; - return application.applications$.pipe( - map((applications) => { - return ( - !!currentAppId && - applications.has(currentAppId) && - !!applications.get(currentAppId)!.chromeless - ); - }) - ); - }) + /** + * There is a staled closure issue here. + * For example, when currentAppId$ is going through A -> B -> C and + * the application.applications$ just get changed in B, then it will always use B as the currentAppId + * even though the latest appId now is C. + */ + switchMap((appId) => + application.applications$.pipe( + map( + (applications) => + !!appId && applications.has(appId) && !!applications.get(appId)!.chromeless + ) + ) + ) ) ); this.isVisible$ = combineLatest([appHidden$, this.isForceHidden$]).pipe( @@ -198,7 +191,7 @@ export class ChromeService { this.headerVariantOverride$ = new BehaviorSubject(undefined); const appHeaderVariant$ = application.currentAppId$.pipe( - flatMap((appId) => + switchMap((appId) => application.applications$.pipe( map( (applications) =>