Skip to content

Commit 3077381

Browse files
committed
feat(orap): add params for TaskFlow and add TaskFlowParams type for event flow
1 parent 7017f4b commit 3077381

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

packages/orap/src/flow/event.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { AutoCrossCheckParam, Providers } from '@ora-io/reku'
22
import type { Context } from '../task'
3+
import type { TaskFlowParams } from '../flow/task'
34
import { TaskFlow } from '../flow/task'
4-
import type { StoreManager } from '../store'
5+
import { StoreManager } from '../store'
56
import type { EventSignalRegisterParams } from '../signal'
67
import { EventVerse } from '../verse/event'
78
import type { TaskVerse } from '../verse/task'
@@ -37,13 +38,19 @@ export class EventFlow implements Flow {
3738
return this
3839
}
3940

40-
// task(store: Store, context?: Context): TaskFlow {
41-
task(sm?: StoreManager, context?: Context): TaskFlow {
42-
const tf = new TaskFlow(this)
43-
if (sm)
44-
tf.cache(sm)
45-
if (context)
46-
tf.context(context)
41+
task(params: TaskFlowParams): TaskFlow
42+
task(sm?: StoreManager | TaskFlowParams, context?: Context): TaskFlow {
43+
let tf: TaskFlow
44+
if (sm instanceof StoreManager) {
45+
tf = new TaskFlow(this)
46+
if (sm)
47+
tf.cache(sm)
48+
if (context)
49+
tf.context(context)
50+
}
51+
else {
52+
tf = new TaskFlow(this, sm)
53+
}
4754
this._taskFlows.push(tf)
4855
return tf
4956
}

packages/orap/src/flow/task.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,26 @@ const defaultHandleFn: HandleFn = () => {
1818
const defaultToKeyFn: ToKeyFn = _ => randomStr(8, alphabetHex)
1919
export interface TaskFlowTTL { taskTtl: Milliseconds; doneTtl: Milliseconds }
2020

21+
export interface TaskFlowParams {
22+
context?: Context
23+
taskPrefix?: Prefix
24+
donePrefix?: Prefix
25+
taskTtl?: Milliseconds
26+
doneTtl?: Milliseconds
27+
cache?: StoreManager
28+
toKeyFn?: ToKeyFn
29+
handleFn?: HandleFn
30+
successFn?: HandleResultFn
31+
failFn?: HandleResultFn
32+
}
33+
2134
// TODO: add 'Failed-Task:' ?
2235
export class TaskFlow implements Flow {
2336
sm: StoreManager = new StoreManager(memoryStore())
2437
taskPrefix: Prefix = 'Task:'
2538
donePrefix: Prefix = 'Done-Task:'
26-
taskTtl?: Milliseconds
27-
doneTtl?: Milliseconds
39+
taskTtl: Milliseconds = 60 * 1000
40+
doneTtl: Milliseconds = 60 * 1000
2841
toKeyFn: ToKeyFn = defaultToKeyFn
2942
handleFn: HandleFn = defaultHandleFn
3043
successFn: HandleResultFn = defaultSuccessFn
@@ -39,7 +52,21 @@ export class TaskFlow implements Flow {
3952

4053
constructor(
4154
private parentFlow: EventFlow,
42-
) { }
55+
params?: TaskFlowParams,
56+
) {
57+
params?.context && this.context(params?.context)
58+
const taskPrefix = params?.taskPrefix ?? this.taskPrefix
59+
const donePrefix = params?.donePrefix ?? this.donePrefix
60+
this.prefix(taskPrefix, donePrefix)
61+
const taskTtl = params?.taskTtl ?? this.taskTtl
62+
const doneTtl = params?.doneTtl ?? this.doneTtl
63+
this.ttl(taskTtl, doneTtl)
64+
params?.cache && this.cache(params?.cache)
65+
params?.toKeyFn && this.key(params?.toKeyFn)
66+
params?.handleFn && this.handle(params?.handleFn)
67+
params?.successFn && this.success(params?.successFn)
68+
params?.failFn && this.fail(params?.failFn)
69+
}
4370

4471
get middlewares() {
4572
return this._middlewares

0 commit comments

Comments
 (0)