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

[7.x] Clean up UI Actions Enhanced server plugin (#83442) #83515

Merged
merged 1 commit into from
Nov 17, 2020
Merged
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 @@ -6,32 +6,29 @@

import { EnhancementRegistryDefinition } from '../../../../src/plugins/embeddable/server';
import { SavedObjectReference } from '../../../../src/core/types';
import { DynamicActionsState, SerializedEvent } from './types';
import { AdvancedUiActionsServerPlugin } from './plugin';
import { ActionFactory, DynamicActionsState, SerializedEvent } from './types';
import { SerializableState } from '../../../../src/plugins/kibana_utils/common';

export const dynamicActionEnhancement = (
uiActionsEnhanced: AdvancedUiActionsServerPlugin
getActionFactory: (id: string) => undefined | ActionFactory
): EnhancementRegistryDefinition => {
return {
id: 'dynamicActions',
telemetry: (state: SerializableState, telemetry: Record<string, any>) => {
let telemetryData = telemetry;
(state as DynamicActionsState).events.forEach((event: SerializedEvent) => {
if (uiActionsEnhanced.getActionFactory(event.action.factoryId)) {
telemetryData = uiActionsEnhanced
.getActionFactory(event.action.factoryId)!
.telemetry(event, telemetryData);
}
const factory = getActionFactory(event.action.factoryId);
if (factory) telemetryData = factory.telemetry(event, telemetryData);
});
return telemetryData;
},
extract: (state: SerializableState) => {
const references: SavedObjectReference[] = [];
const newState: DynamicActionsState = {
events: (state as DynamicActionsState).events.map((event: SerializedEvent) => {
const result = uiActionsEnhanced.getActionFactory(event.action.factoryId)
? uiActionsEnhanced.getActionFactory(event.action.factoryId)!.extract(event)
const factory = getActionFactory(event.action.factoryId);
const result = factory
? factory.extract(event)
: {
state: event,
references: [],
Expand All @@ -45,9 +42,8 @@ export const dynamicActionEnhancement = (
inject: (state: SerializableState, references: SavedObjectReference[]) => {
return {
events: (state as DynamicActionsState).events.map((event: SerializedEvent) => {
return uiActionsEnhanced.getActionFactory(event.action.factoryId)
? uiActionsEnhanced.getActionFactory(event.action.factoryId)!.inject(event, references)
: event;
const factory = getActionFactory(event.action.factoryId);
return factory ? factory.inject(event, references) : event;
}),
} as DynamicActionsState;
},
Expand Down
54 changes: 5 additions & 49 deletions x-pack/plugins/ui_actions_enhanced/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
*/

import { identity } from 'lodash';
import { CoreSetup, Plugin, SavedObjectReference } from '../../../../src/core/server';
import { CoreSetup, Plugin } from '../../../../src/core/server';
import { EmbeddableSetup } from '../../../../src/plugins/embeddable/server';
import { dynamicActionEnhancement } from './dynamic_action_enhancement';
import {
ActionFactoryRegistry,
SerializedEvent,
ActionFactoryDefinition,
DynamicActionsState,
} from './types';
import { ActionFactoryRegistry, SerializedEvent, ActionFactoryDefinition } from './types';

export interface SetupContract {
registerActionFactory: (definition: ActionFactoryDefinition) => void;
Expand All @@ -32,7 +27,9 @@ export class AdvancedUiActionsServerPlugin
constructor() {}

public setup(core: CoreSetup, { embeddable }: SetupDependencies) {
embeddable.registerEnhancement(dynamicActionEnhancement(this));
const getActionFactory = (actionFactoryId: string) => this.actionFactories.get(actionFactoryId);

embeddable.registerEnhancement(dynamicActionEnhancement(getActionFactory));

return {
registerActionFactory: this.registerActionFactory,
Expand Down Expand Up @@ -64,45 +61,4 @@ export class AdvancedUiActionsServerPlugin
migrations: definition.migrations || {},
});
};

public readonly getActionFactory = (actionFactoryId: string) => {
const actionFactory = this.actionFactories.get(actionFactoryId);
return actionFactory;
};

public readonly telemetry = (state: DynamicActionsState, telemetry: Record<string, any> = {}) => {
state.events.forEach((event: SerializedEvent) => {
if (this.actionFactories.has(event.action.factoryId)) {
this.actionFactories.get(event.action.factoryId)!.telemetry(event, telemetry);
}
});
return telemetry;
};

public readonly extract = (state: DynamicActionsState) => {
const references: SavedObjectReference[] = [];
const newState = {
events: state.events.map((event: SerializedEvent) => {
const result = this.actionFactories.has(event.action.factoryId)
? this.actionFactories.get(event.action.factoryId)!.extract(event)
: {
state: event,
references: [],
};
result.references.forEach((r) => references.push(r));
return result.state;
}),
};
return { state: newState, references };
};

public readonly inject = (state: DynamicActionsState, references: SavedObjectReference[]) => {
return {
events: state.events.map((event: SerializedEvent) => {
return this.actionFactories.has(event.action.factoryId)
? this.actionFactories.get(event.action.factoryId)!.inject(event, references)
: event;
}),
};
};
}