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

refactor: 💡 move action factories to x-pack #63190

Merged
merged 3 commits into from
Apr 10, 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
7 changes: 6 additions & 1 deletion src/plugins/embeddable/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ export function plugin(initializerContext: PluginInitializerContext) {
return new EmbeddablePublicPlugin(initializerContext);
}

export { EmbeddableSetup, EmbeddableStart } from './plugin';
export {
EmbeddableSetup,
EmbeddableStart,
EmbeddableSetupDependencies,
EmbeddableStartDependencies,
} from './plugin';
15 changes: 10 additions & 5 deletions src/plugins/embeddable/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
* specific language governing permissions and limitations
* under the License.
*/
import { EmbeddableStart, EmbeddableSetup } from '.';
import {
EmbeddableStart,
EmbeddableSetup,
EmbeddableSetupDependencies,
EmbeddableStartDependencies,
} from '.';
import { EmbeddablePublicPlugin } from './plugin';
import { coreMock } from '../../../core/public/mocks';

Expand Down Expand Up @@ -45,14 +50,14 @@ const createStartContract = (): Start => {
return startContract;
};

const createInstance = () => {
const createInstance = (setupPlugins: Partial<EmbeddableSetupDependencies> = {}) => {
const plugin = new EmbeddablePublicPlugin({} as any);
const setup = plugin.setup(coreMock.createSetup(), {
uiActions: uiActionsPluginMock.createSetupContract(),
uiActions: setupPlugins.uiActions || uiActionsPluginMock.createSetupContract(),
});
const doStart = () =>
const doStart = (startPlugins: Partial<EmbeddableStartDependencies> = {}) =>
plugin.start(coreMock.createStart(), {
uiActions: uiActionsPluginMock.createStartContract(),
uiActions: startPlugins.uiActions || uiActionsPluginMock.createStartContract(),
inspector: inspectorPluginMock.createStartContract(),
});
return {
Expand Down
46 changes: 0 additions & 46 deletions src/plugins/ui_actions/public/actions/action_factory_definition.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/plugins/ui_actions/public/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@

export * from './action';
export * from './action_internal';
export * from './action_factory_definition';
export * from './action_factory';
export * from './create_action';
export * from './incompatible_action_error';
export * from './types';
30 changes: 0 additions & 30 deletions src/plugins/ui_actions/public/actions/types.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/plugins/ui_actions/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ export { UiActionsServiceParams, UiActionsService } from './service';
export {
Action,
ActionDefinition as UiActionsActionDefinition,
ActionFactoryDefinition as UiActionsActionFactoryDefinition,
ActionInternal as UiActionsActionInternal,
createAction,
IncompatibleActionError,
SerializedAction as UiActionsSerializedAction,
SerializedEvent as UiActionsSerializedEvent,
} from './actions';
export { buildContextMenuForActions } from './context_menu';
export {
Expand Down
4 changes: 0 additions & 4 deletions src/plugins/ui_actions/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const createSetupContract = (): Setup => {
attachAction: jest.fn(),
detachAction: jest.fn(),
registerAction: jest.fn(),
registerActionFactory: jest.fn(),
registerTrigger: jest.fn(),
unregisterAction: jest.fn(),
};
Expand All @@ -49,13 +48,10 @@ const createStartContract = (): Start => {
executeTriggerActions: jest.fn(),
fork: jest.fn(),
getAction: jest.fn(),
getActionFactories: jest.fn(),
getActionFactory: jest.fn(),
getTrigger: jest.fn(),
getTriggerActions: jest.fn((id: TriggerId) => []),
getTriggerCompatibleActions: jest.fn(),
registerAction: jest.fn(),
registerActionFactory: jest.fn(),
registerTrigger: jest.fn(),
};

Expand Down
1 change: 0 additions & 1 deletion src/plugins/ui_actions/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export type UiActionsSetup = Pick<
| 'attachAction'
| 'detachAction'
| 'registerAction'
| 'registerActionFactory'
| 'registerTrigger'
| 'unregisterAction'
>;
Expand Down
68 changes: 1 addition & 67 deletions src/plugins/ui_actions/public/service/ui_actions_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
*/

import { UiActionsService } from './ui_actions_service';
import {
Action,
ActionInternal,
createAction,
ActionFactoryDefinition,
ActionFactory,
} from '../actions';
import { Action, ActionInternal, createAction } from '../actions';
import { createHelloWorldAction } from '../tests/test_samples';
import { ActionRegistry, TriggerRegistry, TriggerId, ActionType } from '../types';
import { Trigger } from '../triggers';
Expand Down Expand Up @@ -497,64 +491,4 @@ describe('UiActionsService', () => {
);
});
});

describe('action factories', () => {
const factoryDefinition1: ActionFactoryDefinition = {
id: 'test-factory-1',
CollectConfig: {} as any,
createConfig: () => ({}),
isConfigValid: () => true,
create: () => ({} as any),
};
const factoryDefinition2: ActionFactoryDefinition = {
id: 'test-factory-2',
CollectConfig: {} as any,
createConfig: () => ({}),
isConfigValid: () => true,
create: () => ({} as any),
};

test('.getActionFactories() returns empty array if no action factories registered', () => {
const service = new UiActionsService();

const factories = service.getActionFactories();

expect(factories).toEqual([]);
});

test('can register and retrieve an action factory', () => {
const service = new UiActionsService();

service.registerActionFactory(factoryDefinition1);

const factory = service.getActionFactory(factoryDefinition1.id);

expect(factory).toBeInstanceOf(ActionFactory);
expect(factory.id).toBe(factoryDefinition1.id);
});

test('can retrieve all action factories', () => {
const service = new UiActionsService();

service.registerActionFactory(factoryDefinition1);
service.registerActionFactory(factoryDefinition2);

const factories = service.getActionFactories();
const factoriesSorted = [...factories].sort((f1, f2) => (f1.id > f2.id ? 1 : -1));

expect(factoriesSorted.length).toBe(2);
expect(factoriesSorted[0].id).toBe(factoryDefinition1.id);
expect(factoriesSorted[1].id).toBe(factoryDefinition2.id);
});

test('throws when retrieving action factory that does not exist', () => {
const service = new UiActionsService();

service.registerActionFactory(factoryDefinition1);

expect(() => service.getActionFactory('UNKNOWN_ID')).toThrowError(
'Action factory [actionFactoryId = UNKNOWN_ID] does not exist.'
);
});
});
});
53 changes: 1 addition & 52 deletions src/plugins/ui_actions/public/service/ui_actions_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,8 @@ import {
TriggerId,
TriggerContextMapping,
ActionType,
ActionFactoryRegistry,
} from '../types';
import {
ActionInternal,
Action,
ActionByType,
ActionFactory,
ActionDefinition,
ActionFactoryDefinition,
ActionContext,
} from '../actions';
import { ActionInternal, Action, ActionByType, ActionDefinition, ActionContext } from '../actions';
import { Trigger, TriggerContext } from '../triggers/trigger';
import { TriggerInternal } from '../triggers/trigger_internal';
import { TriggerContract } from '../triggers/trigger_contract';
Expand All @@ -47,25 +38,21 @@ export interface UiActionsServiceParams {
* A 1-to-N mapping from `Trigger` to zero or more `Action`.
*/
readonly triggerToActions?: TriggerToActionsRegistry;
readonly actionFactories?: ActionFactoryRegistry;
}

