@@ -4,7 +4,6 @@ import type { ThunkDispatch } from 'redux-thunk'
44import { createAction } from '../createAction'
55import { nanoid } from '../nanoid'
66
7- import { find } from '../utils'
87import {
98 TaskAbortError ,
109 listenerCancelled ,
@@ -221,9 +220,8 @@ export const createListenerEntry: TypedCreateListenerEntry<unknown> =
221220 ( options : FallbackAddListenerOptions ) => {
222221 const { type, predicate, effect } = getListenerEntryPropsFrom ( options )
223222
224- const id = nanoid ( )
225223 const entry : ListenerEntry < unknown > = {
226- id,
224+ id : nanoid ( ) ,
227225 effect,
228226 type,
229227 predicate,
@@ -238,6 +236,22 @@ export const createListenerEntry: TypedCreateListenerEntry<unknown> =
238236 { withTypes : ( ) => createListenerEntry } ,
239237 ) as unknown as TypedCreateListenerEntry < unknown >
240238
239+ const findListenerEntry = (
240+ listenerMap : Map < string , ListenerEntry > ,
241+ options : FallbackAddListenerOptions ,
242+ ) => {
243+ const { type, effect, predicate } = getListenerEntryPropsFrom ( options )
244+
245+ return Array . from ( listenerMap . values ( ) ) . find ( ( entry ) => {
246+ const matchPredicateOrType =
247+ typeof type === 'string'
248+ ? entry . type === type
249+ : entry . predicate === predicate
250+
251+ return matchPredicateOrType && entry . effect === effect
252+ } )
253+ }
254+
241255const cancelActiveListeners = (
242256 entry : ListenerEntry < unknown , Dispatch < UnknownAction > > ,
243257) => {
@@ -330,7 +344,7 @@ export const createListenerMiddleware = <
330344 assertFunction ( onError , 'onError' )
331345
332346 const insertEntry = ( entry : ListenerEntry ) => {
333- entry . unsubscribe = ( ) => listenerMap . delete ( entry ! . id )
347+ entry . unsubscribe = ( ) => listenerMap . delete ( entry . id )
334348
335349 listenerMap . set ( entry . id , entry )
336350 return ( cancelOptions ?: UnsubscribeListenerOptions ) => {
@@ -342,14 +356,9 @@ export const createListenerMiddleware = <
342356 }
343357
344358 const startListening = ( ( options : FallbackAddListenerOptions ) => {
345- let entry = find (
346- Array . from ( listenerMap . values ( ) ) ,
347- ( existingEntry ) => existingEntry . effect === options . effect ,
348- )
349-
350- if ( ! entry ) {
351- entry = createListenerEntry ( options as any )
352- }
359+ const entry =
360+ findListenerEntry ( listenerMap , options ) ??
361+ createListenerEntry ( options as any )
353362
354363 return insertEntry ( entry )
355364 } ) as AddListenerOverloads < any >
@@ -361,16 +370,7 @@ export const createListenerMiddleware = <
361370 const stopListening = (
362371 options : FallbackAddListenerOptions & UnsubscribeListenerOptions ,
363372 ) : boolean => {
364- const { type, effect, predicate } = getListenerEntryPropsFrom ( options )
365-
366- const entry = find ( Array . from ( listenerMap . values ( ) ) , ( entry ) => {
367- const matchPredicateOrType =
368- typeof type === 'string'
369- ? entry . type === type
370- : entry . predicate === predicate
371-
372- return matchPredicateOrType && entry . effect === effect
373- } )
373+ const entry = findListenerEntry ( listenerMap , options )
374374
375375 if ( entry ) {
376376 entry . unsubscribe ( )
0 commit comments