/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import { pipeline } from "@ogre-tools/fp"; import type { DiContainerForInjection, InjectionToken, } from "@ogre-tools/injectable"; import { filter, map } from "lodash/fp"; export interface Runnable { run: Run; runAfter?: this; } type Run = (parameter: Param) => Promise | void; export type RunMany = ( injectionToken: InjectionToken, void> ) => Run; export function runManyFor(di: DiContainerForInjection): RunMany { return (injectionToken) => async (parameter) => { const allRunnables = di.injectMany(injectionToken); const recursedRun = async ( runAfterRunnable: Runnable | undefined = undefined, ) => await pipeline( allRunnables, filter((runnable) => runnable.runAfter === runAfterRunnable), map(async (runnable) => { await runnable.run(parameter); await recursedRun(runnable); }), (promises) => Promise.all(promises), ); await recursedRun(); }; }