mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
* Convert runMany and runManySync to use injectManyWithMeta Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup type errors due to new Runnable requirements Signed-off-by: Sebastian Malton <sebastian@malton.name> * Add documentation for verifyRunnablesAreDAG Signed-off-by: Sebastian Malton <sebastian@malton.name> * Simplify convertToWithIdWith Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move all utility functions to separate package Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move testing utilities to separate package Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move run-many and run-many-sync to separate package Signed-off-by: Sebastian Malton <sebastian@malton.name> * Replace all internal uses of utilities with new packages Signed-off-by: Sebastian Malton <sebastian@malton.name> * Use new @k8slens/run-many package in core Signed-off-by: Sebastian Malton <sebastian@malton.name> * Add dep to open-lens Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup type errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup uses of @k8slens/test-utils Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup getGlobalOverride Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move tests to new package too Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix type errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup uses of AsyncResult and autoBind Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup remaining import issues Signed-off-by: Sebastian Malton <sebastian@malton.name> * Finial fixups to fix build Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix lint Signed-off-by: Sebastian Malton <sebastian@malton.name> * Revert moving "testUsingFakeTime" to separate package - This fixes tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix integration tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix unit test failing due to spelling fix Signed-off-by: Sebastian Malton <sebastian@malton.name> --------- Signed-off-by: Sebastian Malton <sebastian@malton.name>
72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
/**
|
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
*/
|
|
import type { DiContainerForInjection, InjectionToken } from "@ogre-tools/injectable";
|
|
import { getOrInsert } from "@k8slens/utilities";
|
|
import type TypedEventEmitter from "typed-emitter";
|
|
import EventEmitter from "events";
|
|
import { convertToWithIdWith, verifyRunnablesAreDAG } from "./helpers";
|
|
import type { RunnableWithId, Runnable, Run } from "./types";
|
|
import type { Asyncify } from "type-fest";
|
|
|
|
export type RunMany = <Param>(injectionToken: InjectionToken<Runnable<Param>, void>) => Asyncify<Run<Param>>;
|
|
|
|
interface BarrierEvent {
|
|
finish: (id: string) => void;
|
|
}
|
|
|
|
class DynamicBarrier {
|
|
private readonly finishedIds = new Map<string, Promise<void>>();
|
|
private readonly events: TypedEventEmitter<BarrierEvent> = new EventEmitter();
|
|
|
|
private initFinishingPromise(id: string): Promise<void> {
|
|
return getOrInsert(this.finishedIds, id, new Promise<void>(resolve => {
|
|
const handler = (finishedId: string) => {
|
|
if (finishedId === id) {
|
|
resolve();
|
|
this.events.removeListener("finish", handler);
|
|
}
|
|
};
|
|
|
|
this.events.addListener("finish", handler);
|
|
}));
|
|
}
|
|
|
|
setFinished(id: string): void {
|
|
void this.initFinishingPromise(id);
|
|
|
|
this.events.emit("finish", id);
|
|
}
|
|
|
|
async blockOn(id: string): Promise<void> {
|
|
await this.initFinishingPromise(id);
|
|
}
|
|
}
|
|
|
|
const executeRunnableWith = <Param>(param: Param) => {
|
|
const barrier = new DynamicBarrier();
|
|
|
|
return async (runnable: RunnableWithId<Param>): Promise<void> => {
|
|
for (const parentRunnable of runnable.runAfter) {
|
|
await barrier.blockOn(parentRunnable.id);
|
|
}
|
|
|
|
await runnable.run(param);
|
|
barrier.setFinished(runnable.id);
|
|
};
|
|
};
|
|
|
|
export function runManyFor(di: DiContainerForInjection): RunMany {
|
|
const convertToWithId = convertToWithIdWith(di);
|
|
|
|
return <Param>(injectionToken: InjectionToken<Runnable<Param>, void>) => async (param: Param) => {
|
|
const executeRunnable = executeRunnableWith(param);
|
|
const allRunnables = di.injectManyWithMeta(injectionToken).map(x => convertToWithId(x));
|
|
|
|
verifyRunnablesAreDAG(injectionToken.id, allRunnables);
|
|
|
|
await Promise.all(allRunnables.map(executeRunnable));
|
|
};
|
|
}
|