RN: Refactor Chrome Debugger JS

Summary:
Refactors JavaScript in the `debugger.html` used to operate the Chrome Debugger.

- Pull out all view-related logic into a global `Page` object.
- Persist the dark theme across debug sessions using `localStorage`.

Reviewed By: kassens

Differential Revision: D4786235

fbshipit-source-id: ab959d1c95465a10b4538b14d9c87a51ae3b7078
This commit is contained in:
Tim Yung 2017-03-28 18:50:16 -07:00 committed by Facebook Github Bot
parent 436a9a5bf7
commit 696b49e3b8
1 changed files with 164 additions and 124 deletions

View File

@ -1,4 +1,4 @@
<!doctype html> <!DOCTYPE html>
<!-- <!--
Copyright (c) 2015-present, Facebook, Inc. Copyright (c) 2015-present, Facebook, Inc.
All rights reserved. All rights reserved.
@ -7,142 +7,179 @@
LICENSE file in the root directory of this source tree. An additional grant 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. of patent rights can be found in the PATENTS file in the same directory.
--> -->
<html> <html>
<head> <head>
<meta charset=utf-8> <meta charset=utf-8>
<!-- Base 64 encoded favicon, to avoid extra request to server -->
<link rel="icon" href="data:;base64,iVBORw0KGgo="> <link rel="icon" href="data:;base64,iVBORw0KGgo=">
<title>React Native Debugger</title> <title>React Native Debugger</title>
<script> <script>
/* eslint-env browser */ /* eslint-env browser */
'use strict';
(function() { (function() {
function setStatus(status) { const isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
document.getElementById('status').innerHTML = status; const refreshShortcut = isMacLike ? '⌘R' : 'Ctrl R';
} window.onload = function() {
if (!isMacLike) {
const RUNNING_ICON = 'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAABAJJREFUWMPNV0tIlFEU/nsaPSCrhVFZBBFRFkXvKCpCw6BFtagkWoS1CApp0YMKclHRYxNpj0UUQWUavYmeEi2MsFVS/s7oqOPo6DjqzKjpzOjpnDl35s7MP/8/vyUxFy7c/5x7z/nued5fUcwOgLGKBXIUFYqUaijHqeL0iamGaMSjPbR32IYN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{
document.querySelector('link[rel=icon]').href = 'data:image/png;base64,' + base64;
}
var isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
var refresh_shortcut = isMacLike ? '⌘R' : 'Ctrl R';
window.onload = function() {
if (!isMacLike) {
document.getElementById('dev_tools_shortcut').innerHTML = 'Ctrl⇧J';
}
};
var INITIAL_MESSAGE = 'Waiting, press <span class="shortcut">' + refresh_shortcut + '</span> in simulator to reload and connect.';
function connectToDebuggerProxy() {
var worker;
var ws = new WebSocket('ws://' + window.location.host + '/debugger-proxy?role=debugger&name=Chrome');
function createJSRuntime() {
// This worker will run the application javascript code,
// making sure that it's run in an environment without a global
// document, to make it consistent with the JSC executor environment.
worker = new Worker('debuggerWorker.js');
worker.onmessage = function(message) {
ws.send(JSON.stringify(message.data));
};
window.onbeforeunload = function() {
return 'If you reload this page, it is going to break the debugging session. ' +
'You should press' + refresh_shortcut + 'in simulator to reload.';
};
updateVisibility();
updateFavicon();
}
function shutdownJSRuntime() {
if (worker) {
worker.terminate();
worker = null;
window.onbeforeunload = null;
updateFavicon();
} }
} Page.render();
};
function updateVisibility() { const Assets = {
if (worker) { blueIcon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAABAJJREFUWMPNV0tIlFEU/nsaPSCrhVFZBBFRFkXvKCpCw6BFtagkWoS1CApp0YMKclHRYxNpj0UUQWUavYmeEi2MsFVS/s7oqOPo6DjqzKjpzOjpnDl35s7MP/8/vyUxFy7c/5x7z/nued5fUcwOgLGKBXIUFYqUaijHqeL0iamGaMSjPbR32IYNMpQaKEbhHlQCpibtpTN09q+HBdJQSCEK7DatWDu7QzJI1pBvrULFPyiOt0iFeWtUw2I8YB825RKEXamFLDM31yifqAIcbwO40QmQ3wIwXtUqmFwDcNQJcKeL96apOiB0LUF+0jH7u26AwUEA3wCERmsAYF+z5B9CUF1B5rmDvPeBx8AdCWOCA05zYEEdCz6JtxqB3xsb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W8J/7XKxmlSbZ9VL8dF5SuAeW/ImT1mJlqI/EIf//PRtVIDWNP6Vfv1TLzdWkMlTJlrtfjIyB3/oiOvOTGhUIfNRGbZ01fsazoUO1+AxqoZ6PJK98JRXQeTWXnDUicXyc4gc4wFC17Rcbuh0B72GbKVmr41EmSIR91MgvmoU+E4dFOk3hSzbZURA9BnxpgFahDVWaklHYqPBQxDSZtxQaxt3oL6ONhhEhhclodyAlTBmKWojPSAnpsEKvdC5bFNMcRYtGhqOc+hLd87aCoCRN3bFWS4CPY5mQg5uBAExqL2BzQMHLhQD4kzr3m4MDLpgP6ZO8cB1ajA1HO2hwOTNkhQLkR3lq0AxJlb6fSuDVFwihC0I8QLAV1oCwNAcpRQDDMQxSuD8ReLGAMgSYzlE4Q2EnY/DWzmaSmDlbwIF5Q9IKEP4kDzq1ltTAJu38RhpvzRC+4V8fZ5B6SkYQuMiScldsn0w2IZIbRG1KUM6QMlLNRpfs4KEOVPKdjhqyyjYLS9bNrPWAZehaii2+ZzYYkqU/0mgXINESFCYc+3sPQqJFLQgkX3vQuRJ6luH5XqhTvmgVRnugBPQEcvwtyImxpKX8s9tTlepdiz2bUuifVjN61NqOeP6wknJ21yk02p1ybkWc7nmksVZutDtw4lu6GyAOMaFY3/EjswaKWVTvOeCHByJn5HUdSstoM2oMWETnqWkUyMkcetGLkG01FEGlnyrTnhcTzSjbSLIiH1y0mnRLD+9+B9JWsfb/lusYyRGXM60qW+VK6irsaXzjx4kmVzbYGO5787yHuC/L1H+Z/KV0U1/JF8cdkUfw1WxR/Tp/W3/PHSyH/l51SdaoAAAAASUVORK5CYII=',
visibilityState: document.visibilityState, };
});
}
updateFavicon();
}
function updateFavicon() { const Page = window.Page = {
if (worker) {
if (document.visibilityState === 'visible') { state: {
setFavicon(RUNNING_ICON); isDark: localStorage.getItem('darkTheme') === 'on',
} else { status: {type: 'disconnected'},
setFavicon(SLOW_ICON); visibilityState: document.visibilityState,
},
setState(partialState) {
Page.state = Object.assign({}, Page.state, partialState);
Page.render();
},
render() {
const {
isDark,
status,
visibilityState,
} = Page.state;
const statusNode = document.getElementById('status');
switch (status.type) {
case 'connected':
statusNode.innerHTML = 'Debugger session #' + status.id + ' active.';
break;
case 'error':
statusNode.innerHTML = status.error.reason || 'Disconnected from proxy. Attempting reconnection. Is node server running?';
break;
case 'connecting':
case 'disconnected':
// Fall through.
default:
statusNode.innerHTML = 'Waiting, press <span class="shortcut">' + refreshShortcut + '</span> in simulator to reload and connect.';
break;
} }
} else {
setFavicon(STOPPED_ICON); const linkNode = document.querySelector('link[rel=icon]');
if (status.type === 'disconnected' ||
status.type === 'error') {
linkNode.href = Assets.grayIcon;
} else {
if (visibilityState === 'visible') {
linkNode.href = Assets.blueIcon;
} else {
linkNode.href = Assets.orangeIcon;
}
}
const darkCheckbox = document.getElementById('dark');
document.body.classList.toggle('dark', isDark);
darkCheckbox.checked = isDark;
localStorage.setItem('darkTheme', isDark ? 'on' : '');
},
toggleDarkTheme() {
Page.setState({isDark: !Page.state.isDark});
},
};
function connectToDebuggerProxy() {
const ws = new WebSocket('ws://' + window.location.host + '/debugger-proxy?role=debugger&name=Chrome');
let worker;
function createJSRuntime() {
// This worker will run the application JavaScript code,
// making sure that it's run in an environment without a global
// document, to make it consistent with the JSC executor environment.
worker = new Worker('debuggerWorker.js');
worker.onmessage = function(message) {
ws.send(JSON.stringify(message.data));
};
window.onbeforeunload = function() {
return 'If you reload this page, it is going to break the debugging session. ' +
'You should press' + refreshShortcut + 'in simulator to reload.';
};
updateVisibility();
} }
function shutdownJSRuntime() {
if (worker) {
worker.terminate();
worker = null;
window.onbeforeunload = null;
}
}
function updateVisibility() {
if (worker) {
worker.postMessage({
method: 'setDebuggerVisibility',
visibilityState: document.visibilityState,
});
}
Page.setState({visibilityState: document.visibilityState});
}
ws.onopen = function() {
Page.setState({status: {type: 'connecting'}});
};
ws.onmessage = function(message) {
if (!message.data) {
return;
}
const object = JSON.parse(message.data);
if (object.$event === 'client-disconnected') {
shutdownJSRuntime();
Page.setState({status: {type: 'disconnected'}});
return;
}
if (!object.method) {
return;
}
// Special message that asks for a new JS runtime
if (object.method === 'prepareJSRuntime') {
shutdownJSRuntime();
console.clear();
createJSRuntime();
ws.send(JSON.stringify({replyID: object.id}));
Page.setState({status: {type: 'connected', id: object.id}});
} else if (object.method === '$disconnected') {
shutdownJSRuntime();
Page.setState({status: {type: 'disconnected'}});
} else {
// Otherwise, pass through to the worker.
worker.postMessage(object);
}
};
ws.onclose = function(error) {
shutdownJSRuntime();
Page.setState({status: {type: 'error', error}});
if (error.reason) {
console.warn(error.reason);
}
setTimeout(connectToDebuggerProxy, 500);
};
// Let debuggerWorker.js know when we're not visible so that we can warn about
// poor performance when using remote debugging.
document.addEventListener('visibilitychange', updateVisibility, false);
} }
ws.onopen = function() { connectToDebuggerProxy();
setStatus(INITIAL_MESSAGE);
};
ws.onmessage = function(message) {
if (!message.data) {
return;
}
var object = JSON.parse(message.data);
if (object.$event === 'client-disconnected') {
shutdownJSRuntime();
setStatus('Waiting, press <span class="shortcut">' + refresh_shortcut + '</span> in simulator to reload and connect.');
return;
}
if (!object.method) {
return;
}
// Special message that asks for a new JS runtime
if (object.method === 'prepareJSRuntime') {
shutdownJSRuntime();
console.clear();
createJSRuntime();
ws.send(JSON.stringify({replyID: object.id}));
setStatus('Debugger session #' + object.id + ' active.');
} else if (object.method === '$disconnected') {
shutdownJSRuntime();
setStatus(INITIAL_MESSAGE);
} else {
// Otherwise, pass through to the worker.
worker.postMessage(object);
}
};
ws.onclose = function(e) {
shutdownJSRuntime();
setStatus('Disconnected from proxy. Attempting reconnection. Is node server running?');
if (e.reason) {
setStatus(e.reason);
console.warn(e.reason);
}
setTimeout(connectToDebuggerProxy, 500);
};
// Let debuggerWorker.js know when we're not visible so that we can warn about
// poor performance when using remote debugging.
document.addEventListener('visibilitychange', updateVisibility, false);
}
connectToDebuggerProxy();
})(); })();
</script> </script>
@ -178,17 +215,20 @@ connectToDebuggerProxy();
.dark a { .dark a {
color: #3b99fc; color: #3b99fc;
} }
input[type=checkbox] {
vertical-align: middle;
}
</style> </style>
</head> </head>
<body> <body>
<div class="content"> <div class="content">
<label for="dark"> <label for="dark">
<input type="checkbox" id="dark" onclick="document.body.classList.toggle('dark')"> Dark background <input type="checkbox" id="dark" onclick="Page.toggleDarkTheme()"> Dark Theme
</label> </label>
<p> <p>
React Native JS code runs inside this Chrome tab. React Native JS code runs as a web worker inside this tab.
</p> </p>
<p>Press <kbd id='dev_tools_shortcut' class="shortcut">⌘⌥J</kbd> to open Developer Tools. Enable <a href="https://stackoverflow.com/a/17324511/232122" target="_blank">Pause On Caught Exceptions</a> for a better debugging experience.</p> <p>Press <kbd id="shortcut" class="shortcut">⌘⌥J</kbd> to open Developer Tools. Enable <a href="https://stackoverflow.com/a/17324511/232122" target="_blank">Pause On Caught Exceptions</a> for a better debugging experience.</p>
<p>Status: <span id="status">Loading...</span></p> <p>Status: <span id="status">Loading...</span></p>
</div> </div>
</body> </body>