Skip to content

Commit

Permalink
Samne to <HotBarMenu />
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 24f5f99 commit 807aefd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/renderer/components/+catalog/catalog-entity-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ export class CatalogEntityItem<T extends CatalogEntity> implements ItemObject {
try {
shouldRun = onRunHook(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] onRunHook 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] onRunHook of entity.metadata.uid ${this.entity?.metadata?.uid} rejects, stop before onRun`, error);
});
} else if (shouldRun) {
this.entity.onRun(ctx);
Expand Down
24 changes: 19 additions & 5 deletions src/renderer/components/hotbar/hotbar-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ interface Props {
className?: IClassName;
}

const isPromise = (obj: any): obj is Promise<any> => (obj?.then && typeof obj?.then === "function") ? true: false;

@observer
export class HotbarMenu extends React.Component<Props> {
get hotbar() {
Expand Down Expand Up @@ -140,11 +142,23 @@ export class HotbarMenu extends React.Component<Props> {
}

if (typeof onRunHook === "function") {
// if onRunHook() returns a Promise, we wait for it to resolve
// if not, just take whatever it returns
Promise.resolve(onRunHook(toJS(entity))).then((shouldRun) => {
if (shouldRun) entity.onRun(catalogEntityRunContext);
});
let shouldRun;

try {
shouldRun = onRunHook(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 (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);
});
} else if (shouldRun) {
entity.onRun(catalogEntityRunContext);
}
}
}}
className={cssNames({ isDragging: snapshot.isDragging })}
Expand Down

0 comments on commit 807aefd

Please sign in to comment.