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
162 lines (146 loc) · 4.65 KB
/
ActionBase.ts
File metadata and controls
162 lines (146 loc) · 4.65 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { ActionChain } from './ActionChain'
import { ExecutionContext } from './types'
export class StopExecution {
constructor(public value: any) {}
}
export class ActionBase<Effects> {
static nextActionId: number = 0
private currentExecutionId = 0
constructor(
private actionChain: ActionChain<Effects>,
private initialActionId: number = ActionBase.nextActionId++,
private runOperators?: (
value: any,
executionContext: ExecutionContext,
newPath?: string
) => any | Promise<any>
) {
interface I extends ActionBase<Effects> {
(value: any): I
displayName: string
}
this.getActionChain = this.getActionChain.bind(this)
const instance: I = Object.assign(function(value) {
const initialOperator = typeof arguments[1] === 'undefined'
const newPath = typeof arguments[2] === 'undefined' ? null : arguments[2]
const executionContext: ExecutionContext = initialOperator
? {
__execution: {
operatorId: -1,
actionId: initialActionId,
executionId: instance.currentExecutionId++,
},
__path: [],
}
: arguments[1]
if (initialOperator) {
actionChain.emit('action:start', {
actionId: executionContext.__execution.actionId,
executionId: executionContext.__execution.executionId,
actionName: instance.displayName,
value,
})
}
const returnValue = runOperators
? runOperators(value, executionContext, newPath)
: value
if (initialOperator && returnValue instanceof Promise) {
returnValue.then(() => {
actionChain.emit('action:end', {
actionId: executionContext.__execution.actionId,
executionId: executionContext.__execution.executionId,
actionName: instance.displayName,
})
})
} else if (initialOperator) {
actionChain.emit('action:end', {
actionId: executionContext.__execution.actionId,
executionId: executionContext.__execution.executionId,
actionName: instance.displayName,
})
}
return returnValue
}, this) as any
return instance
}
getActionChain<ExtraEvents = {}>() {
return this.actionChain as ActionChain<Effects, ExtraEvents>
}
createOperatorResult = (
type: string,
name: string,
cb: any
): [ActionChain<Effects>, number, any] => {
return [
this.actionChain,
this.initialActionId,
(props, executionContext, newPath) => {
const executionContextWithPath = {
__execution: executionContext.__execution,
__path: newPath
? executionContext.__path.concat(newPath)
: executionContext.__path.slice(),
}
const prevResult = this.runOperators
? this.runOperators(props, executionContextWithPath)
: props
const produceResult = (currentValue) => {
if (currentValue instanceof StopExecution) {
return currentValue
}
const thisExecution = {
...executionContextWithPath.__execution,
operatorId: executionContextWithPath.__execution.operatorId + 1,
}
executionContextWithPath.__execution.operatorId++
const path = executionContextWithPath.__path
const effects = Object.assign(
{},
this.actionChain.getEffects(thisExecution),
executionContextWithPath
)
this.actionChain.emit('operator:start', {
type,
name,
path,
...thisExecution,
})
const result = cb(effects, currentValue)
if (result instanceof Promise) {
this.actionChain.emit('operator:async', {
type,
name,
path,
isAsync: true,
...thisExecution,
})
return result.then((promiseResult) => {
this.actionChain.emit('operator:end', {
type,
name,
path,
...thisExecution,
isAsync: true,
result: promiseResult,
})
return promiseResult
})
}
this.actionChain.emit('operator:end', {
type,
name,
path,
...thisExecution,
isAsync: false,
result: result,
})
return result
}
if (prevResult instanceof Promise) {
return prevResult.then(produceResult)
}
return produceResult(prevResult)
},
]
}
}