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'
44const useLoadingDelayPlugin : UseRequestPlugin < unknown , unknown [ ] > = ( inst , { loadingDelay } ) => {
55 const delayRef = ref < Timeout > ( )
66
7+ const clear = ( ) => {
8+ if ( delayRef . value ) {
9+ clearTimeout ( unref ( delayRef . value ) )
10+
11+ delayRef . value = undefined
12+ }
13+ }
14+
715 return {
816 name : 'loadingDelayPlugin' ,
917 onFinally : ( ) => {
10- if ( delayRef . value ) {
11- clearTimeout ( unref ( delayRef . value ) )
18+ clear ( )
1219
13- delayRef . value = undefined
14- }
20+ const delay = unref ( loadingDelay )
1521
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 ) {
2030 inst . setState ( {
21- loading : false ,
31+ loading : true ,
2232 } )
23- } , unref ( loadingDelay ) )
33+
34+ delayRef . value = setTimeout ( ( ) => {
35+ inst . setState ( {
36+ loading : false ,
37+ } )
38+ } , delay )
39+ }
40+ } ,
41+ onError : ( ) => {
42+ clear ( )
2443 } ,
2544 }
2645}
You can’t perform that action at this time.
0 commit comments