@@ -413,81 +413,16 @@ export function createActionListenerMiddleware<
413
413
return entry . unsubscribe
414
414
}
415
415
416
- const middleware : Middleware <
417
- {
418
- ( action : Action < 'actionListenerMiddleware/add' > ) : Unsubscribe
419
- } ,
420
- S ,
421
- D
422
- > = ( api ) => ( next ) => ( action ) => {
423
- if ( addListenerAction . match ( action ) ) {
424
- let entry = findListenerEntry (
425
- ( existingEntry ) => existingEntry . listener === action . payload . listener
426
- )
427
-
428
- if ( ! entry ) {
429
- entry = action . payload
416
+ function findListenerEntry (
417
+ comparator : ( entry : ListenerEntry ) = > boolean
418
+ ) : ListenerEntry | undefined {
419
+ for ( const entry of listenerMap . values ( ) ) {
420
+ if ( comparator ( entry ) ) {
421
+ return entry
430
422
}
431
-
432
- return insertEntry ( entry )
433
- }
434
- if ( removeListenerAction . match ( action ) ) {
435
- removeListener ( action . payload . type , action . payload . listener )
436
- return
437
423
}
438
424
439
- if ( listenerMap . size === 0 ) {
440
- return next ( action )
441
- }
442
-
443
- let result : unknown
444
- const originalState = api . getState ( )
445
- const getOriginalState = ( ) => originalState
446
-
447
- for ( const currentPhase of actualMiddlewarePhases ) {
448
- let currentState = api . getState ( )
449
- for ( let entry of listenerMap . values ( ) ) {
450
- const runThisPhase =
451
- entry . when === 'both' || entry . when === currentPhase
452
-
453
- let runListener = runThisPhase
454
-
455
- if ( runListener ) {
456
- try {
457
- runListener = entry . predicate ( action , currentState , originalState )
458
- } catch ( predicateError ) {
459
- safelyNotifyError ( onError , predicateError )
460
- runListener = false
461
- }
462
- }
463
-
464
- if ( ! runListener ) {
465
- continue
466
- }
467
-
468
- try {
469
- entry . listener ( action , {
470
- ...api ,
471
- getOriginalState,
472
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
473
- condition,
474
- currentPhase,
475
- extra,
476
- unsubscribe : entry . unsubscribe ,
477
- subscribe : ( ) => {
478
- listenerMap . set ( entry . id , entry )
479
- } ,
480
- } )
481
- } catch ( listenerError ) {
482
- safelyNotifyError ( onError , listenerError )
483
- }
484
- }
485
- if ( currentPhase === 'beforeReducer' ) {
486
- result = next ( action )
487
- } else {
488
- return result
489
- }
490
- }
425
+ return undefined
491
426
}
492
427
493
428
const addListener = ( ( options : FallbackAddListenerOptions ) => {
@@ -531,18 +466,6 @@ export function createActionListenerMiddleware<
531
466
return true
532
467
}
533
468
534
- function findListenerEntry (
535
- comparator : ( entry : ListenerEntry ) => boolean
536
- ) : ListenerEntry | undefined {
537
- for ( const entry of listenerMap . values ( ) ) {
538
- if ( comparator ( entry ) ) {
539
- return entry
540
- }
541
- }
542
-
543
- return undefined
544
- }
545
-
546
469
const condition : ConditionFunction < S > = async ( predicate , timeout ) => {
547
470
let unsubscribe : Unsubscribe = ( ) => { }
548
471
@@ -578,6 +501,82 @@ export function createActionListenerMiddleware<
578
501
return result
579
502
}
580
503
504
+ const middleware : Middleware <
505
+ {
506
+ ( action : Action < 'actionListenerMiddleware/add' > ) : Unsubscribe
507
+ } ,
508
+ S ,
509
+ D
510
+ > = ( api ) => ( next ) => ( action ) => {
511
+ if ( addListenerAction . match ( action ) ) {
512
+ let entry = findListenerEntry (
513
+ ( existingEntry ) => existingEntry . listener === action . payload . listener
514
+ )
515
+
516
+ if ( ! entry ) {
517
+ entry = action . payload
518
+ }
519
+
520
+ return insertEntry ( entry )
521
+ }
522
+ if ( removeListenerAction . match ( action ) ) {
523
+ removeListener ( action . payload . type , action . payload . listener )
524
+ return
525
+ }
526
+
527
+ if ( listenerMap . size === 0 ) {
528
+ return next ( action )
529
+ }
530
+
531
+ let result : unknown
532
+ const originalState = api . getState ( )
533
+ const getOriginalState = ( ) => originalState
534
+
535
+ for ( const currentPhase of actualMiddlewarePhases ) {
536
+ let currentState = api . getState ( )
537
+ for ( let entry of listenerMap . values ( ) ) {
538
+ const runThisPhase =
539
+ entry . when === 'both' || entry . when === currentPhase
540
+
541
+ let runListener = runThisPhase
542
+
543
+ if ( runListener ) {
544
+ try {
545
+ runListener = entry . predicate ( action , currentState , originalState )
546
+ } catch ( predicateError ) {
547
+ safelyNotifyError ( onError , predicateError )
548
+ runListener = false
549
+ }
550
+ }
551
+
552
+ if ( ! runListener ) {
553
+ continue
554
+ }
555
+
556
+ try {
557
+ entry . listener ( action , {
558
+ ...api ,
559
+ getOriginalState,
560
+ condition,
561
+ currentPhase,
562
+ extra,
563
+ unsubscribe : entry . unsubscribe ,
564
+ subscribe : ( ) => {
565
+ listenerMap . set ( entry . id , entry )
566
+ } ,
567
+ } )
568
+ } catch ( listenerError ) {
569
+ safelyNotifyError ( onError , listenerError )
570
+ }
571
+ }
572
+ if ( currentPhase === 'beforeReducer' ) {
573
+ result = next ( action )
574
+ } else {
575
+ return result
576
+ }
577
+ }
578
+ }
579
+
581
580
return Object . assign (
582
581
middleware ,
583
582
{
0 commit comments