From e2fd44516addc82a827a7447e38e402cc0661660 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Thu, 4 May 2023 18:42:02 -0500 Subject: [PATCH] refactor: internalize operators --- src/core/operators.ts | 19 +++++++++++++++---- src/index.ts | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/core/operators.ts b/src/core/operators.ts index 1368fcc..1e0c086 100644 --- a/src/core/operators.ts +++ b/src/core/operators.ts @@ -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(item: () => V): OperatorFunction { return concatMap(shouldKeep => (shouldKeep ? of(item()) : EMPTY)); } +export function filterMap(cb: (i: In) => Awaitable>): OperatorFunction { + 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 = (e: EventEmitter, eventName: string) => { return (fromEvent(e, eventName) as Observable).pipe(share()) }; - - diff --git a/src/index.ts b/src/index.ts index 91466ec..6a89557 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'