type alias

This commit is contained in:
Jacob Nguyen
2023-09-09 11:39:04 -05:00
parent 4591471e7e
commit c7ee2736b8
11 changed files with 30 additions and 27 deletions

View File

@@ -3,6 +3,7 @@ import type {
CommandModule,
CommandModuleDefs,
Module,
OnError,
} from '../../types/core-modules';
import { CommandType } from '../structures';
@@ -12,8 +13,8 @@ interface MetadataAccess {
}
interface OnErrorAccess {
getErrorCallback(m: Module): Record<string,Function>|undefined;
setErrorCallback(m: Module, c: Record<string,Function>): void;
getErrorCallback(m: Module): OnError;
setErrorCallback(m: Module, c: NonNullable<OnError>): void;
}
/**
* @since 2.0.0

View File

@@ -1,4 +1,4 @@
import type { CommandMeta, Module } from '../../types/core-modules';
import type { CommandMeta, Module, OnError } from '../../types/core-modules';
/**
* Represents a core module store that stores IDs mapped to file paths.
@@ -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, Record<string,Function>>;
onError: WeakMap<Module, NonNullable<OnError>>;
}

View File

@@ -5,7 +5,7 @@ import { basename, extname, join, resolve, parse } from 'path';
import assert from 'assert';
import { createRequire } from 'node:module';
import type { ImportPayload, Wrapper } from '../types/core';
import type { Module } from '../types/core-modules';
import type { Module, OnError } from '../types/core-modules';
export type ModuleResult<T> = Promise<ImportPayload<T>>;
@@ -37,7 +37,7 @@ export async function importModule<T>(absPath: string) {
.unwrapOr({ module: commandModule, onError }) as T;
}
interface FileExtras {
onError : Record<string, Function>|undefined
onError : OnError
}
export async function defaultModuleLoader<T extends Module>(absPath: string): ModuleResult<T> {

View File

@@ -1,4 +1,4 @@
import { CommandMeta, Module } from '../../types/core-modules';
import { CommandMeta, Module, OnError } from '../../types/core-modules';
import { CoreModuleStore } from '../contracts';
/*
@@ -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, Record<string, Function>>();
onError = new WeakMap<Module, NonNullable<OnError>>();
metadata = new WeakMap<Module, CommandMeta>();
commands = new Map<string, string>();
}

View File

@@ -1,7 +1,7 @@
import * as Id from '../../../core/id';
import { CoreModuleStore, ModuleManager } from '../../contracts';
import { Files } from '../../_internal';
import { CommandMeta, CommandModule, CommandModuleDefs, Module } from '../../../types/core-modules';
import { CommandMeta, CommandModule, CommandModuleDefs, Module, OnError } from '../../../types/core-modules';
import { CommandType } from '../enums';
/**
* @internal
@@ -12,10 +12,10 @@ export class DefaultModuleManager implements ModuleManager {
constructor(private moduleStore: CoreModuleStore) {}
getErrorCallback(m: Module): Record<string, Function> | undefined {
getErrorCallback(m: Module): OnError {
return this.moduleStore.onError.get(m);
}
setErrorCallback(m: Module, c: Record<string, Function>): void {
setErrorCallback(m: Module, c: NonNullable<OnError>): void {
this.moduleStore.onError.set(m, c);
}

View File

@@ -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 { CommandModule, Module, Processed } from '../types/core-modules';
import type { CommandModule, Module, OnError, 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: Record<string,Function>|undefined) {
function intoPayload(module: Processed<Module>, onError: OnError) {
return pipe(
arrayifySource,
map(args => ({ module, args, onError })),
@@ -41,7 +41,7 @@ function intoPayload(module: Processed<Module>, onError: Record<string,Function>
const createResult = createResultResolver<
Processed<Module>,
{ module: Processed<Module>; args: unknown[], onError: Record<string,Function>|undefined },
{ module: Processed<Module>; args: unknown[], onError: OnError },
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: Record<string,Function>|undefined, source: unknown) {
export function eventDispatcher(module: Processed<Module>, onError: OnError, 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: Record<strin
export function createDispatcher(payload: {
module: Processed<CommandModule>;
event: BaseInteraction;
onError: Record<string,Function>|undefined
onError: OnError
}) {
assert.ok(
CommandType.Text !== payload.module.type,

View File

@@ -30,7 +30,7 @@ import { SernEmitter } from '../core';
import { Err, Ok, Result } from 'ts-results-es';
import type { Awaitable } from '../types/utility';
import type { ControlPlugin } from '../types/core-plugin';
import type { AnyModule, CommandModule, Module, Processed } from '../types/core-modules';
import type { AnyModule, CommandModule, Module, OnError, Processed } from '../types/core-modules';
import type { ImportPayload } from '../types/core';
function createGenericHandler<Source, Narrowed extends Source, Output>(
@@ -175,7 +175,7 @@ export function executeModule(
*/
export function createResultResolver<
T extends { execute: (...args: any[]) => any; onEvent: ControlPlugin[] },
Args extends { module: T; onError: Record<string, Function>|undefined, [key: string]: unknown },
Args extends { module: T; onError: OnError, [key: string]: unknown },
Output,
>(config: {
onStop?: (module: T) => unknown;
@@ -225,7 +225,7 @@ export function makeModuleExecutor<
Args extends {
module: M;
args: unknown[];
onError: Record<string,Function>|undefined
onError: OnError
},
>(onStop: (m: M) => unknown) {
const onNext = ({ args, module, onError }: Args) => ({

View File

@@ -7,7 +7,7 @@ import { buildModules, callInitPlugins } from './_internal';
import * as assert from 'node:assert';
import * as util from 'node:util';
import type { DependencyList } from '../types/ioc';
import type { AnyModule, Processed } from '../types/core-modules';
import type { AnyModule, OnError, Processed } from '../types/core-modules';
export function startReadyEvent(
[sEmitter, , , moduleManager, client]: DependencyList,
@@ -30,7 +30,7 @@ const once = () => pipe(
ignoreElements()
)
const registerOnError = (manager: ModuleManager, module: Processed<AnyModule>, onError: Record<string,Function>|undefined) => {
const registerOnError = (manager: ModuleManager, module: Processed<AnyModule>, onError: OnError) => {
if(onError) {
manager.setErrorCallback(module, onError)
}

View File

@@ -4,14 +4,14 @@ 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 { EventModule, Processed } from '../types/core-modules';
import type { EventModule, OnError, Processed } from '../types/core-modules';
export function eventsHandler(
[emitter, err, log, moduleManager, client]: DependencyList,
allPaths: ObservableInput<string>,
) {
//code smell
const intoDispatcher = (e: { module: Processed<EventModule>, onError: Record<string, Function>|undefined }) => {
const intoDispatcher = (e: { module: Processed<EventModule>, onError: OnError }) => {
switch (e.module.type) {
case EventType.Sern:
return eventDispatcher(e.module, e.onError, emitter);

View File

@@ -15,11 +15,11 @@ import type {
UserContextMenuCommandInteraction,
UserSelectMenuInteraction,
} from 'discord.js';
import { CommandType, Context, ErrorHandling, EventType } from '../../src/core';
import { CommandType, Context, EventType } from '../../src/core';
import { AnyCommandPlugin, AnyEventPlugin, ControlPlugin, InitPlugin } from './core-plugin';
import { Awaitable, Args, SlashOptions, SernEventsMapping } from './utility';
import { Awaitable, Args, SlashOptions, SernEventsMapping, AnyFunction } from './utility';
export type OnError = (errorHandling: ErrorHandling, err: unknown) => unknown
export type OnError = Record<string, AnyFunction>|undefined
export interface CommandMeta {
fullPath: string;

View File

@@ -1,7 +1,9 @@
import { OnError } from "./core-modules";
export interface ImportPayload<T> {
module: T;
absPath: string;
onError: Record<string, Function>|undefined
onError: OnError
[key: string]: unknown;
}