Skip to content

Commit 7c494f8

Browse files
committed
fix(cli): provide missing typings for workflow "processTask" toolbox action (#878)
1 parent ef498cd commit 7c494f8

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

packages/actor-core-cli/src/workflow.tsx

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export interface Context {
173173
wait: (ms: number) => Promise<undefined>;
174174
task: <T extends UserFnReturnType>(
175175
name: string,
176-
taskFn: (toolbox: Context) => T,
176+
taskFn: (ctx: Context) => T,
177177
opts?: TaskOptions,
178178
) => AsyncGenerator<
179179
WorkflowAction.All,
@@ -218,6 +218,10 @@ interface TaskOptions {
218218
success?: ReactNode;
219219
}
220220

221+
interface RunnerToolbox {
222+
processTask: (task: WorkflowAction.All) => void;
223+
}
224+
221225
let TASK_ID = 0;
222226

223227
function getTaskId() {
@@ -226,15 +230,14 @@ function getTaskId() {
226230

227231
export function workflow(
228232
title: string,
229-
workflowFn: (
230-
toolbox: Context,
231-
) => AsyncGenerator<WorkflowAction.All | undefined>,
233+
workflowFn: (ctx: Context) => AsyncGenerator<WorkflowAction.All | undefined>,
232234
opts: TaskOptions = {},
233235
) {
234236
let renderUtils: ReturnType<typeof render> | null = null;
235237

236238
async function* runner<T extends UserFnReturnType>(
237-
meta: TaskMetadata & { processTask: (task: WorkflowAction.All) => void },
239+
meta: TaskMetadata,
240+
toolbox: RunnerToolbox,
238241
name: string,
239242
taskFn: (ctx: Context) => T,
240243
opts: TaskOptions = {},
@@ -243,7 +246,9 @@ export function workflow(
243246
const p = WorkflowAction.progress.bind(null, { ...meta, id, name, opts });
244247
yield p("running");
245248
try {
246-
const output = taskFn(createContext({ ...meta, id, name, opts, processTask: meta.processTask }));
249+
const output = taskFn(
250+
createContext({ ...meta, id, name, opts }, toolbox),
251+
);
247252
if (output instanceof Promise) {
248253
const result = await output;
249254
yield p("done", { result, ...opts });
@@ -259,17 +264,19 @@ export function workflow(
259264
}
260265
}
261266

262-
function createContext(
263-
meta: TaskMetadata & { processTask: (task: WorkflowAction.All) => void },
264-
): Context {
267+
function createContext(meta: TaskMetadata, toolbox: RunnerToolbox): Context {
265268
return {
266269
wait: (ms: number) =>
267270
new Promise<undefined>((resolve) => setTimeout(resolve, ms)),
268-
task: runner.bind(null, {
269-
...meta,
270-
parent: meta.id,
271-
name: "",
272-
}) as Context["task"],
271+
task: runner.bind(
272+
null,
273+
{
274+
...meta,
275+
parent: meta.id,
276+
name: "",
277+
},
278+
toolbox,
279+
) as Context["task"],
273280
render(children: React.ReactNode) {
274281
return WorkflowAction.hook("afterAll", ({ tasks, logs }) => {
275282
renderUtils?.rerender(
@@ -288,7 +295,7 @@ export function workflow(
288295
});
289296
},
290297
changeLabel: (label: string) => {
291-
meta.processTask(
298+
toolbox.processTask(
292299
WorkflowAction.progress({ ...meta, name: label }, "running"),
293300
);
294301
},
@@ -349,9 +356,9 @@ export function workflow(
349356
};
350357
}
351358

352-
async function* workflowRunner(
353-
processTask: (task: WorkflowAction.All) => void,
354-
): AsyncGenerator<WorkflowAction.All, WorkflowResult> {
359+
async function* workflowRunner({
360+
processTask,
361+
}: RunnerToolbox): AsyncGenerator<WorkflowAction.All, WorkflowResult> {
355362
// task <> parent
356363
const parentMap = new Map<string, string>();
357364
const id = getTaskId();
@@ -361,7 +368,7 @@ export function workflow(
361368
"running",
362369
);
363370
for await (const task of workflowFn(
364-
createContext({ id, name: title, parent: id, processTask }),
371+
createContext({ id, name: title, parent: id }, { processTask }),
365372
)) {
366373
if (!task || typeof task !== "object") {
367374
continue;
@@ -457,7 +464,7 @@ export function workflow(
457464
);
458465
}
459466

460-
for await (const task of workflowRunner(processTask)) {
467+
for await (const task of workflowRunner({ processTask })) {
461468
processTask(task);
462469
}
463470

0 commit comments

Comments
 (0)