File tree Expand file tree Collapse file tree 1 file changed +29
-10
lines changed
packages/hooks/src/useRequest/plugins Expand file tree Collapse file tree 1 file changed +29
-10
lines changed Original file line number Diff line number Diff line change @@ -4,23 +4,42 @@ import { Timeout, UseRequestPlugin } from '../types'
4
4
const useLoadingDelayPlugin : UseRequestPlugin < unknown , unknown [ ] > = ( inst , { loadingDelay } ) => {
5
5
const delayRef = ref < Timeout > ( )
6
6
7
+ const clear = ( ) => {
8
+ if ( delayRef . value ) {
9
+ clearTimeout ( unref ( delayRef . value ) )
10
+
11
+ delayRef . value = undefined
12
+ }
13
+ }
14
+
7
15
return {
8
16
name : 'loadingDelayPlugin' ,
9
17
onFinally : ( ) => {
10
- if ( delayRef . value ) {
11
- clearTimeout ( unref ( delayRef . value ) )
18
+ clear ( )
12
19
13
- delayRef . value = undefined
14
- }
20
+ const delay = unref ( loadingDelay )
15
21
16
- inst . setState ( {
17
- loading : true ,
18
- } )
19
- setTimeout ( ( ) => {
22
+ /**
23
+ *
24
+ * if loadingDelay is set, the loading state will be delayed,
25
+ * until the delay time is reached.
26
+ *
27
+ * if delay is set to 0, the loading state will not be delayed. because 0 is mean nothing.
28
+ */
29
+ if ( delay ) {
20
30
inst . setState ( {
21
- loading : false ,
31
+ loading : true ,
22
32
} )
23
- } , unref ( loadingDelay ) )
33
+
34
+ delayRef . value = setTimeout ( ( ) => {
35
+ inst . setState ( {
36
+ loading : false ,
37
+ } )
38
+ } , delay )
39
+ }
40
+ } ,
41
+ onError : ( ) => {
42
+ clear ( )
24
43
} ,
25
44
}
26
45
}
You can’t perform that action at this time.
0 commit comments