|
| 1 | +import * as PropTypes from 'prop-types'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { isPromise } from '../../utils'; |
| 4 | +import { statusTypes } from './config'; |
| 5 | + |
| 6 | +export interface IResolveProps { |
| 7 | + /** The promise to be resolved */ |
| 8 | + promise?: Promise<any>; |
| 9 | + |
| 10 | + /** Will be displayed after promise is resolved */ |
| 11 | + resolved?: (value) => React.ReactNode; |
| 12 | + |
| 13 | + /** Will be displayed while promise is handled */ |
| 14 | + pending?: React.ReactNode; |
| 15 | + |
| 16 | + /** Will be displayed if promise is rejected */ |
| 17 | + rejected?: (error) => React.ReactNode; |
| 18 | +} |
| 19 | + |
| 20 | +const initialState = { |
| 21 | + status: statusTypes.none, |
| 22 | + value: '', |
| 23 | +}; |
| 24 | + |
| 25 | +type IResolveState = Readonly<typeof initialState>; |
| 26 | + |
| 27 | +/** |
| 28 | + * A component to render based on a promise |
| 29 | + * |
| 30 | + * @example |
| 31 | + * <Resolve |
| 32 | + * promise = {aPromise} |
| 33 | + * resolved = { |
| 34 | + * value => <p>{`Resolved value is ${value}`}</p> |
| 35 | + * } /> |
| 36 | + */ |
| 37 | +class Resolve extends React.Component<IResolveProps, IResolveState> { |
| 38 | + // PropTypes |
| 39 | + public static propTypes = { |
| 40 | + pending: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), |
| 41 | + promise: isPromise, |
| 42 | + rejected: PropTypes.func, |
| 43 | + resolved: PropTypes.func, |
| 44 | + }; |
| 45 | + |
| 46 | + // Initialize state |
| 47 | + public readonly state: IResolveState = initialState; |
| 48 | + |
| 49 | + // Create escape hatch to stop handling of promise if unmounted |
| 50 | + public unmounted = false; |
| 51 | + |
| 52 | + public componentDidMount() { |
| 53 | + // Start handling the promise, must happen after mount as setState is called when promise is handled |
| 54 | + this._handlePromise(this.props.promise); |
| 55 | + } |
| 56 | + |
| 57 | + public componentDidUpdate(prevProps) { |
| 58 | + if (this.props.promise !== prevProps.promise) { |
| 59 | + this.setState({ |
| 60 | + status: statusTypes.none, |
| 61 | + }); |
| 62 | + this._handlePromise(this.props.promise); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public componentWillUnmount() { |
| 67 | + this.unmounted = true; |
| 68 | + } |
| 69 | + |
| 70 | + // Promise resolver function |
| 71 | + public _handlePromise(promise) { |
| 72 | + // Store the current promise to fast exit if promise is change during handling |
| 73 | + const currentPromise = promise; |
| 74 | + this.setState({ |
| 75 | + status: statusTypes.pending, |
| 76 | + }); |
| 77 | + promise |
| 78 | + .then(success => { |
| 79 | + // Escape early as promise is changed |
| 80 | + if (currentPromise !== promise) { |
| 81 | + return; |
| 82 | + } |
| 83 | + if (!this.unmounted) { |
| 84 | + this.setState({ |
| 85 | + status: statusTypes.resolved, |
| 86 | + value: success, |
| 87 | + }); |
| 88 | + } |
| 89 | + }) |
| 90 | + .catch(reason => { |
| 91 | + // Escape early as promise is changed |
| 92 | + if (currentPromise !== promise) { |
| 93 | + return; |
| 94 | + } |
| 95 | + if (!this.unmounted) { |
| 96 | + this.setState({ |
| 97 | + status: statusTypes.rejected, |
| 98 | + value: reason, |
| 99 | + }); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + public render() { |
| 105 | + const { pending, resolved, rejected } = this.props; |
| 106 | + const { status, value } = this.state; |
| 107 | + |
| 108 | + switch (status) { |
| 109 | + case statusTypes.none: |
| 110 | + break; |
| 111 | + case statusTypes.pending: |
| 112 | + if (pending) { |
| 113 | + return pending; |
| 114 | + } |
| 115 | + break; |
| 116 | + case statusTypes.resolved: |
| 117 | + if (resolved) { |
| 118 | + return resolved(value); |
| 119 | + } |
| 120 | + break; |
| 121 | + case statusTypes.rejected: |
| 122 | + if (rejected) { |
| 123 | + return rejected(value); |
| 124 | + } |
| 125 | + break; |
| 126 | + default: |
| 127 | + break; |
| 128 | + } |
| 129 | + |
| 130 | + return null; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +export default Resolve; |
0 commit comments