Files
react-native/Libraries/LogBox/LogBox.js
T
Rick Hanlon cc056907cf Clean up API for LogBoxLogParser
Summary:
This diff cleans up two functions from:

```
LogBoxLogData.add({args: ['A']});
LogBoxLogParser({args: ['A']});
```

to:

```
LogBoxLogData.add(['A']);
LogBoxLogParser.parseLog(['A']);
```

Changelog: [Internal]

Reviewed By: cpojer

Differential Revision: D18056462

fbshipit-source-id: be6108069fc24b1f25d1382ad31c314183c793f8
2019-10-22 14:13:20 -07:00

168 lines
4.0 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 React from 'react';
import Platform from '../Utilities/Platform';
import RCTLog from '../Utilities/RCTLog';
import LogBoxContainer from './UI/LogBoxContainer';
import * as LogBoxData from './Data/LogBoxData';
import type {LogBoxLogs, Subscription, IgnorePattern} from './Data/LogBoxData';
import LogBoxLog from './Data/LogBoxLog';
type Props = $ReadOnly<{||}>;
type State = {|
logs: ?LogBoxLogs,
|};
let LogBoxComponent;
/**
* LogBox displays logs in the app.
*/
if (__DEV__) {
// LogBox needs to insert itself early,
// in order to access the component stacks appended by React DevTools.
const {error, warn} = console;
let errorImpl = error.bind(console);
let warnImpl = warn.bind(console);
(console: any).error = function(...args) {
errorImpl(...args);
};
(console: any).warn = function(...args) {
warnImpl(...args);
};
LogBoxComponent = class LogBox extends React.Component<Props, State> {
// TODO: deprecated, replace with ignoreLogs
static ignoreWarnings(patterns: $ReadOnlyArray<IgnorePattern>): void {
LogBox.ignoreLogs(patterns);
}
static ignoreLogs(patterns: $ReadOnlyArray<IgnorePattern>): void {
LogBoxData.addIgnorePatterns(patterns);
}
static install(): void {
errorImpl = function(...args) {
error.call(console, ...args);
// Show LogBox for the `warning` module.
if (typeof args[0] === 'string' && args[0].startsWith('Warning: ')) {
registerLog(...args);
}
};
warnImpl = function(...args) {
warn.call(console, ...args);
registerLog(...args);
};
if ((console: any).disableLogBox === true) {
LogBoxData.setDisabled(true);
}
(Object.defineProperty: any)(console, 'disableLogBox', {
configurable: true,
get: () => LogBoxData.isDisabled(),
set: value => LogBoxData.setDisabled(value),
});
if (Platform.isTesting) {
(console: any).disableLogBox = true;
}
RCTLog.setWarningHandler((...args) => {
registerLog(...args);
});
}
static uninstall(): void {
errorImpl = error;
warnImpl = warn;
delete (console: any).disableLogBox;
}
_subscription: ?Subscription;
state = {
logs: null,
};
render(): React.Node {
// TODO: Ignore logs that fire when rendering `LogBox` itself.
return this.state.logs == null ? null : (
<LogBoxContainer
onDismiss={this._handleDismiss}
onDismissAll={this._handleDismissAll}
logs={this.state.logs}
/>
);
}
componentDidMount(): void {
this._subscription = LogBoxData.observe(logs => {
this.setState({logs});
});
}
componentWillUnmount(): void {
if (this._subscription != null) {
this._subscription.unsubscribe();
}
}
_handleDismissAll(): void {
LogBoxData.clear();
}
_handleDismiss(log: LogBoxLog): void {
LogBoxData.dismiss(log);
}
};
const registerLog = (...args): void => {
LogBoxData.add(args);
};
} else {
LogBoxComponent = class extends React.Component<Props, State> {
// TODO: deprecated, replace with ignoreLogs
static ignoreWarnings(patterns: $ReadOnlyArray<IgnorePattern>): void {
// Do nothing.
}
static ignoreLogs(patterns: $ReadOnlyArray<IgnorePattern>): void {
// Do nothing.
}
static install(): void {
// Do nothing.
}
static uninstall(): void {
// Do nothing.
}
render(): React.Node {
return null;
}
};
}
module.exports = (LogBoxComponent: Class<React.Component<Props, State>> & {
// TODO: deprecated, replace with ignoreLogs
ignoreWarnings($ReadOnlyArray<IgnorePattern>): void,
ignoreLogs($ReadOnlyArray<IgnorePattern>): void,
install(): void,
uninstall(): void,
});