2016-06-09 00:30:46 +03:00
|
|
|
/**
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-06-09 00:30:46 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-06-09 00:30:46 +03:00
|
|
|
*
|
2017-06-27 02:20:39 +03:00
|
|
|
* @format
|
2016-11-07 07:49:10 +03:00
|
|
|
* @flow
|
2016-06-09 00:30:46 +03:00
|
|
|
*/
|
|
|
|
|
2020-06-27 12:15:58 +03:00
|
|
|
import View from '../Components/View/View';
|
|
|
|
import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter';
|
|
|
|
import StyleSheet from '../StyleSheet/StyleSheet';
|
2020-06-23 23:43:04 +03:00
|
|
|
import {type EventSubscription} from '../vendor/emitter/EventEmitter';
|
2020-06-23 23:43:04 +03:00
|
|
|
import {RootTagContext, createRootTag} from './RootTag';
|
2021-04-27 08:56:28 +03:00
|
|
|
import type {RootTag} from './RootTag';
|
2020-06-27 12:15:58 +03:00
|
|
|
import * as React from 'react';
|
2020-06-23 23:43:04 +03:00
|
|
|
|
2018-05-15 03:36:40 +03:00
|
|
|
type Props = $ReadOnly<{|
|
|
|
|
children?: React.Node,
|
2019-09-10 04:37:23 +03:00
|
|
|
fabric?: boolean,
|
2021-06-11 14:22:28 +03:00
|
|
|
useConcurrentRoot?: boolean,
|
2021-04-27 08:56:28 +03:00
|
|
|
rootTag: number | RootTag,
|
2020-05-05 14:52:13 +03:00
|
|
|
initialProps?: {...},
|
2019-09-10 04:37:23 +03:00
|
|
|
showArchitectureIndicator?: boolean,
|
2018-05-15 03:36:40 +03:00
|
|
|
WrapperComponent?: ?React.ComponentType<any>,
|
2019-11-27 19:25:41 +03:00
|
|
|
internal_excludeLogBox?: ?boolean,
|
2018-05-15 03:36:40 +03:00
|
|
|
|}>;
|
|
|
|
|
|
|
|
type State = {|
|
|
|
|
inspector: ?React.Node,
|
2016-11-07 07:49:10 +03:00
|
|
|
mainKey: number,
|
2019-11-14 22:12:15 +03:00
|
|
|
hasError: boolean,
|
2018-05-15 03:36:40 +03:00
|
|
|
|};
|
2016-06-09 00:30:46 +03:00
|
|
|
|
2017-08-18 04:36:54 +03:00
|
|
|
class AppContainer extends React.Component<Props, State> {
|
2016-11-07 07:49:10 +03:00
|
|
|
state: State = {
|
|
|
|
inspector: null,
|
|
|
|
mainKey: 1,
|
2019-11-14 22:12:15 +03:00
|
|
|
hasError: false,
|
2016-11-07 07:49:10 +03:00
|
|
|
};
|
2018-05-15 03:36:40 +03:00
|
|
|
_mainRef: ?React.ElementRef<typeof View>;
|
2020-06-23 23:43:04 +03:00
|
|
|
_subscription: ?EventSubscription = null;
|
2016-06-09 00:30:46 +03:00
|
|
|
|
2019-11-14 22:12:15 +03:00
|
|
|
static getDerivedStateFromError: any = undefined;
|
|
|
|
|
2016-11-07 07:49:10 +03:00
|
|
|
componentDidMount(): void {
|
|
|
|
if (__DEV__) {
|
2017-05-15 15:12:48 +03:00
|
|
|
if (!global.__RCTProfileIsProfiling) {
|
2017-05-12 06:48:16 +03:00
|
|
|
this._subscription = RCTDeviceEventEmitter.addListener(
|
|
|
|
'toggleElementInspector',
|
|
|
|
() => {
|
2019-05-08 18:44:25 +03:00
|
|
|
const Inspector = require('../Inspector/Inspector');
|
2017-09-27 09:33:30 +03:00
|
|
|
const inspector = this.state.inspector ? null : (
|
|
|
|
<Inspector
|
2019-12-18 05:27:53 +03:00
|
|
|
inspectedView={this._mainRef}
|
2020-03-25 07:35:58 +03:00
|
|
|
onRequestRerenderApp={updateInspectedView => {
|
2017-09-27 09:33:30 +03:00
|
|
|
this.setState(
|
2020-03-25 07:35:58 +03:00
|
|
|
s => ({mainKey: s.mainKey + 1}),
|
2019-12-18 05:27:53 +03:00
|
|
|
() => updateInspectedView(this._mainRef),
|
2017-09-27 09:33:30 +03:00
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2017-05-12 06:48:16 +03:00
|
|
|
this.setState({inspector});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2016-11-07 07:49:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount(): void {
|
2018-05-15 03:36:40 +03:00
|
|
|
if (this._subscription != null) {
|
2016-11-07 07:49:10 +03:00
|
|
|
this._subscription.remove();
|
|
|
|
}
|
|
|
|
}
|
2016-06-09 00:30:46 +03:00
|
|
|
|
2017-08-18 04:36:54 +03:00
|
|
|
render(): React.Node {
|
2020-02-20 04:57:55 +03:00
|
|
|
let logBox = null;
|
2019-12-10 13:28:06 +03:00
|
|
|
if (__DEV__) {
|
|
|
|
if (
|
|
|
|
!global.__RCTProfileIsProfiling &&
|
|
|
|
!this.props.internal_excludeLogBox
|
|
|
|
) {
|
2020-02-20 04:57:55 +03:00
|
|
|
const LogBoxNotificationContainer = require('../LogBox/LogBoxNotificationContainer')
|
|
|
|
.default;
|
|
|
|
logBox = <LogBoxNotificationContainer />;
|
2017-05-12 06:48:16 +03:00
|
|
|
}
|
2016-06-09 00:30:46 +03:00
|
|
|
}
|
2016-11-07 07:49:10 +03:00
|
|
|
|
2017-06-27 02:20:39 +03:00
|
|
|
let innerView = (
|
|
|
|
<View
|
|
|
|
collapsable={!this.state.inspector}
|
|
|
|
key={this.state.mainKey}
|
|
|
|
pointerEvents="box-none"
|
|
|
|
style={styles.appContainer}
|
2020-03-25 07:35:58 +03:00
|
|
|
ref={ref => {
|
2017-06-27 02:20:39 +03:00
|
|
|
this._mainRef = ref;
|
|
|
|
}}>
|
|
|
|
{this.props.children}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
|
|
|
const Wrapper = this.props.WrapperComponent;
|
2018-05-15 03:36:40 +03:00
|
|
|
if (Wrapper != null) {
|
2019-09-10 04:37:23 +03:00
|
|
|
innerView = (
|
|
|
|
<Wrapper
|
2020-05-05 14:52:13 +03:00
|
|
|
initialProps={this.props.initialProps}
|
2019-09-10 04:37:23 +03:00
|
|
|
fabric={this.props.fabric === true}
|
|
|
|
showArchitectureIndicator={
|
|
|
|
this.props.showArchitectureIndicator === true
|
|
|
|
}>
|
|
|
|
{innerView}
|
|
|
|
</Wrapper>
|
|
|
|
);
|
2017-06-27 02:20:39 +03:00
|
|
|
}
|
2016-06-09 00:30:46 +03:00
|
|
|
return (
|
2020-04-22 05:12:57 +03:00
|
|
|
<RootTagContext.Provider value={createRootTag(this.props.rootTag)}>
|
2019-06-25 16:19:52 +03:00
|
|
|
<View style={styles.appContainer} pointerEvents="box-none">
|
2019-11-14 22:12:15 +03:00
|
|
|
{!this.state.hasError && innerView}
|
2019-06-25 16:19:52 +03:00
|
|
|
{this.state.inspector}
|
2020-02-20 04:57:55 +03:00
|
|
|
{logBox}
|
2019-06-25 16:19:52 +03:00
|
|
|
</View>
|
|
|
|
</RootTagContext.Provider>
|
2016-06-09 00:30:46 +03:00
|
|
|
);
|
|
|
|
}
|
2016-11-07 07:49:10 +03:00
|
|
|
}
|
2016-06-09 00:30:46 +03:00
|
|
|
|
2016-11-07 07:49:10 +03:00
|
|
|
const styles = StyleSheet.create({
|
2016-06-09 00:30:46 +03:00
|
|
|
appContainer: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = AppContainer;
|