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

[Execution Context] Update on URL change #200785

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 @@ -9,6 +9,8 @@

import { BehaviorSubject, firstValueFrom } from 'rxjs';
import { analyticsServiceMock } from '@kbn/core-analytics-browser-mocks';
import { applicationServiceMock } from '@kbn/core-application-browser-mocks';
import type { InternalApplicationStart } from '@kbn/core-application-browser-internal';
import type { AnalyticsServiceSetup } from '@kbn/core-analytics-browser';
import type { ExecutionContextSetup } from '@kbn/core-execution-context-browser';
import { ExecutionContextService } from './execution_context_service';
Expand All @@ -18,14 +20,19 @@ describe('ExecutionContextService', () => {
let curApp$: BehaviorSubject<string>;
let execService: ExecutionContextService;
let analytics: jest.Mocked<AnalyticsServiceSetup>;
let history: jest.Mocked<InternalApplicationStart['history']>;

beforeEach(() => {
analytics = analyticsServiceMock.createAnalyticsServiceSetup();
history = applicationServiceMock.createInternalStartContract().history as jest.Mocked<
InternalApplicationStart['history']
>;
execService = new ExecutionContextService();
execContext = execService.setup({ analytics });
curApp$ = new BehaviorSubject('app1');
execContext = execService.start({
curApp$,
history,
});
});

Expand Down Expand Up @@ -96,6 +103,50 @@ describe('ExecutionContextService', () => {
);
});

it('url updates automatically when there is a navigation', async () => {
execContext.set({
type: 'ghf',
meta: {
foo: 1,
},
description: 'first set',
});

expect(execContext.get()).toMatchInlineSnapshot(
{
name: 'app1',
description: 'first set',
type: 'ghf',
url: '/',
},
`
Object {
"description": "first set",
"meta": Object {
"foo": 1,
},
"name": "app1",
"type": "ghf",
"url": "/",
}
`
);

history.listen.mock.calls[0][0]({ ...history.location, pathname: '/another-path' }, 'PUSH');

expect(execContext.get()).toMatchInlineSnapshot(`
Object {
"description": "first set",
"meta": Object {
"foo": 1,
},
"name": "app1",
"type": "ghf",
"url": "/another-path",
}
`);
});

it('sets context and adds current url and appid when getting it', () => {
execContext.set({
type: 'ghf',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import { compact, isEqual, isUndefined, omitBy } from 'lodash';
import type { History } from 'history';
import type { Observable } from 'rxjs';
import { BehaviorSubject, Subscription } from 'rxjs';
import { map } from 'rxjs';
Expand All @@ -32,6 +33,7 @@ export interface SetupDeps {

export interface StartDeps {
curApp$: Observable<string | undefined>;
history: History<unknown>;
}

/** @internal */
Expand Down Expand Up @@ -75,7 +77,7 @@ export class ExecutionContextService
return this.contract;
}

public start({ curApp$ }: StartDeps) {
public start({ curApp$, history }: StartDeps) {
const start = this.contract!;

// Track app id changes and clear context on app change
Expand All @@ -86,6 +88,13 @@ export class ExecutionContextService
})
);

// Track URL changes to make sure that we reflect the new path name
this.subscription.add(
history.listen((location) => {
start.set({ url: location.pathname });
Copy link
Contributor

Choose a reason for hiding this comment

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

it might be that location.pathname didn't change, right? Maybe worth calling only when has changed?

})
);

return start;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"@kbn/core-analytics-browser",
"@kbn/core-analytics-browser-mocks",
"@kbn/core-execution-context-common",
"@kbn/core-execution-context-browser"
"@kbn/core-execution-context-browser",
"@kbn/core-application-browser-mocks",
"@kbn/core-application-browser-internal"
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ export class CoreSystem {

const executionContext = this.executionContext.start({
curApp$: application.currentAppId$,
history: application.history,
});

const chrome = await this.chrome.start({
Expand Down