66 StoreEnhancer ,
77 Dispatch ,
88 Observer ,
9- ExtendState
9+ ExtendState ,
10+ ListenerCallback
1011} from './types/store'
1112import { Action } from './types/actions'
1213import { Reducer } from './types/reducers'
@@ -119,8 +120,9 @@ export function createStore<S, A extends Action, Ext = {}, StateExt = never>(
119120
120121 let currentReducer = reducer
121122 let currentState = preloadedState as S
122- let currentListeners : ( ( ) => void ) [ ] | null = [ ]
123+ let currentListeners : Map < number , ListenerCallback > | null = new Map ( )
123124 let nextListeners = currentListeners
125+ let listenerIdCounter = 0
124126 let isDispatching = false
125127
126128 /**
@@ -132,7 +134,10 @@ export function createStore<S, A extends Action, Ext = {}, StateExt = never>(
132134 */
133135 function ensureCanMutateNextListeners ( ) {
134136 if ( nextListeners === currentListeners ) {
135- nextListeners = currentListeners . slice ( )
137+ nextListeners = new Map ( )
138+ currentListeners . forEach ( ( listener , key ) => {
139+ nextListeners . set ( key , listener )
140+ } )
136141 }
137142 }
138143
@@ -197,7 +202,8 @@ export function createStore<S, A extends Action, Ext = {}, StateExt = never>(
197202 let isSubscribed = true
198203
199204 ensureCanMutateNextListeners ( )
200- nextListeners . push ( listener )
205+ const listenerId = listenerIdCounter ++
206+ nextListeners . set ( listenerId , listener )
201207
202208 return function unsubscribe ( ) {
203209 if ( ! isSubscribed ) {
@@ -214,8 +220,7 @@ export function createStore<S, A extends Action, Ext = {}, StateExt = never>(
214220 isSubscribed = false
215221
216222 ensureCanMutateNextListeners ( )
217- const index = nextListeners . indexOf ( listener )
218- nextListeners . splice ( index , 1 )
223+ nextListeners . delete ( listenerId )
219224 currentListeners = null
220225 }
221226 }
@@ -272,11 +277,9 @@ export function createStore<S, A extends Action, Ext = {}, StateExt = never>(
272277 }
273278
274279 const listeners = ( currentListeners = nextListeners )
275- for ( let i = 0 ; i < listeners . length ; i ++ ) {
276- const listener = listeners [ i ]
280+ listeners . forEach ( listener => {
277281 listener ( )
278- }
279-
282+ } )
280283 return action
281284 }
282285
0 commit comments