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

[poc][wip] async registries poc #65991

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 0 additions & 28 deletions examples/ui_action_examples/public/hello_world_action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,5 @@
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import { EuiText, EuiModalBody, EuiButton } from '@elastic/eui';
import { OverlayStart } from '../../../src/core/public';
import { createAction } from '../../../src/plugins/ui_actions/public';
import { toMountPoint } from '../../../src/plugins/kibana_react/public';

export const ACTION_HELLO_WORLD = 'ACTION_HELLO_WORLD';

interface StartServices {
openModal: OverlayStart['openModal'];
}

export const createHelloWorldAction = (getStartServices: () => Promise<StartServices>) =>
createAction({
type: ACTION_HELLO_WORLD,
getDisplayName: () => 'Hello World!',
execute: async () => {
const { openModal } = await getStartServices();
const overlay = openModal(
toMountPoint(
<EuiModalBody>
<EuiText data-test-subj="helloWorldActionText">Hello world!</EuiText>
<EuiButton data-test-subj="closeModal" onClick={() => overlay.close()}>
Close
</EuiButton>
</EuiModalBody>
)
);
},
});
47 changes: 47 additions & 0 deletions examples/ui_action_examples/public/hello_world_action_lazy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 React from 'react';
import { EuiText, EuiModalBody, EuiButton } from '@elastic/eui';
import { OverlayStart } from '../../../src/core/public';
import { createAction } from '../../../src/plugins/ui_actions/public';
import { toMountPoint } from '../../../src/plugins/kibana_react/public';
import { ACTION_HELLO_WORLD } from './hello_world_action';

interface StartServices {
openModal: OverlayStart['openModal'];
}

export const createHelloWorldAction = (getStartServices: () => Promise<StartServices>) =>
createAction({
type: ACTION_HELLO_WORLD,
getDisplayName: () => 'Hello World!',
execute: async () => {
const { openModal } = await getStartServices();
const overlay = openModal(
toMountPoint(
<EuiModalBody>
<EuiText data-test-subj="helloWorldActionText">Hello world!</EuiText>
<EuiButton data-test-subj="closeModal" onClick={() => overlay.close()}>
Close
</EuiButton>
</EuiModalBody>
)
);
},
});
14 changes: 7 additions & 7 deletions examples/ui_action_examples/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { Plugin, CoreSetup, CoreStart } from '../../../src/core/public';
import { UiActionsSetup, UiActionsStart } from '../../../src/plugins/ui_actions/public';
import { createHelloWorldAction, ACTION_HELLO_WORLD } from './hello_world_action';
import { ACTION_HELLO_WORLD } from './hello_world_action';
import { helloWorldTrigger, HELLO_WORLD_TRIGGER_ID } from './hello_world_trigger';

export interface UiActionExamplesSetupDependencies {
Expand Down Expand Up @@ -49,12 +49,12 @@ export class UiActionExamplesPlugin
) {
uiActions.registerTrigger(helloWorldTrigger);

const helloWorldAction = createHelloWorldAction(async () => ({
openModal: (await core.getStartServices())[0].overlays.openModal,
}));

uiActions.registerAction(helloWorldAction);
uiActions.addTriggerAction(helloWorldTrigger.id, helloWorldAction);
uiActions.registerAction('ACTION_HELLO_WORLD', async () =>
(await import('./hello_world_action_lazy')).createHelloWorldAction(async () => ({
openModal: (await core.getStartServices())[0].overlays.openModal,
}))
);
uiActions.addTriggerAction(helloWorldTrigger.id, 'ACTION_HELLO_WORLD');
}

public start(core: CoreStart, plugins: UiActionExamplesStartDependencies) {}
Expand Down
6 changes: 5 additions & 1 deletion examples/ui_actions_explorer/public/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ const ActionsExplorer = ({ uiActionsApi, openModal }: Props) => {
);
},
});
uiActionsApi.addTriggerAction(HELLO_WORLD_TRIGGER_ID, dynamicAction);

