diff --git a/src/common/runnable/run-many-for.ts b/src/common/runnable/run-many-for.ts index bda6edb9d1..4087faf618 100644 --- a/src/common/runnable/run-many-for.ts +++ b/src/common/runnable/run-many-for.ts @@ -4,7 +4,7 @@ */ import type { DiContainerForInjection, InjectionToken } from "@ogre-tools/injectable"; import type { SingleOrMany } from "../utils"; -import { getOrInsertSet, isDefined } from "../utils"; +import { getOrInsertSetFor, isDefined } from "../utils"; import { observable, when } from "mobx"; import * as uuid from "uuid"; import assert from "assert"; @@ -38,23 +38,24 @@ const verifyRunnablesAreDAG = (injectionToken: InjectionToken>(); const seenIds = new Set(); + const addRunnableId = getOrInsertSetFor(runnableGraph); // Build the Directed graph for (const runnable of runnables) { - getOrInsertSet(runnableGraph, runnable.id); + addRunnableId(runnable.id); if (!runnable.runAfter) { - getOrInsertSet(runnableGraph, rootId).add(runnable.id); + addRunnableId(rootId).add(runnable.id); } else if (Array.isArray(runnable.runAfter)) { for (const parentRunnable of runnable.runAfter) { - getOrInsertSet(runnableGraph, parentRunnable.id).add(runnable.id); + addRunnableId(parentRunnable.id).add(runnable.id); } } else { - getOrInsertSet(runnableGraph, runnable.runAfter.id).add(runnable.id); + addRunnableId(runnable.runAfter.id).add(runnable.id); } } - getOrInsertSet(runnableGraph, rootId); + addRunnableId(rootId); // Do a DFS to find any cycles computedNextEdge([], runnableGraph, rootId, seenIds); diff --git a/src/common/utils/collection-functions.ts b/src/common/utils/collection-functions.ts index d20383c29a..5d6dc80548 100644 --- a/src/common/utils/collection-functions.ts +++ b/src/common/utils/collection-functions.ts @@ -48,6 +48,13 @@ export function getOrInsertSet(map: Map>, key: K): Set { return getOrInsert(map, key, new Set()); } +/** + * A currying version of {@link getOrInsertSet} + */ +export function getOrInsertSetFor(map: Map>): (key: K) => Set { + return (key) => getOrInsertSet(map, key); +} + /** * Like `getOrInsert` but with delayed creation of the item. Which is useful * if it is very expensive to create the initial value.