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

onRunHook > onBeforeRun

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
Hung-Han (Henry) Chen 2021-10-05 16:11:25 +03:00
parent 807aefdd26
commit 74eb89c361
No known key found for this signature in database
GPG Key ID: 54B44603D251B788
7 changed files with 52 additions and 52 deletions

View File

@ -22,7 +22,7 @@
import type { CatalogCategory, CatalogEntity } from "../../common/catalog"; import type { CatalogCategory, CatalogEntity } from "../../common/catalog";
import { catalogEntityRegistry as registry } from "../../renderer/api/catalog-entity-registry"; import { catalogEntityRegistry as registry } from "../../renderer/api/catalog-entity-registry";
import type { CatalogEntityOnRunHook } from "../../renderer/api/catalog-entity-registry"; import type { CatalogEntityOnBeforeRun } from "../../renderer/api/catalog-entity-registry";
export { catalogCategoryRegistry as catalogCategories } from "../../common/catalog/catalog-category-registry"; export { catalogCategoryRegistry as catalogCategories } from "../../common/catalog/catalog-category-registry";
export class CatalogEntityRegistry { export class CatalogEntityRegistry {
@ -50,20 +50,20 @@ export class CatalogEntityRegistry {
} }
/** /**
* Add a onRun hook to a catalog entity. * Add a onBeforeRun hook to a catalog entity.
* @param catalogEntityUid The uid of the catalog entity * @param catalogEntityUid The uid of the catalog entity
* @param onRunHook The function that should return a boolean if the onRun of catalog entity should be triggered. * @param onBeforeRun The function that should return a boolean if the onBeforeRun of catalog entity should be triggered.
* @returns A function to remove that hook * @returns A function to remove that hook
*/ */
addOnRunHook(catalogEntityUid: CatalogEntity["metadata"]["uid"], onRunHook: CatalogEntityOnRunHook) { addOnBeforeRun(catalogEntityUid: CatalogEntity["metadata"]["uid"], onBeforeRun: CatalogEntityOnBeforeRun) {
return registry.addOnRunHook(catalogEntityUid, onRunHook); return registry.addOnBeforeRun(catalogEntityUid, onBeforeRun);
} }
/** /**
* Returns one catalog entity onRun hook by catalog entity uid * Returns one catalog entity onBeforeRun by catalog entity uid
*/ */
getOnRunHook(catalogEntityUid: CatalogEntity["metadata"]["uid"]): CatalogEntityOnRunHook | undefined { onBeforeRun(catalogEntityUid: CatalogEntity["metadata"]["uid"]): CatalogEntityOnBeforeRun | undefined {
return registry.getOnRunHook(catalogEntityUid); return registry.getOnBeforeRun(catalogEntityUid);
} }
} }

View File

