mirror of
https://github.com/sern-handler/handler
synced 2026-06-27 10:12:14 +00:00
wiring onError callback through module loader and resolver
This commit is contained in:
@@ -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>(
|
||||
|
||||
@@ -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>;
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user