feat: change typings of sern emitter

This commit is contained in:
Jacob Nguyen
2022-06-13 01:18:23 -05:00
parent 120c527b34
commit 0fc0782e55
5 changed files with 28 additions and 8 deletions

View File

@@ -216,10 +216,10 @@ export function onInteractionCreate(wrapper: Wrapper) {
}
if (ePlugArr.every(e => e.ok)) {
await execute();
wrapper.sernEmitter?.emit('module.activate', { success: true, module: mod! });
wrapper.sernEmitter?.emit('module.activate', { type: 'success', module: mod! });
} else {
wrapper.sernEmitter?.emit('module.activate', {
success: false,
type: 'failure',
module: mod!,
reason: SernError.PluginFailure,
});

View File

@@ -53,11 +53,11 @@ export const onMessageCreate = (wrapper: Wrapper) => {
next({ mod, ctx, args, res }) {
if (res.every(pl => pl.ok)) {
Promise.resolve(mod.execute(ctx, args)).then(() => {
wrapper.sernEmitter?.emit('module.activate', { success: true, module: mod! });
wrapper.sernEmitter?.emit('module.activate', { type: 'success', module: mod! });
});
} else {
wrapper.sernEmitter?.emit('module.activate', {
success: false,
type: 'failure',
module: mod!,
reason: SernError.PluginFailure,
});

View File

@@ -3,6 +3,7 @@ import { Observable, throwError } from 'rxjs';
import { SernError } from '../structures/errors';
import type { Module, ModuleDefs } from '../structures/module';
import { correctModuleType } from '../utilities/predicates';
import type { Result } from 'ts-results';
export function filterCorrectModule<T extends keyof ModuleDefs>(cmdType: T) {
return (src: Observable<Module | undefined>) =>
new Observable<ModuleDefs[T]>(subscriber => {
@@ -42,3 +43,24 @@ export function ignoreNonBot(prefix: string) {
});
});
}
/**
* If the current value in Result stream is an error, calls callback.
* @param cb
*/
export function errTap(cb: (err: SernError) => void) {
return (src: Observable<Result<{ mod: Module; absPath: string }, SernError>>) =>
new Observable<{ mod: Module; absPath: string }>(subscriber => {
return src.subscribe({
next(value) {
if (value.err) {
cb(value.val);
} else {
subscriber.next(value.val);
}
},
error: e => subscriber.error(e),
complete: () => subscriber.complete(),
});
});
}

View File

@@ -23,7 +23,6 @@ import { match } from 'ts-pattern';
import { SernError } from '../structures/errors';
import type { DefinitelyDefined } from '../../types/handler';
import { CommandType, PluginType } from '../structures/enums';
import { elseMap, elseMapTo, resultMap } from 'ts-results/rxjs-operators';
import { errTap } from './observableHandling';
export const onReady = (wrapper: Wrapper) => {

View File

@@ -2,9 +2,8 @@ import { EventEmitter } from 'events';
import type { Module } from './structures/module';
type Payload =
| { success: true; module: Module }
| { success: false; module: Module | undefined; reason: string | Error };
| { type: 'success'; module: Module }
| { type: 'failure'; module: Module | undefined; reason: string | Error };
export type SernEventsMapping = {
['module.register']: [Payload];
['module.activate']: [Payload];