mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Replace use of mobx from runManyFor with custom barrier
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
fcddf442a0
commit
4dbf5c8637
@ -4,11 +4,12 @@
|
|||||||
*/
|
*/
|
||||||
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 { getOrInsertSetFor, isDefined } from "../utils";
|
import { getOrInsert, getOrInsertSetFor, isDefined } from "../utils";
|
||||||
import { observable, when } from "mobx";
|
|
||||||
import * as uuid from "uuid";
|
import * as uuid from "uuid";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import type { Asyncify } from "type-fest";
|
import type { Asyncify } from "type-fest";
|
||||||
|
import type TypedEventEmitter from "typed-emitter";
|
||||||
|
import EventEmitter from "events";
|
||||||
|
|
||||||
export interface Runnable<TParameter = void> {
|
export interface Runnable<TParameter = void> {
|
||||||
id: string;
|
id: string;
|
||||||
@ -80,18 +81,50 @@ const verifyRunnablesAreDAG = <Param>(injectionToken: InjectionToken<Runnable<Pa
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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(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 executeRunnableWith = <Param>(param: Param) => {
|
||||||
const finishedIds = observable.set<string>();
|
const barrier = new DynamicBarrier();
|
||||||
|
|
||||||
return async (runnable: Runnable<Param>): Promise<void> => {
|
return async (runnable: Runnable<Param>): Promise<void> => {
|
||||||
const parentRunnables = [runnable.runAfter].flat().filter(isDefined);
|
const parentRunnables = [runnable.runAfter].flat().filter(isDefined);
|
||||||
|
|
||||||
for (const parentRunnable of parentRunnables) {
|
for (const parentRunnable of parentRunnables) {
|
||||||
await when(() => finishedIds.has(parentRunnable.id));
|
await barrier.blockOn(parentRunnable.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
await runnable.run(param);
|
await runnable.run(param);
|
||||||
finishedIds.add(runnable.id);
|
barrier.setFinished(runnable.id);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user