From c7ee2736b8cbe650cffb88e10cdadc6e12d5657e Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sat, 9 Sep 2023 11:39:04 -0500 Subject: [PATCH] type alias --- src/core/contracts/module-manager.ts | 5 +++-- src/core/contracts/module-store.ts | 4 ++-- src/core/module-loading.ts | 4 ++-- src/core/structures/module-store.ts | 4 ++-- src/core/structures/services/module-manager.ts | 6 +++--- src/handlers/dispatchers.ts | 10 +++++----- src/handlers/event-utils.ts | 6 +++--- src/handlers/ready-event.ts | 4 ++-- src/handlers/user-defined-events.ts | 4 ++-- src/types/core-modules.ts | 6 +++--- src/types/core.ts | 4 +++- 11 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/core/contracts/module-manager.ts b/src/core/contracts/module-manager.ts index d009554..ec9eac0 100644 --- a/src/core/contracts/module-manager.ts +++ b/src/core/contracts/module-manager.ts @@ -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|undefined; - setErrorCallback(m: Module, c: Record): void; + getErrorCallback(m: Module): OnError; + setErrorCallback(m: Module, c: NonNullable): void; } /** * @since 2.0.0 diff --git a/src/core/contracts/module-store.ts b/src/core/contracts/module-store.ts index 0e6015d..edcb91f 100644 --- a/src/core/contracts/module-store.ts +++ b/src/core/contracts/module-store.ts @@ -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; metadata: WeakMap; - onError: WeakMap>; + onError: WeakMap>; } diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index fe39b98..1cad53a 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -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 = Promise>; @@ -37,7 +37,7 @@ export async function importModule(absPath: string) { .unwrapOr({ module: commandModule, onError }) as T; } interface FileExtras { - onError : Record|undefined + onError : OnError } export async function defaultModuleLoader(absPath: string): ModuleResult { diff --git a/src/core/structures/module-store.ts b/src/core/structures/module-store.ts index 4e00bd3..0912ef4 100644 --- a/src/core/structures/module-store.ts +++ b/src/core/structures/module-store.ts @@ -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>(); + onError = new WeakMap>(); metadata = new WeakMap(); commands = new Map(); } diff --git a/src/core/structures/services/module-manager.ts b/src/core/structures/services/module-manager.ts index 96a7f71..4265d72 100644 --- a/src/core/structures/services/module-manager.ts +++ b/src/core/structures/services/module-manager.ts @@ -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 | undefined { + getErrorCallback(m: Module): OnError { return this.moduleStore.onError.get(m); } - setErrorCallback(m: Module, c: Record): void { + setErrorCallback(m: Module, c: NonNullable): void { this.moduleStore.onError.set(m, c); } diff --git a/src/handlers/dispatchers.ts b/src/handlers/dispatchers.ts index 4252c55..4d5e25a 100644 --- a/src/handlers/dispatchers.ts +++ b/src/handlers/dispatchers.ts @@ -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, args: [Context, Args]) { @@ -32,7 +32,7 @@ function interactionArg(interaction: T) { return [interaction] as [T]; } -function intoPayload(module: Processed, onError: Record|undefined) { +function intoPayload(module: Processed, onError: OnError) { return pipe( arrayifySource, map(args => ({ module, args, onError })), @@ -41,7 +41,7 @@ function intoPayload(module: Processed, onError: Record const createResult = createResultResolver< Processed, - { module: Processed; args: unknown[], onError: Record|undefined }, + { module: Processed; 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, onError: Record|undefined, source: unknown) { +export function eventDispatcher(module: Processed, onError: OnError, source: unknown) { assert.ok(source instanceof EventEmitter, `${source} is not an EventEmitter`); const execute: OperatorFunction = concatMap(async args => @@ -68,7 +68,7 @@ export function eventDispatcher(module: Processed, onError: Record; event: BaseInteraction; - onError: Record|undefined + onError: OnError }) { assert.ok( CommandType.Text !== payload.module.type, diff --git a/src/handlers/event-utils.ts b/src/handlers/event-utils.ts index d742b63..2238aef 100644 --- a/src/handlers/event-utils.ts +++ b/src/handlers/event-utils.ts @@ -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( @@ -175,7 +175,7 @@ export function executeModule( */ export function createResultResolver< T extends { execute: (...args: any[]) => any; onEvent: ControlPlugin[] }, - Args extends { module: T; onError: Record|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|undefined + onError: OnError }, >(onStop: (m: M) => unknown) { const onNext = ({ args, module, onError }: Args) => ({ diff --git a/src/handlers/ready-event.ts b/src/handlers/ready-event.ts index 4cce6a4..f7c8943 100644 --- a/src/handlers/ready-event.ts +++ b/src/handlers/ready-event.ts @@ -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, onError: Record|undefined) => { +const registerOnError = (manager: ModuleManager, module: Processed, onError: OnError) => { if(onError) { manager.setErrorCallback(module, onError) } diff --git a/src/handlers/user-defined-events.ts b/src/handlers/user-defined-events.ts index 96ab46f..88daecf 100644 --- a/src/handlers/user-defined-events.ts +++ b/src/handlers/user-defined-events.ts @@ -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, ) { //code smell - const intoDispatcher = (e: { module: Processed, onError: Record|undefined }) => { + const intoDispatcher = (e: { module: Processed, onError: OnError }) => { switch (e.module.type) { case EventType.Sern: return eventDispatcher(e.module, e.onError, emitter); diff --git a/src/types/core-modules.ts b/src/types/core-modules.ts index d63f8a3..f098eca 100644 --- a/src/types/core-modules.ts +++ b/src/types/core-modules.ts @@ -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|undefined export interface CommandMeta { fullPath: string; diff --git a/src/types/core.ts b/src/types/core.ts index ccc8603..e86d68a 100644 --- a/src/types/core.ts +++ b/src/types/core.ts @@ -1,7 +1,9 @@ +import { OnError } from "./core-modules"; + export interface ImportPayload { module: T; absPath: string; - onError: Record|undefined + onError: OnError [key: string]: unknown; }