Skip to content

Commit

Permalink
feat: optimize code
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Nov 1, 2024
1 parent 2503d6d commit 88f485a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
1 change: 0 additions & 1 deletion src/core/public/chrome/chrome_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ describe('start', () => {
false,
true,
true,
true,
]
`);
});
Expand Down
39 changes: 16 additions & 23 deletions src/core/public/chrome/chrome_service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand All @@ -198,7 +191,7 @@ export class ChromeService {
this.headerVariantOverride$ = new BehaviorSubject<HeaderVariant | undefined>(undefined);

const appHeaderVariant$ = application.currentAppId$.pipe(
flatMap((appId) =>
switchMap((appId) =>
application.applications$.pipe(
map(
(applications) =>
Expand Down

0 comments on commit 88f485a

Please sign in to comment.