Skip to content

Commit

Permalink
Use dashboard factory directly instead of pulling from registry (elas…
Browse files Browse the repository at this point in the history
…tic#193480)

PR removes dashboard embeddable from embeddable registry. No other
application accesses the dashboard embeddable from the embeddable
registry so registration is not needed. Plus, once lens embeddable is
converted to a react embeddable, then we can remove the legacy
embeddable registry prior to refactoring dashboard to not be an
embeddable (which will be a large effort and we want to remove the
legacy embeddable registry as soon as possible to avoid any one else
using it).

---------

Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
3 people authored Oct 9, 2024
1 parent 3dd1ee8 commit 3b6cfb6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {
} from '../../lib/dashboard_panel_converters';
import { DashboardAttributesAndReferences, ParsedDashboardAttributesWithType } from '../../types';
import { DashboardAttributes, SavedDashboardPanel } from '../../content_management';
import {
createExtract,
createInject,
} from '../../dashboard_container/persistable_state/dashboard_container_references';

export interface InjectExtractDeps {
embeddablePersistableStateService: EmbeddablePersistableStateService;
Expand Down Expand Up @@ -45,10 +49,8 @@ export function injectReferences(
const parsedAttributes = parseDashboardAttributesWithType(attributes);

// inject references back into panels via the Embeddable persistable state service.
const injectedState = deps.embeddablePersistableStateService.inject(
parsedAttributes,
references
) as ParsedDashboardAttributesWithType;
const inject = createInject(deps.embeddablePersistableStateService);
const injectedState = inject(parsedAttributes, references) as ParsedDashboardAttributesWithType;
const injectedPanels = convertPanelMapToSavedPanels(injectedState.panels);

const newAttributes = {
Expand All @@ -74,11 +76,11 @@ export function extractReferences(
);
}

const { references: extractedReferences, state: extractedState } =
deps.embeddablePersistableStateService.extract(parsedAttributes) as {
references: Reference[];
state: ParsedDashboardAttributesWithType;
};
const extract = createExtract(deps.embeddablePersistableStateService);
const { references: extractedReferences, state: extractedState } = extract(parsedAttributes) as {
references: Reference[];
state: ParsedDashboardAttributesWithType;
};
const extractedPanels = convertPanelMapToSavedPanels(extractedState.panels);

const newAttributes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import { SavedObjectNotFound } from '@kbn/kibana-utils-plugin/common';
import { setStubKibanaServices as setPresentationPanelMocks } from '@kbn/presentation-panel-plugin/public/mocks';
import { BehaviorSubject } from 'rxjs';
import { DashboardContainerFactory } from '..';
import { DASHBOARD_CONTAINER_TYPE, DashboardCreationOptions } from '../..';
import { embeddableService } from '../../services/kibana_services';
import { DashboardCreationOptions } from '../..';
import { DashboardContainer } from '../embeddable/dashboard_container';
import { DashboardRenderer } from './dashboard_renderer';

jest.mock('../embeddable/dashboard_container_factory', () => ({}));

describe('dashboard renderer', () => {
let mockDashboardContainer: DashboardContainer;
let mockDashboardFactory: DashboardContainerFactory;
Expand All @@ -38,15 +39,17 @@ describe('dashboard renderer', () => {
mockDashboardFactory = {
create: jest.fn().mockReturnValue(mockDashboardContainer),
} as unknown as DashboardContainerFactory;
embeddableService.getEmbeddableFactory = jest.fn().mockReturnValue(mockDashboardFactory);
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('../embeddable/dashboard_container_factory').DashboardContainerFactoryDefinition = jest
.fn()
.mockReturnValue(mockDashboardFactory);
setPresentationPanelMocks();
});

test('calls create method on the Dashboard embeddable factory', async () => {
await act(async () => {
mountWithIntl(<DashboardRenderer />);
});
expect(embeddableService.getEmbeddableFactory).toHaveBeenCalledWith(DASHBOARD_CONTAINER_TYPE);
expect(mockDashboardFactory.create).toHaveBeenCalled();
});

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

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

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

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

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

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

let wrapper: ReactWrapper;
await act(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ import { SavedObjectNotFound } from '@kbn/kibana-utils-plugin/common';
import { useStateFromPublishingSubject } from '@kbn/presentation-publishing';
import { LocatorPublic } from '@kbn/share-plugin/common';

import { DASHBOARD_CONTAINER_TYPE } from '..';
import { DashboardContainerInput } from '../../../common';
import { DashboardApi } from '../../dashboard_api/types';
import { embeddableService, screenshotModeService } from '../../services/kibana_services';
import type { DashboardContainer } from '../embeddable/dashboard_container';
import {
DashboardContainerFactory,
DashboardContainerFactoryDefinition,
} from '../embeddable/dashboard_container_factory';
import { DashboardContainerFactoryDefinition } from '../embeddable/dashboard_container_factory';
import type { DashboardCreationOptions } from '../..';
import { DashboardLocatorParams, DashboardRedirect } from '../types';
import { Dashboard404Page } from './dashboard_404';
Expand Down Expand Up @@ -91,12 +87,8 @@ export function DashboardRenderer({
(async () => {
const creationOptions = await getCreationOptions?.();

const dashboardFactory = embeddableService.getEmbeddableFactory(
DASHBOARD_CONTAINER_TYPE
) as DashboardContainerFactory & {
create: DashboardContainerFactoryDefinition['create'];
};
const container = await dashboardFactory?.create(
const dashboardFactory = new DashboardContainerFactoryDefinition(embeddableService);
const container = await dashboardFactory.create(
{ id } as unknown as DashboardContainerInput, // Input from creationOptions is used instead.
undefined,
creationOptions,
Expand Down
5 changes: 1 addition & 4 deletions src/plugins/dashboard/public/dashboard_container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ export const DASHBOARD_CONTAINER_TYPE = 'dashboard';
export const LATEST_DASHBOARD_CONTAINER_VERSION = convertNumberToDashboardVersion(LATEST_VERSION);

export type { DashboardContainer } from './embeddable/dashboard_container';
export {
type DashboardContainerFactory,
DashboardContainerFactoryDefinition,
} from './embeddable/dashboard_container_factory';
export { type DashboardContainerFactory } from './embeddable/dashboard_container_factory';

export { LazyDashboardRenderer } from './external_api/lazy_dashboard_renderer';
export type { DashboardLocatorParams } from './types';
Expand Down
9 changes: 0 additions & 9 deletions src/plugins/dashboard/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ import {
LEGACY_DASHBOARD_APP_ID,
SEARCH_SESSION_ID,
} from './dashboard_constants';
import { DashboardContainerFactoryDefinition } from './dashboard_container/embeddable/dashboard_container_factory';
import {
GetPanelPlacementSettings,
registerDashboardPanelPlacementSetting,
Expand Down Expand Up @@ -227,14 +226,6 @@ export class DashboardPlugin
},
});

core.getStartServices().then(([, deps]) => {
const dashboardContainerFactory = new DashboardContainerFactoryDefinition(deps.embeddable);
embeddable.registerEmbeddableFactory(
dashboardContainerFactory.type,
dashboardContainerFactory
);
});

this.stopUrlTracking = () => {
stopUrlTracker();
};
Expand Down

0 comments on commit 3b6cfb6

Please sign in to comment.