@@ -173,7 +173,7 @@ export interface Context {
173
173
wait : ( ms : number ) => Promise < undefined > ;
174
174
task : < T extends UserFnReturnType > (
175
175
name : string ,
176
- taskFn : ( toolbox : Context ) => T ,
176
+ taskFn : ( ctx : Context ) => T ,
177
177
opts ?: TaskOptions ,
178
178
) => AsyncGenerator <
179
179
WorkflowAction . All ,
@@ -218,6 +218,10 @@ interface TaskOptions {
218
218
success ?: ReactNode ;
219
219
}
220
220
221
+ interface RunnerToolbox {
222
+ processTask : ( task : WorkflowAction . All ) => void ;
223
+ }
224
+
221
225
let TASK_ID = 0 ;
222
226
223
227
function getTaskId ( ) {
@@ -226,15 +230,14 @@ function getTaskId() {
226
230
227
231
export function workflow (
228
232
title : string ,
229
- workflowFn : (
230
- toolbox : Context ,
231
- ) => AsyncGenerator < WorkflowAction . All | undefined > ,
233
+ workflowFn : ( ctx : Context ) => AsyncGenerator < WorkflowAction . All | undefined > ,
232
234
opts : TaskOptions = { } ,
233
235
) {
234
236
let renderUtils : ReturnType < typeof render > | null = null ;
235
237
236
238
async function * runner < T extends UserFnReturnType > (
237
- meta : TaskMetadata & { processTask : ( task : WorkflowAction . All ) => void } ,
239
+ meta : TaskMetadata ,
240
+ toolbox : RunnerToolbox ,
238
241
name : string ,
239
242
taskFn : ( ctx : Context ) => T ,
240
243
opts : TaskOptions = { } ,
@@ -243,7 +246,9 @@ export function workflow(
243
246
const p = WorkflowAction . progress . bind ( null , { ...meta , id, name, opts } ) ;
244
247
yield p ( "running" ) ;
245
248
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
+ ) ;
247
252
if ( output instanceof Promise ) {
248
253
const result = await output ;
249
254
yield p ( "done" , { result, ...opts } ) ;
@@ -259,17 +264,19 @@ export function workflow(
259
264
}
260
265
}
261
266
262
- function createContext (
263
- meta : TaskMetadata & { processTask : ( task : WorkflowAction . All ) => void } ,
264
- ) : Context {
267
+ function createContext ( meta : TaskMetadata , toolbox : RunnerToolbox ) : Context {
265
268
return {
266
269
wait : ( ms : number ) =>
267
270
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" ] ,
273
280
render ( children : React . ReactNode ) {
274
281
return WorkflowAction . hook ( "afterAll" , ( { tasks, logs } ) => {
275
282
renderUtils ?. rerender (
@@ -288,7 +295,7 @@ export function workflow(
288
295
} ) ;
289
296
} ,
290
297
changeLabel : ( label : string ) => {
291
- meta . processTask (
298
+ toolbox . processTask (
292
299
WorkflowAction . progress ( { ...meta , name : label } , "running" ) ,
293
300
) ;
294
301
} ,
@@ -349,9 +356,9 @@ export function workflow(
349
356
} ;
350
357
}
351
358
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 > {
355
362
// task <> parent
356
363
const parentMap = new Map < string , string > ( ) ;
357
364
const id = getTaskId ( ) ;
@@ -361,7 +368,7 @@ export function workflow(
361
368
"running" ,
362
369
) ;
363
370
for await ( const task of workflowFn (
364
- createContext ( { id, name : title , parent : id , processTask } ) ,
371
+ createContext ( { id, name : title , parent : id } , { processTask } ) ,
365
372
) ) {
366
373
if ( ! task || typeof task !== "object" ) {
367
374
continue ;
@@ -457,7 +464,7 @@ export function workflow(
457
464
) ;
458
465
}
459
466
460
- for await ( const task of workflowRunner ( processTask ) ) {
467
+ for await ( const task of workflowRunner ( { processTask } ) ) {
461
468
processTask ( task ) ;
462
469
}
463
470
0 commit comments