@ -29,7 +29,7 @@ import { Disposer, iter } from "../utils";
import { once } from "lodash"; import { once } from "lodash";
export type EntityFilter = (entity: CatalogEntity) => any; export type EntityFilter = (entity: CatalogEntity) => any;
export type CatalogEntityOnRunHook = (entity: CatalogEntity) => boolean | Promise<boolean>; export type CatalogEntityOnBeforeRun = (entity: CatalogEntity) => boolean | Promise<boolean>;
type CatalogEntityUid = CatalogEntity["metadata"]["uid"]; type CatalogEntityUid = CatalogEntity["metadata"]["uid"];
@ -39,9 +39,9 @@ export class CatalogEntityRegistry {
protected filters = observable.set<EntityFilter>([], { protected filters = observable.set<EntityFilter>([], {
deep: false, deep: false,
}); });
protected entityOnRunHooks = observable.set<{ protected entityOnBeforeRun = observable.set<{
catalogEntityUid: CatalogEntityUid; catalogEntityUid: CatalogEntityUid;
onRunHook: CatalogEntityOnRunHook onBeforeRun: CatalogEntityOnBeforeRun
}>([], { }>([], {
deep: false, deep: false,
}); });
@ -182,26 +182,26 @@ export class CatalogEntityRegistry {
/** /**
* Add a onRun hook to a catalog entity. * Add a onRun hook to a catalog entity.
* @param uid The uid of the catalog entity * @param uid The uid of the catalog entity
* @param onRunHook The function that should return a boolean if the onRun of catalog entity should be triggered. * @param onBeforeRun The function that should return a boolean if the onRun of catalog entity should be triggered.
* @returns A function to remove that hook * @returns A function to remove that hook
*/ */
addOnRunHook(catalogEntityUid: CatalogEntityUid, onRunHook: CatalogEntityOnRunHook): Disposer { addOnBeforeRun(catalogEntityUid: CatalogEntityUid, onBeforeRun: CatalogEntityOnBeforeRun): Disposer {
this.entityOnRunHooks.add({ this.entityOnBeforeRun.add({
catalogEntityUid, catalogEntityUid,
onRunHook, onBeforeRun,
}); });
return once(() => void this.entityOnRunHooks.delete({ return once(() => void this.entityOnBeforeRun.delete({
catalogEntityUid, catalogEntityUid,
onRunHook, onBeforeRun,
})); }));
} }
/** /**
* Returns one catalog entity onRun hook by catalog entity uid * Returns one catalog entity onBeforeRun by catalog entity uid
*/ */
getOnRunHook(_catalogEntityUid: CatalogEntityUid): CatalogEntityOnRunHook | undefined { getOnBeforeRun(_catalogEntityUid: CatalogEntityUid): CatalogEntityOnBeforeRun | undefined {
return Array.from(this.entityOnRunHooks).find(({ catalogEntityUid }) => catalogEntityUid === _catalogEntityUid)?.onRunHook; return Array.from(this.entityOnBeforeRun).find(({ catalogEntityUid }) => catalogEntityUid === _catalogEntityUid)?.onBeforeRun;
} }
} }

View File

