]>
cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/features/ui/components/bundle.js
1 import React
from 'react';
2 import PropTypes
from 'prop-types';
4 const emptyComponent
= () => null;
5 const noop
= () => { };
7 class Bundle
extends React
.PureComponent
{
10 fetchComponent: PropTypes
.func
.isRequired
,
11 loading: PropTypes
.func
,
12 error: PropTypes
.func
,
13 children: PropTypes
.func
.isRequired
,
14 renderDelay: PropTypes
.number
,
15 onFetch: PropTypes
.func
,
16 onFetchSuccess: PropTypes
.func
,
17 onFetchFail: PropTypes
.func
,
20 static defaultProps
= {
21 loading: emptyComponent
,
22 error: emptyComponent
,
29 static cache
= new Map
36 componentWillMount() {
37 this.load(this.props
);
40 componentWillReceiveProps(nextProps
) {
41 if (nextProps
.fetchComponent
!== this.props
.fetchComponent
) {
46 componentWillUnmount () {
48 clearTimeout(this.timeout
);
53 const { fetchComponent
, onFetch
, onFetchSuccess
, onFetchFail
, renderDelay
} = props
|| this.props
;
54 const cachedMod
= Bundle
.cache
.get(fetchComponent
);
56 if (fetchComponent
=== undefined) {
57 this.setState({ mod: null });
58 return Promise
.resolve();
64 this.setState({ mod: cachedMod
.default });
66 return Promise
.resolve();
69 this.setState({ mod: undefined });
71 if (renderDelay
!== 0) {
72 this.timestamp
= new Date();
73 this.timeout
= setTimeout(() => this.setState({ forceRender: true }), renderDelay
);
76 return fetchComponent()
78 Bundle
.cache
.set(fetchComponent
, mod
);
79 this.setState({ mod: mod
.default });
83 this.setState({ mod: null });
89 const { loading: Loading
, error: Error
, children
, renderDelay
} = this.props
;
90 const { mod
, forceRender
} = this.state
;
91 const elapsed
= this.timestamp
? (new Date() - this.timestamp
) : renderDelay
;
93 if (mod
=== undefined) {
94 return (elapsed
>= renderDelay
|| forceRender
) ? <Loading
/> : null;
98 return <Error onRetry
={this.load
} />;
101 return children(mod
);
106 export default Bundle
;
This page took 0.229644 seconds and 4 git commands to generate.