Add spec for Linking (#24877)
Summary: Part of #24875, adds a spec for linking. ## Changelog [General] [Added] - TM Spec for Linking Pull Request resolved: https://github.com/facebook/react-native/pull/24877 Differential Revision: D15374328 Pulled By: fkgozali fbshipit-source-id: 4b86a75d58d275c0ddc864b4f3f1ec489b0b408b
This commit is contained in:
Родитель
c3c3c3c655
Коммит
99899d008f
|
@ -12,15 +12,11 @@
|
|||
|
||||
const InteractionManager = require('../Interaction/InteractionManager');
|
||||
const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter');
|
||||
const NativeModules = require('../BatchedBridge/NativeModules');
|
||||
const Platform = require('../Utilities/Platform');
|
||||
|
||||
const invariant = require('invariant');
|
||||
|
||||
const LinkingManager =
|
||||
Platform.OS === 'android'
|
||||
? NativeModules.IntentAndroid
|
||||
: NativeModules.LinkingManager;
|
||||
import NativeLinking from './NativeLinking';
|
||||
|
||||
/**
|
||||
* `Linking` gives you a general interface to interact with both incoming
|
||||
|
@ -30,7 +26,7 @@ const LinkingManager =
|
|||
*/
|
||||
class Linking extends NativeEventEmitter {
|
||||
constructor() {
|
||||
super(LinkingManager);
|
||||
super(NativeLinking);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -59,7 +55,7 @@ class Linking extends NativeEventEmitter {
|
|||
*/
|
||||
openURL(url: string): Promise<any> {
|
||||
this._validateURL(url);
|
||||
return LinkingManager.openURL(url);
|
||||
return NativeLinking.openURL(url);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -69,7 +65,7 @@ class Linking extends NativeEventEmitter {
|
|||
*/
|
||||
canOpenURL(url: string): Promise<boolean> {
|
||||
this._validateURL(url);
|
||||
return LinkingManager.canOpenURL(url);
|
||||
return NativeLinking.canOpenURL(url);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,7 +74,7 @@ class Linking extends NativeEventEmitter {
|
|||
* See https://facebook.github.io/react-native/docs/linking.html#opensettings
|
||||
*/
|
||||
openSettings(): Promise<any> {
|
||||
return LinkingManager.openSettings();
|
||||
return NativeLinking.openSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,9 +86,9 @@ class Linking extends NativeEventEmitter {
|
|||
getInitialURL(): Promise<?string> {
|
||||
return Platform.OS === 'android'
|
||||
? InteractionManager.runAfterInteractions().then(() =>
|
||||
LinkingManager.getInitialURL(),
|
||||
NativeLinking.getInitialURL(),
|
||||
)
|
||||
: LinkingManager.getInitialURL();
|
||||
: NativeLinking.getInitialURL();
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -103,10 +99,13 @@ class Linking extends NativeEventEmitter {
|
|||
* See https://facebook.github.io/react-native/docs/linking.html#sendintent
|
||||
*/
|
||||
sendIntent(
|
||||
action: String,
|
||||
extras?: [{key: string, value: string | number | boolean}],
|
||||
) {
|
||||
return LinkingManager.sendIntent(action, extras);
|
||||
action: string,
|
||||
extras?: Array<{key: string, value: string | number | boolean}>,
|
||||
): Promise<void> {
|
||||
if (Platform.OS === 'android') {
|
||||
return NativeLinking.sendIntent(action, extras);
|
||||
}
|
||||
return new Promise((resolve, reject) => reject(new Error('Unsupported')));
|
||||
}
|
||||
|
||||
_validateURL(url: string) {
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
* 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.
|
||||
*
|
||||
* @flow
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import type {TurboModule} from 'RCTExport';
|
||||
import * as TurboModuleRegistry from 'TurboModuleRegistry';
|
||||
import Platform from 'Platform';
|
||||
|
||||
export interface Spec extends TurboModule {
|
||||
// Common interface
|
||||
+getInitialURL: () => Promise<string>;
|
||||
+canOpenURL: (url: string) => Promise<boolean>;
|
||||
+openURL: (url: string) => Promise<void>;
|
||||
+openSettings: () => Promise<void>;
|
||||
|
||||
// Android only
|
||||
+sendIntent: (
|
||||
action: string,
|
||||
extras: ?Array<{key: string, value: string | number | boolean}>,
|
||||
) => Promise<void>;
|
||||
|
||||
// Events
|
||||
+addListener: (eventName: string) => void;
|
||||
+removeListeners: (count: number) => void;
|
||||
}
|
||||
|
||||
export default (Platform.OS === 'ios'
|
||||
? TurboModuleRegistry.getEnforcing<Spec>('LinkingManager')
|
||||
: TurboModuleRegistry.getEnforcing<Spec>('IntentAndroid'));
|
Загрузка…
Ссылка в новой задаче