mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
update onError to be record
This commit is contained in:
@@ -12,12 +12,12 @@ interface MetadataAccess {
|
||||
}
|
||||
|
||||
interface OnErrorAccess {
|
||||
getErrorCallback(m: Module): Function|undefined;
|
||||
setErrorCallback(m: Module, c: Function): void;
|
||||
getErrorCallback(m: Module): Record<string,Function>|undefined;
|
||||
setErrorCallback(m: Module, c: Record<string,Function>): void;
|
||||
}
|
||||
/**
|
||||
* @since 2.0.0
|
||||
* @deprecated - direct access to the module manager will be removed in version 4
|
||||
* @internal - direct access to the module manager will be removed in version 4
|
||||
*/
|
||||
export interface ModuleManager extends MetadataAccess, OnErrorAccess {
|
||||
get(id: string): string | undefined;
|
||||
|
||||
@@ -6,5 +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>;
|
||||
onError: WeakMap<Module, Record<string,Function>>;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ export async function importModule<T>(absPath: string) {
|
||||
.unwrapOr({ module: commandModule, onError }) as T;
|
||||
}
|
||||
interface FileExtras {
|
||||
onError : Function
|
||||
onError : Record<string, Function>|undefined
|
||||
}
|
||||
|
||||
export async function defaultModuleLoader<T extends Module>(absPath: string): ModuleResult<T> {
|
||||
|
||||
@@ -7,7 +7,7 @@ import { CoreModuleStore } from '../contracts';
|
||||
* For interacting with modules, use the ModuleManager instead.
|
||||
*/
|
||||
export class ModuleStore implements CoreModuleStore {
|
||||
onError = new WeakMap<Module, Function>();
|
||||
onError = new WeakMap<Module, Record<string, Function>>();
|
||||
metadata = new WeakMap<Module, CommandMeta>();
|
||||
commands = new Map<string, string>();
|
||||
}
|
||||
|
||||
@@ -11,10 +11,11 @@ import { CommandType } from '../enums';
|
||||
export class DefaultModuleManager implements ModuleManager {
|
||||
constructor(private moduleStore: CoreModuleStore) {}
|
||||
|
||||
getErrorCallback(m: Module): Function | undefined {
|
||||
|
||||
getErrorCallback(m: Module): Record<string, Function> | undefined {
|
||||
return this.moduleStore.onError.get(m);
|
||||
}
|
||||
setErrorCallback(m: Module, c: Function): void {
|
||||
setErrorCallback(m: Module, c: Record<string, Function>): void {
|
||||
this.moduleStore.onError.set(m, c);
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import { createResultResolver } from './event-utils';
|
||||
import { BaseInteraction, Message } from 'discord.js';
|
||||
import { CommandType, Context } from '../core';
|
||||
import type { Args } from '../types/utility';
|
||||
import type { BothCommand, CommandModule, Module, Processed } from '../types/core-modules';
|
||||
import type { CommandModule, Module, Processed } from '../types/core-modules';
|
||||
|
||||
//TODO: refactor dispatchers so that it implements a strategy for each different type of payload?
|
||||
export function dispatchMessage(module: Processed<CommandModule>, args: [Context, Args]) {
|
||||
@@ -32,7 +32,7 @@ function interactionArg<T extends BaseInteraction>(interaction: T) {
|
||||
return [interaction] as [T];
|
||||
}
|
||||
|
||||
function intoPayload(module: Processed<Module>, onError: Function|undefined) {
|
||||
function intoPayload(module: Processed<Module>, onError: Record<string,Function>|undefined) {
|
||||
return pipe(
|
||||
arrayifySource,
|
||||
map(args => ({ module, args, onError })),
|
||||
@@ -41,7 +41,7 @@ function intoPayload(module: Processed<Module>, onError: Function|undefined) {
|
||||
|
||||
const createResult = createResultResolver<
|
||||
Processed<Module>,
|
||||
{ module: Processed<Module>; args: unknown[], onError: Function|undefined },
|
||||
{ module: Processed<Module>; args: unknown[], onError: Record<string,Function>|undefined },
|
||||
unknown[]
|
||||
>({
|
||||
createStream: ({ module, args }) => from(module.onEvent).pipe(callPlugin(args)),
|
||||
@@ -52,7 +52,7 @@ const createResult = createResultResolver<
|
||||
* @param module
|
||||
* @param source
|
||||
*/
|
||||
export function eventDispatcher(module: Processed<Module>, onError: Function|undefined, source: unknown) {
|
||||
export function eventDispatcher(module: Processed<Module>, onError: Record<string,Function>|undefined, source: unknown) {
|
||||
assert.ok(source instanceof EventEmitter, `${source} is not an EventEmitter`);
|
||||
|
||||
const execute: OperatorFunction<unknown[], unknown> = concatMap(async args =>
|
||||
@@ -68,7 +68,7 @@ export function eventDispatcher(module: Processed<Module>, onError: Function|und
|
||||
export function createDispatcher(payload: {
|
||||
module: Processed<CommandModule>;
|
||||
event: BaseInteraction;
|
||||
onError: Function
|
||||
onError: Record<string,Function>|undefined
|
||||
}) {
|
||||
assert.ok(
|
||||
CommandType.Text !== payload.module.type,
|
||||
|
||||
@@ -78,7 +78,11 @@ export function createInteractionHandler<T extends Interaction>(
|
||||
return Files
|
||||
.defaultModuleLoader<Processed<CommandModule>>(fullPath)
|
||||
.then(payload =>
|
||||
Ok(createDispatcher({ module: payload.module, event, onError: payload.onError })));
|
||||
Ok(createDispatcher({
|
||||
module: payload.module,
|
||||
onError: payload.onError,
|
||||
event,
|
||||
})));
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -99,7 +103,7 @@ export function createMessageHandler(
|
||||
.defaultModuleLoader<Processed<CommandModule>>(fullPath)
|
||||
.then(payload => {
|
||||
const args = contextArgs(event, rest);
|
||||
return Ok(dispatchMessage(payload.module, args));
|
||||
return Ok({ args, ...payload });
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -171,7 +175,7 @@ export function executeModule(
|
||||
*/
|
||||
export function createResultResolver<
|
||||
T extends { execute: (...args: any[]) => any; onEvent: ControlPlugin[] },
|
||||
Args extends { module: T; onError: Function|undefined, [key: string]: unknown },
|
||||
Args extends { module: T; onError: Record<string, Function>|undefined, [key: string]: unknown },
|
||||
Output,
|
||||
>(config: {
|
||||
onStop?: (module: T) => unknown;
|
||||
@@ -221,7 +225,7 @@ export function makeModuleExecutor<
|
||||
Args extends {
|
||||
module: M;
|
||||
args: unknown[];
|
||||
onError: Function|undefined
|
||||
onError: Record<string,Function>|undefined
|
||||
},
|
||||
>(onStop: (m: M) => unknown) {
|
||||
const onNext = ({ args, module, onError }: Args) => ({
|
||||
|
||||
@@ -30,7 +30,7 @@ const once = () => pipe(
|
||||
ignoreElements()
|
||||
)
|
||||
|
||||
const registerOnError = (manager: ModuleManager, module: Processed<AnyModule>, onError: Function|undefined) => {
|
||||
const registerOnError = (manager: ModuleManager, module: Processed<AnyModule>, onError: Record<string,Function>|undefined) => {
|
||||
if(onError) {
|
||||
manager.setErrorCallback(module, onError)
|
||||
}
|
||||
|
||||
@@ -4,21 +4,21 @@ import { SernError } from '../core/_internal';
|
||||
import { buildModules, callInitPlugins, handleCrash, eventDispatcher } from './_internal';
|
||||
import { Service } from '../core/ioc';
|
||||
import type { DependencyList } from '../types/ioc';
|
||||
import type { CommandModule, EventModule, Processed } from '../types/core-modules';
|
||||
import type { EventModule, Processed } from '../types/core-modules';
|
||||
|
||||
export function eventsHandler(
|
||||
[emitter, err, log, moduleManager, client]: DependencyList,
|
||||
allPaths: ObservableInput<string>,
|
||||
) {
|
||||
//code smell
|
||||
const intoDispatcher = (e: Processed<EventModule | CommandModule>) => {
|
||||
switch (e.type) {
|
||||
const intoDispatcher = (e: { module: Processed<EventModule>, onError: Record<string, Function>|undefined }) => {
|
||||
switch (e.module.type) {
|
||||
case EventType.Sern:
|
||||
return eventDispatcher(e, emitter);
|
||||
return eventDispatcher(e.module, e.onError, emitter);
|
||||
case EventType.Discord:
|
||||
return eventDispatcher(e, client);
|
||||
return eventDispatcher(e.module, e.onError, client);
|
||||
case EventType.External:
|
||||
return eventDispatcher(e, Service(e.emitter));
|
||||
return eventDispatcher(e.module, e.onError, Service(e.module.emitter));
|
||||
default:
|
||||
throw Error(SernError.InvalidModuleType + ' while creating event handler');
|
||||
}
|
||||
@@ -31,7 +31,6 @@ export function eventsHandler(
|
||||
* Where all events are turned on
|
||||
*/
|
||||
mergeAll(),
|
||||
handleCrash(err, log),
|
||||
)
|
||||
handleCrash(err, log))
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
export interface ImportPayload<T> {
|
||||
module: T;
|
||||
absPath: string;
|
||||
onError: Function
|
||||
onError: Record<string, Function>|undefined
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user