Skip to content

Commit

Permalink
Merge pull request #1 from stacey-gammon/stacey-changes
Browse files Browse the repository at this point in the history
suggestions
  • Loading branch information
streamich authored Feb 20, 2020
2 parents 4f8c5a4 + 16ddf6e commit 4dc7ca9
Showing 7 changed files with 37 additions and 33 deletions.
21 changes: 15 additions & 6 deletions src/plugins/embeddable/public/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -16,8 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/

import { UiActionsSetup } from 'src/plugins/ui_actions/public';
import { Filter } from '../../data/public';
import {
applyFilterTrigger,
contextMenuTrigger,
@@ -26,15 +26,24 @@ import {
selectRangeTrigger,
valueClickTrigger,
EmbeddableVisTriggerContext,
IEmbeddable,
APPLY_FILTER_TRIGGER,
VALUE_CLICK_TRIGGER,
SELECT_RANGE_TRIGGER,
CONTEXT_MENU_TRIGGER,
PANEL_BADGE_TRIGGER,
} from './lib';

declare module '../../ui_actions/public' {
export interface TriggerContextMapping {
SELECT_RANGE_TRIGGER: EmbeddableVisTriggerContext;
VALUE_CLICK_TRIGGER: EmbeddableVisTriggerContext;
CONTEXT_MENU_TRIGGER: object;
APPLY_FILTER_TRIGGER: object;
PANEL_BADGE_TRIGGER: object;
[SELECT_RANGE_TRIGGER]: EmbeddableVisTriggerContext;
[VALUE_CLICK_TRIGGER]: EmbeddableVisTriggerContext;
[APPLY_FILTER_TRIGGER]: {
embeddable: IEmbeddable;
filters: Filter[];
};
[CONTEXT_MENU_TRIGGER]: object;
[PANEL_BADGE_TRIGGER]: object;
}
}

2 changes: 1 addition & 1 deletion src/plugins/ui_actions/public/index.ts
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ export { UiActionsSetup, UiActionsStart } from './plugin';
export { UiActionsServiceParams, UiActionsService } from './service';
export { Action, createAction, IncompatibleActionError } from './actions';
export { buildContextMenuForActions } from './context_menu';
export { Trigger, AnyTrigger, TriggerContext } from './triggers';
export { Trigger, TriggerContext } from './triggers';
export { TriggerContextMapping } from './types';

/**
23 changes: 8 additions & 15 deletions src/plugins/ui_actions/public/service/ui_actions_service.ts
Original file line number Diff line number Diff line change
@@ -17,14 +17,9 @@
* under the License.
*/

import {
TriggerRegistry,
ActionRegistry,
TriggerToActionsRegistry,
TriggerContextMapping,
} from '../types';
import { TriggerRegistry, ActionRegistry, TriggerToActionsRegistry, TriggerId } from '../types';
import { Action } from '../actions';
import { AnyTrigger, Trigger } from '../triggers/trigger';
import { Trigger, TriggerContext } from '../triggers/trigger';
import { TriggerInternal } from '../triggers/trigger_internal';
import { TriggerContract } from '../triggers/trigger_contract';

@@ -53,7 +48,7 @@ export class UiActionsService {
this.triggerToActions = triggerToActions;
}

public readonly registerTrigger = <T extends AnyTrigger>(trigger: T) => {
public readonly registerTrigger = (trigger: Trigger) => {
if (this.triggers.has(trigger.id)) {
throw new Error(`Trigger [trigger.id = ${trigger.id}] already registered.`);
}
@@ -64,9 +59,7 @@ export class UiActionsService {
this.triggerToActions.set(trigger.id, []);
};

public readonly getTrigger = <T extends keyof TriggerContextMapping>(
triggerId: T
): TriggerContract<Trigger<T extends string ? T : never>> => {
public readonly getTrigger = <T extends TriggerId>(triggerId: T): TriggerContract<T> => {
const trigger = this.triggers.get(triggerId as string);

if (!trigger) {
@@ -143,11 +136,11 @@ export class UiActionsService {
*
* Use `plugins.uiActions.getTrigger(triggerId).exec(params)` instead.
*/
public readonly executeTriggerActions = async <T extends AnyTrigger>(
triggerId: string,
context: TriggerContextMapping[T['id']]
public readonly executeTriggerActions = async <T extends TriggerId>(
triggerId: T,
context: TriggerContext<T>
) => {
const trigger = this.getTrigger<any>(triggerId);
const trigger = this.getTrigger<T>(triggerId);
await trigger.exec(context);
};

8 changes: 3 additions & 5 deletions src/plugins/ui_actions/public/triggers/trigger.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@
* under the License.
*/

import { TriggerContextMapping } from '../types';
import { TriggerContextMapping, TriggerId } from '../types';

/**
* This is a convenience interface used to register a *trigger*.
@@ -30,7 +30,7 @@ import { TriggerContextMapping } from '../types';
* trigger is *called* it first displays a context menu for user to pick a
* single action to execute.
*/
export interface Trigger<ID extends string = string> {
export interface Trigger<ID extends TriggerId = TriggerId> {
/**
* Unique name of the trigger as identified in `ui_actions` plugin trigger
* registry, such as "SELECT_RANGE_TRIGGER" or "VALUE_CLICK_TRIGGER".
@@ -48,6 +48,4 @@ export interface Trigger<ID extends string = string> {
description?: string;
}

export type AnyTrigger = Trigger<any>;

export type TriggerContext<T> = T extends Trigger<infer ID> ? TriggerContextMapping[ID] : never;
export type TriggerContext<T> = T extends TriggerId ? TriggerContextMapping[T] : never;
7 changes: 4 additions & 3 deletions src/plugins/ui_actions/public/triggers/trigger_contract.ts
Original file line number Diff line number Diff line change
@@ -17,18 +17,19 @@
* under the License.
*/

import { AnyTrigger, TriggerContext } from './trigger';
import { TriggerContext } from './trigger';
import { TriggerInternal } from './trigger_internal';
import { TriggerId } from '../types';

/**
* This is a public representation of a trigger that is provided to other plugins.
*/
export class TriggerContract<T extends AnyTrigger> {
export class TriggerContract<T extends TriggerId> {
/**
* Unique name of the trigger as identified in `ui_actions` plugin trigger
* registry, such as "SELECT_RANGE_TRIGGER" or "VALUE_CLICK_TRIGGER".
*/
public readonly id: string;
public readonly id: T;

/**
* User friendly name of the trigger.
7 changes: 4 additions & 3 deletions src/plugins/ui_actions/public/triggers/trigger_internal.ts
Original file line number Diff line number Diff line change
@@ -17,20 +17,21 @@
* under the License.
*/

import { AnyTrigger, TriggerContext } from './trigger';
import { TriggerContext, Trigger } from './trigger';
import { TriggerContract } from './trigger_contract';
import { UiActionsService } from '../service';
import { Action } from '../actions';
import { buildContextMenuForActions, openContextMenu } from '../context_menu';
import { TriggerId } from '../types';

/**
* Internal representation of a trigger kept for consumption only internally
* within `ui_actions` plugin.
*/
export class TriggerInternal<T extends AnyTrigger> {
export class TriggerInternal<T extends TriggerId> {
public readonly contract = new TriggerContract<T>(this);

constructor(public readonly service: UiActionsService, public readonly trigger: T) {}
constructor(public readonly service: UiActionsService, public readonly trigger: Trigger<T>) {}

public async execute(context: TriggerContext<T>) {
const triggerId = this.trigger.id;
2 changes: 2 additions & 0 deletions src/plugins/ui_actions/public/types.ts
Original file line number Diff line number Diff line change
@@ -24,6 +24,8 @@ export type TriggerRegistry = Map<string, TriggerInternal<any>>;
export type ActionRegistry = Map<string, Action>;
export type TriggerToActionsRegistry = Map<string, string[]>;

export type TriggerId = string;

export interface TriggerContextMapping {
[key: string]: object;
}

0 comments on commit 4dc7ca9

Please sign in to comment.