1
1
import { useState , useEffect , useMemo } from "react"
2
2
3
- const useAsync = props => {
3
+ const useAsync = ( opts , init ) => {
4
4
let counter = 0
5
5
let isMounted = false
6
6
let lastArgs = undefined
7
7
8
- const initialError = props . initialValue instanceof Error ? props . initialValue : undefined
9
- const initialData = initialError ? undefined : props . initialValue
10
- const [ data , setData ] = useState ( initialData )
11
- const [ error , setError ] = useState ( initialError )
12
- const [ startedAt , setStartedAt ] = useState ( props . promiseFn ? new Date ( ) : undefined )
13
- const [ finishedAt , setFinishedAt ] = useState ( props . initialValue ? new Date ( ) : undefined )
8
+ const options = typeof opts === "function" ? { promiseFn : opts , initialValue : init } : opts
9
+ const { promiseFn, deferFn, initialValue, onResolve, onReject, watch } = options
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 )
14
15
15
16
const cancel = ( ) => {
16
17
counter ++
@@ -44,21 +45,21 @@ const useAsync = props => {
44
45
return error
45
46
}
46
47
47
- const onResolve = count => data => count === counter && handleData ( data , props . onResolve )
48
- const onReject = count => error => count === counter && handleError ( error , props . onReject )
48
+ const handleResolve = count => data => count === counter && handleData ( data , onResolve )
49
+ const handleReject = count => error => count === counter && handleError ( error , onReject )
49
50
50
51
const load = ( ) => {
51
- if ( props . promiseFn ) {
52
+ if ( promiseFn ) {
52
53
start ( )
53
- props . promiseFn ( props ) . then ( onResolve ( counter ) , onReject ( counter ) )
54
+ promiseFn ( options ) . then ( handleResolve ( counter ) , handleReject ( counter ) )
54
55
}
55
56
}
56
57
57
58
const run = ( ...args ) => {
58
- if ( props . deferFn ) {
59
+ if ( deferFn ) {
59
60
lastArgs = args
60
61
start ( )
61
- return props . deferFn ( ...args , props ) . then ( onResolve ( counter ) , onReject ( counter ) )
62
+ return deferFn ( ...args , options ) . then ( handleResolve ( counter ) , handleReject ( counter ) )
62
63
}
63
64
}
64
65
@@ -69,7 +70,7 @@ const useAsync = props => {
69
70
return ( ) => ( isMounted = false )
70
71
} , [ ] )
71
72
72
- useEffect ( load , [ props . promiseFn , props . watch ] )
73
+ useEffect ( load , [ promiseFn , watch ] )
73
74
74
75
return useMemo (
75
76
( ) => ( {
@@ -78,12 +79,12 @@ const useAsync = props => {
78
79
finishedAt,
79
80
data,
80
81
error,
82
+ initialValue,
81
83
cancel,
82
84
run,
83
85
reload,
84
86
setData : handleData ,
85
- setError : handleError ,
86
- initialValue : props . initialValue
87
+ setError : handleError
87
88
} ) ,
88
89
[ data , error , startedAt , finishedAt ]
89
90
)
0 commit comments