From 1fc31d39caf55927dfce7fc06afc54d83afbcdf8 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Wed, 1 Mar 2023 08:19:46 -0500 Subject: [PATCH] Add documentation for verifyRunnablesAreDAG Signed-off-by: Sebastian Malton --- packages/core/src/common/runnable/helpers.ts | 18 ++++++++++++------ .../core/src/common/runnable/run-many-for.ts | 2 +- .../src/common/runnable/run-many-sync-for.ts | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/core/src/common/runnable/helpers.ts b/packages/core/src/common/runnable/helpers.ts index fd8dec0bcd..630f4e6917 100644 --- a/packages/core/src/common/runnable/helpers.ts +++ b/packages/core/src/common/runnable/helpers.ts @@ -2,7 +2,7 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -import type { DiContainerForInjection, Injectable, InjectionInstanceWithMeta, InjectionToken } from "@ogre-tools/injectable"; +import type { DiContainerForInjection, Injectable, InjectionInstanceWithMeta } from "@ogre-tools/injectable"; import { getOrInsertSetFor, isDefined } from "../utils"; import * as uuid from "uuid"; import assert from "assert"; @@ -23,10 +23,16 @@ const computedNextEdge = (traversed: string[], graph: Map>, } }; -export function verifyRunnablesAreDAG(injectionToken: InjectionToken, void>, runnables: RunnableWithId[]): void; -export function verifyRunnablesAreDAG(injectionToken: InjectionToken, void>, runnables: RunnableSyncWithId[]): void; +/** + * Verifies that the graph produces by `runnables`'s `runAfter`'s is acyclic. Namely that it + * produces a Directed Acyclic Graph. + * @param tokenId The ID of the injectionToken that was `injectManyWithMeta()`-ed. Used for error messages + * @param runnables The list of runnables to check. + */ +export function verifyRunnablesAreDAG(tokenId: string, runnables: RunnableWithId[]): void; +export function verifyRunnablesAreDAG(tokenId: string, runnables: RunnableSyncWithId[]): void; -export function verifyRunnablesAreDAG(injectionToken: InjectionToken, void> | InjectionToken, void>, runnables: (RunnableWithId[]) | (RunnableSyncWithId[])): void { +export function verifyRunnablesAreDAG(tokenId: string, runnables: (RunnableWithId[]) | (RunnableSyncWithId[])): void { const rootId = uuid.v4(); const runnableGraph = new Map>(); const seenIds = new Set(); @@ -55,7 +61,7 @@ export function verifyRunnablesAreDAG(injectionToken: InjectionToken runnable.id === id); if (!runnable) { - throw new Error(`Runnable "${id}" is not part of the injection token "${injectionToken.id}"`); + throw new Error(`Runnable "${id}" is not part of the injection token "${tokenId}"`); } const runAfters = [runnable.runAfter] @@ -64,7 +70,7 @@ export function verifyRunnablesAreDAG(injectionToken: InjectionToken runnable.id) .join('", "'); - throw new Error(`Runnable "${id}" is unreachable for injection token "${injectionToken.id}": run afters "${runAfters}" are a part of different injection tokens.`); + throw new Error(`Runnable "${id}" is unreachable for injection token "${tokenId}": run afters "${runAfters}" are a part of different injection tokens.`); } } } diff --git a/packages/core/src/common/runnable/run-many-for.ts b/packages/core/src/common/runnable/run-many-for.ts index 88d55b1d1a..4396809626 100644 --- a/packages/core/src/common/runnable/run-many-for.ts +++ b/packages/core/src/common/runnable/run-many-for.ts @@ -64,7 +64,7 @@ export function runManyFor(di: DiContainerForInjection): RunMany { const executeRunnable = executeRunnableWith(param); const allRunnables = di.injectManyWithMeta(injectionToken).map(x => convertToWithId(x)); - verifyRunnablesAreDAG(injectionToken, allRunnables); + verifyRunnablesAreDAG(injectionToken.id, allRunnables); await Promise.all(allRunnables.map(executeRunnable)); }; diff --git a/packages/core/src/common/runnable/run-many-sync-for.ts b/packages/core/src/common/runnable/run-many-sync-for.ts index 13aac9fb37..f324cc4d29 100644 --- a/packages/core/src/common/runnable/run-many-sync-for.ts +++ b/packages/core/src/common/runnable/run-many-sync-for.ts @@ -68,7 +68,7 @@ export function runManySyncFor(di: DiContainerForInjection): RunManySync { const executeRunnable = executeRunnableWith(param); const allRunnables = di.injectManyWithMeta(injectionToken).map(convertToWithId); - verifyRunnablesAreDAG(injectionToken, allRunnables); + verifyRunnablesAreDAG(injectionToken.id, allRunnables); allRunnables.forEach(executeRunnable);