@calmo/task-runner 1.2.1 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/.gemini/commands/openspec/apply.toml +21 -0
  2. package/.gemini/commands/openspec/archive.toml +25 -0
  3. package/.gemini/commands/openspec/proposal.toml +26 -0
  4. package/AGENTS.md +18 -0
  5. package/CHANGELOG.md +23 -0
  6. package/GEMINI.md +8 -0
  7. package/coverage/coverage-final.json +5 -2
  8. package/coverage/index.html +20 -20
  9. package/coverage/lcov-report/index.html +20 -20
  10. package/coverage/lcov-report/src/EventBus.ts.html +337 -0
  11. package/coverage/{TaskGraphValidator.ts.html → lcov-report/src/TaskGraphValidator.ts.html} +44 -14
  12. package/coverage/lcov-report/src/TaskRunner.ts.html +307 -0
  13. package/coverage/lcov-report/src/WorkflowExecutor.ts.html +457 -0
  14. package/coverage/lcov-report/src/contracts/RunnerEvents.ts.html +217 -0
  15. package/coverage/lcov-report/src/contracts/index.html +116 -0
  16. package/coverage/lcov-report/src/index.html +161 -0
  17. package/coverage/lcov.info +190 -165
  18. package/coverage/src/EventBus.ts.html +337 -0
  19. package/coverage/{lcov-report → src}/TaskGraphValidator.ts.html +44 -14
  20. package/coverage/src/TaskRunner.ts.html +307 -0
  21. package/coverage/src/WorkflowExecutor.ts.html +457 -0
  22. package/coverage/src/contracts/RunnerEvents.ts.html +217 -0
  23. package/coverage/src/contracts/index.html +116 -0
  24. package/coverage/src/index.html +161 -0
  25. package/dist/EventBus.d.ts +26 -0
  26. package/dist/EventBus.js +56 -0
  27. package/dist/EventBus.js.map +1 -0
  28. package/dist/TaskGraphValidator.d.ts +6 -0
  29. package/dist/TaskGraphValidator.js +9 -0
  30. package/dist/TaskGraphValidator.js.map +1 -1
  31. package/dist/TaskRunner.d.ts +3 -36
  32. package/dist/TaskRunner.js +8 -127
  33. package/dist/TaskRunner.js.map +1 -1
  34. package/dist/WorkflowExecutor.d.ts +32 -0
  35. package/dist/WorkflowExecutor.js +109 -0
  36. package/dist/WorkflowExecutor.js.map +1 -0
  37. package/dist/contracts/ITaskGraphValidator.d.ts +6 -0
  38. package/dist/contracts/RunnerEvents.d.ts +36 -0
  39. package/dist/contracts/RunnerEvents.js +2 -0
  40. package/dist/contracts/RunnerEvents.js.map +1 -0
  41. package/openspec/AGENTS.md +456 -0
  42. package/openspec/changes/add-external-task-cancellation/proposal.md +14 -0
  43. package/openspec/changes/add-external-task-cancellation/tasks.md +10 -0
  44. package/openspec/project.md +31 -0
  45. package/package.json +1 -1
  46. package/src/EventBus.ts +84 -0
  47. package/src/TaskGraphValidator.ts +10 -0
  48. package/src/TaskRunner.ts +11 -196
  49. package/src/WorkflowExecutor.ts +124 -0
  50. package/src/contracts/ITaskGraphValidator.ts +7 -0
  51. package/src/contracts/RunnerEvents.ts +44 -0
  52. package/test-report.xml +49 -37
  53. package/coverage/TaskRunner.ts.html +0 -862
  54. package/coverage/lcov-report/TaskRunner.ts.html +0 -862
@@ -1,33 +1,7 @@
1
1
  import { TaskStep } from "./TaskStep.js";
2
2
  import { TaskResult } from "./TaskResult.js";
