Skip to content

Commit

Permalink
@streamich review
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Jun 17, 2020
1 parent f77c4cf commit fa41858
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
9 changes: 4 additions & 5 deletions src/plugins/ui_actions/public/actions/action_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ export class ActionInternal<A extends ActionDefinition = ActionDefinition>
}

public async isCompatible(context: Context<A>): Promise<boolean> {
const isCompatibleFromHooks = this.getActionHooks()
.map((hook) => hook.onIsCompatible)
.filter(Boolean)
.reduce((isCompatible, nextHook) => isCompatible && nextHook!(this, context), true);
if (!isCompatibleFromHooks) return false;
for (const { onIsCompatible } of this.getActionHooks()) {
if (onIsCompatible && !onIsCompatible(this, context)) return false;
}

if (!this.definition.isCompatible) return true;
return await this.definition.isCompatible(context);
}
Expand Down
8 changes: 3 additions & 5 deletions src/plugins/ui_actions/public/service/ui_actions_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class UiActionsService {
triggers = new Map(),
actions = new Map(),
triggerToActions = new Map(),
actionHooks = new Set(),
actionHooks = [],
}: UiActionsServiceParams = {}) {
this.triggers = triggers;
this.actions = actions;
Expand Down Expand Up @@ -88,9 +88,7 @@ export class UiActionsService {
throw new Error(`Action [action.id = ${definition.id}] already registered.`);
}

const action = new ActionInternal(definition, () =>
Array.from(this.actionHooksRegistry.values())
);
const action = new ActionInternal(definition, () => this.actionHooksRegistry);

this.actions.set(action.id, action);

Expand Down Expand Up @@ -155,7 +153,7 @@ export class UiActionsService {
};

public readonly registerActionHook = (hook: ActionHook) => {
this.actionHooksRegistry.add(hook);
this.actionHooksRegistry.push(hook);
};

public readonly getAction = <T extends ActionDefinition>(
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 @@ -28,7 +28,7 @@ import { ActionHook } from './actions';
export type TriggerRegistry = Map<TriggerId, TriggerInternal<any>>;
export type ActionRegistry = Map<string, ActionInternal>;
export type TriggerToActionsRegistry = Map<TriggerId, string[]>;
export type ActionHooksRegistry = Set<ActionHook>;
export type ActionHooksRegistry = ActionHook[];

const DEFAULT_TRIGGER = '';

Expand Down

0 comments on commit fa41858

Please sign in to comment.