Skip to content

Commit

Permalink
[embeddable] remove embeddable factory methods from setup and start A…
Browse files Browse the repository at this point in the history
…PI (#204797)

Part of embeddable rebuild cleanup. PR removes legacy embeddable factory
registration APIs.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 19, 2024
1 parent b809806 commit 3b61e7b
Show file tree
Hide file tree
Showing 38 changed files with 172 additions and 1,082 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import { useCallback, useMemo, useRef } from 'react';
import { AsyncSubject, defer, from, lastValueFrom, map, type Subscription } from 'rxjs';

import type { IconType } from '@elastic/eui';
import { COMMON_EMBEDDABLE_GROUPING, EmbeddableFactory } from '@kbn/embeddable-plugin/public';
import { COMMON_EMBEDDABLE_GROUPING } from '@kbn/embeddable-plugin/public';
import { PresentationContainer } from '@kbn/presentation-containers';
import { ADD_PANEL_TRIGGER } from '@kbn/ui-actions-plugin/public';
import { VisGroups, type BaseVisType, type VisTypeAlias } from '@kbn/visualizations-plugin/public';
Expand All @@ -28,14 +27,6 @@ interface UseGetDashboardPanelsArgs {
createNewVisType: (visType: BaseVisType | VisTypeAlias) => () => void;
}

export interface FactoryGroup {
id: string;
appName: string;
icon?: IconType;
factories: EmbeddableFactory[];
order: number;
}

const sortGroupPanelsByOrder = <T extends { order: number }>(panelGroups: T[]): T[] => {
return panelGroups.sort(
// larger number sorted to the top
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ import { buildMockDashboardApi } from '../../mocks';
import { EditorMenu } from './editor_menu';

import { DashboardContext } from '../../dashboard_api/use_dashboard_api';
import {
embeddableService,
uiActionsService,
visualizationsService,
} from '../../services/kibana_services';
import { uiActionsService, visualizationsService } from '../../services/kibana_services';

jest.spyOn(embeddableService, 'getEmbeddableFactories').mockReturnValue(new Map().values());
jest.spyOn(uiActionsService, 'getTriggerCompatibleActions').mockResolvedValue([]);
jest.spyOn(visualizationsService, 'getByGroup').mockReturnValue([]);
jest.spyOn(visualizationsService, 'getAliases').mockReturnValue([]);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/dashboard/public/services/kibana_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { ContentManagementPublicStart } from '@kbn/content-management-plugi
import type { CoreStart } from '@kbn/core/public';
import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
import type { DataViewEditorStart } from '@kbn/data-view-editor-plugin/public';
import type { EmbeddableStart } from '@kbn/embeddable-plugin/public/plugin';
import type { EmbeddableStart } from '@kbn/embeddable-plugin/public';
import type { FieldFormatsStart } from '@kbn/field-formats-plugin/public/plugin';
import type { NavigationPublicPluginStart } from '@kbn/navigation-plugin/public';
import type { NoDataPagePluginStart } from '@kbn/no-data-page-plugin/public';
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/embeddable/common/lib/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { extractBaseEmbeddableInput } from './migrate_base_input';
export const getExtractFunction = (embeddables: CommonEmbeddableStartContract) => {
return (state: EmbeddableStateWithType) => {
const enhancements = state.enhancements || {};
const factory = embeddables.getEmbeddableFactory(state.type);
const factory = embeddables.getEmbeddableFactory?.(state.type);

const baseResponse = extractBaseEmbeddableInput(state);
let updatedInput = baseResponse.state;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/embeddable/common/lib/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { injectBaseEmbeddableInput } from './migrate_base_input';
export const getInjectFunction = (embeddables: CommonEmbeddableStartContract) => {
return (state: EmbeddableStateWithType, references: SavedObjectReference[]) => {
const enhancements = state.enhancements || {};
const factory = embeddables.getEmbeddableFactory(state.type);
const factory = embeddables.getEmbeddableFactory?.(state.type);

let updatedInput = injectBaseEmbeddableInput(state, references);

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/embeddable/common/lib/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type MigrateFunction = (state: SerializableRecord, version: string) => Se
export const getMigrateFunction = (embeddables: CommonEmbeddableStartContract) => {
const migrateFn: MigrateFunction = (state: SerializableRecord, version: string) => {
const enhancements = (state.enhancements as SerializableRecord) || {};
const factory = embeddables.getEmbeddableFactory(state.type as string);
const factory = embeddables.getEmbeddableFactory?.(state.type as string);

let updatedInput = baseEmbeddableMigrations[version]
? baseEmbeddableMigrations[version](state)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/embeddable/common/lib/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const getTelemetryFunction = (embeddables: CommonEmbeddableStartContract)
telemetryData: Record<string, string | number | boolean> = {}
) => {
const enhancements = state.enhancements || {};
const factory = embeddables.getEmbeddableFactory(state.type);
const factory = embeddables.getEmbeddableFactory?.(state.type);

let outputTelemetryData = telemetryBaseEmbeddableInput(state, telemetryData);
if (factory) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/embeddable/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export interface EmbeddableRegistryDefinition<
export type EmbeddablePersistableStateService = PersistableStateService<EmbeddableStateWithType>;

export interface CommonEmbeddableStartContract {
getEmbeddableFactory: (
getEmbeddableFactory?: (
embeddableFactoryId: string
) => PersistableState & { isContainerType: boolean };
getEnhancement: (enhancementId: string) => PersistableState;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
contentManagement,
usageCollection,
} from '../kibana_services';
import { EmbeddableFactoryNotFoundError } from '../lib';
import { getAddFromLibraryType, useAddFromLibraryTypes } from './registry';

const runAddTelemetry = (
Expand Down Expand Up @@ -61,7 +60,12 @@ export const AddFromLibraryFlyout = ({
) => {
const libraryType = getAddFromLibraryType(type);
if (!libraryType) {
core.notifications.toasts.addWarning(new EmbeddableFactoryNotFoundError(type).message);
core.notifications.toasts.addWarning(
i18n.translate('embeddableApi.addPanel.typeNotFound', {
defaultMessage: 'Unable to load type: {type}',
values: { type },
})
);
return;
}

Expand Down
51 changes: 51 additions & 0 deletions src/plugins/embeddable/public/enhancements/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { identity } from 'lodash';
import { SerializableRecord } from '@kbn/utility-types';
import { EnhancementRegistryDefinition, EnhancementRegistryItem } from './types';

export class EnhancementsRegistry {
private registry: Map<string, EnhancementRegistryItem> = new Map();

public registerEnhancement = (enhancement: EnhancementRegistryDefinition) => {
if (this.registry.has(enhancement.id)) {
throw new Error(`enhancement with id ${enhancement.id} already exists in the registry`);
}
this.registry.set(enhancement.id, {
id: enhancement.id,
telemetry: enhancement.telemetry || ((state, stats) => stats),
inject: enhancement.inject || identity,
extract:
enhancement.extract ||
((state: SerializableRecord) => {
return { state, references: [] };
}),
migrations: enhancement.migrations || {},
});
};

public getEnhancements = (): EnhancementRegistryItem[] => {
return Array.from(this.registry.values());
};

public getEnhancement = (id: string): EnhancementRegistryItem => {
return (
this.registry.get(id) || {
id: 'unknown',
telemetry: (state, stats) => stats,
inject: identity,
extract: (state: SerializableRecord) => {
return { state, references: [] };
},
migrations: {},
}
);
};
}
21 changes: 21 additions & 0 deletions src/plugins/embeddable/public/enhancements/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { SerializableRecord } from '@kbn/utility-types';
import { PersistableState, PersistableStateDefinition } from '@kbn/kibana-utils-plugin/common';

export interface EnhancementRegistryDefinition<P extends SerializableRecord = SerializableRecord>
extends PersistableStateDefinition<P> {
id: string;
}

export interface EnhancementRegistryItem<P extends SerializableRecord = SerializableRecord>
extends PersistableState<P> {
id: string;
}
17 changes: 2 additions & 15 deletions src/plugins/embeddable/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ export {
CELL_VALUE_TRIGGER,
contextMenuTrigger,
CONTEXT_MENU_TRIGGER,
defaultEmbeddableFactoryProvider,
Embeddable,
EmbeddableFactoryNotFoundError,
EmbeddableStateTransfer,
ErrorEmbeddable,
isContextMenuTriggerContext,
isExplicitInputWithAttributes,
isMultiValueClickTriggerContext,
isRangeSelectTriggerContext,
isRowClickTriggerContext,
Expand All @@ -37,7 +34,6 @@ export {
PANEL_BADGE_TRIGGER,
PANEL_HOVER_TRIGGER,
PANEL_NOTIFICATION_TRIGGER,
runEmbeddableFactoryMigrations,
SELECT_RANGE_TRIGGER,
VALUE_CLICK_TRIGGER,
ViewMode,
Expand All @@ -47,26 +43,17 @@ export type {
ChartActionContext,
EmbeddableContext,
EmbeddableEditorState,
EmbeddableFactory,
EmbeddableFactoryDefinition,
EmbeddableInput,
EmbeddableInstanceConfiguration,
EmbeddableOutput,
EmbeddablePackageState,
IEmbeddable,
MultiValueClickContext,
OutputSpec,
PropertySpec,
RangeSelectContext,
ValueClickContext,
} from './lib';
export type {
EmbeddableSetup,
EmbeddableSetupDependencies,
EmbeddableStart,
EmbeddableStartDependencies,
} from './plugin';
export type { EnhancementRegistryDefinition } from './types';
export type { EmbeddableSetup, EmbeddableStart } from './types';
export type { EnhancementRegistryDefinition } from './enhancements/types';

export {
ReactEmbeddableRenderer,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/embeddable/public/kibana_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { BehaviorSubject } from 'rxjs';

import { CoreStart } from '@kbn/core/public';

import { EmbeddableStart, EmbeddableStartDependencies } from '.';
import { EmbeddableStart, EmbeddableStartDependencies } from './types';

export let core: CoreStart;
export let embeddableStart: EmbeddableStart;
Expand Down

This file was deleted.

Loading

0 comments on commit 3b61e7b

Please sign in to comment.