export class UiActionsService {
protected readonly triggers: TriggerRegistry;
protected readonly actions: ActionRegistry;
protected readonly triggerToActions: TriggerToActionsRegistry;
protected readonly actionFactories: ActionFactoryRegistry;

constructor({
triggers = new Map(),
actions = new Map(),
triggerToActions = new Map(),
actionFactories = new Map(),
}: UiActionsServiceParams = {}) {
this.triggers = triggers;
this.actions = actions;
this.triggerToActions = triggerToActions;
this.actionFactories = actionFactories;
}

public readonly registerTrigger = (trigger: Trigger) => {
Expand Down Expand Up @@ -215,7 +202,6 @@ export class UiActionsService {
this.actions.clear();
this.triggers.clear();
this.triggerToActions.clear();
this.actionFactories.clear();
};

/**
Expand All @@ -235,41 +221,4 @@ export class UiActionsService {

return new UiActionsService({ triggers, actions, triggerToActions });
};

/**
* Register an action factory. Action factories are used to configure and
* serialize/deserialize dynamic actions.
*/
public readonly registerActionFactory = <
Config extends object = object,
FactoryContext extends object = object,
ActionContext extends object = object
>(
definition: ActionFactoryDefinition<Config, FactoryContext, ActionContext>
) => {
if (this.actionFactories.has(definition.id)) {
throw new Error(`ActionFactory [actionFactory.id = ${definition.id}] already registered.`);
}

const actionFactory = new ActionFactory<Config, FactoryContext, ActionContext>(definition);

this.actionFactories.set(actionFactory.id, actionFactory as ActionFactory<any, any, any>);
};

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

if (!actionFactory) {
throw new Error(`Action factory [actionFactoryId = ${actionFactoryId}] does not exist.`);
}

return actionFactory;
};

/**
* Returns an array of all action factories.
*/
public readonly getActionFactories = (): ActionFactory[] => {
return [...this.actionFactories.values()];
};
}
2 changes: 0 additions & 2 deletions src/plugins/ui_actions/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@

import { ActionInternal } from './actions/action_internal';
import { TriggerInternal } from './triggers/trigger_internal';
import { ActionFactory } from './actions';
import { EmbeddableVisTriggerContext, IEmbeddable } from '../../embeddable/public';
import { Filter } from '../../data/public';
import { SELECT_RANGE_TRIGGER, VALUE_CLICK_TRIGGER, APPLY_FILTER_TRIGGER } from './triggers';

export type TriggerRegistry = Map<TriggerId, TriggerInternal<any>>;
export type ActionRegistry = Map<string, ActionInternal>;
export type TriggerToActionsRegistry = Map<TriggerId, string[]>;
export type ActionFactoryRegistry = Map<string, ActionFactory>;

const DEFAULT_TRIGGER = '';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '@elastic/eui';
import { txtChangeButton } from './i18n';
import './action_wizard.scss';
import { ActionFactory } from '../../services';
import { ActionFactory } from '../../dynamic_actions';

type ActionBaseConfig = object;
type ActionFactoryBaseContext = object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React, { useState } from 'react';
import { EuiFieldText, EuiFormRow, EuiSelect, EuiSwitch } from '@elastic/eui';
import { reactToUiComponent } from '../../../../../../src/plugins/kibana_react/public';
import { ActionWizard } from './action_wizard';
import { ActionFactoryDefinition, ActionFactory } from '../../services';
import { ActionFactoryDefinition, ActionFactory } from '../../dynamic_actions';
import { CollectConfigProps } from '../../util';

type ActionBaseConfig = object;
Expand Down
Loading