Permalink
Browse files

Allow lazy modules registration with the BatchedBridge

Reviewed By: javache

Differential Revision: D5181849

fbshipit-source-id: f63562c360488a9818605b25c1338214daac7411
  • Loading branch information...
fromcelticpark authored and facebook-github-bot committed Jun 6, 2017
1 parent a939109 commit 485bb70691621c903329d30f6c6f923a4a2e51a5
@@ -42,7 +42,7 @@ const TRACE_TAG_REACT_APPS = 1 << 17;
const DEBUG_INFO_LIMIT = 32;
class MessageQueue {
_callableModules: {[key: string]: Object};
_lazyCallableModules: {[key: string]: void => Object};
_queue: [Array<number>, Array<number>, Array<any>, number];
_successCallbacks: Array<?Function>;
_failureCallbacks: Array<?Function>;
@@ -58,7 +58,7 @@ class MessageQueue {
__spy: ?(data: SpyData) => void;
constructor() {
this._callableModules = {};
this._lazyCallableModules = {};
this._queue = [[], [], [], 0];
this._successCallbacks = [];
this._failureCallbacks = [];
@@ -136,7 +136,23 @@ class MessageQueue {
}
registerCallableModule(name: string, module: Object) {
this._callableModules[name] = module;
this._lazyCallableModules[name] = () => module;
}
registerLazyCallableModule(name: string, factory: void => Object) {
let module: Object;
let getValue: ?(void => Object) = factory;
this._lazyCallableModules[name] = () => {
if (getValue) {
module = getValue();
getValue = null;
}
return module;
};
}
_getCallableModule(name: string) {
return this._lazyCallableModules[name]();
}
enqueueNativeCall(moduleID: number, methodID: number, params: Array<any>, onFail: ?Function, onSucc: ?Function) {
@@ -230,7 +246,7 @@ class MessageQueue {
if (this.__spy) {
this.__spy({ type: TO_JS, module, method, args});
}
const moduleMethods = this._callableModules[module];
const moduleMethods = this._getCallableModule(module);
invariant(
!!moduleMethods,
'Module %s is not a registered callable module (calling %s)',
@@ -106,20 +106,18 @@ ExceptionsManager.installConsoleErrorReporter();
// TODO: Move these around to solve the cycle in a cleaner way
const BatchedBridge = require('BatchedBridge');
BatchedBridge.registerCallableModule('Systrace', require('Systrace'));
BatchedBridge.registerCallableModule('JSTimersExecution', require('JSTimersExecution'));
BatchedBridge.registerCallableModule('HeapCapture', require('HeapCapture'));
BatchedBridge.registerCallableModule('SamplingProfiler', require('SamplingProfiler'));
BatchedBridge.registerLazyCallableModule('Systrace', () => require('Systrace'));
BatchedBridge.registerLazyCallableModule('JSTimersExecution', () => require('JSTimersExecution'));
BatchedBridge.registerLazyCallableModule('HeapCapture', () => require('HeapCapture'));
BatchedBridge.registerLazyCallableModule('SamplingProfiler', () => require('SamplingProfiler'));
BatchedBridge.registerLazyCallableModule('RCTLog', () => require('RCTLog'));
if (__DEV__) {
if (!global.__RCTProfileIsProfiling) {
BatchedBridge.registerCallableModule('HMRClient', require('HMRClient'));
}
}
// RCTLog needs to register with BatchedBridge
require('RCTLog');
// Set up error handler
if (!global.__fbDisableExceptionsManager) {
const handleError = (e, isFatal) => {
@@ -226,6 +224,6 @@ if (__DEV__) {
// Just to make sure the JS gets packaged up. Wait until the JS environment has
// been initialized before requiring them.
require('RCTDeviceEventEmitter');
require('RCTNativeAppEventEmitter');
require('PerformanceLogger');
BatchedBridge.registerLazyCallableModule('RCTDeviceEventEmitter', () => require('RCTDeviceEventEmitter'));
BatchedBridge.registerLazyCallableModule('RCTNativeAppEventEmitter', () => require('RCTNativeAppEventEmitter'));
BatchedBridge.registerLazyCallableModule('PerformanceLogger', () => require('PerformanceLogger'));
@@ -13,7 +13,6 @@
const EventEmitter = require('EventEmitter');
const EventSubscriptionVendor = require('EventSubscriptionVendor');
const BatchedBridge = require('BatchedBridge');
import type EmitterSubscription from 'EmitterSubscription';
@@ -70,11 +69,4 @@ class RCTDeviceEventEmitter extends EventEmitter {
}
}
RCTDeviceEventEmitter = new RCTDeviceEventEmitter();
BatchedBridge.registerCallableModule(
'RCTDeviceEventEmitter',
RCTDeviceEventEmitter
);
module.exports = RCTDeviceEventEmitter;
module.exports = new RCTDeviceEventEmitter();
@@ -11,18 +11,11 @@
*/
'use strict';
const BatchedBridge = require('BatchedBridge');
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
/**
* Deprecated - subclass NativeEventEmitter to create granular event modules instead of
* adding all event listeners directly to RCTNativeAppEventEmitter.
*/
const RCTNativeAppEventEmitter = RCTDeviceEventEmitter;
BatchedBridge.registerCallableModule(
'RCTNativeAppEventEmitter',
RCTNativeAppEventEmitter
);
module.exports = RCTNativeAppEventEmitter;
@@ -12,7 +12,6 @@
*/
'use strict';
const BatchedBridge = require('BatchedBridge');
const Systrace = require('Systrace');
const infoLog = require('infoLog');
@@ -179,6 +178,4 @@ const PerformanceLogger = {
},
};
BatchedBridge.registerCallableModule('PerformanceLogger', PerformanceLogger);
module.exports = PerformanceLogger;
@@ -11,8 +11,6 @@
*/
'use strict';
const BatchedBridge = require('BatchedBridge');
const invariant = require('fbjs/lib/invariant');
const levelsMap = {
@@ -48,9 +46,4 @@ class RCTLog {
}
}
BatchedBridge.registerCallableModule(
'RCTLog',
RCTLog
);
module.exports = RCTLog;

0 comments on commit 485bb70

Please sign in to comment.