refactor: internalize operators

This commit is contained in:
Jacob Nguyen
2023-05-04 18:42:02 -05:00
parent 4125508cd3
commit e2fd44516a
2 changed files with 16 additions and 5 deletions

View File

@@ -4,12 +4,13 @@
* and independent of each other
*/
import { concatMap, defaultIfEmpty, EMPTY, every, fromEvent, map, Observable, of, OperatorFunction, pipe, share } from 'rxjs';
import { concatMap, defaultIfEmpty, EMPTY, every, fromEvent, map, Observable, of, OperatorFunction, pipe, share, switchMap } from 'rxjs';
import type { AnyModule } from '../types/module';
import type { PluginResult, VoidResult } from '../types/plugin';
import { Result } from 'ts-results-es';
import { ImportPayload, Processed } from '../types/handler';
import { Awaitable, ImportPayload, Processed } from '../types/handler';
import { EventEmitter } from 'node:events';
/**
* if {src} is true, mapTo V, else ignore
* @param item
@@ -18,6 +19,18 @@ export function filterMapTo<V>(item: () => V): OperatorFunction<boolean, V> {
return concatMap(shouldKeep => (shouldKeep ? of(item()) : EMPTY));
}
export function filterMap<In, Out>(cb: (i: In) => Awaitable<Result<Out, unknown>>): OperatorFunction<In, Out> {
return pipe(
switchMap(async input => cb(input)),
concatMap(s => {
if(s.ok) {
return of(s.val)
}
return EMPTY;
})
)
}
/**
* Calls any plugin with {args}.
* @param args if an array, its spread and plugin called.
@@ -75,5 +88,3 @@ export const sharedObservable = <T>(e: EventEmitter, eventName: string) => {
return (fromEvent(e, eventName) as Observable<T>).pipe(share())
};

View File

@@ -5,5 +5,5 @@ export * from './types/module';
export * from './types/plugin';
export * from './core'
export { controller } from './handler/sern'
export { commandModule, eventModule, CommandExecutable, EventExecutable } from './commands'
export { commandModule, eventModule, discordEvent } from './commands'
export { default as Context } from './classic/context'