11import { isFn } from './checkers'
22import { ArraySet } from './array'
3+ import { ReactionsArraySet } from './reactions-array-set'
34import { IOperation , ReactionsMap , Reaction , PropertyKey } from './types'
45import {
56 ReactionStack ,
@@ -28,28 +29,29 @@ const addRawReactionsMap = (
2829 const reactions = reactionsMap . get ( key )
2930 if ( reactions ) {
3031 reactions . add ( reaction )
32+ return reactions
3133 } else {
32- reactionsMap . set ( key , new ArraySet ( [ reaction ] ) )
34+ reactionsMap . set ( key , new ReactionsArraySet ( [ reaction ] ) )
35+ return reactionsMap . get ( key )
3336 }
34- return reactionsMap
3537 } else {
3638 const reactionsMap : ReactionsMap = new Map ( [
37- [ key , new ArraySet ( [ reaction ] ) ] ,
39+ [ key , new ReactionsArraySet ( [ reaction ] ) ] ,
3840 ] )
3941 RawReactionsMap . set ( target , reactionsMap )
40- return reactionsMap
42+ return reactionsMap . get ( key )
4143 }
4244}
4345
44- const addReactionsMapToReaction = (
46+ const addReactionsSetToReaction = (
4547 reaction : Reaction ,
46- reactionsMap : ReactionsMap
48+ reactionsSet : ReactionsArraySet < Reaction >
4749) => {
4850 const bindSet = reaction . _reactionsSet
4951 if ( bindSet ) {
50- bindSet . add ( reactionsMap )
52+ bindSet . add ( reactionsSet )
5153 } else {
52- reaction . _reactionsSet = new ArraySet ( [ reactionsMap ] )
54+ reaction . _reactionsSet = new ArraySet ( [ reactionsSet ] )
5355 }
5456 return bindSet
5557}
@@ -61,9 +63,7 @@ export const getReactionsFromTargetKey = (target: any, key: PropertyKey) => {
6163 const map = reactionsMap . get ( key )
6264 if ( map ) {
6365 map . forEach ( ( reaction ) => {
64- if ( reactions . indexOf ( reaction ) === - 1 ) {
65- reactions . push ( reaction )
66- }
66+ reactions . push ( reaction )
6767 } )
6868 }
6969 }
@@ -78,7 +78,6 @@ const runReactions = (target: any, key: PropertyKey) => {
7878 const reaction = reactions [ i ]
7979 if ( reaction . _isComputed ) {
8080 reaction . _dirty = true
81- reaction . _dirty_scheduler = true
8281 if ( isScopeBatching ( ) ) {
8382 PendingScopeComputedReactions . add ( reaction )
8483 } else if ( isBatching ( ) ) {
@@ -117,7 +116,7 @@ export const bindTargetKeyWithCurrentReaction = (operation: IOperation) => {
117116 if ( isUntracking ( ) ) return
118117 if ( current ) {
119118 DependencyCollected . value = true
120- addReactionsMapToReaction ( current , addRawReactionsMap ( target , key , current ) )
119+ addReactionsSetToReaction ( current , addRawReactionsMap ( target , key , current ) )
121120 }
122121}
123122
@@ -158,13 +157,11 @@ export const hasRunningReaction = () => {
158157}
159158
160159export const releaseBindingReactions = ( reaction : Reaction ) => {
161- reaction . _reactionsSet ?. forEach ( ( reactionsMap ) => {
162- reactionsMap . forEach ( ( reactions ) => {
163- reactions . delete ( reaction )
164- } )
165- } )
166- PendingReactions . delete ( reaction )
167- PendingScopeReactions . delete ( reaction )
160+ // delete outdated reaction by _reactionId
161+ if ( typeof reaction . _reactionId !== 'number' ) {
162+ reaction . _reactionId = 0
163+ } else reaction . _reactionId ++
164+
168165 delete reaction . _reactionsSet
169166}
170167
@@ -183,6 +180,11 @@ export const suspendComputedReactions = (current: Reaction) => {
183180
184181export const disposeBindingReactions = ( reaction : Reaction ) => {
185182 reaction . _disposed = true
183+
184+ reaction . _reactionsSet ?. forEach ( ( reactionsSet ) => {
185+ reactionsSet . delete ( reaction )
186+ } )
187+
186188 releaseBindingReactions ( reaction )
187189 suspendComputedReactions ( reaction )
188190}
0 commit comments