Skip to content

Commit 2e84858

Browse files
refactor(action-chain): refactor to classes
1 parent 0cd6edf commit 2e84858

File tree

8 files changed

+433
-526
lines changed

8 files changed

+433
-526
lines changed
Lines changed: 95 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,41 @@
11
import { ActionChain, Execution, ExecutionContext } from './ActionChain'
2-
;(actionBaseFactory as any).nextActionId = 0
32

43
export class StopExecution {
54
value: any
65
constructor(value) {
76
this.value = value
87
}
98
}
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: (
3614
value: any,
3715
executionContext: {
3816
__execution: Execution
3917
__path: string[]
4018
},
4119
newPath?: string
4220
) => 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) {
4739
const initialOperator = typeof arguments[1] === 'undefined'
4840
const newPath = typeof arguments[2] === 'undefined' ? null : arguments[2]
4941
const executionContext: ExecutionContext = initialOperator
@@ -81,90 +73,91 @@ export function actionBaseFactory<
8173
}
8274

8375
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
107100

108-
function produceResult(currentValue) {
109-
if (currentValue instanceof StopExecution) {
110-
return currentValue
111-
}
101+
const operatorId = ++executionContextWithPath.__execution.operatorId
112102

113-
const context = actionChain.getContext(executionContextWithPath)
103+
const produceResult = (currentValue) => {
104+
if (currentValue instanceof StopExecution) {
105+
return currentValue
106+
}
114107

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)
123109

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)
146118

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', {
148130
type,
149131
name,
150132
path,
151133
...context.__execution,
152-
isAsync: false,
153-
result: result,
134+
isAsync: true,
135+
result: promiseResult,
154136
operatorId,
155137
})
138+
return promiseResult
139+
})
140+
}
156141

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+
}
159154

160-
if (prevResult instanceof Promise) {
161-
return prevResult.then(produceResult)
162-
}
155+
if (prevResult instanceof Promise) {
156+
return prevResult.then(produceResult)
157+
}
163158

164-
return produceResult(prevResult)
165-
},
166-
]
159+
return produceResult(prevResult)
167160
},
168-
}
169-
)
161+
]
162+
}
170163
}

packages/node_modules/action-chain/src/ActionChain.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,3 @@ export class ActionChain<Context> extends EventEmitter {
7676
return { ...providers, ...executionContext }
7777
}
7878
}
79-
80-
export function actionChainFactory<Context>(
81-
context: Context,
82-
options: ActionChainOptions = {}
83-
): ActionChain<Context> {
84-
return new ActionChain(context, options)
85-
}

0 commit comments

Comments
 (0)