-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Dynamic actions #58216
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
Dynamic actions #58216
Changes from 49 commits
2dada48
0cae149
171a481
6cfc9e7
8e6c65a
d14d876
ac04c80
179ff8a
0796903
cfce078
bd0eaf3
3700583
6e2e30e
4403691
3796961
9ca0267
6e3d10f
dad08ba
12f09dc
60d11b8
70061bb
34c0843
7cbd860
93e17cb
8209a59
8982ca0
358d72c
59ef699
399fa80
95d1424
3c55bd4
199d8d3
cf3669a
b8bacce
e63e92d
22f3188
079a7c8
0279111
a05490d
bed623c
3e81792
7732c84
55b8721
a25cf30
a045b9c
d0f67ec
7fbd1fb
5883734
d056277
2700d00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,10 +19,13 @@ | |
|
|
||
| import { UiComponent } from 'src/plugins/kibana_utils/common'; | ||
| import { ActionType, ActionContextMapping } from '../types'; | ||
| import { Presentable } from '../util/presentable'; | ||
| import { Configurable } from '../util/configurable'; | ||
|
|
||
| export type ActionByType<T extends ActionType> = Action<ActionContextMapping[T], T>; | ||
|
|
||
| export interface Action<Context = {}, T = ActionType> { | ||
| export interface Action<Context extends {} = {}, T = ActionType> | ||
| extends Partial<Presentable<Context>> { | ||
| /** | ||
| * Determined the order when there is more than one action matched to a trigger. | ||
| * Higher numbers are displayed first. | ||
|
|
@@ -72,3 +75,48 @@ export interface Action<Context = {}, T = ActionType> { | |
| */ | ||
| execute(context: Context): Promise<void>; | ||
| } | ||
|
|
||
| /** | ||
| * A convenience interface used to register an action. | ||
| */ | ||
| export interface ActionDefinition< | ||
streamich marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that this has been merged to master after my typescript PR, this is a duplicate type, there is a file |
||
| Context extends object = object, | ||
| Config extends object | undefined = undefined | ||
| > extends Partial<Presentable<Context>>, Partial<Configurable<Config, Context>> { | ||
| /** | ||
| * ID of the action that uniquely identifies this action in the actions registry. | ||
| */ | ||
| readonly id: string; | ||
|
|
||
| /** | ||
| * ID of the factory for this action. Used to construct dynamic actions. | ||
| */ | ||
| readonly type?: ActionType; | ||
|
|
||
| getHref?(context: Context): string | undefined; | ||
|
|
||
| /** | ||
| * Executes the action. | ||
| */ | ||
| execute(context: Context): Promise<void>; | ||
| } | ||
|
|
||
| export type AnyActionDefinition = ActionDefinition<any, any>; | ||
| export type ActionContext<A> = A extends ActionDefinition<infer Context, any> ? Context : never; | ||
| export type ActionConfig<A> = A extends ActionDefinition<any, infer Config> ? Config : never; | ||
|
|
||
| /** | ||
| * A convenience interface used to register a dynamic action. | ||
| * | ||
| * A dynamic action is one that can be create by user and registered into the | ||
| * actions registry at runtime. User can also provide custom config for this | ||
| * action. And dynamic actions can be serialized for storage and deserialized | ||
| * back. | ||
| */ | ||
| export type DynamicActionDefinition< | ||
| Context extends object = object, | ||
| Config extends object | undefined = undefined | ||
| > = ActionDefinition<Context, Config> & | ||
| Required<Pick<ActionDefinition<Context, Config>, 'CollectConfig' | 'defaultConfig' | 'type'>>; | ||
|
|
||
| export type AnyDynamicActionDefinition = DynamicActionDefinition<any, any>; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { ActionInternal } from './action_internal'; | ||
| import { AnyActionDefinition } from './action'; | ||
|
|
||
| /** | ||
| * Action representation that is exposed out to other plugins. | ||
| */ | ||
| export type ActionContract<A extends AnyActionDefinition> = Pick< | ||
| ActionInternal<A>, | ||
| | 'id' | ||
| | 'type' | ||
| | 'order' | ||
| | 'getIconType' | ||
| | 'getDisplayName' | ||
| | 'isCompatible' | ||
| | 'getHref' | ||
| | 'execute' | ||
| >; | ||
|
|
||
| export type AnyActionContract = ActionContract<any>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { ActionDefinition } from './action'; | ||
| import { ActionInternal } from './action_internal'; | ||
| import { ActionType } from '../types'; | ||
|
|
||
| const defaultActionDef: ActionDefinition = { | ||
| id: 'test-action', | ||
| execute: jest.fn(), | ||
| }; | ||
|
|
||
| describe('ActionInternal', () => { | ||
| test('can instantiate from action definition', () => { | ||
| const action = new ActionInternal(defaultActionDef); | ||
| expect(action.id).toBe('test-action'); | ||
| }); | ||
|
|
||
| describe('serialize()', () => { | ||
| test('can serialize very simple action', () => { | ||
| const action = new ActionInternal(defaultActionDef); | ||
| const serialized = action.serialize(); | ||
|
|
||
| expect(serialized).toMatchObject({ | ||
| id: 'test-action', | ||
| state: expect.any(Object), | ||
| }); | ||
| }); | ||
|
|
||
| test('can serialize action with modified state', () => { | ||
| const action = new ActionInternal({ | ||
| ...defaultActionDef, | ||
| type: 'ACTION_TYPE' as ActionType, | ||
| order: 11, | ||
| }); | ||
| action.state.transitions.setConfig({ foo: 'bar' }); | ||
| action.state.transitions.setName('qux'); | ||
|
|
||
| const serialized = action.serialize(); | ||
|
|
||
| expect(serialized).toMatchObject({ | ||
| id: 'test-action', | ||
| type: 'ACTION_TYPE', | ||
| state: { | ||
| name: 'qux', | ||
| config: { | ||
| foo: 'bar', | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('deserialize', () => { | ||
| const serialized = { | ||
| id: 'id', | ||
| type: 'type', | ||
| state: { | ||
| name: 'name', | ||
| order: 0, | ||
| config: { | ||
| foo: 'foo', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| test('can deserialize action state', () => { | ||
| const action = new ActionInternal({ | ||
| ...defaultActionDef, | ||
| }); | ||
|
|
||
| action.deserialize(serialized); | ||
|
|
||
| expect(action.state.get()).toMatchObject(serialized.state); | ||
| }); | ||
|
|
||
| test('does not overwrite action id and type', () => { | ||
| const action = new ActionInternal({ | ||
| ...defaultActionDef, | ||
| }); | ||
|
|
||
| action.deserialize(serialized); | ||
|
|
||
| expect(action.id).toBe('test-action'); | ||
| expect(action.type).toBe(''); | ||
| }); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.