@@ -9,15 +9,26 @@ const isGeneratorFn = (thing) => typeof thing === 'function' &&
9
9
thing . constructor &&
10
10
thing . constructor . name === 'GeneratorFunction' ;
11
11
12
+ const isEvent = ( thing ) => ! ! ( thing && typeof thing === 'object' && thing . type ) ;
13
+
12
14
const useAsyncEffect = ( generator , deps ) => {
13
- let promise ;
15
+ let ref = useRef ( null ) ;
16
+
17
+ const cancel = ( reason ) => {
18
+ const promise = ref . current ;
19
+ if ( promise ) {
20
+ ref . current = null ;
21
+ return promise . cancel ( isEvent ( reason ) ? undefined : reason )
22
+ }
23
+ return false ;
24
+ }
14
25
15
26
useEffect ( ( ) => {
16
27
if ( ! isGeneratorFn ( generator ) ) {
17
28
throw TypeError ( 'useAsyncEffect requires a generator as the first argument' ) ;
18
29
}
19
30
20
- promise = CPromise . resolveGenerator ( generator , { resolveSignatures : true } ) ;
31
+ let promise = ref . current = CPromise . resolveGenerator ( generator , { resolveSignatures : true } ) ;
21
32
22
33
let cb ;
23
34
@@ -33,45 +44,12 @@ const useAsyncEffect = (generator, deps) => {
33
44
} ) ;
34
45
35
46
return ( ) => {
36
- promise . cancel ( E_REASON_UNMOUNTED ) ;
37
- promise = null ;
47
+ cancel ( E_REASON_UNMOUNTED ) ;
38
48
cb && cb ( ) ;
39
49
}
40
50
} , deps ) ;
41
51
42
- return ( reason ) => ! ! promise && promise . cancel ( reason ) ;
43
- }
44
-
45
- function asyncBottleneck ( fn , concurrency = 1 ) {
46
- const queue = [ ] ;
47
- let pending = 0 ;
48
- return ( ...args ) => new CPromise ( resolve => pending === concurrency ? queue . push ( resolve ) : resolve ( ) )
49
- . then ( ( ) => {
50
- pending ++ ;
51
- return fn ( ...args ) . finally ( ( value ) => {
52
- pending -- ;
53
- queue . length && queue . shift ( ) ( ) ;
54
- return value ;
55
- } )
56
- } ) ;
57
- }
58
-
59
- function asyncBottleneck2 ( fn , concurrency = 1 ) {
60
- const queue = [ ] ;
61
- let pending = 0 ;
62
- return async ( ...args ) => {
63
- if ( pending === concurrency ) {
64
- await new Promise ( ( resolve ) => queue . push ( resolve ) ) ;
65
- }
66
-
67
- pending ++ ;
68
-
69
- return fn ( ...args ) . then ( ( value ) => {
70
- pending -- ;
71
- queue . length && queue . shift ( ) ( ) ;
72
- return value ;
73
- } ) ;
74
- } ;
52
+ return cancel ;
75
53
}
76
54
77
55
const useAsyncCallback = ( generator , options ) => {
@@ -103,7 +81,7 @@ const useAsyncCallback = (generator, options) => {
103
81
const resolveGenerator = ( ) => CPromise . resolveGenerator ( generator , { args, resolveSignatures : true } ) ;
104
82
105
83
if ( concurrency ) {
106
- const promise = new CPromise ( resolve => {
84
+ const promise = new CPromise ( resolve => {
107
85
if ( current . pending === concurrency ) {
108
86
return queue . push ( resolve ) ;
109
87
}
@@ -125,14 +103,15 @@ const useAsyncCallback = (generator, options) => {
125
103
cancel ( ) ;
126
104
}
127
105
128
- const promise = resolveGenerator ( ) . finally ( ( ) => {
106
+ const promise = resolveGenerator ( ) . finally ( ( ) => {
129
107
const index = promises . indexOf ( promise ) ;
130
108
index !== - 1 && promises . splice ( index , 1 ) ;
131
109
} ) ;
132
110
133
111
promises . push ( promise ) ;
134
112
135
- return combine ? promise . finally ( ( ) => { } ) : promise ;
113
+ return combine ? promise . finally ( ( ) => {
114
+ } ) : promise ;
136
115
} ) ;
137
116
138
117
const cancel = ( reason ) => {
0 commit comments