@@ -16,6 +16,14 @@ import invariant from 'invariant';
1616import { initAction } from './reducer' ;
1717
1818const reduxSubscribers = new Map ( ) ;
19+ function getReduxSubscribers ( key : string ) : Set < NavigationEventCallback > {
20+ let subscribers = reduxSubscribers . get ( key ) ;
21+ if ( ! subscribers ) {
22+ subscribers = new Set ( ) ;
23+ reduxSubscribers . set ( key , subscribers ) ;
24+ }
25+ return subscribers ;
26+ }
1927
2028// screenProps are a legacy concept in React Navigation,
2129// and should not be used in redux apps
@@ -26,16 +34,12 @@ function createReactNavigationReduxMiddleware<State: {}>(
2634 navStateSelector: (state: State) => NavigationState ,
2735 key ? : string = "root" ,
2836) : Middleware < State , * , * > {
29- reduxSubscribers. set ( key , new Set ( ) ) ;
3037 return store => next => action => {
3138 const oldState = store . getState ( ) ;
3239 const result = next ( action ) ;
3340 const newState = store . getState ( ) ;
34- const subscribers = reduxSubscribers . get ( key ) ;
35- invariant ( subscribers , `subscribers set should exist for ${ key } ` ) ;
3641 triggerAllSubscribers (
3742 key ,
38- subscribers ,
3943 {
4044 type : 'action' ,
4145 action,
@@ -49,12 +53,9 @@ function createReactNavigationReduxMiddleware<State: {}>(
4953
5054const delayedTriggers = new Map ( ) ;
5155
52- function triggerAllSubscribers (
53- key : string ,
54- subscribers : Set < NavigationEventCallback > ,
55- payload : NavigationEventPayload ,
56- ) {
57- const trigger = ( ) => subscribers . forEach ( subscriber => subscriber ( payload ) ) ;
56+ function triggerAllSubscribers ( key : string , payload : NavigationEventPayload ) {
57+ const trigger =
58+ ( ) => getReduxSubscribers ( key ) . forEach ( subscriber => subscriber ( payload ) ) ;
5859 if (
5960 ! payload . action . hasOwnProperty ( 'type' ) ||
6061 ! payload . action . type . startsWith ( "Navigation" ) ||
@@ -87,16 +88,8 @@ function createDidUpdateCallback(key: string) {
8788}
8889
8990function initializeListeners ( key : string , state : NavigationState ) {
90- const subscribers = reduxSubscribers . get ( key ) ;
91- invariant (
92- subscribers ,
93- "Before calling `createReduxContainer`, please call " +
94- "`createReactNavigationReduxMiddleware`, so that we know " +
95- "when to trigger your listener." ,
96- ) ;
9791 triggerAllSubscribers (
9892 key ,
99- subscribers ,
10093 {
10194 type : 'action' ,
10295 action : initAction ,
@@ -108,13 +101,6 @@ function initializeListeners(key: string, state: NavigationState) {
108101}
109102
110103function createNavigationPropConstructor ( key : string ) {
111- const actionSubscribers = reduxSubscribers . get ( key ) ;
112- invariant (
113- actionSubscribers ,
114- "Before calling `createReduxContainer`, please call " +
115- "`createReactNavigationReduxMiddleware`, so that we know " +
116- "when to trigger your listener." ,
117- ) ;
118104 return < State : NavigationState > (
119105 dispatch: NavigationDispatch,
120106 state: State,
@@ -139,7 +125,7 @@ function createNavigationPropConstructor(key: string) {
139125 router ,
140126 state ,
141127 dispatch ,
142- actionSubscribers ,
128+ getReduxSubscribers ( key ) ,
143129 getScreenProps ,
144130 getCurrentNavigation ,
145131 ) ;
0 commit comments