3
- /**
4
- * Define the payload for every possible event in the lifecycle.
5
- */
6
- export interface RunnerEventPayloads<TContext> {
7
- workflowStart: {
8
- context: TContext;
9
- steps: TaskStep<TContext>[];
10
- };
11
- workflowEnd: {
12
- context: TContext;
13
- results: Map<string, TaskResult>;
14
- };
15
- taskStart: {
16
- step: TaskStep<TContext>;
17
- };
18
- taskEnd: {
19
- step: TaskStep<TContext>;
20
- result: TaskResult;
21
- };
22
- taskSkipped: {
23
- step: TaskStep<TContext>;
24
- result: TaskResult;
25
- };
26
- }
27
- /**
28
- * A generic listener type that maps the event key to its specific payload.
29
- */
30
- export type RunnerEventListener<TContext, K extends keyof RunnerEventPayloads<TContext>> = (data: RunnerEventPayloads<TContext>[K]) => void | Promise<void>;
3
+ import { RunnerEventPayloads, RunnerEventListener } from "./contracts/RunnerEvents.js";
4
+ export { RunnerEventPayloads, RunnerEventListener };
31
5
  /**
32
6
  * The main class that orchestrates the execution of a list of tasks
33
7
  * based on their dependencies, with support for parallel execution.
@@ -35,8 +9,7 @@ export type RunnerEventListener<TContext, K extends keyof RunnerEventPayloads<TC
35
9
  */
