Skip to content

Commit

Permalink
Make return value of reportObserved match invoke of onBecomeObserved (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
upsuper committed Nov 7, 2022
1 parent 51a1338 commit fe25cfe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lemon-moles-clap.md
@@ -0,0 +1,5 @@
---
"mobx": patch
---

Make return value of reportObserved match invoke of onBecomeObserved
43 changes: 43 additions & 0 deletions packages/mobx/__tests__/v5/base/observables.js
Expand Up @@ -1400,6 +1400,49 @@ test("atom events #427", () => {
expect(stop).toBe(2)
})

test("#3563 reportObserved in batch", () => {
let start = 0
let stop = 0
let computed = 0
let observed = 0

const a = mobx.createAtom(
"test",
() => start++,
() => stop++
)
const c = mobx.computed(() => {
computed += 1
observed += a.reportObserved() ? 1 : 0
})
c.get()
expect(start).toBe(0)
expect(stop).toBe(0)
expect(computed).toBe(1)
expect(observed).toBe(0)

mobx.runInAction(() => {
c.get()
expect(start).toBe(0)
expect(stop).toBe(0)
expect(computed).toBe(2)
expect(observed).toBe(0)

c.get()
expect(computed).toBe(2)
expect(observed).toBe(0)
})

const c2 = mobx.computed(() => {
c.get()
})
c2.get()
expect(start).toBe(0)
expect(stop).toBe(0)
expect(computed).toBe(3)
expect(observed).toBe(0)
})

test("verify calculation count", () => {
const calcs = []
const a = observable.box(1)
Expand Down
2 changes: 1 addition & 1 deletion packages/mobx/src/core/observable.ts
Expand Up @@ -151,7 +151,7 @@ export function reportObserved(observable: IObservable): boolean {
observable.onBO()
}
}
return true
return observable.isBeingObserved_
} else if (observable.observers_.size === 0 && globalState.inBatch > 0) {
queueForUnobservation(observable)
}
Expand Down

0 comments on commit fe25cfe

Please sign in to comment.