Skip to content

Commit

Permalink
Clean up UI Actions Enhanced server plugin (#83442) (#83515)
Browse files Browse the repository at this point in the history
* chore: 🤖 remove unused code

* refactor: 💡 don't pass plugin definition to a function

* perf: ⚡️ fetch factory from registry only once
  • Loading branch information
streamich authored Nov 17, 2020
1 parent 47c7ecc commit 3a29b3b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 62 deletions.
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;
}),
};
};
}

0 comments on commit 3a29b3b

Please sign in to comment.