@ -22,7 +22,7 @@ import styles from "./catalog.module.css";
import React from "react"; import React from "react";
import { action, computed } from "mobx"; import { action, computed } from "mobx";
import type { CatalogEntity, CatalogEntityActionContext } from "../../api/catalog-entity"; import type { CatalogEntity, CatalogEntityActionContext } from "../../api/catalog-entity";
import type { CatalogEntityOnRunHook } from "../../api/catalog-entity-registry"; import type { CatalogEntityOnBeforeRun } from "../../api/catalog-entity-registry";
import type { ItemObject } from "../../../common/item.store"; import type { ItemObject } from "../../../common/item.store";
import { Badge } from "../badge"; import { Badge } from "../badge";
import { navigation } from "../../navigation"; import { navigation } from "../../navigation";
@ -106,27 +106,27 @@ export class CatalogEntityItem<T extends CatalogEntity> implements ItemObject {
]; ];
} }
onRun(onRunHook: CatalogEntityOnRunHook | undefined, ctx: CatalogEntityActionContext) { onRun(onBeforeRun: CatalogEntityOnBeforeRun | undefined, ctx: CatalogEntityActionContext) {
if (!onRunHook) { if (!onBeforeRun) {
this.entity.onRun(ctx); this.entity.onRun(ctx);
return; return;
} }
if (typeof onRunHook === "function") { if (typeof onBeforeRun === "function") {
let shouldRun; let shouldRun;
try { try {
shouldRun = onRunHook(toJS(this.entity)); shouldRun = onBeforeRun(toJS(this.entity));
} catch (error) { } catch (error) {
if (process?.env?.NODE_ENV !== "test") console.warn(`[CATALOG-ENTITY-ITEM] onRunHook of entity.metadata.uid ${this.entity?.metadata?.uid} throw an exception, stop before onRun`, error); if (process?.env?.NODE_ENV !== "test") console.warn(`[CATALOG-ENTITY-ITEM] onBeforeRun of entity.metadata.uid ${this.entity?.metadata?.uid} throw an exception, stop before onRun`, error);
} }
if (isPromise(shouldRun)) { if (isPromise(shouldRun)) {
Promise.resolve(shouldRun).then((shouldRun) => { Promise.resolve(shouldRun).then((shouldRun) => {
if (shouldRun) this.entity.onRun(ctx); if (shouldRun) this.entity.onRun(ctx);
}).catch((error) => { }).catch((error) => {
if (process?.env?.NODE_ENV !== "test") console.warn(`[CATALOG-ENTITY-ITEM] onRunHook of entity.metadata.uid ${this.entity?.metadata?.uid} rejects, stop before onRun`, error); if (process?.env?.NODE_ENV !== "test") console.warn(`[CATALOG-ENTITY-ITEM] onBeforeRun of entity.metadata.uid ${this.entity?.metadata?.uid} rejects, stop before onRun`, error);
}); });
} else if (shouldRun) { } else if (shouldRun) {
this.entity.onRun(ctx); this.entity.onRun(ctx);

View File

@ -22,7 +22,7 @@
import { computed, IReactionDisposer, makeObservable, observable, reaction } from "mobx"; import { computed, IReactionDisposer, makeObservable, observable, reaction } from "mobx";
import { catalogEntityRegistry as _catalogEntityRegistry, CatalogEntityRegistry } from "../../api/catalog-entity-registry"; import { catalogEntityRegistry as _catalogEntityRegistry, CatalogEntityRegistry } from "../../api/catalog-entity-registry";
import type { CatalogEntity } from "../../api/catalog-entity"; import type { CatalogEntity } from "../../api/catalog-entity";
import type { CatalogEntityOnRunHook } from "../../api/catalog-entity-registry"; import type { CatalogEntityOnBeforeRun } from "../../api/catalog-entity-registry";
import { ItemStore } from "../../../common/item.store"; import { ItemStore } from "../../../common/item.store";
import { CatalogCategory, catalogCategoryRegistry } from "../../../common/catalog"; import { CatalogCategory, catalogCategoryRegistry } from "../../../common/catalog";
import { autoBind } from "../../../common/utils"; import { autoBind } from "../../../common/utils";
@ -58,8 +58,8 @@ export class CatalogEntityStore extends ItemStore<CatalogEntityItem<CatalogEntit
return this.entities.find(e => e.getId() === this.selectedItemId); return this.entities.find(e => e.getId() === this.selectedItemId);
} }
getCatalogEntityOnRunHook(catalogEntityUid: CatalogEntity["metadata"]["uid"]): CatalogEntityOnRunHook | undefined { getCatalogEntityOnBeforeRun(catalogEntityUid: CatalogEntity["metadata"]["uid"]): CatalogEntityOnBeforeRun | undefined {
return this.#catalogEntityRegistry.getOnRunHook(catalogEntityUid); return this.#catalogEntityRegistry.getOnBeforeRun(catalogEntityUid);
} }
watch() { watch() {

View File

@ -112,7 +112,7 @@ describe("<Catalog />", () => {
jest.restoreAllMocks(); jest.restoreAllMocks();
}); });
it("can use catalogEntityRegistry.addOnRunHook to add hooks for catalog entities", (done) => { it("can use catalogEntityRegistry.addOnBeforeRun to add hooks for catalog entities", (done) => {
const onRun = jest.fn(); const onRun = jest.fn();
const catalogEntityItem = new CatalogEntityItem({ const catalogEntityItem = new CatalogEntityItem({
...catalogEntity, ...catalogEntity,
@ -133,7 +133,7 @@ describe("<Catalog />", () => {
let hookGetCalled = false; let hookGetCalled = false;
const onRunDisposer = catalogEntityRegistry.addOnRunHook( const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid, catalogEntityUid,
(entity) => { (entity) => {
hookGetCalled = true; hookGetCalled = true;
@ -185,7 +185,7 @@ describe("<Catalog />", () => {
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon")); userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
}); });
it("addOnRunHook return false => onRun wont be triggered", (done) => { it("onBeforeRun return false => onRun wont be triggered", (done) => {
const onRun = jest.fn(); const onRun = jest.fn();
const catalogEntityItem = new CatalogEntityItem({ const catalogEntityItem = new CatalogEntityItem({
...catalogEntity, ...catalogEntity,
@ -204,7 +204,7 @@ describe("<Catalog />", () => {
return catalogEntityItem; return catalogEntityItem;
}); });
const onRunDisposer = catalogEntityRegistry.addOnRunHook( const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid, catalogEntityUid,
() => { () => {
onRunDisposer?.(); onRunDisposer?.();
@ -229,7 +229,7 @@ describe("<Catalog />", () => {
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon")); userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
}); });
it("addOnRunHook throw an exception => onRun wont be triggered", (done) => { it("addOnBeforeRun throw an exception => onRun wont be triggered", (done) => {
const onRun = jest.fn(); const onRun = jest.fn();
const catalogEntityItem = new CatalogEntityItem({ const catalogEntityItem = new CatalogEntityItem({
...catalogEntity, ...catalogEntity,
@ -248,7 +248,7 @@ describe("<Catalog />", () => {
return catalogEntityItem; return catalogEntityItem;
}); });
const onRunDisposer = catalogEntityRegistry.addOnRunHook( const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid, catalogEntityUid,
() => { () => {
onRunDisposer?.(); onRunDisposer?.();
@ -297,7 +297,7 @@ describe("<Catalog />", () => {
return catalogEntityItem; return catalogEntityItem;
}); });
const onRunDisposer = catalogEntityRegistry.addOnRunHook( const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid, catalogEntityUid,
async () => { async () => {
onRunDisposer?.(); onRunDisposer?.();
@ -340,7 +340,7 @@ describe("<Catalog />", () => {
return catalogEntityItem; return catalogEntityItem;
}); });
const onRunDisposer = catalogEntityRegistry.addOnRunHook( const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid, catalogEntityUid,
async () => { async () => {
onRunDisposer?.(); onRunDisposer?.();
@ -386,7 +386,7 @@ describe("<Catalog />", () => {
return catalogEntityItem; return catalogEntityItem;
}); });
const onRunDisposer = catalogEntityRegistry.addOnRunHook( const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid, catalogEntityUid,
async () => { async () => {
onRunDisposer?.(); onRunDisposer?.();

View File

@ -133,16 +133,16 @@ export class Catalog extends React.Component<Props> {
if (this.catalogEntityStore.selectedItemId) { if (this.catalogEntityStore.selectedItemId) {
this.catalogEntityStore.selectedItemId = null; this.catalogEntityStore.selectedItemId = null;
} else { } else {
const onRunHook = this.catalogEntityStore.getCatalogEntityOnRunHook(item.entity.metadata.uid); const onBeforeRun = this.catalogEntityStore.getCatalogEntityOnBeforeRun(item.entity.metadata.uid);
item.onRun(onRunHook, catalogEntityRunContext); item.onRun(onBeforeRun, catalogEntityRunContext);
} }
}; };
onClickDetailPanelIcon = (item: CatalogEntityItem<CatalogEntity>) => { onClickDetailPanelIcon = (item: CatalogEntityItem<CatalogEntity>) => {
const onRunHook = this.catalogEntityStore.getCatalogEntityOnRunHook(item.entity.metadata.uid); const onBeforeRun = this.catalogEntityStore.getCatalogEntityOnBeforeRun(item.entity.metadata.uid);
item.onRun(onRunHook, catalogEntityRunContext); item.onRun(onBeforeRun, catalogEntityRunContext);
}; };
onMenuItemClick(menuItem: CatalogEntityContextMenu) { onMenuItemClick(menuItem: CatalogEntityContextMenu) {

View File

@ -26,7 +26,7 @@ import { observer } from "mobx-react";
import { HotbarEntityIcon } from "./hotbar-entity-icon"; import { HotbarEntityIcon } from "./hotbar-entity-icon";
import { cssNames, IClassName } from "../../utils"; import { cssNames, IClassName } from "../../utils";
import { catalogEntityRegistry } from "../../api/catalog-entity-registry"; import { catalogEntityRegistry } from "../../api/catalog-entity-registry";
import type { CatalogEntityOnRunHook} from "../../api/catalog-entity-registry"; import type { CatalogEntityOnBeforeRun} from "../../api/catalog-entity-registry";
import { HotbarStore } from "../../../common/hotbar-store"; import { HotbarStore } from "../../../common/hotbar-store";
import { CatalogEntity, catalogEntityRunContext } from "../../api/catalog-entity"; import { CatalogEntity, catalogEntityRunContext } from "../../api/catalog-entity";
import { DragDropContext, Draggable, Droppable, DropResult } from "react-beautiful-dnd"; import { DragDropContext, Draggable, Droppable, DropResult } from "react-beautiful-dnd";
@ -58,8 +58,8 @@ export class HotbarMenu extends React.Component<Props> {
return catalogEntityRegistry.getById(item?.entity.uid) ?? null; return catalogEntityRegistry.getById(item?.entity.uid) ?? null;
} }
getEntityOnRunHook(uid: string): CatalogEntityOnRunHook | undefined { getEntityOnBeforeRun(uid: string): CatalogEntityOnBeforeRun | undefined {
return catalogEntityRegistry.getOnRunHook(uid); return catalogEntityRegistry.getOnBeforeRun(uid);
} }
onDragEnd(result: DropResult) { onDragEnd(result: DropResult) {
@ -133,28 +133,28 @@ export class HotbarMenu extends React.Component<Props> {
index={index} index={index}
entity={entity} entity={entity}
onClick={() => { onClick={() => {
const onRunHook = this.getEntityOnRunHook(entity.metadata.uid); const onBeforeRun = this.getEntityOnBeforeRun(entity.metadata.uid);
if (!onRunHook) { if (!onBeforeRun) {
entity.onRun(catalogEntityRunContext); entity.onRun(catalogEntityRunContext);
return; return;
} }
if (typeof onRunHook === "function") { if (typeof onBeforeRun === "function") {
let shouldRun; let shouldRun;
try { try {
shouldRun = onRunHook(toJS(entity)); shouldRun = onBeforeRun(toJS(entity));
} catch (error) { } catch (error) {
if (process?.env?.NODE_ENV !== "test") console.warn(`[HOT-BAR] onRunHook of entity.metadata.uid ${entity?.metadata?.uid} throw an exception, stop before onRun`, error); if (process?.env?.NODE_ENV !== "test") console.warn(`[HOT-BAR] onBeforeRun of entity.metadata.uid ${entity?.metadata?.uid} throw an exception, stop before onRun`, error);
} }
if (isPromise(shouldRun)) { if (isPromise(shouldRun)) {
Promise.resolve(shouldRun).then((shouldRun) => { Promise.resolve(shouldRun).then((shouldRun) => {
if (shouldRun) entity.onRun(catalogEntityRunContext); if (shouldRun) entity.onRun(catalogEntityRunContext);
}).catch((error) => { }).catch((error) => {
if (process?.env?.NODE_ENV !== "test") console.warn(`[HOT-BAR] onRunHook of entity.metadata.uid ${entity?.metadata?.uid} rejects, stop before onRun`, error); if (process?.env?.NODE_ENV !== "test") console.warn(`[HOT-BAR] onBeforeRun of entity.metadata.uid ${entity?.metadata?.uid} rejects, stop before onRun`, error);
}); });
} else if (shouldRun) { } else if (shouldRun) {
entity.onRun(catalogEntityRunContext); entity.onRun(catalogEntityRunContext);