Skip to content

Commit

Permalink
Add a menu item for HMR
Browse files Browse the repository at this point in the history
Summary:
cc martinbigio
Closes facebook#5092

Reviewed By: svcscm

Differential Revision: D2807241

Pulled By: mkonicek

fb-gh-sync-id: e4418eeb4944d795f30f94be94b80648b4d7034c
  • Loading branch information
satya164 authored and facebook-github-bot-3 committed Jan 6, 2016
1 parent daa93a6 commit 5b3cb05
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class DevInternalSettings implements
private static final String PREFS_ANIMATIONS_DEBUG_KEY = "animations_debug";
private static final String PREFS_RELOAD_ON_JS_CHANGE_KEY = "reload_on_js_change";
private static final String PREFS_INSPECTOR_DEBUG_KEY = "inspector_debug";
private static final String PREFS_HOT_MODULE_REPLACEMENT_KEY = "hot_module_replacement";

private final SharedPreferences mPreferences;
private final DevSupportManager mDebugManager;
Expand Down Expand Up @@ -77,6 +78,14 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
}
}

public boolean isHotModuleReplacementEnabled() {
return mPreferences.getBoolean(PREFS_HOT_MODULE_REPLACEMENT_KEY, false);
}

public void setHotModuleReplacementEnabled(boolean enabled) {
mPreferences.edit().putBoolean(PREFS_HOT_MODULE_REPLACEMENT_KEY, enabled).apply();
}

public boolean isReloadOnJSChangeEnabled() {
return mPreferences.getBoolean(PREFS_RELOAD_ON_JS_CHANGE_KEY, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.common.ReactConstants;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.ConnectionPool;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;

import okio.Okio;
import okio.Sink;

Expand All @@ -55,7 +55,7 @@ public class DevServerHelper {
private static final String DEVICE_LOCALHOST = "localhost:8081";

private static final String BUNDLE_URL_FORMAT =
"http://%s/%s.bundle?platform=android&dev=%s";
"http://%s/%s.bundle?platform=android&dev=%s&hot=%s";
private static final String SOURCE_MAP_URL_FORMAT =
BUNDLE_URL_FORMAT.replaceFirst("\\.bundle", ".map");
private static final String LAUNCH_CHROME_DEVTOOLS_COMMAND_URL_FORMAT =
Expand Down Expand Up @@ -120,12 +120,19 @@ private static String getHostForJSProxy() {
}

/**
* @return whether we should enabled dev mode or not when requesting JS bundles.
* @return whether we should enable dev mode when requesting JS bundles.
*/
private boolean getDevMode() {
return mSettings.isJSDevModeEnabled();
}

/**
* @return whether we should enabled HMR when requesting JS bundles.
*/
private boolean getHMR() {
return mSettings.isHotModuleReplacementEnabled();
}

/**
* @return the host to use when connecting to the bundle server.
*/
Expand Down Expand Up @@ -160,15 +167,15 @@ private boolean isRunningOnStockEmulator() {
return Build.FINGERPRINT.contains("generic");
}

private static String createBundleURL(String host, String jsModulePath, boolean devMode) {
return String.format(Locale.US, BUNDLE_URL_FORMAT, host, jsModulePath, devMode);
private static String createBundleURL(String host, String jsModulePath, boolean devMode, boolean hmr) {
return String.format(Locale.US, BUNDLE_URL_FORMAT, host, jsModulePath, devMode, hmr);
}

public void downloadBundleFromURL(
final BundleDownloadCallback callback,
final String jsModulePath,
final File outputFile) {
final String bundleURL = createBundleURL(getDebugServerHost(), jsModulePath, getDevMode());
final String bundleURL = createBundleURL(getDebugServerHost(), jsModulePath, getDevMode(), getHMR());
Request request = new Request.Builder()
.url(bundleURL)
.build();
Expand Down Expand Up @@ -354,17 +361,17 @@ public void onResponse(Response response) throws IOException {
}

public String getSourceMapUrl(String mainModuleName) {
return String.format(Locale.US, SOURCE_MAP_URL_FORMAT, getDebugServerHost(), mainModuleName, getDevMode());
return String.format(Locale.US, SOURCE_MAP_URL_FORMAT, getDebugServerHost(), mainModuleName, getDevMode(), getHMR());
}

public String getSourceUrl(String mainModuleName) {
return String.format(Locale.US, BUNDLE_URL_FORMAT, getDebugServerHost(), mainModuleName, getDevMode());
return String.format(Locale.US, BUNDLE_URL_FORMAT, getDebugServerHost(), mainModuleName, getDevMode(), getHMR());
}

public String getJSBundleURLForRemoteDebugging(String mainModuleName) {
// The host IP we use when connecting to the JS bundle server from the emulator is not the
// same as the one needed to connect to the same server from the Chrome proxy running on the
// host itself.
return createBundleURL(getHostForJSProxy(), mainModuleName, getDevMode());
return createBundleURL(getHostForJSProxy(), mainModuleName, getDevMode(), getHMR());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ public void onOptionSelected() {
handleReloadJS();
}
});
options.put(
mDevSettings.isHotModuleReplacementEnabled()
? mApplicationContext.getString(R.string.catalyst_hot_module_replacement_off)
: mApplicationContext.getString(R.string.catalyst_hot_module_replacement),
new DevOptionHandler() {
@Override
public void onOptionSelected() {
mDevSettings.setHotModuleReplacementEnabled(!mDevSettings.isHotModuleReplacementEnabled());
handleReloadJS();
}
});
options.put(
mDevSettings.isReloadOnJSChangeEnabled()
? mApplicationContext.getString(R.string.catalyst_live_reload_off)
Expand Down
2 changes: 2 additions & 0 deletions ReactAndroid/src/main/res/devsupport/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<string name="catalyst_reloadjs" project="catalyst" translatable="false">Reload JS</string>
<string name="catalyst_debugjs" project="catalyst" translatable="false">Debug in Chrome</string>
<string name="catalyst_debugjs_off" project="catalyst" translatable="false">Stop Chrome Debugging</string>
<string name="catalyst_hot_module_replacement" project="catalyst" translatable="false">Enable Hot Module Replacement</string>
<string name="catalyst_hot_module_replacement_off" project="catalyst" translatable="false">Disable Hot Module Replacement</string>
<string name="catalyst_live_reload" project="catalyst" translatable="false">Enable Live Reload</string>
<string name="catalyst_live_reload_off" project="catalyst" translatable="false">Disable Live Reload</string>
<string name="catalyst_perf_monitor" project="catalyst" translatable="false">Enable Perf Monitor</string>
Expand Down

0 comments on commit 5b3cb05

Please sign in to comment.