Skip to content

Commit

Permalink
fix(reactive): fix dispose omission clean pendding reactions (#1994)
Browse files Browse the repository at this point in the history
* fix(reactive): fix dispose omission clean pendding reactions

* fix(reactive): fix ci

* test(reactive): add test cases
  • Loading branch information
janryWang authored Aug 12, 2021
1 parent 2e06559 commit a60134b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
16 changes: 16 additions & 0 deletions packages/reactive/src/__tests__/autorun.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,19 @@ test('autorun.effect not in autorun', () => {
test('autorun.effect with invalid params', () => {
autorun.effect({} as any)
})

test('autorun dispose in batch', () => {
const obs = observable({
value: 123,
})
const handler = jest.fn()
const dispose = autorun(() => {
handler(obs.value)
})

batch(() => {
obs.value = 321
dispose()
})
expect(handler).toBeCalledTimes(1)
})
2 changes: 0 additions & 2 deletions packages/reactive/src/autorun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ export const autorun = (tracker: Reaction, name = 'AutoRun') => {
cursor: 0,
}
}
reaction._disposed = false
reaction._boundary = 0
reaction._name = name
cleanRefs()
reaction()
return () => {
reaction._disposed = true
disposeBindingReactions(reaction)
disposeEffects(reaction)
cleanRefs()
Expand Down
43 changes: 20 additions & 23 deletions packages/reactive/src/reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,6 @@ export const bindComputedReactions = (reaction: Reaction) => {
}
}

export const suspendComputedReactions = (reaction: Reaction) => {
const computes = reaction._computesSet
if (computes) {
computes.forEach((reaction) => {
const reactions = getReactionsFromTargetKey(
reaction._context,
reaction._property
)
if (reactions.length === 0) {
disposeBindingReactions(reaction)
reaction._dirty = true
}
})
}
}

export const runReactionsFromTargetKey = (operation: IOperation) => {
let { key, type, target, oldTarget } = operation
notifyObservers(operation)
Expand All @@ -154,18 +138,31 @@ export const hasRunningReaction = () => {
}

export const releaseBindingReactions = (reaction: Reaction) => {
const bindingSet = reaction._reactionsSet
if (bindingSet) {
bindingSet.forEach((reactionsMap) => {
reactionsMap.forEach((reactions) => {
reactions.delete(reaction)
})
reaction._reactionsSet?.forEach((reactionsMap) => {
reactionsMap.forEach((reactions) => {
reactions.delete(reaction)
})
}
})
PendingReactions.delete(reaction)
PendingScopeReactions.delete(reaction)
delete reaction._reactionsSet
}

export const suspendComputedReactions = (current: Reaction) => {
current._computesSet?.forEach((reaction) => {
const reactions = getReactionsFromTargetKey(
reaction._context,
reaction._property
)
if (reactions.length === 0) {
disposeBindingReactions(reaction)
reaction._dirty = true
}
})
}

export const disposeBindingReactions = (reaction: Reaction) => {
reaction._disposed = true
releaseBindingReactions(reaction)
suspendComputedReactions(reaction)
}
Expand Down

0 comments on commit a60134b

Please sign in to comment.