mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-02-08 17:22:05 +08:00
Enable HMR
Reviewed By: svcscm Differential Revision: D2932137 fb-gh-sync-id: 8bfab09aaac22ae498ac4fa896eee495111abc0d shipit-source-id: 8bfab09aaac22ae498ac4fa896eee495111abc0d
This commit is contained in:
committed by
facebook-github-bot-4
parent
8eddead868
commit
e018aa3100
@@ -19,6 +19,7 @@ import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
|
||||
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
||||
import com.facebook.react.modules.core.ExceptionsManagerModule;
|
||||
import com.facebook.react.devsupport.HMRClient;
|
||||
import com.facebook.react.modules.core.JSTimersExecution;
|
||||
import com.facebook.react.modules.core.RCTNativeAppEventEmitter;
|
||||
import com.facebook.react.modules.core.Timing;
|
||||
@@ -95,6 +96,7 @@ import com.facebook.systrace.Systrace;
|
||||
RCTNativeAppEventEmitter.class,
|
||||
AppRegistry.class,
|
||||
com.facebook.react.bridge.Systrace.class,
|
||||
HMRClient.class,
|
||||
DebugComponentOwnershipModule.RCTDebugComponentOwnership.class);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ public interface DevSupportManager extends NativeModuleCallExceptionHandler {
|
||||
void addCustomDevOption(String optionName, DevOptionHandler optionHandler);
|
||||
void showNewJSError(String message, ReadableArray details, int errorCookie);
|
||||
void updateJSError(final String message, final ReadableArray details, final int errorCookie);
|
||||
void hideRedboxDialog();
|
||||
void showDevOptionsDialog();
|
||||
void setDevSupportEnabled(boolean isDevSupportEnabled);
|
||||
boolean getDevSupportEnabled();
|
||||
|
||||
@@ -13,6 +13,8 @@ import javax.annotation.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
@@ -40,7 +42,6 @@ import com.facebook.react.R;
|
||||
import com.facebook.react.bridge.CatalystInstance;
|
||||
import com.facebook.react.bridge.DefaultNativeModuleCallExceptionHandler;
|
||||
import com.facebook.react.bridge.JavaJSExecutor;
|
||||
import com.facebook.react.bridge.NativeModuleCallExceptionHandler;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.bridge.ReadableArray;
|
||||
import com.facebook.react.bridge.UiThreadUtil;
|
||||
@@ -216,6 +217,14 @@ public class DevSupportManagerImpl implements DevSupportManager {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hideRedboxDialog() {
|
||||
// dismiss redbox if exists
|
||||
if (mRedBoxDialog != null) {
|
||||
mRedBoxDialog.dismiss();
|
||||
}
|
||||
}
|
||||
|
||||
private void showNewError(
|
||||
final String message,
|
||||
final StackFrame[] stack,
|
||||
@@ -522,6 +531,18 @@ public class DevSupportManagerImpl implements DevSupportManager {
|
||||
mDebugOverlayController = new DebugOverlayController(reactContext);
|
||||
}
|
||||
|
||||
if (mDevSettings.isHotModuleReplacementEnabled() && mCurrentContext != null) {
|
||||
try {
|
||||
URL sourceUrl = new URL(getSourceUrl());
|
||||
String path = sourceUrl.getPath().substring(1); // strip initial slash in path
|
||||
String host = sourceUrl.getHost();
|
||||
int port = sourceUrl.getPort();
|
||||
mCurrentContext.getJSModule(HMRClient.class).enable("android", path, host, port);
|
||||
} catch (MalformedURLException e) {
|
||||
showNewJavaError(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
reloadSettings();
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,11 @@ public class DisabledDevSupportManager implements DevSupportManager {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hideRedboxDialog() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDevOptionsDialog() {
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.facebook.react.devsupport;
|
||||
|
||||
import com.facebook.react.bridge.JavaScriptModule;
|
||||
|
||||
/**
|
||||
* JS module interface for HMRClient
|
||||
*
|
||||
* The HMR(Hot Module Replacement)Client allows for the application to receive updates
|
||||
* from the packager server (over a web socket), allowing for injection of JavaScript to
|
||||
* the running application (without a refresh).
|
||||
*/
|
||||
public interface HMRClient extends JavaScriptModule {
|
||||
|
||||
/**
|
||||
* Enable the HMRClient so that the client will receive updates
|
||||
* from the packager server.
|
||||
* @param platform The platform in which HMR updates will be enabled. Should be "android".
|
||||
* @param bundleEntry The path to the bundle entry file (e.g. index.ios.bundle).
|
||||
* @param host The host that the HMRClient should communicate with.
|
||||
* @param port The port that the HMRClient should communicate with on the host.
|
||||
*/
|
||||
void enable(String platform, String bundleEntry, String host, int port);
|
||||
}
|
||||
@@ -77,4 +77,11 @@ public class ExceptionsManagerModule extends BaseJavaModule {
|
||||
mDevSupportManager.updateJSError(title, details, exceptionId);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void dismissRedbox() {
|
||||
if (mDevSupportManager.getDevSupportEnabled()) {
|
||||
mDevSupportManager.hideRedboxDialog();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,13 @@ static JSValueRef nativePerformanceNow(
|
||||
size_t argumentCount,
|
||||
const JSValueRef arguments[],
|
||||
JSValueRef *exception);
|
||||
static JSValueRef nativeInjectHMRUpdate(
|
||||
JSContextRef ctx,
|
||||
JSObjectRef function,
|
||||
JSObjectRef thisObject,
|
||||
size_t argumentCount,
|
||||
const JSValueRef arguments[],
|
||||
JSValueRef *exception);
|
||||
|
||||
static std::string executeJSCallWithJSC(
|
||||
JSGlobalContextRef ctx,
|
||||
@@ -93,6 +100,7 @@ JSCExecutor::JSCExecutor(FlushImmediateCallback cb, const std::string& cacheDir)
|
||||
installGlobalFunction(m_context, "nativeStartWorker", nativeStartWorker);
|
||||
installGlobalFunction(m_context, "nativePostMessageToWorker", nativePostMessageToWorker);
|
||||
installGlobalFunction(m_context, "nativeTerminateWorker", nativeTerminateWorker);
|
||||
installGlobalFunction(m_context, "nativeInjectHMRUpdate", nativeInjectHMRUpdate);
|
||||
|
||||
installGlobalFunction(m_context, "nativeLoggingHook", JSLogging::nativeHook);
|
||||
|
||||
@@ -140,7 +148,7 @@ void JSCExecutor::executeApplicationScript(
|
||||
if (!jsSourceURL) {
|
||||
evaluateScript(m_context, jsScript, jsSourceURL);
|
||||
} else {
|
||||
// If we're evaluating a script, get the device's cache dir
|
||||
// If we're evaluating a script, get the device's cache dir
|
||||
// in which a cache file for that script will be stored.
|
||||
evaluateScript(m_context, jsScript, jsSourceURL, m_deviceCacheDir.c_str());
|
||||
}
|
||||
@@ -471,4 +479,16 @@ static JSValueRef nativePerformanceNow(
|
||||
return JSValueMakeNumber(ctx, (nano / (double)NANOSECONDS_IN_MILLISECOND));
|
||||
}
|
||||
|
||||
static JSValueRef nativeInjectHMRUpdate(
|
||||
JSContextRef ctx,
|
||||
JSObjectRef function,
|
||||
JSObjectRef thisObject,
|
||||
size_t argumentCount,
|
||||
const JSValueRef arguments[], JSValueRef *exception) {
|
||||
String execJSString = Value(ctx, arguments[0]).toString();
|
||||
String jsURL = Value(ctx, arguments[1]).toString();
|
||||
evaluateScript(ctx, execJSString, jsURL);
|
||||
return JSValueMakeUndefined(ctx);
|
||||
}
|
||||
|
||||
} }
|
||||
|
||||
Reference in New Issue
Block a user