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