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:
parent
807aefdd26
commit
74eb89c361
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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"];
|
||||
|
||||
@ -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,
|
||||
});
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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<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);
|
||||
|
||||
@ -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<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() {
|
||||
|
||||
@ -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,
|
||||
@ -133,7 +133,7 @@ describe("<Catalog />", () => {
|
||||
|
||||
let hookGetCalled = false;
|
||||
|
||||
const onRunDisposer = catalogEntityRegistry.addOnRunHook(
|
||||
const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
|
||||
catalogEntityUid,
|
||||
(entity) => {
|
||||
hookGetCalled = true;
|
||||
@ -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,
|
||||
@ -204,7 +204,7 @@ describe("<Catalog />", () => {
|
||||
return catalogEntityItem;
|
||||
});
|
||||
|
||||
const onRunDisposer = catalogEntityRegistry.addOnRunHook(
|
||||
const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
|
||||
catalogEntityUid,
|
||||
() => {
|
||||
onRunDisposer?.();
|
||||
@ -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,
|
||||
@ -248,7 +248,7 @@ describe("<Catalog />", () => {
|
||||
return catalogEntityItem;
|
||||
});
|
||||
|
||||
const onRunDisposer = catalogEntityRegistry.addOnRunHook(
|
||||
const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
|
||||
catalogEntityUid,
|
||||
() => {
|
||||
onRunDisposer?.();
|
||||
@ -297,7 +297,7 @@ describe("<Catalog />", () => {
|
||||
return catalogEntityItem;
|
||||
});
|
||||
|
||||
const onRunDisposer = catalogEntityRegistry.addOnRunHook(
|
||||
const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
|
||||
catalogEntityUid,
|
||||
async () => {
|
||||
onRunDisposer?.();
|
||||
@ -340,7 +340,7 @@ describe("<Catalog />", () => {
|
||||
return catalogEntityItem;
|
||||
});
|
||||
|
||||
const onRunDisposer = catalogEntityRegistry.addOnRunHook(
|
||||
const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
|
||||
catalogEntityUid,
|
||||
async () => {
|
||||
onRunDisposer?.();
|
||||
@ -386,7 +386,7 @@ describe("<Catalog />", () => {
|
||||
return catalogEntityItem;
|
||||
});
|
||||
|
||||
const onRunDisposer = catalogEntityRegistry.addOnRunHook(
|
||||
const onRunDisposer = catalogEntityRegistry.addOnBeforeRun(
|
||||
catalogEntityUid,
|
||||
async () => {
|
||||
onRunDisposer?.();
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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<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) {
|
||||
@ -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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user