Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Reaction From Keeping a Reference to The OldValue #3812

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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