Skip to content

Commit

Permalink
resolve memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Dec 18, 2024
1 parent 9fe4b36 commit 85c947a
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion tests/vanilla/effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Ref = {
inProgress: number
isPending: boolean
deps: Set<AnyAtom>
unsub?: () => void
}

function atomSyncEffect(effect: Effect) {
Expand Down Expand Up @@ -51,6 +52,7 @@ function atomSyncEffect(effect: Effect) {
ref.cleanup = null
ref.isPending = false
ref.deps.clear()
ref.unsub?.()
}
},
)
Expand All @@ -67,7 +69,7 @@ function atomSyncEffect(effect: Effect) {
ref.isPending = true
})
internalAtom.unstable_onInit = (store) => {
store.unstable_onChange(() => {
const unsub = store.unstable_onChange(() => {
const ref = store.get(refAtom)
if (!ref.isPending || ref.inProgress > 0) {
return
Expand All @@ -87,6 +89,7 @@ function atomSyncEffect(effect: Effect) {
}
: null
})
new FinalizationRegistry(unsub).register(internalAtom, null)
}
if (process.env.NODE_ENV !== 'production') {
refAtom.debugPrivate = true
Expand Down

0 comments on commit 85c947a

Please # to comment.