@@ -4,7 +4,6 @@ import type { ThunkDispatch } from 'redux-thunk'
4
4
import { createAction } from '../createAction'
5
5
import { nanoid } from '../nanoid'
6
6
7
- import { find } from '../utils'
8
7
import {
9
8
TaskAbortError ,
10
9
listenerCancelled ,
@@ -221,9 +220,8 @@ export const createListenerEntry: TypedCreateListenerEntry<unknown> =
221
220
( options : FallbackAddListenerOptions ) => {
222
221
const { type, predicate, effect } = getListenerEntryPropsFrom ( options )
223
222
224
- const id = nanoid ( )
225
223
const entry : ListenerEntry < unknown > = {
226
- id,
224
+ id : nanoid ( ) ,
227
225
effect,
228
226
type,
229
227
predicate,
@@ -238,6 +236,22 @@ export const createListenerEntry: TypedCreateListenerEntry<unknown> =
238
236
{ withTypes : ( ) => createListenerEntry } ,
239
237
) as unknown as TypedCreateListenerEntry < unknown >
240
238
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
+
241
255
const cancelActiveListeners = (
242
256
entry : ListenerEntry < unknown , Dispatch < UnknownAction > > ,
243
257
) => {
@@ -330,7 +344,7 @@ export const createListenerMiddleware = <
330
344
assertFunction ( onError , 'onError' )
331
345
332
346
const insertEntry = ( entry : ListenerEntry ) => {
333
- entry . unsubscribe = ( ) => listenerMap . delete ( entry ! . id )
347
+ entry . unsubscribe = ( ) => listenerMap . delete ( entry . id )
334
348
335
349
listenerMap . set ( entry . id , entry )
336
350
return ( cancelOptions ?: UnsubscribeListenerOptions ) => {
@@ -342,14 +356,9 @@ export const createListenerMiddleware = <
342
356
}
343
357
344
358
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 )
353
362
354
363
return insertEntry ( entry )
355
364
} ) as AddListenerOverloads < any >
@@ -361,16 +370,7 @@ export const createListenerMiddleware = <
361
370
const stopListening = (
362
371
options : FallbackAddListenerOptions & UnsubscribeListenerOptions ,
363
372
) : 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 )
374
374
375
375
if ( entry ) {
376
376
entry . unsubscribe ( )
0 commit comments