1 | import { EventEmitter } from 'fbemitter';
|
2 | import React from 'react';
|
3 | import NativeAppLoading from './AppLoadingNativeWrapper';
|
4 | export default class AppLoading extends React.Component {
|
5 | constructor() {
|
6 | super(...arguments);
|
7 | this._isMounted = false;
|
8 | this._startLoadingAppResourcesAsync = async () => {
|
9 | if (!this.props.onFinish) {
|
10 | throw new Error('AppLoading onFinish prop is required if startAsync is provided');
|
11 | }
|
12 | try {
|
13 | await this.props.startAsync();
|
14 | }
|
15 | catch (e) {
|
16 | if (!this._isMounted)
|
17 | return;
|
18 | if (this.props.onError) {
|
19 | this.props.onError(e);
|
20 | }
|
21 | else {
|
22 | throw e;
|
23 | }
|
24 | }
|
25 | finally {
|
26 | if (!this._isMounted)
|
27 | return;
|
28 |
|
29 |
|
30 | if (this.props.onFinish) {
|
31 | this.props.onFinish();
|
32 | }
|
33 | }
|
34 | };
|
35 | }
|
36 | componentDidMount() {
|
37 | this._isMounted = true;
|
38 | _emitEvent('componentDidMount');
|
39 |
|
40 |
|
41 | if (this.props.startAsync) {
|
42 | this._startLoadingAppResourcesAsync().catch(error => {
|
43 | console.error(`AppLoading threw an unexpected error when loading:\n${error.stack}`);
|
44 | });
|
45 | }
|
46 | }
|
47 | componentWillUnmount() {
|
48 | this._isMounted = false;
|
49 | _emitEvent('componentWillUnmount');
|
50 | }
|
51 | render() {
|
52 | return React.createElement(NativeAppLoading, Object.assign({}, this.props));
|
53 | }
|
54 | }
|
55 | let _lifecycleEmitter = null;
|
56 | function _emitEvent(event) {
|
57 | if (_lifecycleEmitter) {
|
58 | _lifecycleEmitter.emit(event);
|
59 | }
|
60 | }
|
61 | export function getAppLoadingLifecycleEmitter() {
|
62 | if (!_lifecycleEmitter) {
|
63 | _lifecycleEmitter = new EventEmitter();
|
64 | }
|
65 | return _lifecycleEmitter;
|
66 | }
|
67 |
|
\ | No newline at end of file |