forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionBase.ts
More file actions
146 lines (131 loc) · 4.08 KB
/
ActionBase.ts
File metadata and controls
146 lines (131 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { ActionChain, Execution } from './ActionChain'
;(actionBaseFactory as any).nextActionId = 0
export class StopExecution {
value: any
constructor(value) {
this.value = value
}
}
export interface ActionBase<Context, InitialValue, Value = InitialValue> {
(value: InitialValue): Value
createOperatorResult(
type: string,
name: string,
cb: any
): [ActionChain<Context>, number, any]
}
export interface NoValueActionBase<Context, InitialValue, Value = InitialValue>
extends ActionBase<Context, InitialValue, Value> {
(): Value
}
export function actionBaseFactory<Context, InitialValue, Value = InitialValue>(
actionChain: ActionChain<Context>,
initialActionId: number = (actionBaseFactory as any).nextActionId++,
runOperators?: (
value: any,
execution: Execution,
path: string[]
) => any | Promise<any>
): InitialValue extends undefined
? NoValueActionBase<Context, InitialValue, Value>
: ActionBase<Context, InitialValue, Value> {
let currentExecutionId = 0
return Object.assign(
function(value) {
const initialOperator = typeof arguments[1] === 'undefined'
const execution = initialOperator
? {
operatorId: -1,
actionId: initialActionId,
executionId: currentExecutionId++,
}
: arguments[1]
const path =
typeof arguments[2] === 'undefined' ? [] : (arguments[2] as string[])
if (initialOperator) {
actionChain.emit('action:start', {
actionId: execution.actionId,
executionId: execution.executionId,
})
}
const returnValue = runOperators
? runOperators(value, execution, path)
: value
if (initialOperator && returnValue instanceof Promise) {
returnValue.then(() => {
actionChain.emit('action:end', {
actionId: execution.actionId,
executionId: execution.executionId,
})
})
} else if (initialOperator) {
actionChain.emit('action:end', {
actionId: execution.actionId,
executionId: execution.executionId,
})
}
return returnValue
} as any,
{
createOperatorResult(
type: string,
name: string,
cb: any
): [ActionChain<Context>, number, any] {
return [
actionChain,
initialActionId,
(props, execution, path) => {
const prevResult = runOperators
? runOperators(props, execution, path)
: props
function produceResult(currentValue) {
if (currentValue instanceof StopExecution) {
return currentValue.value
}
execution.operatorId++
const context = actionChain.getContext(execution, path)
actionChain.emit('operator:start', {
type,
name,
path,
...context.execution,
})
const result = actionChain.getOptions().actionWrapper
? actionChain
.getOptions()
.actionWrapper(currentValue, context, cb)
: cb(currentValue, context)
if (result instanceof Promise) {
return result.then((promiseResult) => {
actionChain.emit('operator:end', {
type,
name,
path,
...context.execution,
isAsync: true,
result: promiseResult,
})
return promiseResult
})
}
actionChain.emit('operator:end', {
type,
name,
path,
...context.execution,
isAsync: false,
result: result,
})
return result
}
if (prevResult instanceof Promise) {
return prevResult.then(produceResult)
}
return produceResult(prevResult)
},
]
},
}
)
}