@@ -13,6 +13,14 @@ export const createInstance = (defaultProps = {}) => {
13
13
constructor ( props ) {
14
14
super ( props )
15
15
16
+ this . load = this . load . bind ( this )
17
+ this . run = this . run . bind ( this )
18
+ this . cancel = this . cancel . bind ( this )
19
+ this . onResolve = this . onResolve . bind ( this )
20
+ this . onReject = this . onReject . bind ( this )
21
+ this . setData = this . setData . bind ( this )
22
+ this . setError = this . setError . bind ( this )
23
+
16
24
const promiseFn = props . promiseFn || defaultProps . promiseFn
17
25
const initialValue = props . initialValue || defaultProps . initialValue
18
26
const initialError = initialValue instanceof Error ? initialValue : undefined
@@ -35,7 +43,7 @@ export const createInstance = (defaultProps = {}) => {
35
43
this . run ( ...this . args )
36
44
} ,
37
45
setData : this . setData ,
38
- setError : this . setError
46
+ setError : this . setError ,
39
47
}
40
48
}
41
49
@@ -57,50 +65,57 @@ export const createInstance = (defaultProps = {}) => {
57
65
this . mounted = false
58
66
}
59
67
60
- load = ( ) => {
68
+ load ( ) {
61
69
const promiseFn = this . props . promiseFn || defaultProps . promiseFn
62
70
if ( ! promiseFn ) return
63
71
this . counter ++
64
72
this . setState ( { isLoading : true , startedAt : new Date ( ) , finishedAt : undefined } )
65
73
return promiseFn ( this . props ) . then ( this . onResolve ( this . counter ) , this . onReject ( this . counter ) )
66
74
}
67
75
68
- run = ( ...args ) => {
76
+ run ( ...args ) {
69
77
const deferFn = this . props . deferFn || defaultProps . deferFn
70
78
if ( ! deferFn ) return
71
79
this . counter ++
72
80
this . args = args
73
81
this . setState ( { isLoading : true , startedAt : new Date ( ) , finishedAt : undefined } )
74
- return deferFn ( ...args , this . props ) . then ( this . onResolve ( this . counter ) , this . onReject ( this . counter ) )
82
+ return deferFn ( ...args , this . props ) . then (
83
+ this . onResolve ( this . counter ) ,
84
+ this . onReject ( this . counter )
85
+ )
75
86
}
76
87
77
- cancel = ( ) => {
88
+ cancel ( ) {
78
89
this . counter ++
79
90
this . setState ( { isLoading : false , startedAt : undefined } )
80
91
}
81
92
82
- onResolve = counter => data => {
83
- if ( this . mounted && this . counter === counter ) {
84
- const onResolve = this . props . onResolve || defaultProps . onResolve
85
- this . setData ( data , ( ) => onResolve && onResolve ( data ) )
93
+ onResolve ( counter ) {
94
+ return data => {
95
+ if ( this . mounted && this . counter === counter ) {
96
+ const onResolve = this . props . onResolve || defaultProps . onResolve
97
+ this . setData ( data , ( ) => onResolve && onResolve ( data ) )
98
+ }
99
+ return data
86
100
}
87
- return data
88
101
}
89
102
90
- onReject = counter => error => {
91
- if ( this . mounted && this . counter === counter ) {
92
- const onReject = this . props . onReject || defaultProps . onReject
93
- this . setError ( error , ( ) => onReject && onReject ( error ) )
103
+ onReject ( counter ) {
104
+ return error => {
105
+ if ( this . mounted && this . counter === counter ) {
106
+ const onReject = this . props . onReject || defaultProps . onReject
107
+ this . setError ( error , ( ) => onReject && onReject ( error ) )
108
+ }
109
+ return error
94
110
}
95
- return error
96
111
}
97
112
98
- setData = ( data , callback ) => {
113
+ setData ( data , callback ) {
99
114
this . setState ( { data, error : undefined , isLoading : false , finishedAt : new Date ( ) } , callback )
100
115
return data
101
116
}
102
117
103
- setError = ( error , callback ) => {
118
+ setError ( error , callback ) {
104
119
this . setState ( { error, isLoading : false , finishedAt : new Date ( ) } , callback )
105
120
return error
106
121
}
0 commit comments