1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Samne to <HotBarMenu />

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
Hung-Han (Henry) Chen 2021-10-05 09:52:57 +03:00
parent 24f5f9950a
commit 807aefdd26
No known key found for this signature in database
GPG Key ID: 54B44603D251B788
2 changed files with 21 additions and 7 deletions

View File

@ -119,14 +119,14 @@ export class CatalogEntityItem<T extends CatalogEntity> implements ItemObject {
try { try {
shouldRun = onRunHook(toJS(this.entity)); shouldRun = onRunHook(toJS(this.entity));
} catch (error) { } 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)) { if (isPromise(shouldRun)) {
Promise.resolve(shouldRun).then((shouldRun) => { Promise.resolve(shouldRun).then((shouldRun) => {
if (shouldRun) this.entity.onRun(ctx); if (shouldRun) this.entity.onRun(ctx);
}).catch((error) => { }).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) { } else if (shouldRun) {
this.entity.onRun(ctx); this.entity.onRun(ctx);

View File

@ -40,6 +40,8 @@ interface Props {
className?: IClassName; className?: IClassName;
} }
const isPromise = (obj: any): obj is Promise<any> => (obj?.then && typeof obj?.then === "function") ? true: false;
@observer @observer
export class HotbarMenu extends React.Component<Props> { export class HotbarMenu extends React.Component<Props> {
get hotbar() { get hotbar() {
@ -140,11 +142,23 @@ export class HotbarMenu extends React.Component<Props> {
} }
if (typeof onRunHook === "function") { if (typeof onRunHook === "function") {
// if onRunHook() returns a Promise, we wait for it to resolve let shouldRun;
// if not, just take whatever it returns
Promise.resolve(onRunHook(toJS(entity))).then((shouldRun) => { try {
if (shouldRun) entity.onRun(catalogEntityRunContext); 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 })} className={cssNames({ isDragging: snapshot.isDragging })}