Skip to content

Commit

Permalink
fix 2941 (#2944)
Browse files Browse the repository at this point in the history
  • Loading branch information
urugator committed May 18, 2021
1 parent 87b3e1d commit d678ebd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-moons-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": patch
---

Fix [#2941](https://github.com/mobxjs/mobx/issues/2941) - flow.bound replaces method on proto with bound function
17 changes: 17 additions & 0 deletions packages/mobx/__tests__/v5/base/make-observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1617,3 +1617,20 @@ test("makeAutoObservable + override + annotation cache #2832", () => {
expect(isObservable(x.override)).toBe(false)
})
})

test("flow.bound #2941", async () => {
class Clazz {
constructor() {
makeObservable(this, {
flowBound: flow.bound
})
}
*flowBound() {
return this
}
}
new Clazz()
new Clazz()
expect(isFlow(Clazz.prototype.flowBound)).toBe(true)
expect(await Clazz.prototype.flowBound.call("ctx")).toBe("ctx")
})
7 changes: 4 additions & 3 deletions packages/mobx/src/types/flowannotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function make_(
// rest of the proto chain must be annotated already
return MakeResult.Break
}
const flowDescriptor = createFlowDescriptor(adm, this, key, descriptor, false)
const flowDescriptor = createFlowDescriptor(adm, this, key, descriptor, false, false)
defineProperty(source, key, flowDescriptor)
return MakeResult.Continue
}
Expand All @@ -52,7 +52,7 @@ function extend_(
descriptor: PropertyDescriptor,
proxyTrap: boolean
): boolean | null {
const flowDescriptor = createFlowDescriptor(adm, this, key, descriptor)
const flowDescriptor = createFlowDescriptor(adm, this, key, descriptor, this.options_?.bound)
return adm.defineProperty_(key, flowDescriptor, proxyTrap)
}

Expand All @@ -75,12 +75,13 @@ function createFlowDescriptor(
annotation: Annotation,
key: PropertyKey,
descriptor: PropertyDescriptor,
bound: boolean,
// provides ability to disable safeDescriptors for prototypes
safeDescriptors: boolean = globalState.safeDescriptors
): PropertyDescriptor {
assertFlowDescriptor(adm, annotation, key, descriptor)
let { value } = descriptor
if (annotation.options_?.bound) {
if (bound) {
value = value.bind(adm.proxy_ ?? adm.target_)
}
return {
Expand Down

0 comments on commit d678ebd

Please sign in to comment.