feat: more event handling progress

This commit is contained in:
Jacob Nguyen
2022-06-17 14:54:18 -05:00
parent d42ab26417
commit c56dcc62f3
3 changed files with 26 additions and 3 deletions

View File

@@ -3,7 +3,7 @@ import { onReady } from './events/readyEvent';
import { onMessageCreate } from './events/messageEvent';
import { onInteractionCreate } from './events/interactionCreate';
import { Err, Ok } from 'ts-results';
import { buildData } from './utilities/readFile';
import { buildData, ExternalEventEmitters } from './utilities/readFile';
import type { EventModule } from './structures/module';
import { from, fromEvent, map, throwError } from 'rxjs';
import { match } from 'ts-pattern';
@@ -13,6 +13,7 @@ import { SernError } from './structures/errors';
import type { SpreadParams } from '../types/handler';
import * as Files from './utilities/readFile';
import { basename } from 'path';
import type { EventEmitter } from 'events';
export function init(wrapper: Wrapper) {
const { events } = wrapper;
@@ -24,6 +25,13 @@ export function init(wrapper: Wrapper) {
onInteractionCreate(wrapper);
}
export function addExternal<T extends EventEmitter>(emitter: T) {
if (ExternalEventEmitters.has(emitter.constructor.name)) {
throw Error(`${emitter.constructor.name} already exists!`);
}
ExternalEventEmitters.set(emitter.constructor.name, emitter);
}
function processEvents(wrapper: Wrapper, events: string | EventModule[] | (() => EventModule[])) {
const eventStream = eventObservable$(wrapper, events);
const processPlugins$ = eventStream.pipe(map(mod => mod)); //for now, until i figure out what to do with how plugins are registered
@@ -56,7 +64,16 @@ function processEvents(wrapper: Wrapper, events: string | EventModule[] | (() =>
m.execute as SpreadParams<typeof m.execute>,
),
)
.when(isExternalEvent, m => fromEvent(m.emitter, m.name!, m.execute))
.when(isExternalEvent, m => {
if (!ExternalEventEmitters.has(m.emitter)) {
throw Error(
SernError.UndefinedSernEmitter +
`Could not locate
a dependency ${m.emitter} to call this event listener`,
);
}
return fromEvent(ExternalEventEmitters.get(m.emitter)!, m.name!, m.execute);
})
.run();
}),
);

View File

@@ -5,5 +5,5 @@ export enum SernError {
NotSupportedInteraction = `This interaction is not supported.`,
PluginFailure = `A plugin failed to call controller.next()`,
MismatchEvent = `You cannot use message when an interaction fired or vice versa`,
UndefinedSernEmitter = `Could not find a Sern emitter`,
UndefinedSernEmitter = `Could not find an Emitter`,
}

View File

@@ -6,6 +6,7 @@ import type { Module } from '../structures/module';
import { SernError } from '../structures/errors';
import type { Result } from 'ts-results';
import { Err, Ok } from 'ts-results';
import type { EventEmitter } from 'events';
//Maybe move this? this probably doesnt belong in utlities/
export const BothCommands = new Map<string, Module>();
@@ -25,6 +26,11 @@ export const TextCommands = {
aliases: new Map<string, Module>(),
};
export const ModalSubmitCommands = new Map<string, Module>();
/**
* keeps all external emitters stored here
*/
export const ExternalEventEmitters = new Map<string, EventEmitter>();
// Courtesy @Townsy45
function readPath(dir: string, arrayOfFiles: string[] = []): string[] {
try {