Added stubs for some native modules
Reviewed By: javache Differential Revision: D5111649 fbshipit-source-id: eef2f84556611dec01978d845b89fa145ec5d4db
This commit is contained in:
parent
bd5051adeb
commit
a93b7a2da0
|
@ -11,7 +11,7 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
const EventEmitter = require('EventEmitter');
|
||||
const MissingNativeEventEmitterShim = require('MissingNativeEventEmitterShim');
|
||||
const NativeEventEmitter = require('NativeEventEmitter');
|
||||
const NativeModules = require('NativeModules');
|
||||
const RCTAppState = NativeModules.AppState;
|
||||
|
@ -87,7 +87,7 @@ class AppState extends NativeEventEmitter {
|
|||
|
||||
_eventHandlers: Object;
|
||||
currentState: ?string;
|
||||
isAvailable: boolean;
|
||||
isAvailable: boolean = true;
|
||||
|
||||
constructor() {
|
||||
super(RCTAppState);
|
||||
|
@ -176,48 +176,31 @@ class AppState extends NativeEventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
function throwMissingNativeModule() {
|
||||
invariant(
|
||||
false,
|
||||
'Cannot use AppState module when native RCTAppState is not included in the build.\n' +
|
||||
'Either include it, or check AppState.isAvailable before calling any methods.'
|
||||
);
|
||||
}
|
||||
|
||||
class MissingNativeAppStateShim extends EventEmitter {
|
||||
// AppState
|
||||
isAvailable: boolean = false;
|
||||
currentState: ?string = null;
|
||||
|
||||
addEventListener() {
|
||||
throwMissingNativeModule();
|
||||
if (__DEV__ && !RCTAppState) {
|
||||
class MissingNativeAppStateShim extends MissingNativeEventEmitterShim {
|
||||
constructor() {
|
||||
super('RCTAppState', 'AppState');
|
||||
}
|
||||
|
||||
removeEventListener() {
|
||||
throwMissingNativeModule();
|
||||
get currentState(): ?string {
|
||||
this.throwMissingNativeModule();
|
||||
}
|
||||
|
||||
// EventEmitter
|
||||
addListener() {
|
||||
throwMissingNativeModule();
|
||||
addEventListener(...args: Array<any>) {
|
||||
this.throwMissingNativeModule();
|
||||
}
|
||||
|
||||
removeAllListeners() {
|
||||
throwMissingNativeModule();
|
||||
removeEventListener(...args: Array<any>) {
|
||||
this.throwMissingNativeModule();
|
||||
}
|
||||
}
|
||||
|
||||
removeSubscription() {
|
||||
throwMissingNativeModule();
|
||||
}
|
||||
}
|
||||
|
||||
// This module depends on the native `RCTAppState` module. If you don't include it,
|
||||
// `AppState.isAvailable` will return `false`, and any method calls will throw.
|
||||
// We reassign the class variable to keep the autodoc generator happy.
|
||||
if (RCTAppState) {
|
||||
AppState = new AppState();
|
||||
} else {
|
||||
// This module depends on the native `RCTAppState` module. If you don't include it,
|
||||
// `AppState.isAvailable` will return `false`, and any method calls will throw.
|
||||
// We reassign the class variable to keep the autodoc generator happy.
|
||||
AppState = new MissingNativeAppStateShim();
|
||||
} else {
|
||||
AppState = new AppState();
|
||||
}
|
||||
|
||||
module.exports = AppState;
|
||||
|
|
|
@ -13,9 +13,12 @@
|
|||
|
||||
if (__DEV__) {
|
||||
const AppState = require('AppState');
|
||||
const WebSocket = require('WebSocket');
|
||||
const {PlatformConstants} = require('NativeModules');
|
||||
const {connectToDevTools} = require('react-devtools-core');
|
||||
|
||||
// Initialize dev tools only if the native module for WebSocket is available
|
||||
if (WebSocket.isAvailable) {
|
||||
connectToDevTools({
|
||||
isAppActive() {
|
||||
// Don't steal the DevTools from currently active app.
|
||||
|
@ -33,4 +36,5 @@ if (__DEV__) {
|
|||
port: window.__REACT_DEVTOOLS_PORT__,
|
||||
resolveRNStyle: require('flattenStyle'),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @providesModule MissingNativeEventEmitterShim
|
||||
* @flow
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const EmitterSubscription = require('EmitterSubscription');
|
||||
const EventEmitter = require('EventEmitter');
|
||||
|
||||
const invariant = require('fbjs/lib/invariant');
|
||||
|
||||
class MissingNativeEventEmitterShim extends EventEmitter {
|
||||
isAvailable: boolean = false;
|
||||
_nativeModuleName: string;
|
||||
_nativeEventEmitterName: string;
|
||||
|
||||
constructor(nativeModuleName: string, nativeEventEmitterName: string) {
|
||||
super(null);
|
||||
this._nativeModuleName = nativeModuleName;
|
||||
this._nativeEventEmitterName = nativeEventEmitterName;
|
||||
}
|
||||
|
||||
throwMissingNativeModule() {
|
||||
invariant(
|
||||
false,
|
||||
`Cannot use '${this._nativeEventEmitterName}' module when ` +
|
||||
`native '${this._nativeModuleName}' is not included in the build. ` +
|
||||
`Either include it, or check '${this._nativeEventEmitterName}'.isAvailable ` +
|
||||
'before calling any methods.'
|
||||
);
|
||||
}
|
||||
|
||||
// EventEmitter
|
||||
addListener(eventType: string, listener: Function, context: ?Object): EmitterSubscription {
|
||||
this.throwMissingNativeModule();
|
||||
}
|
||||
|
||||
removeAllListeners(eventType: string) {
|
||||
this.throwMissingNativeModule();
|
||||
}
|
||||
|
||||
removeSubscription(subscription: EmitterSubscription) {
|
||||
this.throwMissingNativeModule();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MissingNativeEventEmitterShim;
|
|
@ -23,7 +23,6 @@ import type EmitterSubscription from 'EmitterSubscription';
|
|||
* a subset of the standard EventEmitter node module API.
|
||||
*/
|
||||
class NativeEventEmitter extends EventEmitter {
|
||||
|
||||
_nativeModule: Object;
|
||||
|
||||
constructor(nativeModule: Object) {
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
|
||||
// Do not require the native RCTNetworking module directly! Use this wrapper module instead.
|
||||
// It will add the necessary requestId, so that you don't have to generate it yourself.
|
||||
const FormData = require('FormData');
|
||||
const MissingNativeEventEmitterShim = require('MissingNativeEventEmitterShim');
|
||||
const NativeEventEmitter = require('NativeEventEmitter');
|
||||
const RCTNetworkingNative = require('NativeModules').Networking;
|
||||
const convertRequestBody = require('convertRequestBody');
|
||||
|
@ -43,6 +43,8 @@ function generateRequestId(): number {
|
|||
*/
|
||||
class RCTNetworking extends NativeEventEmitter {
|
||||
|
||||
isAvailable: boolean = true;
|
||||
|
||||
constructor() {
|
||||
super(RCTNetworkingNative);
|
||||
}
|
||||
|
@ -90,4 +92,31 @@ class RCTNetworking extends NativeEventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
module.exports = new RCTNetworking();
|
||||
if (__DEV__ && !RCTNetworkingNative) {
|
||||
class MissingNativeRCTNetworkingShim extends MissingNativeEventEmitterShim {
|
||||
constructor() {
|
||||
super('RCTAppState', 'AppState');
|
||||
}
|
||||
|
||||
sendRequest(...args: Array<any>) {
|
||||
this.throwMissingNativeModule();
|
||||
}
|
||||
|
||||
abortRequest(...args: Array<any>) {
|
||||
this.throwMissingNativeModule();
|
||||
}
|
||||
|
||||
clearCookies(...args: Array<any>) {
|
||||
this.throwMissingNativeModule();
|
||||
}
|
||||
}
|
||||
|
||||
// This module depends on the native `RCTNetworkingNative` module. If you don't include it,
|
||||
// `RCTNetworking.isAvailable` will return `false`, and any method calls will throw.
|
||||
// We reassign the class variable to keep the autodoc generator happy.
|
||||
RCTNetworking = new MissingNativeRCTNetworkingShim();
|
||||
} else {
|
||||
RCTNetworking = new RCTNetworking();
|
||||
}
|
||||
|
||||
module.exports = RCTNetworking;
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
const FormData = require('FormData');
|
||||
const MissingNativeEventEmitterShim = require('MissingNativeEventEmitterShim');
|
||||
const NativeEventEmitter = require('NativeEventEmitter');
|
||||
const RCTNetworkingNative = require('NativeModules').Networking;
|
||||
const convertRequestBody = require('convertRequestBody');
|
||||
|
@ -20,6 +20,8 @@ import type {RequestBody} from 'convertRequestBody';
|
|||
|
||||
class RCTNetworking extends NativeEventEmitter {
|
||||
|
||||
isAvailable: boolean = true;
|
||||
|
||||
constructor() {
|
||||
super(RCTNetworkingNative);
|
||||
}
|
||||
|
@ -58,4 +60,31 @@ class RCTNetworking extends NativeEventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
module.exports = new RCTNetworking();
|
||||
if (__DEV__ && !RCTNetworkingNative) {
|
||||
class MissingNativeRCTNetworkingShim extends MissingNativeEventEmitterShim {
|
||||
constructor() {
|
||||
super('RCTAppState', 'AppState');
|
||||
}
|
||||
|
||||
sendRequest(...args: Array<any>) {
|
||||
this.throwMissingNativeModule();
|
||||
}
|
||||
|
||||
abortRequest(...args: Array<any>) {
|
||||
this.throwMissingNativeModule();
|
||||
}
|
||||
|
||||
clearCookies(...args: Array<any>) {
|
||||
this.throwMissingNativeModule();
|
||||
}
|
||||
}
|
||||
|
||||
// This module depends on the native `RCTNetworkingNative` module. If you don't include it,
|
||||
// `RCTNetworking.isAvailable` will return `false`, and any method calls will throw.
|
||||
// We reassign the class variable to keep the autodoc generator happy.
|
||||
RCTNetworking = new MissingNativeRCTNetworkingShim();
|
||||
} else {
|
||||
RCTNetworking = new RCTNetworking();
|
||||
}
|
||||
|
||||
module.exports = RCTNetworking;
|
||||
|
|
|
@ -71,6 +71,10 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) {
|
|||
readyState: number = CONNECTING;
|
||||
url: ?string;
|
||||
|
||||
// This module depends on the native `RCTWebSocketModule` module. If you don't include it,
|
||||
// `WebSocket.isAvailable` will return `false`, and WebSocket constructor will throw an error
|
||||
static isAvailable: boolean = !!RCTWebSocketModule;
|
||||
|
||||
constructor(url: string, protocols: ?string | ?Array<string>, options: ?{origin?: string}) {
|
||||
super();
|
||||
if (typeof protocols === 'string') {
|
||||
|
@ -81,6 +85,11 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) {
|
|||
protocols = null;
|
||||
}
|
||||
|
||||
if (!WebSocket.isAvailable) {
|
||||
throw new Error('Cannot initialize WebSocket module. ' +
|
||||
'Native module RCTWebSocketModule is missing.');
|
||||
}
|
||||
|
||||
this._eventEmitter = new NativeEventEmitter(RCTWebSocketModule);
|
||||
this._socketId = nextWebSocketId++;
|
||||
this._registerEvents();
|
||||
|
|
Loading…
Reference in New Issue