refactor: remove cast

This commit is contained in:
Jacob Nguyen
2023-05-18 21:36:41 -05:00
parent 08e358b32d
commit ea78a98f69
3 changed files with 20 additions and 5 deletions

View File

@@ -54,7 +54,7 @@ export interface SernEventCommand<T extends keyof SernEventsMapping = keyof Sern
}
export interface ExternalEventCommand extends Module {
name?: string;
emitter: string;
emitter: keyof Dependencies;
type: EventType.External;
execute(...args: unknown[]): Awaitable<unknown>;
}

View File

@@ -9,10 +9,12 @@ import {
throwError,
tap,
MonoTypeOperatorFunction,
catchError,
finalize,
} from 'rxjs';
import { ModuleManager } from '../../core';
import { ErrorHandling, Logging, ModuleManager, useContainerRaw } from '../../core';
import { SernError } from '../../core/structures/errors';
import { callPlugin, everyPluginOk, filterMap, filterMapTo } from '../../core/operators';
import { callPlugin, everyPluginOk, filterMap, filterMapTo, handleError } from '../../core/operators';
import { defaultModuleLoader } from '../../core/module-loading';
import { CommandModule, Module, AnyModule } from '../../core/types/modules';
import { contextArgs, createDispatcher, dispatchMessage } from './dispatchers';
@@ -214,3 +216,17 @@ export function makeModuleExecutor<
}),
);
}
export function handleCrash(
errorHandler: ErrorHandling,
logger?: Logging,
) {
return pipe(
catchError(handleError(errorHandler, logger)),
finalize(() => {
logger?.info({ message: 'A stream closed or reached end of lifetime' });
useContainerRaw()?.disposeAll()
.then(() => logger?.info({ message: 'Cleaning container and crashing' }));
})
);
}

View File

@@ -8,7 +8,6 @@ import { buildModules, callInitPlugins } from './generic';
import { handleError } from '../../core/operators';
import { Service, useContainerRaw } from '../../core/ioc';
import { DependencyList, Processed } from '../types';
import { Dependencies } from '../../core/ioc/types';
export function makeEventsHandler(
[emitter, err, log, moduleManager, client]: DependencyList,
@@ -22,7 +21,7 @@ export function makeEventsHandler(
case EventType.Discord:
return eventDispatcher(e, client);
case EventType.External:
return eventDispatcher(e, Service(e.emitter as keyof Dependencies));
return eventDispatcher(e, Service(e.emitter));
default:
return err.crash(
Error(SernError.InvalidModuleType + ' while creating event handler'),