Skip to content

Commit

Permalink
Add onReactBridgeInitialized to Java native modules
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
appden committed Jan 26, 2016
1 parent 2ef3be5 commit 4add326
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ public boolean canOverrideExistingModule() {
return false;
}

@Override
public void onReactBridgeInitialized(ReactBridge bridge) {
// do nothing
}

@Override
public void onCatalystInstanceDestroy() {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ private ReactBridge initializeBridge(
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}

mJavaRegistry.notifyReactBridgeInitialized(bridge);
return bridge;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4add326

Please sign in to comment.