Skip to content

Commit 30e9937

Browse files
Merge pull request cerebral#20 from cerebral/proxyProviders
feat(action-chain): use proxies to track execution of providers, allo…
2 parents 075deb7 + 8815570 commit 30e9937

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,39 @@ export function actionChainFactory<Context>(
3434
return options
3535
},
3636
getContext(executionContext: ExecutionContext) {
37+
const instance = this
3738
const providers = Object.keys(context).reduce((currentContext, key) => {
3839
if (IS_DEVELOPMENT && options.providerExceptions.indexOf(key) === -1) {
39-
currentContext[key] = Object.keys(context[key]).reduce(
40-
(currentProvider, method) => {
41-
currentProvider[method] = (...args) => {
42-
const result = context[key][method](...args)
43-
if (result instanceof Promise) {
44-
result.then((promisedResult) => {
45-
this.emit('provider', {
40+
currentContext[key] = new Proxy(context[key], {
41+
get(target, prop) {
42+
if (typeof target[prop] === 'function') {
43+
return (...args) => {
44+
const result = target[prop](...args)
45+
if (result instanceof Promise) {
46+
result.then((promisedResult) => {
47+
instance.emit('provider', {
48+
...executionContext.__execution,
49+
name: key,
50+
method: prop,
51+
result: promisedResult,
52+
})
53+
})
54+
} else {
55+
instance.emit('provider', {
4656
...executionContext.__execution,
4757
name: key,
48-
method,
49-
result: promisedResult,
58+
method: prop,
59+
result,
5060
})
51-
})
52-
} else {
53-
this.emit('provider', {
54-
...executionContext.__execution,
55-
name: key,
56-
method,
57-
result,
58-
})
61+
}
62+
63+
return result
5964
}
60-
return result
6165
}
6266

63-
return currentProvider
67+
return target[prop]
6468
},
65-
{}
66-
)
69+
})
6770
} else {
6871
currentContext[key] = context[key]
6972
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,19 @@ function actionFactory<Context, InitialValue, Value = InitialValue>(
8383
)
8484
}
8585

86+
class TestProvider {
87+
foo() {
88+
return 'bar'
89+
}
90+
}
91+
8692
const context = {
8793
foo: {
8894
bar() {
8995
return 'baz'
9096
},
9197
},
98+
test: new TestProvider(),
9299
}
93100

94101
type Context = typeof context
@@ -154,6 +161,24 @@ describe('PROVIDER', () => {
154161
})
155162
foo()
156163
})
164+
test('should track execution of clas instance providers', () => {
165+
expect.assertions(2)
166+
const foo = action().test((_, { test }) => {
167+
expect(test.foo()).toBe('bar')
168+
})
169+
170+
actionChain.once('provider', (task) => {
171+
expect(task).toEqual({
172+
operatorId: 0,
173+
actionId: 0,
174+
executionId: 0,
175+
method: 'foo',
176+
name: 'test',
177+
result: 'bar',
178+
})
179+
})
180+
foo()
181+
})
157182
})
158183

159184
describe('ACTION CHAIN', () => {

0 commit comments

Comments
 (0)