chore: move operators into core

This commit is contained in:
jacoobes
2023-04-14 12:59:13 -05:00
parent 09e57c73ac
commit 1a4e046f50
6 changed files with 14 additions and 14 deletions

View File

@@ -1,3 +1,3 @@
import SernEmitter from './sernEmitter'
export * from './operators'
export { SernEmitter };

82
src/core/operators.ts Normal file
View File

@@ -0,0 +1,82 @@
/**
* This file holds sern's rxjs operators used for processing data.
* Each function should be modular and testable, not bound to discord / sern
* and independent of each other
*/
import { concatMap, defaultIfEmpty, EMPTY, every, map, of, OperatorFunction, pipe } from 'rxjs';
import type { AnyModule } from '../types/module';
import { nameOrFilename } from './utilities/functions';
import type { PluginResult, VoidResult } from '../types/plugin';
import { guayin } from './plugins';
import { controller } from '../../sern';
import { Result } from 'ts-results-es';
import { ImportPayload } from '../types/handler';
/**
* if {src} is true, mapTo V, else ignore
* @param item
*/
export function filterMapTo<V>(item: () => V): OperatorFunction<boolean, V> {
return concatMap(shouldKeep => (shouldKeep ? of(item()) : EMPTY));
}
/**
* Calls any plugin with {args}.
* @param args if an array, its spread and plugin called.
*/
export function callPlugin(args: unknown): OperatorFunction<
{
execute: (...args: unknown[]) => PluginResult;
},
VoidResult
> {
return concatMap(async plugin => {
const isNewPlugin = Reflect.has(plugin, guayin);
if (isNewPlugin) {
if (Array.isArray(args)) {
return plugin.execute(...args);
}
return plugin.execute(args);
} else {
return plugin.execute(args, controller);
}
});
}
export const arrayifySource = map(src => (Array.isArray(src) ? (src as unknown[]) : [src]));
export const fillDefaults = <T extends AnyModule>({ module, absPath }: ImportPayload<T>) => {
return {
absPath,
module: {
name: nameOrFilename(module?.name, absPath),
description: module?.description ?? '...',
...module,
},
};
};
/**
* If the current value in Result stream is an error, calls callback.
* This also extracts the Ok value from Result
* @param cb
* @returns Observable<{ module: T; absPath: string }>
*/
export function errTap<Ok, Err>(cb: (err: Err) => void): OperatorFunction<Result<Ok, Err>, Ok> {
return concatMap(result => {
if (result.ok) {
return of(result.val);
} else {
cb(result.val as Err);
return EMPTY;
}
});
}
/**
* Checks if the stream of results is all ok.
*/
export const everyPluginOk: OperatorFunction<VoidResult, boolean> = pipe(
every(result => result.ok),
defaultIfEmpty(true),
);

View File

@@ -3,7 +3,6 @@ import type { Processed } from '../../types/handler';
import { ApplicationCommandType, ComponentType } from './enums';
/**
* @since 2.0.0
* Storing all command modules

View File

@@ -9,6 +9,7 @@ interface Wrapper {
readonly defaultPrefix?: string;
readonly commands: string;
readonly events?: string;
readonly strategy: PlatformStrategy;
readonly containerConfig: {
get: (...keys: (keyof Dependencies)[]) => unknown[];
};