diff --git a/src/core/contracts/error-handling.ts b/src/core/contracts/error-handling.ts index c7a3950..dbe64f7 100644 --- a/src/core/contracts/error-handling.ts +++ b/src/core/contracts/error-handling.ts @@ -7,12 +7,6 @@ export interface ErrorHandling { */ keepAlive: number; - /** - * Utility function to crash - * @param error - */ - crash(error: Error): never; - /** * A function that is called on every crash. Updates keepAlive * @param error diff --git a/src/core/contracts/module-manager.ts b/src/core/contracts/module-manager.ts index e123ec0..7c7d9fe 100644 --- a/src/core/contracts/module-manager.ts +++ b/src/core/contracts/module-manager.ts @@ -11,5 +11,4 @@ export interface ModuleManager { set(id: string, path: string): void; getPublishableCommands(): Promise; getByNameCommandType(name: string, commandType: T): Promise|undefined; - remove(id: string): boolean; } diff --git a/src/core/operators.ts b/src/core/operators.ts index 8c289b8..1d08a5b 100644 --- a/src/core/operators.ts +++ b/src/core/operators.ts @@ -98,9 +98,6 @@ export function handleError(crashHandler: ErrorHandling, logging?: Logging) { return (pload: unknown, caught: Observable) => { // This is done to fit the ErrorHandling contract const err = pload instanceof Error ? pload : Error(util.inspect(pload, { colors: true })); - if (crashHandler.keepAlive == 0) { - crashHandler.crash(err); - } //formatted payload logging?.error({ message: util.inspect(pload) }); crashHandler.updateAlive(err); diff --git a/src/core/structures/services/error-handling.ts b/src/core/structures/services/error-handling.ts index 9827de2..88cfbbc 100644 --- a/src/core/structures/services/error-handling.ts +++ b/src/core/structures/services/error-handling.ts @@ -6,11 +6,13 @@ import { ErrorHandling } from '../../contracts'; * Version 4.0.0 will internalize this api. Please refrain from using ModuleStore! */ export class DefaultErrorHandling implements ErrorHandling { + keepAlive = 5; - crash(error: Error): never { - throw error; - } - updateAlive(_: Error) { + + updateAlive(err: Error) { this.keepAlive--; + if(this.keepAlive === 0) { + throw err; + } } } diff --git a/src/core/structures/services/module-manager.ts b/src/core/structures/services/module-manager.ts index 099394a..bbfbb07 100644 --- a/src/core/structures/services/module-manager.ts +++ b/src/core/structures/services/module-manager.ts @@ -1,4 +1,4 @@ -import { uniqueId } from '../../../handler/id'; +import { createId } from '../../../handler/id'; import { CoreModuleStore, ModuleManager } from '../../contracts'; import { importModule } from '../../module-loading'; import { CommandMeta, CommandModule, CommandModuleDefs, Module } from '../../types/modules'; @@ -12,7 +12,7 @@ export class DefaultModuleManager implements ModuleManager { constructor(private moduleStore: CoreModuleStore) {} getByNameCommandType(name: string, commandType: T) { - const id = this.get(`${name}_${uniqueId(commandType)}`); + const id = this.get(createId(name, commandType)); if(!id) { return undefined; } @@ -31,9 +31,6 @@ export class DefaultModuleManager implements ModuleManager { return maybeModule; } - remove(id: string): boolean { - throw new Error('Method not implemented.'); - } get(id: string) { return this.moduleStore.commands.get(id); diff --git a/src/handler/events/generic.ts b/src/handler/events/generic.ts index eca1d69..91920d1 100644 --- a/src/handler/events/generic.ts +++ b/src/handler/events/generic.ts @@ -27,7 +27,7 @@ import { fmt } from './messages'; import { ControlPlugin, VoidResult } from '../../core/types/plugins'; import { ImportPayload, Processed } from '../types'; import { Awaitable } from '../../shared'; -import { createId, uniqueId } from '../id'; +import { createId, reconstructId } from '../id'; function createGenericHandler( source: Observable, @@ -49,7 +49,7 @@ export function createInteractionHandler( return createGenericHandler>( source, event => { - const fullPath = mg.get(createId(event as unknown as Interaction)); + const fullPath = mg.get(reconstructId(event as unknown as Interaction)); if (!fullPath) return Err(SernError.UndefinedModule + ' No full path found in module store'); return defaultModuleLoader>(fullPath).then(res => @@ -89,7 +89,7 @@ function assignDefaults( moduleManager.setMetadata(module, { isClass: module.constructor.name === 'Function', fullPath: absPath, - id: `${module.name}_${uniqueId(module.type)}`, + id: createId(module.name, module.type), }); }); } diff --git a/src/handler/id.ts b/src/handler/id.ts index 041bbcb..d25b072 100644 --- a/src/handler/id.ts +++ b/src/handler/id.ts @@ -2,11 +2,11 @@ import { Interaction, InteractionType } from 'discord.js'; import { CommandType, EventType } from '../core'; /** - * Creates a unique ID for a given interaction object. + * Construct unique ID for a given interaction object. * @param event The interaction object for which to create an ID. * @returns A unique string ID based on the type and properties of the interaction object. */ -export function createId(event: T) { +export function reconstructId(event: T) { switch (event.type) { case InteractionType.MessageComponent: { return `${event.customId}_C${event.componentType}`; @@ -38,7 +38,12 @@ function apiType(t: CommandType | EventType) { * A is for any ApplicationCommand. C is for any ComponentCommand * Then, another number generated by apiType function is appended */ -export function uniqueId(t: CommandType | EventType) { +export function uniqueSuffix(t: CommandType | EventType) { const am = (appBitField & t) !== 0 ? 'A' : 'C'; return am + apiType(t); } + + +export function createId(name: string, type: CommandType | EventType) { + return name+"_"+uniqueSuffix(type) +} diff --git a/test/core/services.test.ts b/test/core/services.test.ts index 5fa4c11..a2dc741 100644 --- a/test/core/services.test.ts +++ b/test/core/services.test.ts @@ -3,7 +3,7 @@ import { CoreContainer } from '../../src/core/structures/container' import { DefaultLogging } from "../../src/core"; import { faker } from '@faker-js/faker' import { commandModule } from "../../src"; -import { uniqueId } from '../../src/handler/id' +import { createId } from '../../src/handler/id' import { CommandMeta } from "../../src/core/types/modules"; describe('services', () => { @@ -36,14 +36,14 @@ describe('services', () => { .map((path,i) => `${path}/${modules[i]}.js`); const metadata: CommandMeta[] = modules.map((cm, i) => ({ - id: cm.name+'_'+uniqueId(cm.type), + id: createId(cm.name, cm.type), isClass: false, fullPath: `${paths[i]}/${cm.name}.js` })); const moduleManager = container.get('@sern/modules'); let i =0; for(const m of modules) { - moduleManager.set(m.name+'_'+uniqueId(m.type), paths[i]); + moduleManager.set(createId(m.name,m.type), paths[i]); moduleManager.setMetadata(m, metadata[i]); i++ }