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

Commit

Permalink
fix: Invoke destroy handler in batch mode (#32)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Aleksei Dmitriev and stephan-noel authored Feb 12, 2021
1 parent e0f7032 commit 132c6fc
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions core/garment/src/garment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ export interface CacheEntry {
output: File[];
}

interface ActionMeta {
isWatchActive: boolean;
subscriptions: Set<Subscription>;
logs: LogEntry[];
inputHandler?: MetaInputHandler;
onDestroyHandler?: () => void;
outputPath?: string;
}

interface BatchActionMeta {
onDestroyHandler?: () => void;
}

export type MetaInputHandler =
| { type: 'single'; fn: InputFnCallBack }
| { type: 'multi'; fn: InputFnCallBack<File[]> };
Expand Down Expand Up @@ -190,17 +203,7 @@ async function garmentFromWorkspace(
const getSnapshotId = (hash: string) => `action-${hash}`;

// The map to store an information related to each action, so it can be reused between action executions
const metaByAction = new Map<
Action,
{
isWatchActive: boolean;
subscriptions: Set<Subscription>;
logs: LogEntry[];
inputHandler?: MetaInputHandler;
onDestroyHandler?: () => void;
outputPath?: string;
}
>();
const metaByAction = new Map<Action, ActionMeta>();

let allSubscriptionsCached: Subscription[] = [];

Expand Down Expand Up @@ -875,6 +878,7 @@ async function garmentFromWorkspace(
_ => _.watch
);

const batchMetaByAction = new Map<Action, BatchActionMeta>();
if (batchActionsToExecute.length) {
await runActionsInBatch(batchActionsToExecute, {
runnerOptions
Expand All @@ -896,6 +900,12 @@ async function garmentFromWorkspace(
watch: true,
runnerOptions
});
} else {
if (!watcherStarted) {
for (const meta of batchMetaByAction.values()) {
meta?.onDestroyHandler?.();
}
}
}

async function startWatcher() {
Expand Down Expand Up @@ -1008,13 +1018,19 @@ async function garmentFromWorkspace(
{
runner: RunnerMeta;
batch: { project: Project; options: any }[];
batchMeta?: BatchActionMeta;
}
>();
for (const action of actions) {
if (!batchesByRunner.has(action.runner.handlerPath)) {
/**We don't want more than one meta per runner, since longRunning should be invoked once for all projects.
* In the future we may want a meta for each action.
*/
batchMetaByAction.set(action, {});
batchesByRunner.set(action.runner.handlerPath, {
runner: action.runner,
batch: []
batch: [],
batchMeta: batchMetaByAction.get(action)
});
}
const { batch } = batchesByRunner.get(action.runner.handlerPath)!;
Expand All @@ -1029,7 +1045,7 @@ async function garmentFromWorkspace(
})
});
}
for (const { runner, batch } of batchesByRunner.values()) {
for (const { runner, batch, batchMeta } of batchesByRunner.values()) {
onUpdate({ type: 'before-batch', runner, batch });

const options = {
Expand Down Expand Up @@ -1066,7 +1082,12 @@ async function garmentFromWorkspace(
dependsOnFile() {},
cacheProvider,
batch,
logger
logger,
longRunning(onDestroy) {
if (batchMeta) {
batchMeta.onDestroyHandler = onDestroy;
}
}
});

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

0 comments on commit 132c6fc

Please sign in to comment.