wiring onError callback through module loader and resolver

This commit is contained in:
Jacob Nguyen
2023-09-08 23:11:46 -05:00
parent 59e7927816
commit cafca503f9
8 changed files with 73 additions and 61 deletions

View File

@@ -6,13 +6,22 @@ import type {
} from '../../types/core-modules';
import { CommandType } from '../structures';
/**
* @since 2.0.0
*/
export interface ModuleManager {
get(id: string): string | undefined;
interface MetadataAccess {
getMetadata(m: Module): CommandMeta | undefined;
setMetadata(m: Module, c: CommandMeta): void;
}
interface OnErrorAccess {
getErrorCallback(m: Module): Function|undefined;
setErrorCallback(m: Module, c: Function): void;
}
/**
* @since 2.0.0
* @deprecated - direct access to the module manager will be removed in version 4
*/
export interface ModuleManager extends MetadataAccess, OnErrorAccess {
get(id: string): string | undefined;
set(id: string, path: string): void;
getPublishableCommands(): Promise<CommandModule[]>;
getByNameCommandType<T extends CommandType>(

View File

@@ -6,4 +6,5 @@ import type { CommandMeta, Module } from '../../types/core-modules';
export interface CoreModuleStore {
commands: Map<string, string>;
metadata: WeakMap<Module, CommandMeta>;
onError: WeakMap<Module, Function>;
}

View File

@@ -36,15 +36,14 @@ export async function importModule<T>(absPath: string) {
.wrap(() => ({ module: commandModule.getInstance(), onError }))
.unwrapOr({ module: commandModule, onError }) as T;
}
interface FileModuleImports<T extends Module> {
module: T,
interface FileExtras {
onError : Function
}
export async function defaultModuleLoader<T extends Module>(absPath: string): ModuleResult<T> {
let module = await importModule<T>(absPath);
let { onError, module } = await importModule<{ module: T } & FileExtras>(absPath);
assert(module, `Found an undefined module: ${absPath}`);
return { module, absPath, errorHandler: '' };
return { module, absPath, onError };
}
export const fmtFileName = (fileName: string) => parse(fileName).name;
@@ -58,7 +57,8 @@ export const fmtFileName = (fileName: string) => parse(fileName).name;
export function buildModuleStream<T extends Module>(
input: ObservableInput<string>,
): Observable<ImportPayload<T>> {
return from(input).pipe(mergeMap(defaultModuleLoader<T>));
return from(input)
.pipe(mergeMap(defaultModuleLoader<T>));
}
export const getFullPathTree = (dir: string) => readPaths(resolve(dir));