Skip to content
This repository was archived by the owner on Jan 3, 2024. It is now read-only.

Commit 132c6fc

Browse files
Aleksei Dmitrievstephan-noel
Aleksei Dmitriev
andauthored
fix: Invoke destroy handler in batch mode (#32)
* fix: Invoke destroy handler in batch mode * fix: Add onDestroyHandlers to meta so that they can be invoked Co-authored-by: Stephan Noel <[email protected]>
1 parent e0f7032 commit 132c6fc

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

core/garment/src/garment.ts

+35-14
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ export interface CacheEntry {
6868
output: File[];
6969
}
7070

71+
interface ActionMeta {
72+
isWatchActive: boolean;
73+
subscriptions: Set<Subscription>;
74+
logs: LogEntry[];
75+
inputHandler?: MetaInputHandler;
76+
onDestroyHandler?: () => void;
77+
outputPath?: string;
78+
}
79+
80+
interface BatchActionMeta {
81+
onDestroyHandler?: () => void;
82+
}
83+
7184
export type MetaInputHandler =
7285
| { type: 'single'; fn: InputFnCallBack }
7386
| { type: 'multi'; fn: InputFnCallBack<File[]> };
@@ -190,17 +203,7 @@ async function garmentFromWorkspace(
190203
const getSnapshotId = (hash: string) => `action-${hash}`;
191204

192205
// The map to store an information related to each action, so it can be reused between action executions
193-
const metaByAction = new Map<
194-
Action,
195-
{
196-
isWatchActive: boolean;
197-
subscriptions: Set<Subscription>;
198-
logs: LogEntry[];
199-
inputHandler?: MetaInputHandler;
200-
onDestroyHandler?: () => void;
201-
outputPath?: string;
202-
}
203-
>();
206+
const metaByAction = new Map<Action, ActionMeta>();
204207

205208
let allSubscriptionsCached: Subscription[] = [];
206209

@@ -875,6 +878,7 @@ async function garmentFromWorkspace(
875878
_ => _.watch
876879
);
877880

881+
const batchMetaByAction = new Map<Action, BatchActionMeta>();
878882
if (batchActionsToExecute.length) {
879883
await runActionsInBatch(batchActionsToExecute, {
880884
runnerOptions
@@ -896,6 +900,12 @@ async function garmentFromWorkspace(
896900
watch: true,
897901
runnerOptions
898902
});
903+
} else {
904+
if (!watcherStarted) {
905+
for (const meta of batchMetaByAction.values()) {
906+
meta?.onDestroyHandler?.();
907+
}
908+
}
899909
}
900910

901911
async function startWatcher() {
@@ -1008,13 +1018,19 @@ async function garmentFromWorkspace(
10081018
{
10091019
runner: RunnerMeta;
10101020
batch: { project: Project; options: any }[];
1021+
batchMeta?: BatchActionMeta;
10111022
}
10121023
>();
10131024
for (const action of actions) {
10141025
if (!batchesByRunner.has(action.runner.handlerPath)) {
1026+
/**We don't want more than one meta per runner, since longRunning should be invoked once for all projects.
1027+
* In the future we may want a meta for each action.
1028+
*/
1029+
batchMetaByAction.set(action, {});
10151030
batchesByRunner.set(action.runner.handlerPath, {
10161031
runner: action.runner,
1017-
batch: []
1032+
batch: [],
1033+
batchMeta: batchMetaByAction.get(action)
10181034
});
10191035
}
10201036
const { batch } = batchesByRunner.get(action.runner.handlerPath)!;
@@ -1029,7 +1045,7 @@ async function garmentFromWorkspace(
10291045
})
10301046
});
10311047
}
1032-
for (const { runner, batch } of batchesByRunner.values()) {
1048+
for (const { runner, batch, batchMeta } of batchesByRunner.values()) {
10331049
onUpdate({ type: 'before-batch', runner, batch });
10341050

10351051
const options = {
@@ -1066,7 +1082,12 @@ async function garmentFromWorkspace(
10661082
dependsOnFile() {},
10671083
cacheProvider,
10681084
batch,
1069-
logger
1085+
logger,
1086+
longRunning(onDestroy) {
1087+
if (batchMeta) {
1088+
batchMeta.onDestroyHandler = onDestroy;
1089+
}
1090+
}
10701091
});
10711092

10721093
const handler = watch ? getWatcher(runner) : getHandler(runner);

0 commit comments

Comments
 (0)