|
1 | 1 | import { ActionChain, Execution, ExecutionContext } from './ActionChain' |
2 | | -;(actionBaseFactory as any).nextActionId = 0 |
3 | 2 |
|
4 | 3 | export class StopExecution { |
5 | 4 | value: any |
6 | 5 | constructor(value) { |
7 | 6 | this.value = value |
8 | 7 | } |
9 | 8 | } |
10 | | - |
11 | | -export interface ActionBase<Context, InitialValue, Value = InitialValue> { |
12 | | - (value: InitialValue): Value |
13 | | - createOperatorResult( |
14 | | - type: string, |
15 | | - name: string, |
16 | | - cb: any |
17 | | - ): [ActionChain<Context>, number, any] |
18 | | -} |
19 | | - |
20 | | -export interface NoValueActionBase<Context, InitialValue, Value = InitialValue> |
21 | | - extends ActionBase<Context, InitialValue, Value> { |
22 | | - (): Value |
23 | | -} |
24 | | - |
25 | | -export function actionBaseFactory< |
26 | | - Context, |
27 | | - InitialValue, |
28 | | - Value = InitialValue, |
29 | | - ReturnAction = InitialValue extends void |
30 | | - ? NoValueActionBase<Context, InitialValue, Value> |
31 | | - : ActionBase<Context, InitialValue, Value> |
32 | | ->( |
33 | | - actionChain: ActionChain<Context>, |
34 | | - initialActionId: number = (actionBaseFactory as any).nextActionId++, |
35 | | - runOperators?: ( |
| 9 | +export class ActionBase<Context, InitialValue, Value = InitialValue> { |
| 10 | + static nextActionId: number = 0 |
| 11 | + private initialActionId: number |
| 12 | + private actionChain: ActionChain<Context> |
| 13 | + private runOperators: ( |
36 | 14 | value: any, |
37 | 15 | executionContext: { |
38 | 16 | __execution: Execution |
39 | 17 | __path: string[] |
40 | 18 | }, |
41 | 19 | newPath?: string |
42 | 20 | ) => any | Promise<any> |
43 | | -): ReturnAction { |
44 | | - let currentExecutionId = 0 |
45 | | - return Object.assign( |
46 | | - function(value) { |
| 21 | + constructor( |
| 22 | + actionChain: ActionChain<Context>, |
| 23 | + initialActionId: number = ActionBase.nextActionId++, |
| 24 | + runOperators?: ( |
| 25 | + value: any, |
| 26 | + executionContext: { |
| 27 | + __execution: Execution |
| 28 | + __path: string[] |
| 29 | + }, |
| 30 | + newPath?: string |
| 31 | + ) => any | Promise<any> |
| 32 | + ) { |
| 33 | + this.actionChain = actionChain |
| 34 | + this.runOperators = runOperators |
| 35 | + this.initialActionId = initialActionId |
| 36 | + |
| 37 | + let currentExecutionId = 0 |
| 38 | + return Object.assign(function(value) { |
47 | 39 | const initialOperator = typeof arguments[1] === 'undefined' |
48 | 40 | const newPath = typeof arguments[2] === 'undefined' ? null : arguments[2] |
49 | 41 | const executionContext: ExecutionContext = initialOperator |
@@ -81,90 +73,91 @@ export function actionBaseFactory< |
81 | 73 | } |
82 | 74 |
|
83 | 75 | return returnValue |
84 | | - } as any, |
85 | | - { |
86 | | - createOperatorResult( |
87 | | - type: string, |
88 | | - name: string, |
89 | | - cb: any |
90 | | - ): [ActionChain<Context>, number, any] { |
91 | | - return [ |
92 | | - actionChain, |
93 | | - initialActionId, |
94 | | - (props, executionContext, newPath) => { |
95 | | - const executionContextWithPath = { |
96 | | - __execution: executionContext.__execution, |
97 | | - __path: newPath |
98 | | - ? executionContext.__path.concat(newPath) |
99 | | - : executionContext.__path.slice(), |
100 | | - } |
101 | | - const path = executionContextWithPath.__path |
102 | | - const prevResult = runOperators |
103 | | - ? runOperators(props, executionContextWithPath) |
104 | | - : props |
105 | | - |
106 | | - const operatorId = ++executionContextWithPath.__execution.operatorId |
| 76 | + }, this) as any |
| 77 | + } |
| 78 | + getActionChain = () => { |
| 79 | + return this.actionChain |
| 80 | + } |
| 81 | + createOperatorResult = ( |
| 82 | + type: string, |
| 83 | + name: string, |
| 84 | + cb: any |
| 85 | + ): [ActionChain<Context>, number, any] => { |
| 86 | + return [ |
| 87 | + this.actionChain, |
| 88 | + this.initialActionId, |
| 89 | + (props, executionContext, newPath) => { |
| 90 | + const executionContextWithPath = { |
| 91 | + __execution: executionContext.__execution, |
| 92 | + __path: newPath |
| 93 | + ? executionContext.__path.concat(newPath) |
| 94 | + : executionContext.__path.slice(), |
| 95 | + } |
| 96 | + const path = executionContextWithPath.__path |
| 97 | + const prevResult = this.runOperators |
| 98 | + ? this.runOperators(props, executionContextWithPath) |
| 99 | + : props |
107 | 100 |
|
108 | | - function produceResult(currentValue) { |
109 | | - if (currentValue instanceof StopExecution) { |
110 | | - return currentValue |
111 | | - } |
| 101 | + const operatorId = ++executionContextWithPath.__execution.operatorId |
112 | 102 |
|
113 | | - const context = actionChain.getContext(executionContextWithPath) |
| 103 | + const produceResult = (currentValue) => { |
| 104 | + if (currentValue instanceof StopExecution) { |
| 105 | + return currentValue |
| 106 | + } |
114 | 107 |
|
115 | | - actionChain.emit('operator:start', { |
116 | | - type, |
117 | | - name, |
118 | | - path, |
119 | | - ...context.__execution, |
120 | | - operatorId, |
121 | | - }) |
122 | | - const result = cb(currentValue, context) |
| 108 | + const context = this.actionChain.getContext(executionContextWithPath) |
123 | 109 |
|
124 | | - if (result instanceof Promise) { |
125 | | - actionChain.emit('operator:async', { |
126 | | - type, |
127 | | - name, |
128 | | - path, |
129 | | - isAsync: true, |
130 | | - ...context.__execution, |
131 | | - operatorId, |
132 | | - }) |
133 | | - return result.then((promiseResult) => { |
134 | | - actionChain.emit('operator:end', { |
135 | | - type, |
136 | | - name, |
137 | | - path, |
138 | | - ...context.__execution, |
139 | | - isAsync: true, |
140 | | - result: promiseResult, |
141 | | - operatorId, |
142 | | - }) |
143 | | - return promiseResult |
144 | | - }) |
145 | | - } |
| 110 | + this.actionChain.emit('operator:start', { |
| 111 | + type, |
| 112 | + name, |
| 113 | + path, |
| 114 | + ...context.__execution, |
| 115 | + operatorId, |
| 116 | + }) |
| 117 | + const result = cb(currentValue, context) |
146 | 118 |
|
147 | | - actionChain.emit('operator:end', { |
| 119 | + if (result instanceof Promise) { |
| 120 | + this.actionChain.emit('operator:async', { |
| 121 | + type, |
| 122 | + name, |
| 123 | + path, |
| 124 | + isAsync: true, |
| 125 | + ...context.__execution, |
| 126 | + operatorId, |
| 127 | + }) |
| 128 | + return result.then((promiseResult) => { |
| 129 | + this.actionChain.emit('operator:end', { |
148 | 130 | type, |
149 | 131 | name, |
150 | 132 | path, |
151 | 133 | ...context.__execution, |
152 | | - isAsync: false, |
153 | | - result: result, |
| 134 | + isAsync: true, |
| 135 | + result: promiseResult, |
154 | 136 | operatorId, |
155 | 137 | }) |
| 138 | + return promiseResult |
| 139 | + }) |
| 140 | + } |
156 | 141 |
|
157 | | - return result |
158 | | - } |
| 142 | + this.actionChain.emit('operator:end', { |
| 143 | + type, |
| 144 | + name, |
| 145 | + path, |
| 146 | + ...context.__execution, |
| 147 | + isAsync: false, |
| 148 | + result: result, |
| 149 | + operatorId, |
| 150 | + }) |
| 151 | + |
| 152 | + return result |
| 153 | + } |
159 | 154 |
|
160 | | - if (prevResult instanceof Promise) { |
161 | | - return prevResult.then(produceResult) |
162 | | - } |
| 155 | + if (prevResult instanceof Promise) { |
| 156 | + return prevResult.then(produceResult) |
| 157 | + } |
163 | 158 |
|
164 | | - return produceResult(prevResult) |
165 | | - }, |
166 | | - ] |
| 159 | + return produceResult(prevResult) |
167 | 160 | }, |
168 | | - } |
169 | | - ) |
| 161 | + ] |
| 162 | + } |
170 | 163 | } |
0 commit comments