From 7c53f6e2d90a6c977e200feb038ef290d7bbaa6a Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 7 Nov 2022 11:12:10 -0500 Subject: [PATCH] Convert runManySyncFor to use composite and to enfore sync-ness Signed-off-by: Sebastian Malton --- src/common/runnable/run-many-sync-for.test.ts | 16 ++--- src/common/runnable/run-many-sync-for.ts | 65 ++++++++++--------- 2 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/common/runnable/run-many-sync-for.test.ts b/src/common/runnable/run-many-sync-for.test.ts index 43d2832663..3aadcad0bf 100644 --- a/src/common/runnable/run-many-sync-for.test.ts +++ b/src/common/runnable/run-many-sync-for.test.ts @@ -69,7 +69,7 @@ describe("runManySyncFor", () => { instantiate: (di) => ({ id: "some-injectable-1", - run: () => runMock("third-level-run"), + run: () => void runMock("third-level-run"), runAfter: di.inject(someInjectable2), }), @@ -81,7 +81,7 @@ describe("runManySyncFor", () => { instantiate: (di) => ({ id: "some-injectable-2", - run: () => runMock("second-level-run"), + run: () => void runMock("second-level-run"), runAfter: di.inject(someInjectable3), }), @@ -92,7 +92,7 @@ describe("runManySyncFor", () => { id: "some-injectable-3", instantiate: () => ({ id: "some-injectable-3", - run: () => runMock("first-level-run"), + run: () => void runMock("first-level-run"), }), injectionToken: someInjectionTokenForRunnables, }); @@ -151,13 +151,13 @@ describe("runManySyncFor", () => { someInjectionToken, ); - 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.', + return expect(() => runMany()).toThrow( + /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"/, ); }); describe("when running many with parameter", () => { - let runMock: jest.Mock<(arg: string, arg2: string) => void>; + let runMock: jest.Mock<(arg: string, arg2: string) => undefined>; beforeEach(() => { const rootDi = createContainer("irrelevant"); @@ -175,7 +175,7 @@ describe("runManySyncFor", () => { instantiate: () => ({ id: "some-runnable-1", - run: (parameter) => runMock("run-of-some-runnable-1", parameter), + run: (parameter) => void runMock("run-of-some-runnable-1", parameter), }), injectionToken: someInjectionTokenForRunnablesWithParameter, @@ -186,7 +186,7 @@ describe("runManySyncFor", () => { instantiate: () => ({ id: "some-runnable-2", - run: (parameter) => runMock("run-of-some-runnable-2", parameter), + run: (parameter) => void runMock("run-of-some-runnable-2", parameter), }), injectionToken: someInjectionTokenForRunnablesWithParameter, diff --git a/src/common/runnable/run-many-sync-for.ts b/src/common/runnable/run-many-sync-for.ts index 1d3dcec2c5..08dba2f72d 100644 --- a/src/common/runnable/run-many-sync-for.ts +++ b/src/common/runnable/run-many-sync-for.ts @@ -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 { id: string; @@ -14,35 +13,43 @@ export interface RunnableSync { runAfter?: RunnableSync; } -type RunSync = (parameter: Param) => void; +/** + * NOTE: this is the worse of two evils. This makes sure that `RunnableSync` always is sync. + * If the return type is `void` instead then async functions (those return `Promise`) can + * coerce to it. + */ +type RunSync = (parameter: Param) => undefined; -export type RunManySync = ( - injectionToken: InjectionToken, void> -) => RunSync; +export type RunManySync = (injectionToken: InjectionToken, void>) => RunSync; + +function runCompositeRunnableSyncs(param: Param, composite: Composite>): undefined { + composite.value.run(param); + composite.children.map(composite => runCompositeRunnableSyncs(param, composite)); + + return undefined; +} export function runManySyncFor(di: DiContainerForInjection): RunManySync { - return (injectionToken) => async (parameter) => { + return (injectionToken: InjectionToken, void>) => (param: Param): undefined => { const allRunnables = di.injectMany(injectionToken); + const rootId = uuid.v4(); + const getCompositeRunnables = getCompositeFor>({ + 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: () => undefined, + }, + ...allRunnables, + ]); - const throwWithIncorrectHierarchy = throwWithIncorrectHierarchyFor((injectionToken as any).id, allRunnables); - - const recursedRun = ( - runAfterRunnable: RunnableSync | undefined = undefined, - ) => - pipeline( - allRunnables, - - tap(runnables => forEach(throwWithIncorrectHierarchy, runnables)), - - filter((runnable) => runnable.runAfter === runAfterRunnable), - - map((runnable) => { - runnable.run(parameter); - - recursedRun(runnable); - }), - ); - - recursedRun(); + return runCompositeRunnableSyncs(param, composite); }; }