refactor: add asyncResolveArray.ts to resolve Awaitables easier

This commit is contained in:
Jacob Nguyen
2022-06-27 12:35:07 -05:00
parent 1b447e3c4f
commit a6d309ab5a
3 changed files with 13 additions and 9 deletions

View File

@@ -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$ = <T extends CommandType>(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<void, void>[] = [];
for await (const res of eventPluginRes) {
if (isPromise(res)) {
ePlugArr.push(res);
}
ePlugArr.push(res as Awaited<Result<void, void>>);
}
const ePlugArr: Result<void, void>[] = await asyncResolveArray(eventPluginRes);
if (ePlugArr.every(e => e.ok)) {
await execute();
wrapper.sernEmitter?.emit('module.activate', { type: 'success', module: mod! });

View File

@@ -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);
}),

View File

@@ -0,0 +1,9 @@
import type { Awaitable } from 'discord.js';
export async function asyncResolveArray<T>(promiseLike: Awaitable<T>[]): Promise<T[]> {
const arr: T[] = [];
for await (const el of promiseLike) {
arr.push(el);
}
return arr;
}