From 4add3264cbf2ebc574ee9e41184f972d12137b03 Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Mon, 25 Jan 2016 14:54:34 -0800 Subject: [PATCH] Add onReactBridgeInitialized to Java native modules This native module callback will be called on the JS thread after a ReactBridge instance has been created, but before the JS bundle is loaded. This allows for advanced native modules to add custom functionality (that isn't possible with the existing APIs) into the context that will be available when the JS bundle loads. --- .../com/facebook/react/bridge/BaseJavaModule.java | 5 +++++ .../facebook/react/bridge/CatalystInstanceImpl.java | 1 + .../com/facebook/react/bridge/NativeModule.java | 9 +++++++++ .../facebook/react/bridge/NativeModuleRegistry.java | 13 +++++++++++++ 4 files changed, 28 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/BaseJavaModule.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/BaseJavaModule.java index 1a84c05fa4102f..6d05c003ac7b6e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/BaseJavaModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/BaseJavaModule.java @@ -332,6 +332,11 @@ public boolean canOverrideExistingModule() { return false; } + @Override + public void onReactBridgeInitialized(ReactBridge bridge) { + // do nothing + } + @Override public void onCatalystInstanceDestroy() { // do nothing diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java index d3bcc1abd09aad..cc34bf6ffaa81f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java @@ -127,6 +127,7 @@ private ReactBridge initializeBridge( Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); } + mJavaRegistry.notifyReactBridgeInitialized(bridge); return bridge; } diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModule.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModule.java index 905b5be9352183..5dae9ac3e49970 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModule.java @@ -59,6 +59,15 @@ interface NativeMethod { */ boolean canOverrideExistingModule(); + /** + * Called on the JS thread after a ReactBridge has been created. This is useful for native modules + * that need to do any setup before the JS bundle has been loaded. An example of this would be + * installing custom functionality into the JavaScriptCore context. + * + * @param bridge the ReactBridge instance that has just been created + */ + void onReactBridgeInitialized(ReactBridge bridge); + /** * Called before {CatalystInstance#onHostDestroy} */ diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModuleRegistry.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModuleRegistry.java index 389fd66b4616b2..131def5fb06352 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModuleRegistry.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModuleRegistry.java @@ -114,6 +114,19 @@ private NativeModuleRegistry( } } + /* package */ void notifyReactBridgeInitialized(ReactBridge bridge) { + Systrace.beginSection( + Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, + "NativeModuleRegistry_notifyReactBridgeInitialized"); + try { + for (NativeModule nativeModule : mModuleInstances.values()) { + nativeModule.onReactBridgeInitialized(bridge); + } + } finally { + Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); + } + } + public void onBatchComplete() { for (int i = 0; i < mBatchCompleteListenerModules.size(); i++) { mBatchCompleteListenerModules.get(i).onBatchComplete();