diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 02e259e6d5acb..1f936be9c5c27 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -48,12 +48,12 @@ export type AllHooks = { export type HookName = keyof AllHooks & string; export interface MeshPubSub { - publish(triggerName: THook, payload: AllHooks[THook]): Promise; + publish(triggerName: THook, payload: AllHooks[THook]): void; subscribe( triggerName: THook, onMessage: (data: AllHooks[THook]) => void, options?: any - ): Promise; + ): number; unsubscribe(subId: number): void; asyncIterator(triggers: THook): AsyncIterable; } diff --git a/packages/utils/src/load-from-module-export-expression.ts b/packages/utils/src/load-from-module-export-expression.ts index df33ce86b5342..6e065b5a7634f 100644 --- a/packages/utils/src/load-from-module-export-expression.ts +++ b/packages/utils/src/load-from-module-export-expression.ts @@ -9,7 +9,7 @@ type LoadFromModuleExportExpressionOptions = { importFn: ImportFn; }; -export function loadFromModuleExportExpression( +export async function loadFromModuleExportExpression( expression: T | string, options: LoadFromModuleExportExpressionOptions ): Promise { @@ -19,16 +19,17 @@ export function loadFromModuleExportExpression( const { defaultExportName, cwd, importFn = defaultImportFn } = options || {}; const [modulePath, exportName = defaultExportName] = expression.split('#'); - return tryImport(modulePath, cwd, importFn).then( - mod => mod[exportName] || (mod.default && mod.default[exportName]) || mod.default || mod - ); + const mod = await tryImport(modulePath, cwd, importFn); + return mod[exportName] || (mod.default && mod.default[exportName]) || mod.default || mod; } -function tryImport(modulePath: string, cwd: string, importFn: ImportFn) { - return importFn(modulePath).catch((e1: Error): any => { +async function tryImport(modulePath: string, cwd: string, importFn: ImportFn) { + try { + return await importFn(modulePath); + } catch { if (!path.isAbsolute(modulePath)) { const absoluteModulePath = path.isAbsolute(modulePath) ? modulePath : path.join(cwd, modulePath); return importFn(absoluteModulePath); } - }); + } } diff --git a/packages/utils/src/pubsub.ts b/packages/utils/src/pubsub.ts index 5fa05153ed9da..1b5278c72a5ad 100644 --- a/packages/utils/src/pubsub.ts +++ b/packages/utils/src/pubsub.ts @@ -8,14 +8,14 @@ export class PubSub implements MeshPubSub { private listenerEventMap = new Map(); private eventNameListenersMap = new Map>(); - async publish(triggerName: THook, detail: AllHooks[THook]): Promise { + publish(triggerName: THook, detail: AllHooks[THook]): void { const eventNameListeners = this.eventNameListenersMap.get(triggerName); if (eventNameListeners) { Promise.allSettled([...eventNameListeners].map(listener => listener(detail))).catch(e => console.error(e)); } } - async subscribe(triggerName: THook, onMessage: Listener): Promise { + subscribe(triggerName: THook, onMessage: Listener): number { let eventNameListeners = this.eventNameListenersMap.get(triggerName); if (!eventNameListeners) { eventNameListeners = new Set(); @@ -46,9 +46,9 @@ export class PubSub implements MeshPubSub { asyncIterator(triggerName: THook): AsyncIterable { return observableToAsyncIterable({ subscribe: observer => { - const subId$ = this.subscribe(triggerName, data => observer.next(data)); + const subId = this.subscribe(triggerName, data => observer.next(data)); return { - unsubscribe: () => subId$.then(subId => this.unsubscribe(subId)), + unsubscribe: () => this.unsubscribe(subId), }; }, });