1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Remove duplication of code in RunManyFor

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-12-14 09:05:06 -05:00
parent 8416d965e7
commit 7b8e97ba08
2 changed files with 14 additions and 6 deletions

View File

@ -4,7 +4,7 @@
*/ */
import type { DiContainerForInjection, InjectionToken } from "@ogre-tools/injectable"; import type { DiContainerForInjection, InjectionToken } from "@ogre-tools/injectable";
import type { SingleOrMany } from "../utils"; import type { SingleOrMany } from "../utils";
import { getOrInsertSet, isDefined } from "../utils"; import { getOrInsertSetFor, isDefined } from "../utils";
import { observable, when } from "mobx"; import { observable, when } from "mobx";
import * as uuid from "uuid"; import * as uuid from "uuid";
import assert from "assert"; import assert from "assert";
@ -38,23 +38,24 @@ const verifyRunnablesAreDAG = <Param>(injectionToken: InjectionToken<Runnable<Pa
const rootId = uuid.v4(); const rootId = uuid.v4();
const runnableGraph = new Map<string, Set<string>>(); const runnableGraph = new Map<string, Set<string>>();
const seenIds = new Set<string>(); const seenIds = new Set<string>();
const addRunnableId = getOrInsertSetFor(runnableGraph);
// Build the Directed graph // Build the Directed graph
for (const runnable of runnables) { for (const runnable of runnables) {
getOrInsertSet(runnableGraph, runnable.id); addRunnableId(runnable.id);
if (!runnable.runAfter) { if (!runnable.runAfter) {
getOrInsertSet(runnableGraph, rootId).add(runnable.id); addRunnableId(rootId).add(runnable.id);
} else if (Array.isArray(runnable.runAfter)) { } else if (Array.isArray(runnable.runAfter)) {
for (const parentRunnable of runnable.runAfter) { for (const parentRunnable of runnable.runAfter) {
getOrInsertSet(runnableGraph, parentRunnable.id).add(runnable.id); addRunnableId(parentRunnable.id).add(runnable.id);
} }
} else { } 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 // Do a DFS to find any cycles
computedNextEdge([], runnableGraph, rootId, seenIds); computedNextEdge([], runnableGraph, rootId, seenIds);

View File

@ -48,6 +48,13 @@ export function getOrInsertSet<K, SK>(map: Map<K, Set<SK>>, key: K): Set<SK> {
return getOrInsert(map, key, new Set<SK>()); return getOrInsert(map, key, new Set<SK>());
} }
/**
* A currying version of {@link getOrInsertSet}
*/
export function getOrInsertSetFor<K, SK>(map: Map<K, Set<SK>>): (key: K) => Set<SK> {
return (key) => getOrInsertSet(map, key);
}
/** /**
* Like `getOrInsert` but with delayed creation of the item. Which is useful * Like `getOrInsert` but with delayed creation of the item. Which is useful
* if it is very expensive to create the initial value. * if it is very expensive to create the initial value.