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

fix: staled closure inside chrome_service #8783

Merged
merged 6 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions changelogs/fragments/8783.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Staled closure inside chrome_service ([#8783](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8783))
35 changes: 34 additions & 1 deletion src/core/public/chrome/chrome_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { HeaderVariant } from './constants';

class FakeApp implements App {
public title: string;
public appRoute: string;
public mount = () => () => {};

constructor(
Expand All @@ -54,6 +55,7 @@ class FakeApp implements App {
public headerVariant?: HeaderVariant
) {
this.title = `${this.id} App`;
this.appRoute = this.id;
}
}
const store = new Map();
Expand All @@ -78,12 +80,18 @@ function defaultStartDeps(availableApps?: App[]) {
uiSettings: uiSettingsServiceMock.createStartContract(),
overlays: overlayServiceMock.createStartContract(),
workspaces: workspacesServiceMock.createStartContract(),
updateApplications: (() => {}) as (applications?: App[]) => void,
};

if (availableApps) {
deps.application.applications$ = new Rx.BehaviorSubject<Map<string, PublicAppInfo>>(
const applications$ = new Rx.BehaviorSubject<Map<string, PublicAppInfo>>(
new Map(availableApps.map((app) => [app.id, getAppInfo(app) as PublicAppInfo]))
);
deps.application.applications$ = applications$;
deps.updateApplications = (applications?: App[]) =>
applications$.next(
new Map(applications?.map((app) => [app.id, getAppInfo(app) as PublicAppInfo]))
);
}

return deps;
Expand Down Expand Up @@ -285,6 +293,31 @@ describe('start', () => {
]
`);
});

it('should use correct current app id to tell if hidden', async () => {
const apps = [new FakeApp('alpha', true), new FakeApp('beta', false)];
const startDeps = defaultStartDeps(apps);
const { navigateToApp } = startDeps.application;
const { chrome } = await start({ startDeps });
const visibleChangedArray: boolean[] = [];
const visible$ = chrome.getIsVisible$();
visible$.subscribe((visible) => visibleChangedArray.push(visible));

await navigateToApp('alpha');

await navigateToApp('beta');
startDeps.updateApplications(apps);

expect(visibleChangedArray).toMatchInlineSnapshot(`
Array [
false,
false,
true,
true,
true,
]
`);
});
});

describe('header variant', () => {
Expand Down
24 changes: 19 additions & 5 deletions src/core/public/chrome/chrome_service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,32 @@ 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) =>
application.applications$.pipe(
flatMap((appId) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems change to switchMap could solve the issue?

// Update the currentAppId to latest
currentAppId = appId;
return application.applications$.pipe(
map((applications) => {
return !!appId && applications.has(appId) && !!applications.get(appId)!.chromeless;
return (
!!currentAppId &&
applications.has(currentAppId) &&
!!applications.get(currentAppId)!.chromeless
);
})
)
)
);
})
)
);
this.isVisible$ = combineLatest([appHidden$, this.isForceHidden$]).pipe(
Expand Down
Loading