|
| 1 | +import { useState, useEffect, useMemo, useRef } from "react" |
| 2 | + |
| 3 | +const useAsync = (opts, init) => { |
| 4 | + const counter = useRef(0) |
| 5 | + const isMounted = useRef(true) |
| 6 | + const lastArgs = useRef(undefined) |
| 7 | + |
| 8 | + const options = typeof opts === "function" ? { promiseFn: opts, initialValue: init } : opts |
| 9 | + const { promiseFn, deferFn, initialValue, onResolve, onReject, watch } = options |
| 10 | + |
| 11 | + const [state, setState] = useState({ |
| 12 | + data: initialValue instanceof Error ? undefined : initialValue, |
| 13 | + error: initialValue instanceof Error ? initialValue : undefined, |
| 14 | + startedAt: promiseFn ? new Date() : undefined, |
| 15 | + finishedAt: initialValue ? new Date() : undefined, |
| 16 | + }) |
| 17 | + |
| 18 | + const handleData = (data, callback = () => {}) => { |
| 19 | + if (isMounted.current) { |
| 20 | + setState(state => ({ ...state, data, error: undefined, finishedAt: new Date() })) |
| 21 | + callback(data) |
| 22 | + } |
| 23 | + return data |
| 24 | + } |
| 25 | + |
| 26 | + const handleError = (error, callback = () => {}) => { |
| 27 | + if (isMounted.current) { |
| 28 | + setState(state => ({ ...state, error, finishedAt: new Date() })) |
| 29 | + callback(error) |
| 30 | + } |
| 31 | + return error |
| 32 | + } |
| 33 | + |
| 34 | + const handleResolve = count => data => count === counter.current && handleData(data, onResolve) |
| 35 | + const handleReject = count => error => count === counter.current && handleError(error, onReject) |
| 36 | + |
| 37 | + const start = () => { |
| 38 | + counter.current++ |
| 39 | + setState(state => ({ |
| 40 | + ...state, |
| 41 | + startedAt: new Date(), |
| 42 | + finishedAt: undefined, |
| 43 | + })) |
| 44 | + } |
| 45 | + |
| 46 | + const load = () => { |
| 47 | + const isPreInitialized = initialValue && counter.current === 0 |
| 48 | + if (promiseFn && !isPreInitialized) { |
| 49 | + start() |
| 50 | + promiseFn(options).then(handleResolve(counter.current), handleReject(counter.current)) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + const run = (...args) => { |
| 55 | + if (deferFn) { |
| 56 | + start() |
| 57 | + lastArgs.current = args |
| 58 | + return deferFn(...args, options).then(handleResolve(counter.current), handleReject(counter.current)) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + useEffect(load, [promiseFn, watch]) |
| 63 | + useEffect(() => () => (isMounted.current = false), []) |
| 64 | + |
| 65 | + return useMemo( |
| 66 | + () => ({ |
| 67 | + ...state, |
| 68 | + isLoading: state.startedAt && (!state.finishedAt || state.finishedAt < state.startedAt), |
| 69 | + initialValue, |
| 70 | + run, |
| 71 | + reload: () => (lastArgs.current ? run(...lastArgs.current) : load()), |
| 72 | + cancel: () => { |
| 73 | + counter.current++ |
| 74 | + setState(state => ({ ...state, startedAt: undefined })) |
| 75 | + }, |
| 76 | + setData: handleData, |
| 77 | + setError: handleError, |
| 78 | + }), |
| 79 | + [state] |
| 80 | + ) |
| 81 | +} |
| 82 | + |
| 83 | +const unsupported = () => { |
| 84 | + throw new Error( |
| 85 | + "useAsync requires react@16.7.0-alpha. Upgrade your React version or use the <Async> component instead." |
| 86 | + ) |
| 87 | +} |
| 88 | + |
| 89 | +export default (useState ? useAsync : unsupported) |
0 commit comments