diff --git a/src/extensions/renderer-api/catalog.ts b/src/extensions/renderer-api/catalog.ts index 28c98c1be165..0af7c17c87cd 100644 --- a/src/extensions/renderer-api/catalog.ts +++ b/src/extensions/renderer-api/catalog.ts @@ -22,7 +22,7 @@ import type { CatalogCategory, CatalogEntity } from "../../common/catalog"; 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 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 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 */ - addOnRunHook(catalogEntityUid: CatalogEntity["metadata"]["uid"], onRunHook: CatalogEntityOnRunHook) { - return registry.addOnRunHook(catalogEntityUid, onRunHook); + addOnBeforeRun(catalogEntityUid: CatalogEntity["metadata"]["uid"], onBeforeRun: CatalogEntityOnBeforeRun) { + 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 { - return registry.getOnRunHook(catalogEntityUid); + onBeforeRun(catalogEntityUid: CatalogEntity["metadata"]["uid"]): CatalogEntityOnBeforeRun | undefined { + return registry.getOnBeforeRun(catalogEntityUid); } } diff --git a/src/renderer/api/catalog-entity-registry.ts b/src/renderer/api/catalog-entity-registry.ts index 4c53a94901a6..8eccff7c00f8 100644 --- a/src/renderer/api/catalog-entity-registry.ts +++ b/src/renderer/api/catalog-entity-registry.ts @@ -29,7 +29,7 @@ import { Disposer, iter } from "../utils"; import { once } from "lodash"; export type EntityFilter = (entity: CatalogEntity) => any; -export type CatalogEntityOnRunHook = (entity: CatalogEntity) => boolean | Promise; +export type CatalogEntityOnBeforeRun = (entity: CatalogEntity) => boolean | Promise; type CatalogEntityUid = CatalogEntity["metadata"]["uid"]; @@ -39,9 +39,9 @@ export class CatalogEntityRegistry { protected filters = observable.set([], { deep: false, }); - protected entityOnRunHooks = observable.set<{ + protected entityOnBeforeRun = observable.set<{ catalogEntityUid: CatalogEntityUid; - onRunHook: CatalogEntityOnRunHook + onBeforeRun: CatalogEntityOnBeforeRun }>([], { deep: false, }); @@ -182,26 +182,26 @@ export class CatalogEntityRegistry { /** * Add a onRun hook to a 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 */ - addOnRunHook(catalogEntityUid: CatalogEntityUid, onRunHook: CatalogEntityOnRunHook): Disposer { - this.entityOnRunHooks.add({ + addOnBeforeRun(catalogEntityUid: CatalogEntityUid, onBeforeRun: CatalogEntityOnBeforeRun): Disposer { + this.entityOnBeforeRun.add({ catalogEntityUid, - onRunHook, + onBeforeRun, }); - return once(() => void this.entityOnRunHooks.delete({ + return once(() => void this.entityOnBeforeRun.delete({ 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 { - return Array.from(this.entityOnRunHooks).find(({ catalogEntityUid }) => catalogEntityUid === _catalogEntityUid)?.onRunHook; + getOnBeforeRun(_catalogEntityUid: CatalogEntityUid): CatalogEntityOnBeforeRun | undefined { + return Array.from(this.entityOnBeforeRun).find(({ catalogEntityUid }) => catalogEntityUid === _catalogEntityUid)?.onBeforeRun; } } diff --git a/src/renderer/components/+catalog/catalog-entity-item.tsx b/src/renderer/components/+catalog/catalog-entity-item.tsx index 416b0c09baec..903b676ceea0 100644 --- a/src/renderer/components/+catalog/catalog-entity-item.tsx +++ b/src/renderer/components/+catalog/catalog-entity-item.tsx @@ -22,7 +22,7 @@ import styles from "./catalog.module.css"; import React from "react"; import { action, computed } from "mobx"; 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 { Badge } from "../badge"; import { navigation } from "../../navigation"; @@ -106,27 +106,27 @@ export class CatalogEntityItem implements ItemObject { ]; } - onRun(onRunHook: CatalogEntityOnRunHook | undefined, ctx: CatalogEntityActionContext) { - if (!onRunHook) { + onRun(onBeforeRun: CatalogEntityOnBeforeRun | undefined, ctx: CatalogEntityActionContext) { + if (!onBeforeRun) { this.entity.onRun(ctx); return; } - if (typeof onRunHook === "function") { + if (typeof onBeforeRun === "function") { let shouldRun; try { - shouldRun = onRunHook(toJS(this.entity)); + shouldRun = onBeforeRun(toJS(this.entity)); } 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)) { Promise.resolve(shouldRun).then((shouldRun) => { if (shouldRun) this.entity.onRun(ctx); }).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) { this.entity.onRun(ctx); diff --git a/src/renderer/components/+catalog/catalog-entity.store.tsx b/src/renderer/components/+catalog/catalog-entity.store.tsx index 030b74a4aacf..bace71d4068e 100644 --- a/src/renderer/components/+catalog/catalog-entity.store.tsx +++ b/src/renderer/components/+catalog/catalog-entity.store.tsx @@ -22,7 +22,7 @@ import { computed, IReactionDisposer, makeObservable, observable, reaction } from "mobx"; import { catalogEntityRegistry as _catalogEntityRegistry, CatalogEntityRegistry } from "../../api/catalog-entity-registry"; 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 { CatalogCategory, catalogCategoryRegistry } from "../../../common/catalog"; import { autoBind } from "../../../common/utils"; @@ -58,8 +58,8 @@ export class CatalogEntityStore extends ItemStore e.getId() === this.selectedItemId); } - getCatalogEntityOnRunHook(catalogEntityUid: CatalogEntity["metadata"]["uid"]): CatalogEntityOnRunHook | undefined { - return this.#catalogEntityRegistry.getOnRunHook(catalogEntityUid); + getCatalogEntityOnBeforeRun(catalogEntityUid: CatalogEntity["metadata"]["uid"]): CatalogEntityOnBeforeRun | undefined { + return this.#catalogEntityRegistry.getOnBeforeRun(catalogEntityUid); } watch() { diff --git a/src/renderer/components/+catalog/catalog.test.tsx b/src/renderer/components/+catalog/catalog.test.tsx index 4f77fc16e93c..8405152ed5e8 100644 --- a/src/renderer/components/+catalog/catalog.test.tsx +++ b/src/renderer/components/+catalog/catalog.test.tsx @@ -112,7 +112,7 @@ describe("", () => { 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 catalogEntityItem = new CatalogEntityItem({ ...catalogEntity, @@ -133,7 +133,7 @@ describe("", () => { let hookGetCalled = false; - const onRunDisposer = catalogEntityRegistry.addOnRunHook( + const onRunDisposer = catalogEntityRegistry.addOnBeforeRun( catalogEntityUid, (entity) => { hookGetCalled = true; @@ -185,7 +185,7 @@ describe("", () => { 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 catalogEntityItem = new CatalogEntityItem({ ...catalogEntity, @@ -204,7 +204,7 @@ describe("", () => { return catalogEntityItem; }); - const onRunDisposer = catalogEntityRegistry.addOnRunHook( + const onRunDisposer = catalogEntityRegistry.addOnBeforeRun( catalogEntityUid, () => { onRunDisposer?.(); @@ -229,7 +229,7 @@ describe("", () => { 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 catalogEntityItem = new CatalogEntityItem({ ...catalogEntity, @@ -248,7 +248,7 @@ describe("", () => { return catalogEntityItem; }); - const onRunDisposer = catalogEntityRegistry.addOnRunHook( + const onRunDisposer = catalogEntityRegistry.addOnBeforeRun( catalogEntityUid, () => { onRunDisposer?.(); @@ -297,7 +297,7 @@ describe("", () => { return catalogEntityItem; }); - const onRunDisposer = catalogEntityRegistry.addOnRunHook( + const onRunDisposer = catalogEntityRegistry.addOnBeforeRun( catalogEntityUid, async () => { onRunDisposer?.(); @@ -340,7 +340,7 @@ describe("", () => { return catalogEntityItem; }); - const onRunDisposer = catalogEntityRegistry.addOnRunHook( + const onRunDisposer = catalogEntityRegistry.addOnBeforeRun( catalogEntityUid, async () => { onRunDisposer?.(); @@ -386,7 +386,7 @@ describe("", () => { return catalogEntityItem; }); - const onRunDisposer = catalogEntityRegistry.addOnRunHook( + const onRunDisposer = catalogEntityRegistry.addOnBeforeRun( catalogEntityUid, async () => { onRunDisposer?.(); diff --git a/src/renderer/components/+catalog/catalog.tsx b/src/renderer/components/+catalog/catalog.tsx index 8678455b5b69..8793a03997f7 100644 --- a/src/renderer/components/+catalog/catalog.tsx +++ b/src/renderer/components/+catalog/catalog.tsx @@ -133,16 +133,16 @@ export class Catalog extends React.Component { if (this.catalogEntityStore.selectedItemId) { this.catalogEntityStore.selectedItemId = null; } 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) => { - 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) { diff --git a/src/renderer/components/hotbar/hotbar-menu.tsx b/src/renderer/components/hotbar/hotbar-menu.tsx index a56c55a95a3f..51d346df639c 100644 --- a/src/renderer/components/hotbar/hotbar-menu.tsx +++ b/src/renderer/components/hotbar/hotbar-menu.tsx @@ -26,7 +26,7 @@ import { observer } from "mobx-react"; import { HotbarEntityIcon } from "./hotbar-entity-icon"; import { cssNames, IClassName } from "../../utils"; 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 { CatalogEntity, catalogEntityRunContext } from "../../api/catalog-entity"; import { DragDropContext, Draggable, Droppable, DropResult } from "react-beautiful-dnd"; @@ -58,8 +58,8 @@ export class HotbarMenu extends React.Component { return catalogEntityRegistry.getById(item?.entity.uid) ?? null; } - getEntityOnRunHook(uid: string): CatalogEntityOnRunHook | undefined { - return catalogEntityRegistry.getOnRunHook(uid); + getEntityOnBeforeRun(uid: string): CatalogEntityOnBeforeRun | undefined { + return catalogEntityRegistry.getOnBeforeRun(uid); } onDragEnd(result: DropResult) { @@ -133,28 +133,28 @@ export class HotbarMenu extends React.Component { index={index} entity={entity} onClick={() => { - const onRunHook = this.getEntityOnRunHook(entity.metadata.uid); + const onBeforeRun = this.getEntityOnBeforeRun(entity.metadata.uid); - if (!onRunHook) { + if (!onBeforeRun) { entity.onRun(catalogEntityRunContext); return; } - if (typeof onRunHook === "function") { + if (typeof onBeforeRun === "function") { let shouldRun; try { - shouldRun = onRunHook(toJS(entity)); + shouldRun = onBeforeRun(toJS(entity)); } 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)) { Promise.resolve(shouldRun).then((shouldRun) => { if (shouldRun) entity.onRun(catalogEntityRunContext); }).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) { entity.onRun(catalogEntityRunContext);