Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jun 10, 2022
1 parent 7d267b0 commit e8973be
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export type AllHooks = {
export type HookName = keyof AllHooks & string;

export interface MeshPubSub {
publish<THook extends HookName>(triggerName: THook, payload: AllHooks[THook]): Promise<void>;
publish<THook extends HookName>(triggerName: THook, payload: AllHooks[THook]): void;
subscribe<THook extends HookName>(
triggerName: THook,
onMessage: (data: AllHooks[THook]) => void,
options?: any
): Promise<number>;
): number;
unsubscribe(subId: number): void;
asyncIterator<THook extends HookName>(triggers: THook): AsyncIterable<AllHooks[THook]>;
}
Expand Down
15 changes: 8 additions & 7 deletions packages/utils/src/load-from-module-export-expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type LoadFromModuleExportExpressionOptions = {
importFn: ImportFn;
};

export function loadFromModuleExportExpression<T>(
export async function loadFromModuleExportExpression<T>(
expression: T | string,
options: LoadFromModuleExportExpressionOptions
): Promise<T> {
Expand All @@ -19,16 +19,17 @@ export function loadFromModuleExportExpression<T>(

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);
}
});
}
}
8 changes: 4 additions & 4 deletions packages/utils/src/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export class PubSub implements MeshPubSub {
private listenerEventMap = new Map<Listener, HookName>();
private eventNameListenersMap = new Map<HookName, Set<Listener>>();

async publish<THook extends HookName>(triggerName: THook, detail: AllHooks[THook]): Promise<void> {
publish<THook extends HookName>(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<THook extends HookName>(triggerName: THook, onMessage: Listener<THook>): Promise<number> {
subscribe<THook extends HookName>(triggerName: THook, onMessage: Listener<THook>): number {
let eventNameListeners = this.eventNameListenersMap.get(triggerName);
if (!eventNameListeners) {
eventNameListeners = new Set();
Expand Down Expand Up @@ -46,9 +46,9 @@ export class PubSub implements MeshPubSub {
asyncIterator<THook extends HookName>(triggerName: THook): AsyncIterable<AllHooks[THook]> {
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),
};
},
});
Expand Down

0 comments on commit e8973be

Please sign in to comment.