Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated removeEventListener() calls #297

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 31 additions & 5 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
Linking,
Platform,
AppState,
NativeModules
NativeModules,
EmitterSubscription
} from 'react-native';
import type {
BrowserResult,
Expand All @@ -22,6 +23,7 @@ import type {
export const RNInAppBrowser = NativeModules.RNInAppBrowser;

let _redirectHandler: ?(event: RedirectEvent) => void;
let _linkingEventSubscription: ?EmitterSubscription;

type AppStateStatus = typeof AppState.currentState

Expand All @@ -33,7 +35,10 @@ function waitForRedirectAsync(returnUrl: string): Promise<RedirectResult> {
}
};

Linking.addEventListener('url', _redirectHandler);
_linkingEventSubscription = Linking.addEventListener(
'url',
_redirectHandler
);
});
}

Expand All @@ -46,13 +51,26 @@ function handleAppStateActiveOnce(): Promise<void> {
if (AppState.currentState === 'active') {
return resolve();
}
let appStateEventSubscription: ?EmitterSubscription;

function handleAppStateChange(nextAppState: AppStateStatus) {
if (nextAppState === 'active') {
AppState.removeEventListener('change', handleAppStateChange);
if (
appStateEventSubscription &&
appStateEventSubscription.remove !== undefined
) {
appStateEventSubscription.remove();
} else {
AppState.removeEventListener('change', handleAppStateChange);
}
resolve();
}
}
AppState.addEventListener('change', handleAppStateChange);

appStateEventSubscription = AppState.addEventListener(
'change',
handleAppStateChange
);
});
}

Expand Down Expand Up @@ -137,7 +155,15 @@ export async function openAuthSessionPolyfillAsync(

export function closeAuthSessionPolyfillAsync(): void {
if (_redirectHandler) {
Linking.removeEventListener('url', _redirectHandler);
if (
_linkingEventSubscription &&
_linkingEventSubscription.remove !== undefined
) {
_linkingEventSubscription.remove();
_linkingEventSubscription = null;
} else {
Linking.removeEventListener('url', _redirectHandler);
}
_redirectHandler = null;
}
}
Expand Down