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

[TM] Add spec for Linking #24877

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions Libraries/Linking/Linking.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,7 +26,7 @@ const LinkingManager =
*/
class Linking extends NativeEventEmitter {
constructor() {
super(LinkingManager);
super(NativeLinking);
}

/**
Expand Down Expand Up @@ -59,7 +55,7 @@ class Linking extends NativeEventEmitter {
*/
openURL(url: string): Promise<any> {
this._validateURL(url);
return LinkingManager.openURL(url);
return NativeLinking.openURL(url);
}

/**
Expand All @@ -69,7 +65,7 @@ class Linking extends NativeEventEmitter {
*/
canOpenURL(url: string): Promise<boolean> {
this._validateURL(url);
return LinkingManager.canOpenURL(url);
return NativeLinking.canOpenURL(url);
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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();
}

/*
Expand All @@ -103,10 +99,12 @@ 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}],
action: string,
extras?: Array<{key: string, value: string | number | boolean}>,
) {
return LinkingManager.sendIntent(action, extras);
if (Platform.OS === 'android') {
return NativeLinking.sendIntent(action, extras);
}
}

_validateURL(url: string) {
Expand Down
37 changes: 37 additions & 0 deletions Libraries/Linking/NativeLinking.js
Original file line number Diff line number Diff line change
@@ -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'));