Don't die when flipper isn't initialized and a plugin is used
Summary: This diff makes sure that the app doesn't die, or dies in a meaningful way if things are misconfigured. Reviewed By: passy Differential Revision: D19468273 fbshipit-source-id: 2471c263391906113446af57d795d1199eb1730e
This commit is contained in:
committed by
Facebook Github Bot
parent
0fa0899299
commit
e306aeb010
@@ -7,6 +7,7 @@
|
||||
|
||||
package com.facebook.flipper.reactnative;
|
||||
|
||||
import com.facebook.react.bridge.Callback;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
@@ -40,8 +41,9 @@ public class FlipperModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void registerPlugin(final String pluginId, final Boolean inBackground) {
|
||||
this.manager.registerPlugin(this, pluginId, inBackground);
|
||||
public void registerPlugin(
|
||||
final String pluginId, final Boolean inBackground, final Callback statusCallback) {
|
||||
this.manager.registerPlugin(this, pluginId, inBackground, statusCallback);
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.facebook.flipper.core.FlipperArray;
|
||||
import com.facebook.flipper.core.FlipperClient;
|
||||
import com.facebook.flipper.core.FlipperObject;
|
||||
import com.facebook.flipper.core.FlipperResponder;
|
||||
import com.facebook.react.bridge.Callback;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
@@ -51,7 +52,15 @@ public class FlipperReactNativeJavaScriptPluginManager {
|
||||
}
|
||||
|
||||
public void registerPlugin(
|
||||
FlipperModule module, final String pluginId, final Boolean inBackground) {
|
||||
FlipperModule module,
|
||||
final String pluginId,
|
||||
final Boolean inBackground,
|
||||
final Callback statusCallback) {
|
||||
if (this.flipperClient == null) {
|
||||
// Flipper is not available in this build
|
||||
statusCallback.invoke("noflipper");
|
||||
return;
|
||||
}
|
||||
FlipperReactNativeJavaScriptPlugin existing = getPlugin(pluginId);
|
||||
if (existing != null) {
|
||||
// Make sure events are emitted on the right application context
|
||||
@@ -60,6 +69,7 @@ public class FlipperReactNativeJavaScriptPluginManager {
|
||||
if (existing.isConnected()) {
|
||||
existing.fireOnConnect();
|
||||
}
|
||||
statusCallback.invoke("ok");
|
||||
return;
|
||||
}
|
||||
// we always create a new plugin class on the fly,
|
||||
@@ -69,6 +79,7 @@ public class FlipperReactNativeJavaScriptPluginManager {
|
||||
// inner class with no new members
|
||||
};
|
||||
this.flipperClient.addPlugin(plugin);
|
||||
statusCallback.invoke("ok");
|
||||
}
|
||||
|
||||
public void send(String pluginId, String method, String data) {
|
||||
|
||||
1
react-native/react-native-flipper/index.d.ts
vendored
1
react-native/react-native-flipper/index.d.ts
vendored
@@ -64,6 +64,7 @@ declare module 'Flipper' {
|
||||
export function registerPlugin(
|
||||
pluginId: string,
|
||||
runInBackground: boolean,
|
||||
statusCallback: (status: 'noflipper' | 'ok') => void,
|
||||
): void;
|
||||
export function send(pluginId: string, method: string, data: string): void;
|
||||
export function reportErrorWithMetadata(
|
||||
|
||||
23
react-native/react-native-flipper/index.js
vendored
23
react-native/react-native-flipper/index.js
vendored
@@ -125,6 +125,10 @@ function startEventListeners() {
|
||||
|
||||
// $FlowFixMe
|
||||
export function addPlugin(plugin) {
|
||||
if (!Flipper) {
|
||||
printNoFlipperWarning();
|
||||
return;
|
||||
}
|
||||
if (!plugin || typeof plugin !== 'object') {
|
||||
throw new Error('Expected plugin, got ' + plugin);
|
||||
}
|
||||
@@ -141,7 +145,22 @@ export function addPlugin(plugin) {
|
||||
plugin._connection = new Connection(id);
|
||||
plugins[id] = plugin;
|
||||
|
||||
Flipper.registerPlugin(id, runInBackground);
|
||||
Flipper.registerPlugin(id, runInBackground, status => {
|
||||
if (status === 'noflipper') {
|
||||
printNoFlipperWarning();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
startEventListeners();
|
||||
function printNoFlipperWarning() {
|
||||
// $FlowFixMe
|
||||
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
||||
console.warn(
|
||||
'The native module for Flipper seems unavailable. Make sure `initializeFlipper(this, getReactNativeHost().getReactInstanceManager()` is called in MainApplication.java:onCreate, and that `react-native-flipper` is installed as yarn dependency',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (Flipper) {
|
||||
startEventListeners();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user