UNPKG

2.25 kBJavaScriptView Raw
1import { EventEmitter } from 'fbemitter';
2import React from 'react';
3import NativeAppLoading from './AppLoadingNativeWrapper';
4export 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 // If we get to this point then we know that either there was no error, or the error was
29 // handled.
30 if (this.props.onFinish) {
31 this.props.onFinish();
32 }
33 }
34 };
35 }
36 componentDidMount() {
37 this._isMounted = true;
38 _emitEvent('componentDidMount');
39 // startAsync is optional, you can do this process manually if you prefer (this is mainly for
40 // backwards compatibility and it is not recommended)
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}
55let _lifecycleEmitter = null;
56function _emitEvent(event) {
57 if (_lifecycleEmitter) {
58 _lifecycleEmitter.emit(event);
59 }
60}
61export function getAppLoadingLifecycleEmitter() {
62 if (!_lifecycleEmitter) {
63 _lifecycleEmitter = new EventEmitter();
64 }
65 return _lifecycleEmitter;
66}
67//# sourceMappingURL=AppLoading.js.map
\No newline at end of file