Skip to content

Commit a0a4299

Browse files
fix(overmind): should proxify effects more dynamically
1 parent 9a91cff commit a0a4299

File tree

2 files changed

+24
-46
lines changed

2 files changed

+24
-46
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ export class Overmind<ThisConfig extends IConfiguration>
678678
return actionFunc
679679
}
680680
private trackEffects(effects = {}, execution) {
681+
681682
if (process.env.NODE_ENV === 'production') {
682683
return effects
683684
}

packages/node_modules/overmind/src/proxyfyEffects.ts

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,36 @@ function isObject(value) {
77
let hasWarnedConstructor = false
88
let currentEffectId = 0
99

10-
function wrapEffect(target, prop, path, cb) {
11-
const func = function(...args) {
12-
const effectId = currentEffectId++
13-
14-
return cb({
15-
func: target[prop].bind(target),
16-
effectId,
17-
name: path,
18-
method: prop,
19-
args,
20-
})
21-
}
22-
23-
return new Proxy(func, {
24-
construct(_, args) {
10+
export function proxifyEffects<Effects>(
11+
effects: Effects,
12+
cb: (effect) => void,
13+
path: string = ''
14+
): Effects {
15+
return new Proxy(effects as any, {
16+
apply(target, thisArg, agumentsList) {
17+
const effectId = currentEffectId++
18+
const name = path.split('.')
19+
const method = name.pop()
20+
return cb({
21+
func: target.bind(thisArg),
22+
effectId,
23+
name: name.join('.'),
24+
method,
25+
args: agumentsList,
26+
})
27+
},
28+
construct(Target, args) {
2529
if (!hasWarnedConstructor) {
2630
console.warn(
27-
`EFFECTS - It is highly recommended to create a custom effect, exposing a method that deals with the instantiation of "${path}.${prop.toString()}". It improves readability and debugability of your app`
31+
`EFFECTS - It is highly recommended to create a custom effect, exposing a method that deals with the instantiation of "${path}". It improves readability and debugability of your app`
2832
)
2933
hasWarnedConstructor = true
3034
}
3135

32-
return new target[prop](...args)
36+
return new Target(...args)
3337
},
34-
})
35-
}
36-
37-
function createProxyGetHandler(
38-
path: string,
39-
cb: (effect) => void
40-
): ProxyHandler<any>['get'] {
41-
return (target, prop) => {
42-
if (typeof target[prop] === 'function') {
43-
return wrapEffect(target, prop, path, cb)
38+
get(target, prop) {
39+
return proxifyEffects(target[prop], cb, path ? path + '.' + prop.toString() : prop.toString())
4440
}
45-
46-
if (isObject(target[prop])) {
47-
return new Proxy(target[prop], {
48-
get: createProxyGetHandler(
49-
path ? path + '.' + prop.toString() : prop.toString(),
50-
cb
51-
),
52-
})
53-
}
54-
return target[prop]
55-
}
56-
}
57-
58-
export function proxifyEffects<Effects>(
59-
effects: Effects,
60-
cb: (effect) => void
61-
): Effects {
62-
return new Proxy(effects, {
63-
get: createProxyGetHandler('', cb),
6441
})
6542
}

0 commit comments

Comments
 (0)