mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
b70152cdef
Summary: In D24324247 (https://github.com/facebook/react-native/commit/56c363e39af6488904cbfd2046314c45babeb0f4), I split NativeLinking into NativeLinkingManager and NativeIntentAndroid. There was this line in NativeLinking.js, that I didn't migrate correctly: ``` export default ((Platform.OS === 'android' ? TurboModuleRegistry.getEnforcing<Spec>('IntentAndroid') : TurboModuleRegistry.getEnforcing<Spec>('LinkingManager')): Spec); ``` I separated this conditional statement into two others: ``` export default TurboModuleRegistry.getEnforcing<Spec>('IntentAndroid'); export default TurboModuleRegistry.getEnforcing<Spec>('LinkingManager'); ``` The problem here is that now on iOS, we're hard requiring IntentAndroid, and on Android, we're hard requiring LinkingManager. Understandably, this started throwing errors in our e2e infra. This diff fixes this problem by: 1. Changing the relevant `getEnforcing` calls into `get` calls. 2. Wrapping all usages of NativeIntentAndroid, and NativeLinkingManager, which are already guarded by `Platform.OS` checks, by a nullthrows. This should satisfy flow. **Note:** NativeIntentAndroid is only used on Android, where it must be available. Similarly, NativeLinkingManager is only used on iOS, where it must be available. Changelog: [Internal] build-break overriding_review_checks_triggers_an_audit_and_retroactive_review Oncall Short Name: fbandroid_sheriff Differential Revision: D24338558 fbshipit-source-id: b0d22cba77e67837834269deaa317dc73d2457dc
138 lines
3.7 KiB
JavaScript
138 lines
3.7 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @flow strict-local
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
|
|
import InteractionManager from '../Interaction/InteractionManager';
|
|
import Platform from '../Utilities/Platform';
|
|
import NativeLinkingManager from './NativeLinkingManager';
|
|
import NativeIntentAndroid from './NativeIntentAndroid';
|
|
import invariant from 'invariant';
|
|
import nullthrows from 'nullthrows';
|
|
|
|
/**
|
|
* `Linking` gives you a general interface to interact with both incoming
|
|
* and outgoing app links.
|
|
*
|
|
* See https://reactnative.dev/docs/linking.html
|
|
*/
|
|
class Linking extends NativeEventEmitter {
|
|
constructor() {
|
|
super(Platform.OS === 'ios' ? nullthrows(NativeLinkingManager) : undefined);
|
|
}
|
|
|
|
/**
|
|
* Add a handler to Linking changes by listening to the `url` event type
|
|
* and providing the handler
|
|
*
|
|
* See https://reactnative.dev/docs/linking.html#addeventlistener
|
|
*/
|
|
addEventListener<T>(type: string, handler: T) {
|
|
this.addListener(type, handler);
|
|
}
|
|
|
|
/**
|
|
* Remove a handler by passing the `url` event type and the handler.
|
|
*
|
|
* See https://reactnative.dev/docs/linking.html#removeeventlistener
|
|
*/
|
|
removeEventListener<T>(type: string, handler: T) {
|
|
this.removeListener(type, handler);
|
|
}
|
|
|
|
/**
|
|
* Try to open the given `url` with any of the installed apps.
|
|
*
|
|
* See https://reactnative.dev/docs/linking.html#openurl
|
|
*/
|
|
openURL(url: string): Promise<void> {
|
|
this._validateURL(url);
|
|
if (Platform.OS === 'android') {
|
|
return nullthrows(NativeIntentAndroid).openURL(url);
|
|
} else {
|
|
return nullthrows(NativeLinkingManager).openURL(url);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Determine whether or not an installed app can handle a given URL.
|
|
*
|
|
* See https://reactnative.dev/docs/linking.html#canopenurl
|
|
*/
|
|
canOpenURL(url: string): Promise<boolean> {
|
|
this._validateURL(url);
|
|
if (Platform.OS === 'android') {
|
|
return nullthrows(NativeIntentAndroid).canOpenURL(url);
|
|
} else {
|
|
return nullthrows(NativeLinkingManager).canOpenURL(url);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Open app settings.
|
|
*
|
|
* See https://reactnative.dev/docs/linking.html#opensettings
|
|
*/
|
|
openSettings(): Promise<void> {
|
|
if (Platform.OS === 'android') {
|
|
return nullthrows(NativeIntentAndroid).openSettings();
|
|
} else {
|
|
return nullthrows(NativeLinkingManager).openSettings();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* If the app launch was triggered by an app link,
|
|
* it will give the link url, otherwise it will give `null`
|
|
*
|
|
* See https://reactnative.dev/docs/linking.html#getinitialurl
|
|
*/
|
|
getInitialURL(): Promise<?string> {
|
|
return Platform.OS === 'android'
|
|
? InteractionManager.runAfterInteractions().then(() =>
|
|
nullthrows(NativeIntentAndroid).getInitialURL(),
|
|
)
|
|
: nullthrows(NativeLinkingManager).getInitialURL();
|
|
}
|
|
|
|
/*
|
|
* Launch an Android intent with extras (optional)
|
|
*
|
|
* @platform android
|
|
*
|
|
* See https://reactnative.dev/docs/linking.html#sendintent
|
|
*/
|
|
sendIntent(
|
|
action: string,
|
|
extras?: Array<{
|
|
key: string,
|
|
value: string | number | boolean,
|
|
...
|
|
}>,
|
|
): Promise<void> {
|
|
if (Platform.OS === 'android') {
|
|
return nullthrows(NativeIntentAndroid).sendIntent(action, extras);
|
|
} else {
|
|
return new Promise((resolve, reject) => reject(new Error('Unsupported')));
|
|
}
|
|
}
|
|
|
|
_validateURL(url: string) {
|
|
invariant(
|
|
typeof url === 'string',
|
|
'Invalid URL: should be a string. Was: ' + url,
|
|
);
|
|
invariant(url, 'Invalid URL: cannot be empty');
|
|
}
|
|
}
|
|
|
|
module.exports = (new Linking(): Linking);
|