From 807aefdd2690f48aac45cda2d8d9d161eb9efecc Mon Sep 17 00:00:00 2001 From: "Hung-Han (Henry) Chen" Date: Tue, 5 Oct 2021 09:52:57 +0300 Subject: [PATCH] Samne to Signed-off-by: Hung-Han (Henry) Chen --- .../+catalog/catalog-entity-item.tsx | 4 ++-- .../components/hotbar/hotbar-menu.tsx | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/renderer/components/+catalog/catalog-entity-item.tsx b/src/renderer/components/+catalog/catalog-entity-item.tsx index 62590205446c..416b0c09baec 100644 --- a/src/renderer/components/+catalog/catalog-entity-item.tsx +++ b/src/renderer/components/+catalog/catalog-entity-item.tsx @@ -119,14 +119,14 @@ export class CatalogEntityItem 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); diff --git a/src/renderer/components/hotbar/hotbar-menu.tsx b/src/renderer/components/hotbar/hotbar-menu.tsx index 70702d0bc5a6..a56c55a95a3f 100644 --- a/src/renderer/components/hotbar/hotbar-menu.tsx +++ b/src/renderer/components/hotbar/hotbar-menu.tsx @@ -40,6 +40,8 @@ interface Props { className?: IClassName; } +const isPromise = (obj: any): obj is Promise => (obj?.then && typeof obj?.then === "function") ? true: false; + @observer export class HotbarMenu extends React.Component { get hotbar() { @@ -140,11 +142,23 @@ export class HotbarMenu extends React.Component { } 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 })}