@@ -27,6 +27,7 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
27
27
this . setData = this . setData . bind ( this )
28
28
this . setError = this . setError . bind ( this )
29
29
30
+ const promise = props . promise
30
31
const promiseFn = props . promiseFn || defaultProps . promiseFn
31
32
const initialValue = props . initialValue || defaultProps . initialValue
32
33
const initialError = initialValue instanceof Error ? initialValue : undefined
@@ -40,8 +41,8 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
40
41
initialValue,
41
42
data : initialData ,
42
43
error : initialError ,
43
- isLoading : ! initialValue && isFunction ( promiseFn ) ,
44
- startedAt : undefined ,
44
+ isLoading : ! ! promise || ( promiseFn && ! initialValue ) ,
45
+ startedAt : promise ? new Date ( ) : undefined ,
45
46
finishedAt : initialValue ? new Date ( ) : undefined ,
46
47
counter : this . counter ,
47
48
cancel : this . cancel ,
@@ -57,14 +58,20 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
57
58
58
59
componentDidMount ( ) {
59
60
this . mounted = true
60
- this . state . initialValue || this . load ( )
61
+ if ( this . props . promise || ! this . state . initialValue ) {
62
+ this . load ( )
63
+ }
61
64
}
62
65
63
66
componentDidUpdate ( prevProps ) {
64
- const { watch, watchFn = defaultProps . watchFn , promiseFn } = this . props
67
+ const { watch, watchFn = defaultProps . watchFn , promise , promiseFn } = this . props
65
68
if ( watch !== prevProps . watch ) this . load ( )
66
69
if ( watchFn && watchFn ( { ...defaultProps , ...this . props } , { ...defaultProps , ...prevProps } ) )
67
70
this . load ( )
71
+ if ( promise !== prevProps . promise ) {
72
+ if ( promise ) this . load ( )
73
+ else this . cancel ( )
74
+ }
68
75
if ( promiseFn !== prevProps . promiseFn ) {
69
76
if ( promiseFn ) this . load ( )
70
77
else this . cancel ( )
@@ -91,24 +98,32 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
91
98
}
92
99
93
100
load ( ) {
101
+ const promise = this . props . promise
102
+ if ( promise ) {
103
+ this . start ( )
104
+ return promise . then ( this . onResolve ( this . counter ) , this . onReject ( this . counter ) )
105
+ }
106
+
94
107
const promiseFn = this . props . promiseFn || defaultProps . promiseFn
95
- if ( ! promiseFn ) return
96
- this . start ( )
97
- return promiseFn ( this . props , this . abortController ) . then (
98
- this . onResolve ( this . counter ) ,
99
- this . onReject ( this . counter )
100
- )
108
+ if ( promiseFn ) {
109
+ this . start ( )
110
+ return promiseFn ( this . props , this . abortController ) . then (
111
+ this . onResolve ( this . counter ) ,
112
+ this . onReject ( this . counter )
113
+ )
114
+ }
101
115
}
102
116
103
117
run ( ...args ) {
104
118
const deferFn = this . props . deferFn || defaultProps . deferFn
105
- if ( ! deferFn ) return
106
- this . args = args
107
- this . start ( )
108
- return deferFn ( args , { ...defaultProps , ...this . props } , this . abortController ) . then (
109
- this . onResolve ( this . counter ) ,
110
- this . onReject ( this . counter )
111
- )
119
+ if ( deferFn ) {
120
+ this . args = args
121
+ this . start ( )
122
+ return deferFn ( args , { ...defaultProps , ...this . props } , this . abortController ) . then (
123
+ this . onResolve ( this . counter ) ,
124
+ this . onReject ( this . counter )
125
+ )
126
+ }
112
127
}
113
128
114
129
cancel ( ) {
@@ -162,6 +177,7 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
162
177
if ( PropTypes ) {
163
178
Async . propTypes = {
164
179
children : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . func ] ) ,
180
+ promise : PropTypes . instanceOf ( Promise ) ,
165
181
promiseFn : PropTypes . func ,
166
182
deferFn : PropTypes . func ,
167
183
watch : PropTypes . any ,
0 commit comments