Skip to content

Commit 6faeb25

Browse files
Merge pull request cerebral#23 from cerebral/ac-class
refactor(action-chain): class implementation for ActionChain
2 parents 4051135 + 58e61d6 commit 6faeb25

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

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

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ export type ExecutionContext = {
77
__path: string[]
88
}
99

10-
export interface ActionChain<Context> extends EventEmitter {
11-
getOptions(): ActionChainOptions
12-
getContext(executionContext: ExecutionContext): Context & ExecutionContext
13-
}
14-
1510
export type ActionChainOptions = {
1611
actionWrapper?: any
1712
providerExceptions?: string[]
@@ -23,21 +18,24 @@ export type Execution = {
2318
executionId: number
2419
}
2520

26-
export function actionChainFactory<Context>(
27-
context: Context,
28-
options: ActionChainOptions = {}
29-
): ActionChain<Context> {
30-
options.providerExceptions = options.providerExceptions || []
21+
export class ActionChain<Context> extends EventEmitter {
22+
constructor(
23+
private context: Context,
24+
private options: ActionChainOptions = {}
25+
) {
26+
super()
27+
this.options.providerExceptions = options.providerExceptions || []
28+
}
3129

32-
return Object.assign(new EventEmitter(), {
33-
getOptions() {
34-
return options
35-
},
36-
getContext(executionContext: ExecutionContext) {
37-
const instance = this
38-
const providers = Object.keys(context).reduce((currentContext, key) => {
39-
if (IS_DEVELOPMENT && options.providerExceptions.indexOf(key) === -1) {
40-
currentContext[key] = new Proxy(context[key], {
30+
getContext(executionContext: ExecutionContext) {
31+
const instance = this
32+
const providers = Object.keys(this.context).reduce(
33+
(currentContext, key) => {
34+
if (
35+
IS_DEVELOPMENT &&
36+
this.options.providerExceptions.indexOf(key) === -1
37+
) {
38+
currentContext[key] = new Proxy(this.context[key], {
4139
get(target, prop) {
4240
if (typeof target[prop] === 'function') {
4341
return (...args) => {
@@ -68,14 +66,20 @@ export function actionChainFactory<Context>(
6866
},
6967
})
7068
} else {
71-
currentContext[key] = context[key]
69+
currentContext[key] = this.context[key]
7270
}
7371

7472
return currentContext
75-
}, {})
73+
},
74+
{}
75+
)
76+
return { ...providers, ...executionContext }
77+
}
78+
}
7679

77-
return Object.assign({}, providers, executionContext) as Context &
78-
ExecutionContext
79-
},
80-
})
80+
export function actionChainFactory<Context>(
81+
context: Context,
82+
options: ActionChainOptions = {}
83+
): ActionChain<Context> {
84+
return new ActionChain(context, options)
8185
}

packages/node_modules/action-chain/src/index.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,18 @@ describe('CONTEXT', () => {
134134

135135
test('should be able to extend context', () => {
136136
expect.assertions(1)
137-
const foo = action().test((_, { foo }) => {
137+
const fn = action().test((_, { foo }) => {
138138
expect(foo.bar()).toBe('baz')
139139
})
140140

141-
foo()
141+
fn()
142142
})
143143
})
144144

145145
describe('PROVIDER', () => {
146146
test('should track execution of providers', () => {
147147
expect.assertions(2)
148-
const foo = action().test((_, { foo }) => {
148+
const fn = action().test((_, { foo }) => {
149149
expect(foo.bar()).toBe('baz')
150150
})
151151

@@ -159,11 +159,11 @@ describe('PROVIDER', () => {
159159
result: 'baz',
160160
})
161161
})
162-
foo()
162+
fn()
163163
})
164164
test('should track execution of clas instance providers', () => {
165165
expect.assertions(2)
166-
const foo = action().test((_, { test }) => {
166+
const fn = action().test((_, { test }) => {
167167
expect(test.foo()).toBe('bar')
168168
})
169169

@@ -177,14 +177,14 @@ describe('PROVIDER', () => {
177177
result: 'bar',
178178
})
179179
})
180-
foo()
180+
fn()
181181
})
182182
})
183183

184184
describe('ACTION CHAIN', () => {
185185
test('should track execution', () => {
186186
expect.assertions(4)
187-
const foo = action().test(() => 'foo')
187+
const fn = action().test(() => 'foo')
188188

189189
actionChain.once('action:start', (data) => {
190190
expect(data).toEqual({
@@ -221,7 +221,7 @@ describe('ACTION CHAIN', () => {
221221
})
222222
})
223223

224-
foo()
224+
fn()
225225
})
226226
test('should track async execution', () => {
227227
expect.assertions(2)

0 commit comments

Comments
 (0)