uiActionsApi.registerAction(dynamicAction.id, () =>
Promise.resolve(dynamicAction)
);
uiActionsApi.addTriggerAction(HELLO_WORLD_TRIGGER_ID, dynamicAction.id);
setConfirmationText(
`You've successfully added a new action: ${dynamicAction.getDisplayName(
{}
Expand Down
44 changes: 25 additions & 19 deletions src/plugins/ui_actions/public/service/ui_actions_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ export class UiActionsService {
};

public readonly registerAction = <A extends ActionDefinition>(
definition: A
): Action<ActionContext<A>> => {
if (this.actions.has(definition.id)) {
throw new Error(`Action [action.id = ${definition.id}] already registered.`);
actionId: string,
definition: () => Promise<A>
): (() => Promise<Action<ActionContext<A>>>) => {
if (this.actions.has(actionId)) {
throw new Error(`Action [action.id = ${actionId}] already registered.`);
}

const action = new ActionInternal(definition);

this.actions.set(action.id, action);
const action = async () => new ActionInternal(await definition());
this.actions.set(actionId, action);

return action;
};
Expand Down Expand Up @@ -140,33 +140,39 @@ export class UiActionsService {
triggerId: T,
// The action can accept partial or no context, but if it needs context not provided
// by this type of trigger, typescript will complain. yay!
action: Action<TriggerContextMapping[T]>
actionId: string
): void => {
if (!this.actions.has(action.id)) this.registerAction(action);
this.attachAction(triggerId, action.id);
// if (!this.actions.has(action.id)) this.registerAction(action.id, action.definition);
this.attachAction(triggerId, actionId);
};

public readonly getAction = <T extends ActionDefinition>(
public readonly getAction = async <T extends ActionDefinition>(
id: string
): Action<ActionContext<T>> => {
): Promise<Action<ActionContext<T>>> => {
if (!this.actions.has(id)) {
throw new Error(`Action [action.id = ${id}] not registered.`);
}

return this.actions.get(id) as ActionInternal<T>;
const actionGetter = this.actions.get(id);
const action = await actionGetter!();

return action as ActionInternal<T>;
};

public readonly getTriggerActions = <T extends TriggerId>(
public readonly getTriggerActions = async <T extends TriggerId>(
triggerId: T
): Array<Action<TriggerContextMapping[T]>> => {
): Promise<Array<Action<TriggerContextMapping[T]>>> => {
// This line checks if trigger exists, otherwise throws.
this.getTrigger!(triggerId);

const actionIds = this.triggerToActions.get(triggerId);

const actions = actionIds!
.map(actionId => this.actions.get(actionId) as ActionInternal)
.filter(Boolean);
const actions = await Promise.all(
actionIds!
.map(actionId => this.actions.get(actionId))
.filter(Boolean)
.map(actionGetter => actionGetter!())
);

return actions as Array<Action<TriggerContext<T>>>;
};
Expand All @@ -175,7 +181,7 @@ export class UiActionsService {
triggerId: T,
context: TriggerContextMapping[T]
): Promise<Array<Action<TriggerContextMapping[T]>>> => {
const actions = this.getTriggerActions!(triggerId);
const actions = await this.getTriggerActions!(triggerId);
const isCompatibles = await Promise.all(actions.map(action => action.isCompatible(context)));
return actions.reduce(
(acc: Array<Action<TriggerContextMapping[T]>>, action, i) =>
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/ui_actions/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { IEmbeddable } from '../../embeddable/public';
import { RangeSelectTriggerContext, ValueClickTriggerContext } from '../../embeddable/public';

export type TriggerRegistry = Map<TriggerId, TriggerInternal<any>>;
export type ActionRegistry = Map<string, ActionInternal>;
export type ActionRegistry = Map<string, () => Promise<ActionInternal>>;
export type TriggerToActionsRegistry = Map<TriggerId, string[]>;

const DEFAULT_TRIGGER = '';
Expand Down