mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Convert run-many and run-many-sync to use composite as a builder
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
a06bd63701
commit
4f3b9cb326
@ -223,7 +223,7 @@ describe("runManyFor", () => {
|
||||
);
|
||||
|
||||
return expect(() => runMany()).rejects.toThrow(
|
||||
'Tried to run runnable "some-runnable-1" after the runnable "some-runnable-2" which does not share the "some-injection-token" injection token.',
|
||||
/Tried to get a composite but encountered missing parent ids: "some-runnable-2".\n\nAvailable parent ids are:\n"[0-9a-z-]+",\n"some-runnable-1"/,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@ -2,13 +2,10 @@
|
||||
* 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, forEach, map, tap } from "lodash/fp";
|
||||
import { throwWithIncorrectHierarchyFor } from "./throw-with-incorrect-hierarchy-for";
|
||||
import type { DiContainerForInjection, InjectionToken } from "@ogre-tools/injectable";
|
||||
import type { Composite } from "../utils/composite/get-composite/get-composite";
|
||||
import { getCompositeFor } from "../utils/composite/get-composite/get-composite";
|
||||
import * as uuid from "uuid";
|
||||
|
||||
export interface Runnable<TParameter = void> {
|
||||
id: string;
|
||||
@ -18,35 +15,34 @@ export interface Runnable<TParameter = void> {
|
||||
|
||||
type Run<Param> = (parameter: Param) => Promise<void> | void;
|
||||
|
||||
export type RunMany = <Param>(
|
||||
injectionToken: InjectionToken<Runnable<Param>, void>
|
||||
) => Run<Param>;
|
||||
export type RunMany = <Param>(injectionToken: InjectionToken<Runnable<Param>, void>) => Run<Param>;
|
||||
|
||||
async function runCompositeRunnables<Param>(param: Param, composite: Composite<Runnable<Param>>) {
|
||||
await composite.value.run(param);
|
||||
await Promise.all(composite.children.map(composite => runCompositeRunnables(param, composite)));
|
||||
}
|
||||
|
||||
export function runManyFor(di: DiContainerForInjection): RunMany {
|
||||
return (injectionToken) => async (parameter) => {
|
||||
return <Param>(injectionToken: InjectionToken<Runnable<Param>, void>) => async (param: Param) => {
|
||||
const allRunnables = di.injectMany(injectionToken);
|
||||
const rootId = uuid.v4();
|
||||
const getCompositeRunnables = getCompositeFor<Runnable<Param>>({
|
||||
getId: (runnable) => runnable.id,
|
||||
getParentId: (runnable) => (
|
||||
runnable.id === rootId
|
||||
? undefined
|
||||
: runnable.runAfter?.id ?? rootId
|
||||
),
|
||||
});
|
||||
const composite = getCompositeRunnables([
|
||||
// This is a dummy runnable to conform to the requirements of `getCompositeFor` to only have one root
|
||||
{
|
||||
id: rootId,
|
||||
run: () => {},
|
||||
},
|
||||
...allRunnables,
|
||||
]);
|
||||
|
||||
const throwWithIncorrectHierarchy = throwWithIncorrectHierarchyFor((injectionToken as any).id, allRunnables);
|
||||
|
||||
const recursedRun = async (
|
||||
runAfterRunnable: Runnable<any> | undefined = undefined,
|
||||
) =>
|
||||
await pipeline(
|
||||
allRunnables,
|
||||
|
||||
tap(runnables => forEach(throwWithIncorrectHierarchy, runnables)),
|
||||
|
||||
filter((runnable) => runnable.runAfter === runAfterRunnable),
|
||||
|
||||
map(async (runnable) => {
|
||||
await runnable.run(parameter);
|
||||
|
||||
await recursedRun(runnable);
|
||||
}),
|
||||
|
||||
(promises) => Promise.all(promises),
|
||||
);
|
||||
|
||||
await recursedRun();
|
||||
await runCompositeRunnables(param, composite);
|
||||
};
|
||||
}
|
||||
|
||||
@ -152,7 +152,7 @@ describe("runManySyncFor", () => {
|
||||
);
|
||||
|
||||
return expect(() => runMany()).rejects.toThrow(
|
||||
'Tried to run runnable "some-runnable-1" after the runnable "some-runnable-2" which does not share the "some-injection-token" injection token.',
|
||||
/Tried to get a composite but encountered missing parent ids: "some-runnable-2".\n\nAvailable parent ids are:\n"[0-9a-z-]+",\n"some-runnable-1"/,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@ -2,11 +2,10 @@
|
||||
* 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, forEach, map, tap } from "lodash/fp";
|
||||
import type { Runnable } from "./run-many-for";
|
||||
import { throwWithIncorrectHierarchyFor } from "./throw-with-incorrect-hierarchy-for";
|
||||
import type { Composite } from "../utils/composite/get-composite/get-composite";
|
||||
import { getCompositeFor } from "../utils/composite/get-composite/get-composite";
|
||||
import * as uuid from "uuid";
|
||||
|
||||
export interface RunnableSync<TParameter = void> {
|
||||
id: string;
|
||||
@ -16,33 +15,34 @@ export interface RunnableSync<TParameter = void> {
|
||||
|
||||
type RunSync<Param> = (parameter: Param) => void;
|
||||
|
||||
export type RunManySync = <Param>(
|
||||
injectionToken: InjectionToken<Runnable<Param>, void>
|
||||
) => RunSync<Param>;
|
||||
export type RunManySync = <Param>(injectionToken: InjectionToken<RunnableSync<Param>, void>) => RunSync<Param>;
|
||||
|
||||
function runCompositeRunnableSyncs<Param>(param: Param, composite: Composite<RunnableSync<Param>>) {
|
||||
composite.value.run(param);
|
||||
composite.children.map(composite => runCompositeRunnableSyncs(param, composite));
|
||||
}
|
||||
|
||||
export function runManySyncFor(di: DiContainerForInjection): RunManySync {
|
||||
return (injectionToken) => async (parameter) => {
|
||||
return <Param>(injectionToken: InjectionToken<RunnableSync<Param>, void>) => async (param: Param) => {
|
||||
const allRunnables = di.injectMany(injectionToken);
|
||||
const rootId = uuid.v4();
|
||||
const getCompositeRunnables = getCompositeFor<RunnableSync<Param>>({
|
||||
getId: (runnable) => runnable.id,
|
||||
getParentId: (runnable) => (
|
||||
runnable.id === rootId
|
||||
? undefined
|
||||
: runnable.runAfter?.id ?? rootId
|
||||
),
|
||||
});
|
||||
const composite = getCompositeRunnables([
|
||||
// This is a dummy runnable to conform to the requirements of `getCompositeFor` to only have one root
|
||||
{
|
||||
id: rootId,
|
||||
run: () => {},
|
||||
},
|
||||
...allRunnables,
|
||||
]);
|
||||
|
||||
const throwWithIncorrectHierarchy = throwWithIncorrectHierarchyFor((injectionToken as any).id, allRunnables);
|
||||
|
||||
const recursedRun = (
|
||||
runAfterRunnable: RunnableSync<any> | undefined = undefined,
|
||||
) =>
|
||||
pipeline(
|
||||
allRunnables,
|
||||
|
||||
tap(runnables => forEach(throwWithIncorrectHierarchy, runnables)),
|
||||
|
||||
filter((runnable) => runnable.runAfter === runAfterRunnable),
|
||||
|
||||
map((runnable) => {
|
||||
runnable.run(parameter);
|
||||
|
||||
recursedRun(runnable);
|
||||
}),
|
||||
);
|
||||
|
||||
recursedRun();
|
||||
runCompositeRunnableSyncs(param, composite);
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user