36
10
  export declare class TaskRunner<TContext> {
37
11
  private context;
38
- private running;
39
- private listeners;
12
+ private eventBus;
40
13
  private validator;
41
14
  /**
42
15
  * @param context The shared context object to be passed to each task.
@@ -54,12 +27,6 @@ export declare class TaskRunner<TContext> {
54
27
  * @param callback The callback to remove.
55
28
  */
56
29
  off<K extends keyof RunnerEventPayloads<TContext>>(event: K, callback: RunnerEventListener<TContext, K>): void;
57
- /**
58
- * Emit an event to all subscribers.
59
- * @param event The event name.
60
- * @param data The payload for the event.
61
- */
62
- private emit;
63
30
  /**
64
31
  * Executes a list of tasks, respecting their dependencies and running
65
32
  * independent tasks in parallel.
@@ -1,4 +1,6 @@
1
1
  import { TaskGraphValidator } from "./TaskGraphValidator.js";
2
+ import { EventBus } from "./EventBus.js";
3
+ import { WorkflowExecutor } from "./WorkflowExecutor.js";
2
4
  /**
3
5
  * The main class that orchestrates the execution of a list of tasks
4
6
  * based on their dependencies, with support for parallel execution.
@@ -6,8 +8,7 @@ import { TaskGraphValidator } from "./TaskGraphValidator.js";
6
8
  */
7
9
  export class TaskRunner {
8
10
  context;
9
- running = new Set();
10
- listeners = {};
11
+ eventBus = new EventBus();
11
12
  validator = new TaskGraphValidator();
12
13
  /**
13
14
  * @param context The shared context object to be passed to each task.
@@ -21,13 +22,7 @@ export class TaskRunner {
21
22
  * @param callback The callback to execute when the event is emitted.
22
23
  */
23
24
  on(event, callback) {
24
- if (!this.listeners[event]) {
25
- // Type assertion needed because TypeScript cannot verify that the generic K
26
- // matches the specific key in the mapped type during assignment.
27
- this.listeners[event] = new Set();
28
- }
29
- // Type assertion needed to tell TS that this specific Set matches the callback type
30
- this.listeners[event].add(callback);
25
+ this.eventBus.on(event, callback);
31
26
  }
32
27
  /**
33
28
  * Unsubscribe from an event.
@@ -35,28 +30,7 @@ export class TaskRunner {
35
30
  * @param callback The callback to remove.
36
31
  */
37
32
  off(event, callback) {
38
- if (this.listeners[event]) {
39
- this.listeners[event].delete(callback);
40
- }
41
- }
42
- /**
43
- * Emit an event to all subscribers.
44
- * @param event The event name.
45
- * @param data The payload for the event.
46
- */
47
- emit(event, data) {
48
- const listeners = this.listeners[event];
49
- if (listeners) {
50
- for (const listener of listeners) {
51
- try {
52
- listener(data);
53
- }
54
- catch (error) {
55
- // Prevent listener errors from bubbling up
56
- console.error(`Error in event listener for ${String(event)}:`, error);
57
- }
58
- }
59
- }
33
+ this.eventBus.off(event, callback);
60
34
  }
61
35
  /**
62
36
  * Executes a list of tasks, respecting their dependencies and running
@@ -75,103 +49,10 @@ export class TaskRunner {
75
49
  };
76
50
  const validationResult = this.validator.validate(taskGraph);
77
51
  if (!validationResult.isValid) {
78
- // Construct error message compatible with legacy tests
79
- const affectedTasks = new Set();
80
- const errorDetails = [];
81
- for (const error of validationResult.errors) {
82
- errorDetails.push(error.message);
83
- switch (error.type) {
84
- case "cycle": {
85
- // details is { cyclePath: string[] }
86
- const path = error.details.cyclePath;
87
- // The last element duplicates the first in the path representation, so valid unique tasks are slice(0, -1) or just all as Set handles uniq
88
- path.forEach((t) => affectedTasks.add(t));
89
- break;
90
- }
91
- case "missing_dependency": {
92
- // details is { taskId: string, missingDependencyId: string }
93
- const d = error.details;
94
- affectedTasks.add(d.taskId);
95
- break;
96
- }
97
- case "duplicate_task": {
98
- const d = error.details;
99
- affectedTasks.add(d.taskId);
100
- break;
101
- }
102
- }
103
- }
104
- // Legacy error format: "Circular dependency or missing dependency detected. Unable to run tasks: A, B"
105
- const taskList = Array.from(affectedTasks).join(", ");
106
- const legacyMessage = `Circular dependency or missing dependency detected. Unable to run tasks: ${taskList}`;
107
- const detailedMessage = `Task graph validation failed: ${errorDetails.join("; ")}`;
108
- // Combine them to satisfy both legacy tests (checking for legacy message) and new requirements (clear details)
109
- throw new Error(`${legacyMessage} | ${detailedMessage}`);
110
- }
111
- this.emit("workflowStart", { context: this.context, steps });
112
- const results = new Map();
113
- const executingPromises = new Set();
114
- // Helper to process pending steps and launch ready ones
115
- const processPendingSteps = () => {
116
- const pendingSteps = steps.filter((step) => !results.has(step.name) && !this.running.has(step.name));
117
- // 1. Identify and mark skipped tasks
118
- for (const step of pendingSteps) {
119
- const deps = step.dependencies ?? [];
120
- const failedDep = deps.find((dep) => results.has(dep) && results.get(dep)?.status !== "success");
121
- if (failedDep) {
122
- const result = {
123
- status: "skipped",
124
- message: `Skipped due to failed dependency: ${failedDep}`,
125
- };
126
- results.set(step.name, result);
127
- this.emit("taskSkipped", { step, result });
128
- }
129
- }
130
- // Re-filter pending steps as some might have been skipped above
131
- const readySteps = steps.filter((step) => {
132
- if (results.has(step.name) || this.running.has(step.name))
133
- return false;
134
- const deps = step.dependencies ?? [];
135
- return deps.every((dep) => results.has(dep) && results.get(dep)?.status === "success");
136
- });
137
- // 2. Launch ready tasks
138
- for (const step of readySteps) {
139
- this.running.add(step.name);
140
- this.emit("taskStart", { step });
141
- const taskPromise = (async () => {
142
- try {
143
- const result = await step.run(this.context);
144
- results.set(step.name, result);
145
- }
146
- catch (e) {
147
- results.set(step.name, {
148
- status: "failure",
149
- error: e instanceof Error ? e.message : String(e),
150
- });
151
- }
152
- finally {
153
- this.running.delete(step.name);
154
- const result = results.get(step.name);
155
- this.emit("taskEnd", { step, result });
156
- }
157
- })();
158
- // Wrap the task promise to ensure we can track it in the Set
159
- const trackedPromise = taskPromise.then(() => {
160
- executingPromises.delete(trackedPromise);
161
- });
162
- executingPromises.add(trackedPromise);
163
- }
164
- };
165
- // Initial check to start independent tasks
166
- processPendingSteps();
167
- while (results.size < steps.length && executingPromises.size > 0) {
168
- // Wait for the next task to finish
169
- await Promise.race(executingPromises);
170
- // After a task finishes, check for new work
171
- processPendingSteps();
52
+ throw new Error(this.validator.createErrorMessage(validationResult));
172
53
  }
173
- this.emit("workflowEnd", { context: this.context, results });
174
- return results;
54
+ const executor = new WorkflowExecutor(this.context, this.eventBus);
55
+ return executor.execute(steps);
175
56
  }
176
57
  }
177
58
  //# sourceMappingURL=TaskRunner.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"TaskRunner.js","sourceRoot":"","sources":["../src/TaskRunner.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AA6C7D;;;;GAIG;AACH,MAAM,OAAO,UAAU;IAQD;IAPZ,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAC5B,SAAS,GAA0B,EAAE,CAAC;IACtC,SAAS,GAAG,IAAI,kBAAkB,EAAE,CAAC;IAE7C;;OAEG;IACH,YAAoB,OAAiB;QAAjB,YAAO,GAAP,OAAO,CAAU;IAAG,CAAC;IAEzC;;;;OAIG;IACI,EAAE,CACP,KAAQ,EACR,QAA0C;QAE1C,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YAC3B,4EAA4E;YAC5E,iEAAiE;YACjE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,IAAI,GAAG,EAAyC,CAAC;QAC3E,CAAC;QACD,oFAAoF;QACnF,IAAI,CAAC,SAAS,CAAC,KAAK,CAA2C,CAAC,GAAG,CAClE,QAAQ,CACT,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACI,GAAG,CACR,KAAQ,EACR,QAA0C;QAE1C,IAAI,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,IAAI,CAAC,SAAS,CAAC,KAAK,CAA2C,CAAC,MAAM,CACrE,QAAQ,CACT,CAAC;QACJ,CAAC;IACH,CAAC;IAED;;;;OAIG;IACK,IAAI,CACV,KAAQ,EACR,IAAsC;QAEtC,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAEzB,CAAC;QACd,IAAI,SAAS,EAAE,CAAC;YACd,KAAK,MAAM,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AAC,CAAC;gBAC1B,EAAE,EAAE,IAAI,CAAC,IAAI;gBACb,YAAY,EAAE,IAAI,CAAC,YAAY,IAAI,EAAE;aACtC,CAAC,CAAC;SACJ,CAAC;QAEF,MAAM,gBAAgB,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;QAC5D,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,CAAC;YAC9B,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,CAAC,CAAC;QACvE,CAAC;QAED,MAAM,QAAQ,GAAG,IAAI,gBAAgB,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;QACnE,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC;CACF"}
@@ -0,0 +1,32 @@
1
+ import { TaskStep } from "./TaskStep.js";
2
+ import { TaskResult } from "./TaskResult.js";
3
+ import { EventBus } from "./EventBus.js";
4
+ /**
5
+ * Handles the execution of the workflow steps.
6
+ * @template TContext The shape of the shared context object.
7
+ */
8
+ export declare class WorkflowExecutor<TContext> {
9
+ private context;
10
+ private eventBus;
11
+ private running;
12
+ /**
13
+ * @param context The shared context object.
14
+ * @param eventBus The event bus to emit events.
15
+ */
16
+ constructor(context: TContext, eventBus: EventBus<TContext>);
17
+ /**
18
+ * Executes the given steps.
19
+ * @param steps The list of steps to execute.
20
+ * @returns A Promise that resolves to a map of task results.
21
+ */
22
+ execute(steps: TaskStep<TContext>[]): Promise<Map<string, TaskResult>>;
23
+ /**
24
+ * Logic to identify tasks that can be started or must be skipped.
25
+ * Iterate only over pending steps to avoid O(N^2) checks on completed tasks.
26
+ */
27
+ private processQueue;
28
+ /**
29
+ * Handles the lifecycle of a single task execution.
30
+ */
31
+ private runStep;
32
+ }
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Handles the execution of the workflow steps.
3
+ * @template TContext The shape of the shared context object.
4
+ */
5
+ export class WorkflowExecutor {
6
+ context;
7
+ eventBus;
8
+ running = new Set();
9
+ /**
10
+ * @param context The shared context object.
11
+ * @param eventBus The event bus to emit events.
12
+ */
13
+ constructor(context, eventBus) {
14
+ this.context = context;
15
+ this.eventBus = eventBus;
16
+ }
17
+ /**
18
+ * Executes the given steps.
19
+ * @param steps The list of steps to execute.
20
+ * @returns A Promise that resolves to a map of task results.
21
+ */
22
+ async execute(steps) {
23
+ this.eventBus.emit("workflowStart", { context: this.context, steps });
24
+ const results = new Map();
25
+ const executingPromises = new Set();
26
+ const pendingSteps = new Set(steps);
27
+ // Initial pass
28
+ this.processQueue(pendingSteps, results, executingPromises);
29
+ while (results.size < steps.length && executingPromises.size > 0) {
30
+ // Wait for the next task to finish
31
+ await Promise.race(executingPromises);
32
+ // After a task finishes, check for new work
33
+ this.processQueue(pendingSteps, results, executingPromises);
34
+ }
35
+ this.eventBus.emit("workflowEnd", { context: this.context, results });
36
+ return results;
37
+ }
38
+ /**
39
+ * Logic to identify tasks that can be started or must be skipped.
40
+ * Iterate only over pending steps to avoid O(N^2) checks on completed tasks.
41
+ */
42
+ processQueue(pendingSteps, results, executingPromises) {
43
+ const toRemove = [];
44
+ const toRun = [];
45
+ for (const step of pendingSteps) {
46
+ const deps = step.dependencies ?? [];
47
+ let blocked = false;
48
+ let failedDep;
49
+ for (const dep of deps) {
50
+ const depResult = results.get(dep);
51
+ if (!depResult) {
52
+ // Dependency not finished yet
53
+ blocked = true;
54
+ }
55
+ else if (depResult.status !== "success") {
56
+ failedDep = dep;
57
+ break;
58
+ }
59
+ }
60
+ if (failedDep) {
61
+ const result = {
62
+ status: "skipped",
63
+ message: `Skipped due to failed dependency: ${failedDep}`,
64
+ };
65
+ results.set(step.name, result);
66
+ this.eventBus.emit("taskSkipped", { step, result });
67
+ toRemove.push(step);
68
+ }
69
+ else if (!blocked) {
70
+ toRun.push(step);
71
+ toRemove.push(step);
72
+ }
73
+ }
74
+ // Cleanup pending set
75
+ for (const step of toRemove) {
76
+ pendingSteps.delete(step);
77
+ }
78
+ // Execute ready tasks
79
+ for (const step of toRun) {
80
+ const taskPromise = this.runStep(step, results).then(() => {
81
+ executingPromises.delete(taskPromise);
82
+ });
83
+ executingPromises.add(taskPromise);
84
+ }
85
+ }
86
+ /**
87
+ * Handles the lifecycle of a single task execution.
88
+ */
89
+ async runStep(step, results) {
90
+ this.running.add(step.name);
91
+ this.eventBus.emit("taskStart", { step });
92
+ try {
93
+ const result = await step.run(this.context);
94
+ results.set(step.name, result);
95
+ }
96
+ catch (e) {
97
+ results.set(step.name, {
98
+ status: "failure",
99
+ error: e instanceof Error ? e.message : String(e),
100
+ });
101
+ }
102
+ finally {
103
+ this.running.delete(step.name);
104
+ const result = results.get(step.name);
105
+ this.eventBus.emit("taskEnd", { step, result });
106
+ }
107
+ }
108
+ }
109
+ //# sourceMappingURL=WorkflowExecutor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WorkflowExecutor.js","sourceRoot":"","sources":["../src/WorkflowExecutor.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,OAAO,gBAAgB;IAQjB;IACA;IARF,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAEpC;;;OAGG;IACH,YACU,OAAiB,EACjB,QAA4B;QAD5B,YAAO,GAAP,OAAO,CAAU;QACjB,aAAQ,GAAR,QAAQ,CAAoB;IACnC,CAAC;IAEJ;;;;OAIG;IACH,KAAK,CAAC,OAAO,CAAC,KAA2B;QACvC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,eAAe,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;QAEtE,MAAM,OAAO,GAAG,IAAI,GAAG,EAAsB,CAAC;QAC9C,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAAiB,CAAC;QACnD,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;QAEpC,eA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CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACjC,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE;gBACrB,MAAM,EAAE,SAAS;gBACjB,KAAK,EAAE,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;aAClD,CAAC,CAAC;QACL,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC/B,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAE,CAAC;YACvC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;CACF"}
@@ -10,4 +10,10 @@ export interface ITaskGraphValidator {
10
10
  * @returns A ValidationResult object indicating the outcome of the validation.
11
11
  */
12
12
  validate(taskGraph: TaskGraph): ValidationResult;
13
+ /**
14
+ * Creates a human-readable error message from a validation result.
15
+ * @param result The validation result containing errors.
16
+ * @returns A formatted error string.
17
+ */
18
+ createErrorMessage(result: ValidationResult): string;
13
19
  }
@@ -0,0 +1,36 @@
1
+ import { TaskStep } from "../TaskStep.js";
2
+ import { TaskResult } from "../TaskResult.js";
3
+ /**
4
+ * Define the payload for every possible event in the lifecycle.
5
+ */
6
+ export interface RunnerEventPayloads<TContext> {
7
+ workflowStart: {
8
+ context: TContext;
9
+ steps: TaskStep<TContext>[];
10
+ };
11
+ workflowEnd: {
12
+ context: TContext;
13
+ results: Map<string, TaskResult>;
14
+ };
15
+ taskStart: {
16
+ step: TaskStep<TContext>;
17
+ };
18
+ taskEnd: {
19
+ step: TaskStep<TContext>;
20
+ result: TaskResult;
21
+ };
22
+ taskSkipped: {
23
+ step: TaskStep<TContext>;
24
+ result: TaskResult;
25
+ };
26
+ }
27
+ /**
28
+ * A generic listener type that maps the event key to its specific payload.
29
+ */
30
+ export type RunnerEventListener<TContext, K extends keyof RunnerEventPayloads<TContext>> = (data: RunnerEventPayloads<TContext>[K]) => void | Promise<void>;
31
+ /**
32
+ * Helper type for the listeners map.
33
+ */
34
+ export type ListenerMap<TContext> = {
35
+ [K in keyof RunnerEventPayloads<TContext>]?: Set<RunnerEventListener<TContext, K>>;
36
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=RunnerEvents.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RunnerEvents.js","sourceRoot":"","sources":["../../src/contracts/RunnerEvents.ts"],"names":[],"mappings":""}