| 94 | // 组件销毁时 abort 数据 |
| 95 | let didCancel = false; |
| 96 | const fetchData = async () => { |
| 97 | // 当请求中时,避免多次请求 |
| 98 | if (state.isLoading) return; |
| 99 | // 前置处理 |
| 100 | onPreHandler?.(); |
| 101 | // fetch init |
| 102 | dispatch({ type: 'FETCH_INIT' }); |
| 103 | try { |
| 104 | const res = await fetchApi(...apiArgs); |
| 105 | // 当组件销毁时,将isLoading,isError置为false |
| 106 | if (didCancel) { |
| 107 | dispatch({ type: 'FETCH_CANCEL' }); |
| 108 | return; |
| 109 | } |
| 110 | dispatch({ type: 'FETCH_SUCCESS', payload: res }); |
| 111 | onSuccess?.(res); |
| 112 | } catch (error) { |
| 113 | if (didCancel) { |
| 114 | dispatch({ type: 'FETCH_CANCEL' }); |
| 115 | return; |
| 116 | } |
| 117 | dispatch({ type: 'FETCH_FAILURE', payload: error }); |
| 118 | onFail?.(error); |
| 119 | } finally { |
| 120 | onFinally?.(); |
| 121 | } |
| 122 | }; |
| 123 | fetchData(); |
| 124 | return () => { |
| 125 | didCancel = true; |