Skip to content

Commit

Permalink
Prevent Reaction From Keeping a Reference to The OldValue (#3812)
Browse files Browse the repository at this point in the history
  • Loading branch information
barroij authored Jan 10, 2024
1 parent 55260aa commit 620f78c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-pugs-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": patch
---

Prevent `reaction` from heeping a Reference to the OldValue that would prevent GC.
3 changes: 1 addition & 2 deletions packages/mobx/src/api/autorun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ export function reaction<T, FireImmediately extends boolean = false>(
let firstTime = true
let isScheduled = false
let value: T
let oldValue: T | undefined

const equals: IEqualsComparer<T> = (opts as any).compareStructural
? comparer.structural
Expand All @@ -165,10 +164,10 @@ export function reaction<T, FireImmediately extends boolean = false>(
return
}
let changed: boolean = false
const oldValue = value
r.track(() => {
const nextValue = allowStateChanges(false, () => expression(r))
changed = firstTime || !equals(value, nextValue)
oldValue = value
value = nextValue
})

Expand Down

0 comments on commit 620f78c

Please sign in to comment.