Skip to content

Commit 051354f

Browse files
committed
fix(overmind): ensure backward compatibility by properly binding methods in namespaced modules
1 parent 5ebcd25 commit 051354f

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

packages/overmind/src/Overmind.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,35 @@ export class Overmind<ThisConfig extends IConfiguration>
399399

400400
// Special handling for StateMachine: bind methods to preserve context
401401
if (utils.isStateMachine(namespaceObj)) {
402-
const value = namespaceObj[prop]
403-
return typeof value === 'function' ? value.bind(namespaceObj) : value
402+
if (prop in namespaceObj) {
403+
const value = namespaceObj[prop]
404+
if (prop in obj && utils.isStateMachine(obj[prop])) {
405+
return obj[prop]
406+
}
407+
408+
if (typeof value === 'function') {
409+
return value.bind(namespaceObj)
410+
}
411+
return value
412+
}
413+
return obj[prop]
404414
}
405415

406416
// Standard scoping: prefer namespace, fallback to root
407-
return namespaceObj && prop in namespaceObj
408-
? namespaceObj[prop]
409-
: obj[prop]
417+
const value =
418+
namespaceObj && prop in namespaceObj ? namespaceObj[prop] : obj[prop]
419+
420+
// If we got a function from the namespace object (like an effect method),
421+
// bind it to maintain the correct 'this' context
422+
if (
423+
namespaceObj &&
424+
prop in namespaceObj &&
425+
typeof value === 'function'
426+
) {
427+
return value.bind(namespaceObj)
428+
}
429+
430+
return value
410431
},
411432

412433
set: (obj, prop, value) => {
@@ -474,7 +495,6 @@ export class Overmind<ThisConfig extends IConfiguration>
474495
private createContext(execution, tree) {
475496
const namespacePath = execution.namespacePath || []
476497

477-
// Create base actions proxy
478498
const actionsProxy = utils.createActionsProxy(this.actions, (action) => {
479499
return (value) => action(value, execution.isRunning ? execution : null)
480500
})

0 commit comments

Comments
 (0)