2017-07-08 00:06:02 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2023-12-09 20:29:23 +01:00
|
|
|
import { PureComponent } from 'react';
|
2017-07-08 00:06:02 +02:00
|
|
|
|
|
|
|
const emptyComponent = () => null;
|
|
|
|
const noop = () => { };
|
|
|
|
|
2023-12-09 20:29:23 +01:00
|
|
|
class Bundle extends PureComponent {
|
2017-07-08 00:06:02 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
fetchComponent: PropTypes.func.isRequired,
|
|
|
|
loading: PropTypes.func,
|
|
|
|
error: PropTypes.func,
|
|
|
|
children: PropTypes.func.isRequired,
|
|
|
|
renderDelay: PropTypes.number,
|
|
|
|
onFetch: PropTypes.func,
|
|
|
|
onFetchSuccess: PropTypes.func,
|
|
|
|
onFetchFail: PropTypes.func,
|
2023-02-03 20:52:07 +01:00
|
|
|
};
|
2017-07-08 00:06:02 +02:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
loading: emptyComponent,
|
|
|
|
error: emptyComponent,
|
|
|
|
renderDelay: 0,
|
|
|
|
onFetch: noop,
|
|
|
|
onFetchSuccess: noop,
|
|
|
|
onFetchFail: noop,
|
2023-02-03 20:52:07 +01:00
|
|
|
};
|
2017-07-08 00:06:02 +02:00
|
|
|
|
2023-12-09 20:29:23 +01:00
|
|
|
static cache = new Map;
|
2017-07-09 14:52:03 +02:00
|
|
|
|
2017-07-08 00:06:02 +02:00
|
|
|
state = {
|
|
|
|
mod: undefined,
|
|
|
|
forceRender: false,
|
2023-02-03 20:52:07 +01:00
|
|
|
};
|
2017-07-08 00:06:02 +02:00
|
|
|
|
2023-05-10 09:05:32 +02:00
|
|
|
UNSAFE_componentWillMount() {
|
2017-07-08 00:06:02 +02:00
|
|
|
this.load(this.props);
|
|
|
|
}
|
|
|
|
|
2023-05-10 09:05:32 +02:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2017-07-08 00:06:02 +02:00
|
|
|
if (nextProps.fetchComponent !== this.props.fetchComponent) {
|
|
|
|
this.load(nextProps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
if (this.timeout) {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
load = (props) => {
|
|
|
|
const { fetchComponent, onFetch, onFetchSuccess, onFetchFail, renderDelay } = props || this.props;
|
2023-12-09 20:29:23 +01:00
|
|
|
const cachedMod = Bundle.cache.get(fetchComponent);
|
2017-07-08 00:06:02 +02:00
|
|
|
|
2018-11-28 14:56:22 +01:00
|
|
|
if (fetchComponent === undefined) {
|
|
|
|
this.setState({ mod: null });
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2017-07-08 00:06:02 +02:00
|
|
|
onFetch();
|
|
|
|
|
2023-12-09 20:29:23 +01:00
|
|
|
if (cachedMod) {
|
|
|
|
this.setState({ mod: cachedMod.default });
|
2017-07-09 14:52:03 +02:00
|
|
|
onFetchSuccess();
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2017-07-25 01:05:44 +02:00
|
|
|
this.setState({ mod: undefined });
|
|
|
|
|
|
|
|
if (renderDelay !== 0) {
|
|
|
|
this.timestamp = new Date();
|
|
|
|
this.timeout = setTimeout(() => this.setState({ forceRender: true }), renderDelay);
|
|
|
|
}
|
|
|
|
|
2017-07-08 00:06:02 +02:00
|
|
|
return fetchComponent()
|
|
|
|
.then((mod) => {
|
2023-12-09 20:29:23 +01:00
|
|
|
Bundle.cache.set(fetchComponent, mod);
|
2017-07-08 00:06:02 +02:00
|
|
|
this.setState({ mod: mod.default });
|
|
|
|
onFetchSuccess();
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this.setState({ mod: null });
|
|
|
|
onFetchFail(error);
|
|
|
|
});
|
2023-02-03 20:52:07 +01:00
|
|
|
};
|
2017-07-08 00:06:02 +02:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { loading: Loading, error: Error, children, renderDelay } = this.props;
|
|
|
|
const { mod, forceRender } = this.state;
|
|
|
|
const elapsed = this.timestamp ? (new Date() - this.timestamp) : renderDelay;
|
|
|
|
|
|
|
|
if (mod === undefined) {
|
|
|
|
return (elapsed >= renderDelay || forceRender) ? <Loading /> : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod === null) {
|
|
|
|
return <Error onRetry={this.load} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return children(mod);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Bundle;
|