Files
react-native/Libraries/LogBox/Data/LogBoxLog.js
T
Rick Hanlon 8524b6182d LogBox - Optimistically symbolicate
Summary:
This diff adds optimistic loading for symbolicated stack traces by so that we (almost) never show a loading state for stack traces. Because of this, we also remove the "Stack Trace" status except when it is loading or failed. Also refactored the related components to hooks 🎣

Changelog: [Internal]

Reviewed By: mmmulani

Differential Revision: D18110403

fbshipit-source-id: a93b0a63e1c9490fea73ca6ec7c5707670bdea53
2019-10-31 16:26:42 -07:00

112 lines
2.7 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import * as LogBoxSymbolication from './LogBoxSymbolication';
import type {Category, Message, ComponentStack} from './parseLogBoxLog';
import type {Stack} from './LogBoxSymbolication';
export type LogLevel = 'warn' | 'error';
export type SymbolicationRequest = $ReadOnly<{|
abort: () => void,
|}>;
class LogBoxLog {
message: Message;
category: Category;
componentStack: ComponentStack;
stack: Stack;
count: number;
level: LogLevel;
symbolicated:
| $ReadOnly<{|error: null, stack: null, status: 'NONE'|}>
| $ReadOnly<{|error: null, stack: null, status: 'PENDING'|}>
| $ReadOnly<{|error: null, stack: Stack, status: 'COMPLETE'|}>
| $ReadOnly<{|error: Error, stack: null, status: 'FAILED'|}> = {
error: null,
stack: null,
status: 'NONE',
};
constructor(
level: LogLevel,
message: Message,
stack: Stack,
category: string,
componentStack: ComponentStack,
) {
this.level = level;
this.message = message;
this.stack = stack;
this.category = category;
this.componentStack = componentStack;
this.count = 1;
}
incrementCount(): void {
this.count += 1;
}
getAvailableStack(): Stack {
return this.symbolicated.status === 'COMPLETE'
? this.symbolicated.stack
: this.stack;
}
retrySymbolicate(callback?: () => void): SymbolicationRequest {
if (this.symbolicated.status !== 'COMPLETE') {
LogBoxSymbolication.deleteStack(this.stack);
}
return this.symbolicate(callback);
}
symbolicate(callback?: () => void): SymbolicationRequest {
let aborted = false;
if (this.symbolicated.status !== 'COMPLETE') {
const updateStatus = (error: ?Error, stack: ?Stack): void => {
if (error != null) {
this.symbolicated = {error, stack: null, status: 'FAILED'};
} else if (stack != null) {
this.symbolicated = {error: null, stack, status: 'COMPLETE'};
} else {
this.symbolicated = {error: null, stack: null, status: 'PENDING'};
}
if (!aborted) {
if (callback != null) {
callback();
}
}
};
updateStatus(null, null);
LogBoxSymbolication.symbolicate(this.stack).then(
stack => {
updateStatus(null, stack);
},
error => {
updateStatus(error, null);
},
);
}
return {
abort(): void {
aborted = true;
},
};
}
}
export default LogBoxLog;