1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix iterating onBeforeRunHooks

Signed-off-by: Juho Heikka <juho.heikka@gmail.com>
This commit is contained in:
Juho Heikka 2021-10-07 18:32:11 +03:00
parent fd3f25cc4e
commit 912f239abb
2 changed files with 12 additions and 18 deletions

View File

@ -22,11 +22,11 @@
import type { CatalogEntity } from "../catalog"; import type { CatalogEntity } from "../catalog";
export class CatalogRunEvent { export class CatalogRunEvent {
private _preventDefaulted: boolean; private _defaultPrevented: boolean;
private _target: CatalogEntity; private _target: CatalogEntity;
get preventDefaulted() { get defaultPrevented() {
return this._preventDefaulted; return this._defaultPrevented;
} }
get target() { get target() {
@ -34,11 +34,11 @@ export class CatalogRunEvent {
} }
constructor({ target }: { target: CatalogEntity }) { constructor({ target }: { target: CatalogEntity }) {
this._preventDefaulted = false; this._defaultPrevented = false;
this._target = target; this._target = target;
} }
preventDefault() { preventDefault() {
this._preventDefaulted = true; this._defaultPrevented = true;
} }
} }

View File

@ -32,7 +32,7 @@ import { catalogEntityRunContext } from "./catalog-entity";
import { CatalogRunEvent } from "../../common/catalog/catalog-run-event"; import { CatalogRunEvent } from "../../common/catalog/catalog-run-event";
export type EntityFilter = (entity: CatalogEntity) => any; export type EntityFilter = (entity: CatalogEntity) => any;
export type CatalogEntityOnBeforeRun = ((event: CatalogRunEvent) => void) | ((event: CatalogRunEvent) => Promise<void>); export type CatalogEntityOnBeforeRun = (event: CatalogRunEvent) => void | Promise<void>;
export class CatalogEntityRegistry { export class CatalogEntityRegistry {
@observable protected activeEntityId: string | undefined = undefined; @observable protected activeEntityId: string | undefined = undefined;
@ -198,25 +198,19 @@ export class CatalogEntityRegistry {
async onBeforeRun(entity: CatalogEntity): Promise<boolean> { async onBeforeRun(entity: CatalogEntity): Promise<boolean> {
logger.debug(`[CATALOG-ENTITY-REGISTRY]: run onBeforeRun on ${entity.getId()}`); logger.debug(`[CATALOG-ENTITY-REGISTRY]: run onBeforeRun on ${entity.getId()}`);
const runEvent = new CatalogRunEvent({ target: entity });
for (const onBeforeRun of this.onBeforeRunHooks) { for (const onBeforeRun of this.onBeforeRunHooks) {
try { try { 
const runEvent = new CatalogRunEvent({ target: entity });
await onBeforeRun(runEvent); await onBeforeRun(runEvent);
const shouldContinue = !runEvent.preventDefaulted;
return shouldContinue;
} catch (error) { } catch (error) {
logger.warn(`[CATALOG-ENTITY-REGISTRY]: entity ${entity.getId()} onBeforeRun threw an error`, error); logger.warn(`[CATALOG-ENTITY-REGISTRY]: entity ${entity.getId()} onBeforeRun threw an error`, error);
// If a handler throws treat it as if it has returned `false`
// Namely: assume that its internal logic has failed and didn't complete as expected
return false;
} }
} }
return true; const shouldContinue = !runEvent.defaultPrevented;
return shouldContinue;
} }
/** /**