From a6d309ab5af348f288f762cdcb6db8ef54a06528 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Mon, 27 Jun 2022 12:35:07 -0500 Subject: [PATCH] refactor: add asyncResolveArray.ts to resolve Awaitables easier --- src/handler/events/interactionCreate.ts | 10 ++-------- src/handler/events/messageEvent.ts | 3 ++- src/handler/utilities/asyncResolveArray.ts | 9 +++++++++ 3 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 src/handler/utilities/asyncResolveArray.ts diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index a1d53b0..dcbb907 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -21,7 +21,6 @@ import { isMessageComponent, isMessageCtxMenuCmd, isModalSubmit, - isPromise, isSelectMenu, isUserContextMenuCmd, } from '../utilities/predicates'; @@ -29,6 +28,7 @@ import { filterCorrectModule } from './observableHandling'; import { CommandType } from '../structures/enums'; import type { Result } from 'ts-results'; import type { AutocompleteInteraction } from 'discord.js'; +import { asyncResolveArray } from '../utilities/asyncResolveArray'; function applicationCommandHandler(mod: Module | undefined, interaction: CommandInteraction) { const mod$ = (cmdTy: T) => of(mod).pipe(filterCorrectModule(cmdTy)); @@ -211,13 +211,7 @@ export function onInteractionCreate(wrapper: Wrapper) { ) .subscribe({ async next({ mod, res: eventPluginRes, execute }) { - const ePlugArr: Result[] = []; - for await (const res of eventPluginRes) { - if (isPromise(res)) { - ePlugArr.push(res); - } - ePlugArr.push(res as Awaited>); - } + const ePlugArr: Result[] = await asyncResolveArray(eventPluginRes); if (ePlugArr.every(e => e.ok)) { await execute(); wrapper.sernEmitter?.emit('module.activate', { type: 'success', module: mod! }); diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index dd5c155..e921fad 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -8,6 +8,7 @@ import * as Files from '../utilities/readFile'; import { filterCorrectModule, ignoreNonBot } from './observableHandling'; import { CommandType } from '../structures/enums'; import { SernError } from '../structures/errors'; +import { asyncResolveArray } from '../utilities/asyncResolveArray'; export const onMessageCreate = (wrapper: Wrapper) => { const { client, defaultPrefix } = wrapper; @@ -40,7 +41,7 @@ export const onMessageCreate = (wrapper: Wrapper) => { const processEventPlugins$ = ensureModuleType$.pipe( concatMap(({ ctx, args, mod }) => { - const res = Promise.all( + const res = asyncResolveArray( mod.onEvent.map(ePlug => { return ePlug.execute([ctx, args], controller); }), diff --git a/src/handler/utilities/asyncResolveArray.ts b/src/handler/utilities/asyncResolveArray.ts new file mode 100644 index 0000000..fe64618 --- /dev/null +++ b/src/handler/utilities/asyncResolveArray.ts @@ -0,0 +1,9 @@ +import type { Awaitable } from 'discord.js'; + +export async function asyncResolveArray(promiseLike: Awaitable[]): Promise { + const arr: T[] = []; + for await (const el of promiseLike) { + arr.push(el); + } + return arr; +}