Skip to content

Commit b05ebde

Browse files
author
Marcel Lasaj
committed
Made the change backwards-compatible with older RN versions
1 parent 89c256a commit b05ebde

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

utils.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,22 @@ import type {
2222

2323
export const RNInAppBrowser = NativeModules.RNInAppBrowser;
2424

25+
let _redirectHandler: ?(event: RedirectEvent) => void;
2526
let _linkingEventSubscription: ?EmitterSubscription;
2627

2728
type AppStateStatus = typeof AppState.currentState
2829

2930
function waitForRedirectAsync(returnUrl: string): Promise<RedirectResult> {
3031
return new Promise(function (resolve) {
31-
const redirectHandler = (event: RedirectEvent) => {
32+
_redirectHandler = (event: RedirectEvent) => {
3233
if (event.url && event.url.startsWith(returnUrl)) {
3334
resolve({ url: event.url, type: 'success' });
3435
}
3536
};
3637

3738
_linkingEventSubscription = Linking.addEventListener(
3839
'url',
39-
redirectHandler
40+
_redirectHandler
4041
);
4142
});
4243
}
@@ -54,8 +55,13 @@ function handleAppStateActiveOnce(): Promise<void> {
5455

5556
function handleAppStateChange(nextAppState: AppStateStatus) {
5657
if (nextAppState === 'active') {
57-
if (appStateEventSubscription) {
58+
if (
59+
appStateEventSubscription &&
60+
appStateEventSubscription.remove !== undefined
61+
) {
5862
appStateEventSubscription.remove();
63+
} else {
64+
AppState.removeEventListener('change', handleAppStateChange);
5965
}
6066
resolve();
6167
}
@@ -129,8 +135,8 @@ export async function openAuthSessionPolyfillAsync(
129135
options?: InAppBrowserOptions
130136
): Promise<AuthSessionResult> {
131137
invariant(
132-
!_linkingEventSubscription,
133-
'InAppBrowser.openAuth is in a bad state. _linkingEventSubscription is defined when it should not be.'
138+
!_redirectHandler,
139+
'InAppBrowser.openAuth is in a bad state. _redirectHandler is defined when it should not be.'
134140
);
135141
let response = null;
136142
try {
@@ -148,9 +154,17 @@ export async function openAuthSessionPolyfillAsync(
148154
}
149155

150156
export function closeAuthSessionPolyfillAsync(): void {
151-
if (_linkingEventSubscription) {
152-
_linkingEventSubscription.remove();
153-
_linkingEventSubscription = null;
157+
if (_redirectHandler) {
158+
if (
159+
_linkingEventSubscription &&
160+
_linkingEventSubscription.remove !== undefined
161+
) {
162+
_linkingEventSubscription.remove();
163+
_linkingEventSubscription = null;
164+
} else {
165+
Linking.removeEventListener('url', _redirectHandler);
166+
}
167+
_redirectHandler = null;
154168
}
155169
}
156170

0 commit comments

Comments
 (0)