1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/packages/utility-features/run-many/src/run-many-sync-for.ts
Sebastian Malton 2789bcebcb
Convert runMany and runManySync to use injectManyWithMeta + move to seperate package (#7244)
* 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>
2023-03-10 10:07:28 +02:00

78 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 type { Disposer } from "@k8slens/utilities";
import type { RunnableSync, RunSync, RunnableSyncWithId } from "./types";
import { convertToWithIdWith, verifyRunnablesAreDAG } from "./helpers";
import type TypedEventEmitter from "typed-emitter";
import EventEmitter from "events";
export type RunManySync = <Param>(injectionToken: InjectionToken<RunnableSync<Param>, void>) => RunSync<Param>;
class SyncBarrier {
private readonly finishedIds = new Set<string>();
private readonly events: TypedEventEmitter<Record<string, () => void>> = new EventEmitter();
setFinished(id: string): void {
this.finishedIds.add(id);
this.events.emit(id);
}
onceParentsAreFinished(id: string, parentIds: string[], action: () => void) {
const finishers = new Map<string, Disposer>();
const checkAndRun = () => {
if (finishers.size === 0) {
action();
this.setFinished(id);
}
};
for (const parentId of parentIds) {
if (this.finishedIds.has(parentId)) {
continue;
}
const onParentFinished = () => {
this.events.removeListener(parentId, onParentFinished);
finishers.delete(parentId);
checkAndRun();
};
finishers.set(parentId, onParentFinished);
this.events.once(parentId, onParentFinished);
}
checkAndRun();
}
}
const executeRunnableWith = <Param>(param: Param) => {
const barrier = new SyncBarrier();
return (runnable: RunnableSyncWithId<Param>) => {
barrier.onceParentsAreFinished(
runnable.id,
runnable.runAfter.map(r => r.id),
() => runnable.run(param),
);
};
};
export function runManySyncFor(di: DiContainerForInjection): RunManySync {
const convertToWithId = convertToWithIdWith(di);
return <Param>(injectionToken: InjectionToken<RunnableSync<Param>, void>) => (param: Param): undefined => {
const executeRunnable = executeRunnableWith(param);
const allRunnables = di.injectManyWithMeta(injectionToken).map(convertToWithId);
verifyRunnablesAreDAG(injectionToken.id, allRunnables);
allRunnables.forEach(executeRunnable);
return undefined;
};
}