mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
refactor: internalize operators
This commit is contained in:
@@ -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())
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user