Skip to content

Commit 7cf64b7

Browse files
[8.x] Use dashboard factory directly instead of pulling from registry (#193480) (#195615)
# Backport This will backport the following commits from `main` to `8.x`: - [Use dashboard factory directly instead of pulling from registry (#193480)](#193480) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Nathan Reese","email":"reese.nathan@elastic.co"},"sourceCommit":{"committedDate":"2024-10-09T14:26:43Z","message":"Use dashboard factory directly instead of pulling from registry (#193480)\n\nPR removes dashboard embeddable from embeddable registry. No other\r\napplication accesses the dashboard embeddable from the embeddable\r\nregistry so registration is not needed. Plus, once lens embeddable is\r\nconverted to a react embeddable, then we can remove the legacy\r\nembeddable registry prior to refactoring dashboard to not be an\r\nembeddable (which will be a large effort and we want to remove the\r\nlegacy embeddable registry as soon as possible to avoid any one else\r\nusing it).\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>\r\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>","sha":"3b6cfb685d29e5db8c66c4239e709485a81d0db0","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Presentation","release_note:skip","v9.0.0","project:embeddableRebuild","v8.16.0","backport:version"],"title":"Use dashboard factory directly instead of pulling from registry","number":193480,"url":"https://github.com/elastic/kibana/pull/193480","mergeCommit":{"message":"Use dashboard factory directly instead of pulling from registry (#193480)\n\nPR removes dashboard embeddable from embeddable registry. No other\r\napplication accesses the dashboard embeddable from the embeddable\r\nregistry so registration is not needed. Plus, once lens embeddable is\r\nconverted to a react embeddable, then we can remove the legacy\r\nembeddable registry prior to refactoring dashboard to not be an\r\nembeddable (which will be a large effort and we want to remove the\r\nlegacy embeddable registry as soon as possible to avoid any one else\r\nusing it).\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>\r\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>","sha":"3b6cfb685d29e5db8c66c4239e709485a81d0db0"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/193480","number":193480,"mergeCommit":{"message":"Use dashboard factory directly instead of pulling from registry (#193480)\n\nPR removes dashboard embeddable from embeddable registry. No other\r\napplication accesses the dashboard embeddable from the embeddable\r\nregistry so registration is not needed. Plus, once lens embeddable is\r\nconverted to a react embeddable, then we can remove the legacy\r\nembeddable registry prior to refactoring dashboard to not be an\r\nembeddable (which will be a large effort and we want to remove the\r\nlegacy embeddable registry as soon as possible to avoid any one else\r\nusing it).\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>\r\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>","sha":"3b6cfb685d29e5db8c66c4239e709485a81d0db0"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Nathan Reese <reese.nathan@elastic.co>
1 parent 5991f4c commit 7cf64b7

5 files changed

Lines changed: 46 additions & 43 deletions

File tree

src/plugins/dashboard/common/dashboard_saved_object/persistable_state/dashboard_saved_object_references.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import {
1616
} from '../../lib/dashboard_panel_converters';
1717
import { DashboardAttributesAndReferences, ParsedDashboardAttributesWithType } from '../../types';
1818
import { DashboardAttributes, SavedDashboardPanel } from '../../content_management';
19+
import {
20+
createExtract,
21+
createInject,
22+
} from '../../dashboard_container/persistable_state/dashboard_container_references';
1923

2024
export interface InjectExtractDeps {
2125
embeddablePersistableStateService: EmbeddablePersistableStateService;
@@ -45,10 +49,8 @@ export function injectReferences(
4549
const parsedAttributes = parseDashboardAttributesWithType(attributes);
4650

4751
// inject references back into panels via the Embeddable persistable state service.
48-
const injectedState = deps.embeddablePersistableStateService.inject(
49-
parsedAttributes,
50-
references
51-
) as ParsedDashboardAttributesWithType;
52+
const inject = createInject(deps.embeddablePersistableStateService);
53+
const injectedState = inject(parsedAttributes, references) as ParsedDashboardAttributesWithType;
5254
const injectedPanels = convertPanelMapToSavedPanels(injectedState.panels);
5355

5456
const newAttributes = {
@@ -74,11 +76,11 @@ export function extractReferences(
7476
);
7577
}
7678

77-
const { references: extractedReferences, state: extractedState } =
78-
deps.embeddablePersistableStateService.extract(parsedAttributes) as {
79-
references: Reference[];
80-
state: ParsedDashboardAttributesWithType;
81-
};
79+
const extract = createExtract(deps.embeddablePersistableStateService);
80+
const { references: extractedReferences, state: extractedState } = extract(parsedAttributes) as {
81+
references: Reference[];
82+
state: ParsedDashboardAttributesWithType;
83+
};
8284
const extractedPanels = convertPanelMapToSavedPanels(extractedState.panels);
8385

8486
const newAttributes = {

src/plugins/dashboard/public/dashboard_container/external_api/dashboard_renderer.test.tsx

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ import { SavedObjectNotFound } from '@kbn/kibana-utils-plugin/common';
1818
import { setStubKibanaServices as setPresentationPanelMocks } from '@kbn/presentation-panel-plugin/public/mocks';
1919
import { BehaviorSubject } from 'rxjs';
2020
import { DashboardContainerFactory } from '..';
21-
import { DASHBOARD_CONTAINER_TYPE, DashboardCreationOptions } from '../..';
22-
import { embeddableService } from '../../services/kibana_services';
21+
import { DashboardCreationOptions } from '../..';
2322
import { DashboardContainer } from '../embeddable/dashboard_container';
2423
import { DashboardRenderer } from './dashboard_renderer';
2524

25+
jest.mock('../embeddable/dashboard_container_factory', () => ({}));
26+
2627
describe('dashboard renderer', () => {
2728
let mockDashboardContainer: DashboardContainer;
2829
let mockDashboardFactory: DashboardContainerFactory;
@@ -38,15 +39,17 @@ describe('dashboard renderer', () => {
3839
mockDashboardFactory = {
3940
create: jest.fn().mockReturnValue(mockDashboardContainer),
4041
} as unknown as DashboardContainerFactory;
41-
embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockDashboardFactory);
42+
// eslint-disable-next-line @typescript-eslint/no-var-requires
43+
require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest
44+
.fn()
45+
.mockReturnValue(mockDashboardFactory);
4246
setPresentationPanelMocks();
4347
});
4448

4549
test('calls create method on the Dashboard embeddable factory', async () => {
4650
await act(async () => {
4751
mountWithIntl(<DashboardRenderer />);
4852
});
49-
expect(embeddableService.getEmbeddableFactory).toHaveBeenCalledWith(DASHBOARD_CONTAINER_TYPE);
5053
expect(mockDashboardFactory.create).toHaveBeenCalled();
5154
});
5255

@@ -103,7 +106,10 @@ describe('dashboard renderer', () => {
103106
mockDashboardFactory = {
104107
create: jest.fn().mockReturnValue(mockErrorEmbeddable),
105108
} as unknown as DashboardContainerFactory;
106-
embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockDashboardFactory);
109+
// eslint-disable-next-line @typescript-eslint/no-var-requires
110+
require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest
111+
.fn()
112+
.mockReturnValue(mockDashboardFactory);
107113

108114
let wrapper: ReactWrapper;
109115
await act(async () => {
@@ -125,7 +131,10 @@ describe('dashboard renderer', () => {
125131
const mockErrorFactory = {
126132
create: jest.fn().mockReturnValue(mockErrorEmbeddable),
127133
} as unknown as DashboardContainerFactory;
128-
embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockErrorFactory);
134+
// eslint-disable-next-line @typescript-eslint/no-var-requires
135+
require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest
136+
.fn()
137+
.mockReturnValue(mockErrorFactory);
129138

130139
// render the dashboard - it should run into an error and render the error embeddable.
131140
let wrapper: ReactWrapper;
@@ -146,7 +155,10 @@ describe('dashboard renderer', () => {
146155
const mockSuccessFactory = {
147156
create: jest.fn().mockReturnValue(mockSuccessEmbeddable),
148157
} as unknown as DashboardContainerFactory;
149-
embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockSuccessFactory);
158+
// eslint-disable-next-line @typescript-eslint/no-var-requires
159+
require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest
160+
.fn()
161+
.mockReturnValue(mockSuccessFactory);
150162

151163
// update the saved object id to trigger another dashboard load.
152164
await act(async () => {
@@ -175,7 +187,10 @@ describe('dashboard renderer', () => {
175187
const mockErrorFactory = {
176188
create: jest.fn().mockReturnValue(mockErrorEmbeddable),
177189
} as unknown as DashboardContainerFactory;
178-
embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockErrorFactory);
190+
// eslint-disable-next-line @typescript-eslint/no-var-requires
191+
require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest
192+
.fn()
193+
.mockReturnValue(mockErrorFactory);
179194

180195
// render the dashboard - it should run into an error and render the error embeddable.
181196
let wrapper: ReactWrapper;
@@ -238,7 +253,10 @@ describe('dashboard renderer', () => {
238253
const mockSuccessFactory = {
239254
create: jest.fn().mockReturnValue(mockSuccessEmbeddable),
240255
} as unknown as DashboardContainerFactory;
241-
embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockSuccessFactory);
256+
// eslint-disable-next-line @typescript-eslint/no-var-requires
257+
require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest
258+
.fn()
259+
.mockReturnValue(mockSuccessFactory);
242260

243261
let wrapper: ReactWrapper;
244262
await act(async () => {
@@ -263,7 +281,10 @@ describe('dashboard renderer', () => {
263281
const mockUseMarginFalseFactory = {
264282
create: jest.fn().mockReturnValue(mockUseMarginFalseEmbeddable),
265283
} as unknown as DashboardContainerFactory;
266-
embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockUseMarginFalseFactory);
284+
// eslint-disable-next-line @typescript-eslint/no-var-requires
285+
require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest
286+
.fn()
287+
.mockReturnValue(mockUseMarginFalseFactory);
267288

268289
let wrapper: ReactWrapper;
269290
await act(async () => {

src/plugins/dashboard/public/dashboard_container/external_api/dashboard_renderer.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,11 @@ import { SavedObjectNotFound } from '@kbn/kibana-utils-plugin/common';
2020
import { useStateFromPublishingSubject } from '@kbn/presentation-publishing';
2121
import { LocatorPublic } from '@kbn/share-plugin/common';
2222

23-
import { DASHBOARD_CONTAINER_TYPE } from '..';
2423
import { DashboardContainerInput } from '../../../common';
2524
import { DashboardApi } from '../../dashboard_api/types';
2625
import { embeddableService, screenshotModeService } from '../../services/kibana_services';
2726
import type { DashboardContainer } from '../embeddable/dashboard_container';
28-
import {
29-
DashboardContainerFactory,
30-
DashboardContainerFactoryDefinition,
31-
} from '../embeddable/dashboard_container_factory';
27+
import { DashboardContainerFactoryDefinition } from '../embeddable/dashboard_container_factory';
3228
import type { DashboardCreationOptions } from '../..';
3329
import { DashboardLocatorParams, DashboardRedirect } from '../types';
3430
import { Dashboard404Page } from './dashboard_404';
@@ -91,12 +87,8 @@ export function DashboardRenderer({
9187
(async () => {
9288
const creationOptions = await getCreationOptions?.();
9389

94-
const dashboardFactory = embeddableService.getEmbeddableFactory(
95-
DASHBOARD_CONTAINER_TYPE
96-
) as DashboardContainerFactory & {
97-
create: DashboardContainerFactoryDefinition['create'];
98-
};
99-
const container = await dashboardFactory?.create(
90+
const dashboardFactory = new DashboardContainerFactoryDefinition(embeddableService);
91+
const container = await dashboardFactory.create(
10092
{ id } as unknown as DashboardContainerInput, // Input from creationOptions is used instead.
10193
undefined,
10294
creationOptions,

src/plugins/dashboard/public/dashboard_container/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ export const DASHBOARD_CONTAINER_TYPE = 'dashboard';
1515
export const LATEST_DASHBOARD_CONTAINER_VERSION = convertNumberToDashboardVersion(LATEST_VERSION);
1616

1717
export type { DashboardContainer } from './embeddable/dashboard_container';
18-
export {
19-
type DashboardContainerFactory,
20-
DashboardContainerFactoryDefinition,
21-
} from './embeddable/dashboard_container_factory';
18+
export { type DashboardContainerFactory } from './embeddable/dashboard_container_factory';
2219

2320
export { LazyDashboardRenderer } from './external_api/lazy_dashboard_renderer';
2421
export type { DashboardLocatorParams } from './types';

src/plugins/dashboard/public/plugin.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ import {
7272
LEGACY_DASHBOARD_APP_ID,
7373
SEARCH_SESSION_ID,
7474
} from './dashboard_constants';
75-
import { DashboardContainerFactoryDefinition } from './dashboard_container/embeddable/dashboard_container_factory';
7675
import {
7776
GetPanelPlacementSettings,
7877
registerDashboardPanelPlacementSetting,
@@ -227,14 +226,6 @@ export class DashboardPlugin
227226
},
228227
});
229228

230-
core.getStartServices().then(([, deps]) => {
231-
const dashboardContainerFactory = new DashboardContainerFactoryDefinition(deps.embeddable);
232-
embeddable.registerEmbeddableFactory(
233-
dashboardContainerFactory.type,
234-
dashboardContainerFactory
235-
);
236-
});
237-
238229
this.stopUrlTracking = () => {
239230
stopUrlTracker();
240231
};

0 commit comments

Comments
 (0)