99 Linking ,
1010 Platform ,
1111 AppState ,
12- NativeModules
12+ NativeModules ,
13+ EmitterSubscription
1314} from 'react-native' ;
1415import type {
1516 BrowserResult ,
@@ -21,19 +22,22 @@ import type {
2122
2223export const RNInAppBrowser = NativeModules . RNInAppBrowser ;
2324
24- let _redirectHandler : ?( event : RedirectEvent ) = > void ;
25+ let _linkingEventSubscription : ?EmitterSubscription ;
2526
2627type AppStateStatus = typeof AppState . currentState
2728
2829function waitForRedirectAsync ( returnUrl : string ) : Promise < RedirectResult > {
2930 return new Promise ( function ( resolve ) {
30- _redirectHandler = ( event : RedirectEvent ) => {
31+ const redirectHandler = ( event : RedirectEvent ) => {
3132 if ( event . url && event . url . startsWith ( returnUrl ) ) {
3233 resolve ( { url : event . url , type : 'success' } ) ;
3334 }
3435 } ;
3536
36- Linking . addEventListener ( 'url' , _redirectHandler ) ;
37+ _linkingEventSubscription = Linking . addEventListener (
38+ 'url' ,
39+ redirectHandler
40+ ) ;
3741 } ) ;
3842}
3943
@@ -46,13 +50,21 @@ function handleAppStateActiveOnce(): Promise<void> {
4650 if ( AppState . currentState === 'active' ) {
4751 return resolve ( ) ;
4852 }
53+ let appStateEventSubscription : ?EmitterSubscription ;
54+
4955 function handleAppStateChange ( nextAppState : AppStateStatus ) {
5056 if ( nextAppState === 'active' ) {
51- AppState . removeEventListener ( 'change' , handleAppStateChange ) ;
57+ if ( appStateEventSubscription ) {
58+ appStateEventSubscription . remove ( ) ;
59+ }
5260 resolve ( ) ;
5361 }
5462 }
55- AppState . addEventListener ( 'change' , handleAppStateChange ) ;
63+
64+ appStateEventSubscription = AppState . addEventListener (
65+ 'change' ,
66+ handleAppStateChange
67+ ) ;
5668 } ) ;
5769}
5870
@@ -117,8 +129,8 @@ export async function openAuthSessionPolyfillAsync(
117129 options ? : InAppBrowserOptions
118130) : Promise < AuthSessionResult > {
119131 invariant (
120- ! _redirectHandler ,
121- 'InAppBrowser.openAuth is in a bad state. _redirectHandler is defined when it should not be.'
132+ ! _linkingEventSubscription ,
133+ 'InAppBrowser.openAuth is in a bad state. _linkingEventSubscription is defined when it should not be.'
122134 ) ;
123135 let response = null ;
124136 try {
@@ -136,9 +148,9 @@ export async function openAuthSessionPolyfillAsync(
136148}
137149
138150export function closeAuthSessionPolyfillAsync ( ) : void {
139- if ( _redirectHandler ) {
140- Linking . removeEventListener ( 'url' , _redirectHandler ) ;
141- _redirectHandler = null ;
151+ if ( _linkingEventSubscription ) {
152+ _linkingEventSubscription . remove ( ) ;
153+ _linkingEventSubscription = null ;
142154 }
143155}
144156
0 commit comments