Skip to content

Commit

Permalink
onRunHook > onBeforeRun
Browse files Browse the repository at this point in the history
Signed-off-by: Hung-Han (Henry) Chen <[email protected]>
  • Loading branch information
chenhunghan committed Oct 5, 2021
1 parent 807aefd commit 74eb89c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 52 deletions.
16 changes: 8 additions & 8 deletions src/extensions/renderer-api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/renderer/api/catalog-entity-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>;
export type CatalogEntityOnBeforeRun = (entity: CatalogEntity) => boolean | Promise<boolean>;

type CatalogEntityUid = CatalogEntity["metadata"]["uid"];

Expand All @@ -39,9 +39,9 @@ export class CatalogEntityRegistry {
protected filters = observable.set<EntityFilter>([], {
deep: false,
});
protected entityOnRunHooks = observable.set<{
protected entityOnBeforeRun = observable.set<{
catalogEntityUid: CatalogEntityUid;
onRunHook: CatalogEntityOnRunHook
onBeforeRun: CatalogEntityOnBeforeRun
}>([], {
deep: false,
});
Expand Down Expand Up @@ -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;
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/renderer/components/+catalog/catalog-entity-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -106,27 +106,27 @@ export class CatalogEntityItem<T extends CatalogEntity> 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);
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/components/+catalog/catalog-entity.store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -58,8 +58,8 @@ export class CatalogEntityStore extends ItemStore<CatalogEntityItem<CatalogEntit
return this.entities.find(e => 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() {
Expand Down
18 changes: 9 additions & 9 deletions src/renderer/components/+catalog/catalog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe("<Catalog />", () => {
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,
Expand All @@ -133,7 +133,7 @@ describe("<Catalog />", () => {

let hookGetCalled = false;

const onRunDisposer = catalogEntityRegistry.addOnRunHook(
const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
(entity) => {
hookGetCalled = true;
Expand Down Expand Up @@ -185,7 +185,7 @@ describe("<Catalog />", () => {
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,
Expand All @@ -204,7 +204,7 @@ describe("<Catalog />", () => {
return catalogEntityItem;
});

const onRunDisposer = catalogEntityRegistry.addOnRunHook(
const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
() => {
onRunDisposer?.();
Expand All @@ -229,7 +229,7 @@ describe("<Catalog />", () => {
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,
Expand All @@ -248,7 +248,7 @@ describe("<Catalog />", () => {
return catalogEntityItem;
});

const onRunDisposer = catalogEntityRegistry.addOnRunHook(
const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
() => {
onRunDisposer?.();
Expand Down Expand Up @@ -297,7 +297,7 @@ describe("<Catalog />", () => {
return catalogEntityItem;
});

const onRunDisposer = catalogEntityRegistry.addOnRunHook(
const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
async () => {
onRunDisposer?.();
Expand Down Expand Up @@ -340,7 +340,7 @@ describe("<Catalog />", () => {
return catalogEntityItem;
});

const onRunDisposer = catalogEntityRegistry.addOnRunHook(
const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
async () => {
onRunDisposer?.();
Expand Down Expand Up @@ -386,7 +386,7 @@ describe("<Catalog />", () => {
return catalogEntityItem;
});

const onRunDisposer = catalogEntityRegistry.addOnRunHook(
const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
catalogEntityUid,
async () => {
onRunDisposer?.();
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/components/+catalog/catalog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ export class Catalog extends React.Component<Props> {
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<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) {
Expand Down
18 changes: 9 additions & 9 deletions src/renderer/components/hotbar/hotbar-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -58,8 +58,8 @@ export class HotbarMenu extends React.Component<Props> {
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) {
Expand Down Expand Up @@ -133,28 +133,28 @@ export class HotbarMenu extends React.Component<Props> {
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);
Expand Down

0 comments on commit 74eb89c

Please sign in to comment.