Summary:
If you `console.log` early enough in the initialization sequence, it won't get sent to Metro because `hmrClient` isn't initialized yet. I've added a rolling array to catch at most 100 of those, and send them after we initialize.

Changelog: [General] [Fixed] - Early logs don't get dropped by Metro now

Reviewed By: cpojer

Differential Revision: D17952097

fbshipit-source-id: 964b4735a6a7c3ccd115f44151139d718bf5b26d
This commit is contained in:
Dan Abramov 2019-10-16 08:01:41 -07:00 коммит произвёл Facebook Github Bot
Родитель 0d1b9b5397
Коммит 4ed05ca241
1 изменённых файлов: 45 добавлений и 26 удалений

Просмотреть файл

@ -23,6 +23,7 @@ let hmrClient = null;
let hmrUnavailableReason: string | null = null; let hmrUnavailableReason: string | null = null;
let currentCompileErrorMessage: string | null = null; let currentCompileErrorMessage: string | null = null;
let didConnect: boolean = false; let didConnect: boolean = false;
let pendingLogs: Array<[LogLevel, Array<mixed>]> = [];
type LogLevel = type LogLevel =
| 'trace' | 'trace'
@ -101,8 +102,16 @@ const HMRClient: HMRClientNativeInterface = {
}, },
log(level: LogLevel, data: Array<mixed>) { log(level: LogLevel, data: Array<mixed>) {
if (!hmrClient) {
// Catch a reasonable number of early logs
// in case hmrClient gets initialized later.
pendingLogs.push([level, data]);
if (pendingLogs.length > 100) {
pendingLogs.shift();
}
return;
}
try { try {
if (hmrClient) {
let message; let message;
if (global.Symbol) { if (global.Symbol) {
message = JSON.stringify({ message = JSON.stringify({
@ -133,7 +142,6 @@ const HMRClient: HMRClientNativeInterface = {
} }
hmrClient.send(message); hmrClient.send(message);
}
} catch (error) { } catch (error) {
// If sending logs causes any failures we want to silently ignore them // If sending logs causes any failures we want to silently ignore them
// to ensure we do not cause infinite-logging loops. // to ensure we do not cause infinite-logging loops.
@ -244,6 +252,7 @@ Error: ${e.message}`;
} }
registerBundleEntryPoints(hmrClient); registerBundleEntryPoints(hmrClient);
flushEarlyLogs(hmrClient);
}, },
}; };
@ -276,6 +285,16 @@ function registerBundleEntryPoints(client) {
} }
} }
function flushEarlyLogs(client) {
try {
pendingLogs.forEach(([level: LogLevel, data: Array<mixed>]) => {
HMRClient.log(level, data);
});
} finally {
pendingLogs.length = 0;
}
}
function dismissRedbox() { function dismissRedbox() {
if ( if (
Platform.OS === 'ios' && Platform.OS === 'ios' &&