vue3-request 1.0.24 → 1.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -21
- package/dist/vue3-request.cjs.js +1 -1
- package/dist/vue3-request.es.d.ts +32 -20
- package/dist/vue3-request.es.js +234 -251
- package/dist/vue3-request.umd.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,36 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
更多内容请查看<a href="https://Flame-00.github.io/vue3-request/" target="_blank">文档</a>
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[演练场](https://codesandbox.io/p/devbox/yrxqp8)
|
|
4
|
+
|
|
5
|
+
## 核心优势
|
|
4
6
|
|
|
5
7
|
Vue3-Request 是一个专为 Vue 3 设计的异步请求处理库,它通过统一的 useRequest Hook 和强大的插件生态,彻底简化了异步操作和 API 调用的复杂性:
|
|
6
8
|
|
|
7
9
|
✨ 统一状态管理
|
|
8
10
|
|
|
9
11
|
- 🚀 所有数据都具有响应式
|
|
10
|
-
- 📊 自动管理 loading、data、error、signal、params 等状态
|
|
12
|
+
- 📊 自动管理 loading、data、res、error、signal、params 等状态
|
|
11
13
|
- 🔄 提供 run、runAsync、refresh、cancel、abort 等便捷方法
|
|
12
14
|
- 🎛️ 支持手动和自动执行模式,满足不同业务场景
|
|
13
15
|
|
|
14
16
|
🧩 强大的插件生态 内置 9 大核心插件,开箱即用:
|
|
15
17
|
|
|
16
|
-
- <img src="https://
|
|
17
|
-
- 🔄 错误重试
|
|
18
|
-
- ⚡ 防抖节流
|
|
19
|
-
- 💾 智能缓存
|
|
20
|
-
- 🔁 轮询请求
|
|
21
|
-
- 👁️ 窗口聚焦刷新
|
|
22
|
-
- 📦 依赖刷新
|
|
23
|
-
- ⏳ 准备状态控制
|
|
24
|
-
- 💻 完整 TypeScript 支持
|
|
25
|
-
- 🔌 框架无关
|
|
26
|
-
- 📝 丰富的生命周期
|
|
27
|
-
- 🎨 可扩展架构
|
|
28
|
-
- 📚 完善文档
|
|
29
|
-
|
|
30
|
-
## 为什么选择 Vue3-Request?
|
|
31
|
-
|
|
32
|
-
在一个充满挑战的异步世界中,Vue3-Request 不仅仅是一个工具库,更是你的开发伙伴。它让复杂的异步逻辑变得简单优雅,让重复的样板代码成为历史,让你可以专注于真正重要的业务逻辑。
|
|
33
|
-
从简单的数据获取到复杂的业务场景,从性能优化到用户体验,Vue3-Request 都为你提供了最佳实践的解决方案。让我们一起拥抱更高效、更优雅的异步编程方式!
|
|
18
|
+
- <img src="https://Flame-00.github.io/vue3-request/cancel.svg" alt="取消请求" width="20" height="20" /> 请求中止
|
|
19
|
+
- 🔄 错误重试
|
|
20
|
+
- ⚡ 防抖节流
|
|
21
|
+
- 💾 智能缓存
|
|
22
|
+
- 🔁 轮询请求
|
|
23
|
+
- 👁️ 窗口聚焦刷新
|
|
24
|
+
- 📦 依赖刷新
|
|
25
|
+
- ⏳ 准备状态控制
|
|
26
|
+
- 💻 完整 TypeScript 支持
|
|
27
|
+
- 🔌 框架无关
|
|
28
|
+
- 📝 丰富的生命周期
|
|
29
|
+
- 🎨 可扩展架构
|
|
30
|
+
- 📚 完善文档
|
|
34
31
|
|
|
35
32
|
## ⚡ 快速安装
|
|
36
33
|
|
package/dist/vue3-request.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("vue"),p=e=>e,y=e=>e==null,S=typeof window>"u";function x(e){return y(e)?e:typeof e=="number"&&!isNaN(e)}function b(e,t){return x(e)===void 0?{is:!1,value:0}:x(e)?t&&e===-1?{is:!0,value:-1}:{is:!0,value:e===0||e&&e<0?0:e}:{is:!1,value:0}}const M=(e,t)=>{let n=t;for(let i=e.length;i-- >0;){const r=e[i];n=r(n)}return n?.()},V=()=>new Promise(()=>{}),P=new Map,O=(e,t,{data:n,params:i,time:r})=>{let a;const u=L(e),{is:o,value:f}=b(t,!0);o&&(u?.timer&&clearTimeout(u.timer),f!==-1&&(a=setTimeout(()=>{A(e)},f)),P.set(e,{data:n,params:i,time:r,timer:a}))},L=e=>P.get(e),A=e=>{e&&typeof e=="string"?P.delete(e):P.clear()};class j{constructor(t,n){this.service=t,this.options=n,this.state=s.reactive({data:void 0,res:void 0,error:void 0,params:n?.defaultParams||[],loading:!1,isFinished:!1,isAborted:!1,signal:new AbortController().signal})}currentRequestId=0;pluginImpls=[];state;abort=()=>{};setState=t=>{if(Object.assign(this.state,t),t.data&&typeof t.data=="object"&&!y(t.data)){const n=this.options.resKey||"data";Reflect.has(t.data,n)&&(this.state.res=t.data[n])}};executePlugin=(t,...n)=>{if(t==="onRequest")return{servicePromise:M(this.pluginImpls.map(r=>r.onRequest).filter(Boolean),n[0])};{const i=this.pluginImpls.map(r=>r[t]?.(...n)).filter(Boolean);return Object.assign({},...i)}};loading=t=>{this.setState({loading:t,isFinished:!t})};onFinished=()=>{this.executePlugin("onFinally",this.state.params),this.loading(!1),this.options.onFinally?.(this.state.params)};runAsync=async(...t)=>{const n=++this.currentRequestId,{isReturn:i,isReady:r,...a}=this.executePlugin("onBefore",t);if(!r)return V();if(this.setState({params:t,...a}),this.loading(!0),i)return this.loading(!1),a.data;this.options.onBefore?.(t);try{const u=()=>this.service(...t);let{servicePromise:o}=this.executePlugin("onRequest",u);return o||(o=u()),n!==this.currentRequestId?V():(this.setState({data:await o,error:void 0}),this.executePlugin("onSuccess",t),this.options.onSuccess?.(t),this.onFinished(),this.state.data)}catch(u){if(n!==this.currentRequestId)return V();throw this.setState({data:void 0,res:void 0,error:u}),this.executePlugin("onError",t),this.options.onError?.(t),this.onFinished(),this.state.error}};run=(...t)=>{this.runAsync(...t).catch(n=>{this.options.onError})};refresh=()=>{this.run(...this.state.params)};refreshAsync=async()=>await this.runAsync(...this.state.params);cancel=()=>{this.executePlugin("onCancel"),this.currentRequestId++,this.loading(!1)}}function N(e,t,n){const i={manual:!1,...t},r=new j(e,i);r.pluginImpls=n.map(h=>h(r,i)),s.onMounted(()=>{if(!i.manual){const h=r.state.params;r.run(...h)}}),s.onUnmounted(r.cancel);const{run:a,cancel:u,refresh:o,runAsync:f,refreshAsync:v,abort:l}=r;return{...s.toRefs(r.state),run:a,cancel:u,refresh:o,runAsync:f,abort:l,refreshAsync:v,clearCache:A}}const U=p((e,{abortPrevious:t=!0})=>{let n=null;const i=()=>{n=new AbortController,e.setState({signal:n.signal,isAborted:n.signal.aborted})};return e.abort=()=>{n&&!n.signal.aborted&&!e.state.isFinished&&(n.abort(),e.setState({isAborted:n.signal.aborted}))},{onBefore:()=>{typeof t=="boolean"&&t&&e.abort(),i()},onRequest:r=>()=>r(),onCancel:()=>{e.abort(),n=null}}}),R=new Map,W=(e,{data:t,params:n,time:i})=>{R.has(e)&&R.get(e)?.forEach(a=>a({data:t,params:n,time:i}))},B=(e,t)=>(R.has(e)?R.get(e)?.add(t):R.set(e,new Set),()=>{const n=R.get(e);n?.delete(t),n?.size===0&&R.delete(e)}),C=new Map,_=(e,t)=>{C.set(e,t),t.then(()=>{}).catch(()=>{}).finally(()=>{H(e)})},D=e=>C.get(e),H=e=>{e&&typeof e=="string"?C.delete(e):C.clear()},$=p((e,{cacheKey:t,cacheTime:n=3e5,staleTime:i=0,setCache:r,getCache:a})=>{const u=s.ref(null);let o=null;const{is:f,value:v}=b(i,!0);if(!f)return{};const l=typeof t=="function"?t:()=>t,h=(d,c)=>{r?r(d,c):O(d,n,c),W(d,c)},m=d=>a?a(d):L(d);function w(){const d=l();if(!d)return;const c=m(d);c&&Reflect.has(c,"data")&&e.setState({data:c.data,params:c.params}),u.value=B(d,g=>{e.setState({data:g.data})})}return w(),s.onUnmounted(()=>{u.value?.()}),{onBefore:d=>{const c=l(d);if(!c)return null;const g=m(c);return!g||!Reflect.has(g,"data")?{}:v===-1||Date.now()-g.time<v?{data:g.data,error:void 0,isReturn:!0}:{data:g.data,error:void 0}},onRequest:d=>{const c=l(e.state.params);if(!c)return d;let g=D(c);return g&&g!==o?()=>g:(g=d(),o=g,_(c,g),()=>g)},onSuccess:d=>{const c=l(d);c&&(u.value?.(),h(c,{data:e.state.data,params:d,time:Date.now()}),u.value=B(c,g=>{e.setState({data:g.data})}))}}}),z=p((e,{errorRetryCount:t=0,errorRetryInterval:n})=>{const i=s.ref(),r=s.ref(0),a=s.ref(!1),u=s.computed(()=>1e3*Math.pow(2,r.value)),o=()=>{let l=null;r.value++;const{value:h}=b(s.toValue(t),!0),m=h===-1,w=r.value<=h;if(m||w){let{is:d,value:c}=b(s.toValue(n));d||(c=s.toValue(Math.min(u.value,3e4))),l=setTimeout(()=>{a.value=!0,e.refresh()},c)}return()=>{l&&clearTimeout(l)}},f=()=>{i.value?.()},{is:v}=b(s.toValue(t),!0);return v?{onBefore:()=>{a.value||(r.value=0),a.value=!1,f()},onSuccess:()=>{r.value=0},onError:()=>{i.value=o()},onCancel:()=>{r.value=0,f()}}:{}}),E=()=>S||y(window.document?.visibilityState)?!0:document.visibilityState==="visible",T=new Set,J=e=>(T.add(e),()=>{T.delete(e)}),Q=()=>{E()&&T.forEach(e=>{e()})};!S&&window?.addEventListener&&window.addEventListener("visibilitychange",Q,!1);const X=p((e,{pollingInterval:t,pollingWhenHidden:n=!0,errorRetryCount:i})=>{const r=s.ref(null),a=s.ref(),u=()=>{let o;const{value:f}=b(s.toValue(i),!0);if(e.state.error&&f!==0)return;const{is:v,value:l}=b(s.toValue(t));return v?(o=setTimeout(()=>{!s.toValue(n)&&!E()?r.value=J(e.refresh):e.refresh()},l),()=>{o&&clearTimeout(o),r.value?.()}):void 0};return s.watch([()=>s.toValue(t),()=>s.toValue(n)],()=>{a.value?.(),a.value=u()}),s.onUnmounted(()=>{r.value?.()}),{onBefore:()=>{a.value?.()},onCancel:()=>{a.value?.()},onFinally:()=>{a.value=u()}}}),Y=p((e,{manual:t,refreshDeps:n,refreshDepsAction:i})=>{if(y(n)||typeof n!="object"&&typeof n!="function")return{};const r=s.watch(n,()=>{t||(i?i():e.refresh())},{deep:!0});return{onCancel:()=>{r()}}}),Z=()=>(!S&&window.navigator?.onLine)??!0,F=new Set,G=e=>(F.add(e),()=>{F.delete(e)}),q=()=>{!E()||!Z()||F.forEach(e=>{e()})};!S&&window?.addEventListener&&(window.addEventListener("visibilitychange",q,!1),window.addEventListener("focus",q,!1));const I=p((e,{refocusTimespan:t=5e3,refreshOnWindowFocus:n=!1})=>{const i=s.ref(),{is:r}=b(s.toValue(t));if(!r||y(s.toValue(n))||typeof s.toValue(n)!="boolean")return{};const a=(u,o)=>{let f=!1;return()=>{f||(f=!0,u(),setTimeout(()=>{f=!1},o))}};return s.watchEffect(()=>{i.value?.(),s.toValue(n)&&(i.value=G(a(e.refresh,s.toValue(t))))}),s.onUnmounted(()=>{i.value?.()}),{}}),k=p((e,{manual:t,ready:n=!0,defaultParams:i=[]})=>{const r=s.watch(()=>s.toValue(n),a=>{!t&&a&&e.run(...i)},{flush:"sync"});return{onBefore:()=>({isReady:s.toValue(n)}),onCancel:()=>{r()}}});function K(e,t,n){var i=null,r=null,a=typeof n=="boolean"?{leading:n,trailing:!n}:Object.assign({leading:!1,trailing:!0},n),u=!1,o=null,f=a.leading,v=a.trailing,l=function(){i=null,r=null},h=function(){u=!0,e.apply(r,i),l()},m=function(){if(f===!0&&(o=null),u){l();return}if(v===!0){h();return}l()},w=function(){var c=o!==null;return c&&clearTimeout(o),l(),o=null,u=!1,c},d=function(){u=!1,i=arguments,r=this,o===null?f===!0&&h():clearTimeout(o),o=setTimeout(m,t)};return d.cancel=w,d}const ee=p((e,{debounceWait:t,debounceOptions:n,manual:i})=>{const r=s.ref(!1);let a=null;const u=e.runAsync;return i||(r.value=!0),s.watchEffect(()=>{const{is:o,value:f}=b(s.toValue(t));if(!o)return;const v=s.toValue(n);a=K(l=>l(),f,v),e.runAsync=(...l)=>new Promise((h,m)=>{r.value?(r.value=!1,u(...l).then(h).catch(m)):a(()=>{u(...l).then(h).catch(m)})}),s.onWatcherCleanup(()=>{a?.cancel(),e.runAsync=u})}),{onCancel:()=>{a?.cancel()}}});function te(e,t,n){var i=null,r=null,a=!1,u=null,o=Object.assign({leading:!0,trailing:!0},n),f=o.leading,v=o.trailing,l=function(){i=null,r=null},h=function(){a=!0,e.apply(r,i),u=setTimeout(m,t),l()},m=function(){if(u=null,a){l();return}if(v===!0){h();return}l()},w=function(){var c=u!==null;return c&&clearTimeout(u),l(),u=null,a=!1,c},d=function(){if(i=arguments,r=this,a=!1,u===null&&f===!0){h();return}v===!0&&(u=setTimeout(m,t))};return d.cancel=w,d}const ne=p((e,{throttleWait:t,throttleOptions:n})=>{let i=null;const r=e.runAsync;return s.watchEffect(()=>{const{is:a,value:u}=b(s.toValue(t));if(!a)return;const o=s.toValue(n);i=te(f=>f(),u,o),e.runAsync=(...f)=>new Promise((v,l)=>{i(()=>{r(...f).then(v).catch(l)})}),s.onWatcherCleanup(()=>{i?.cancel(),e.runAsync=r})}),{onCancel:()=>{i?.cancel()}}}),re=[z,X,Y,I,U,k,ee,ne,$];function ie(e,t,n){return N(e,t||{},[...n||[],...re])}exports.clearCache=A;exports.definePlugin=p;exports.useRequest=ie;
|
|
@@ -3,11 +3,11 @@ import { Ref } from 'vue';
|
|
|
3
3
|
import { ToRefs } from 'vue';
|
|
4
4
|
import { WatchSource } from 'vue';
|
|
5
5
|
|
|
6
|
-
declare type BaseOptions<D, P extends any[]> = Partial<{
|
|
6
|
+
declare type BaseOptions<D, P extends any[], K = "data"> = Partial<{
|
|
7
7
|
onBefore: (params: P) => void;
|
|
8
|
-
onSuccess: (
|
|
9
|
-
onFinally: (params: P
|
|
10
|
-
onError: (
|
|
8
|
+
onSuccess: (params: P) => void;
|
|
9
|
+
onFinally: (params: P) => void;
|
|
10
|
+
onError: (params: P) => void;
|
|
11
11
|
manual: boolean;
|
|
12
12
|
defaultParams: P;
|
|
13
13
|
refreshDeps: WatchSource | WatchSource[] | object;
|
|
@@ -29,10 +29,12 @@ declare type BaseOptions<D, P extends any[]> = Partial<{
|
|
|
29
29
|
throttleWait: Ref<number> | number;
|
|
30
30
|
throttleOptions: Reactive<ThrottleOptionsType> | ThrottleOptionsType;
|
|
31
31
|
abortPrevious: boolean;
|
|
32
|
-
}
|
|
32
|
+
}> & (D extends object ? {
|
|
33
|
+
resKey?: K;
|
|
34
|
+
} : {});
|
|
33
35
|
|
|
34
36
|
declare type CacheParamsType<D = any, P = any> = {
|
|
35
|
-
data
|
|
37
|
+
data: D;
|
|
36
38
|
params: P;
|
|
37
39
|
time: number;
|
|
38
40
|
} & {
|
|
@@ -48,12 +50,15 @@ declare type DebounceOptionsType = {
|
|
|
48
50
|
|
|
49
51
|
export declare const definePlugin: <D = any, P extends any[] = any, O = {}>(plugin: Plugin_2<D, P, O>) => Plugin_2<D, P, O>;
|
|
50
52
|
|
|
51
|
-
declare
|
|
52
|
-
|
|
53
|
+
declare type ExtractFieldType<D, K> = D extends object ? K extends keyof D ? D[K] : "data" extends keyof D ? D["data"] : never : never;
|
|
54
|
+
|
|
55
|
+
declare interface IState<D, P extends any[], K = "data"> {
|
|
56
|
+
data: D;
|
|
57
|
+
res: ExtractFieldType<D, K>;
|
|
53
58
|
loading: boolean;
|
|
54
59
|
isFinished: boolean;
|
|
55
60
|
isAborted: boolean;
|
|
56
|
-
error
|
|
61
|
+
error: Error;
|
|
57
62
|
params: P;
|
|
58
63
|
signal: AbortSignal;
|
|
59
64
|
}
|
|
@@ -66,27 +71,30 @@ declare type PluginMethodsReturn<D, P extends any[]> = Partial<{
|
|
|
66
71
|
signal: AbortSignal;
|
|
67
72
|
isReturn: boolean;
|
|
68
73
|
isReady: boolean;
|
|
69
|
-
data
|
|
74
|
+
data: D;
|
|
70
75
|
}>;
|
|
71
76
|
|
|
72
77
|
declare type PluginReturn<D, P extends any[]> = Partial<{
|
|
73
78
|
onBefore: (params: P) => void;
|
|
74
|
-
onSuccess: (
|
|
75
|
-
|
|
76
|
-
|
|
79
|
+
onSuccess: (params: P) => void;
|
|
80
|
+
onError: (params: P) => void;
|
|
81
|
+
onFinally: (params: P) => void;
|
|
77
82
|
onCancel: () => void;
|
|
78
|
-
onMutate: (data: D) => void;
|
|
79
83
|
onRequest: (service: ServiceType<D, P>) => ServiceType<D, P>;
|
|
80
84
|
}>;
|
|
81
85
|
|
|
82
86
|
declare class Request_2<D, P extends any[]> {
|
|
83
87
|
service: ServiceType<D, P>;
|
|
84
|
-
options: BaseOptions<D, P
|
|
88
|
+
options: BaseOptions<D, P> & {
|
|
89
|
+
resKey: keyof D;
|
|
90
|
+
};
|
|
85
91
|
currentRequestId: number;
|
|
86
92
|
pluginImpls: PluginReturn<D, P>[];
|
|
87
93
|
state: IState<D, P>;
|
|
88
94
|
abort: () => void;
|
|
89
|
-
constructor(service: ServiceType<D, P>, options: BaseOptions<D, P>
|
|
95
|
+
constructor(service: ServiceType<D, P>, options: BaseOptions<D, P> & {
|
|
96
|
+
resKey: keyof D;
|
|
97
|
+
});
|
|
90
98
|
setState: (s: Partial<IState<D, P>>) => void;
|
|
91
99
|
executePlugin: (event: keyof PluginReturn<D, P>, ...rest: any[]) => PluginMethodsReturn<D, P>;
|
|
92
100
|
loading: (loading: boolean) => void;
|
|
@@ -95,7 +103,6 @@ declare class Request_2<D, P extends any[]> {
|
|
|
95
103
|
run: (...params: P) => void;
|
|
96
104
|
refresh: () => void;
|
|
97
105
|
refreshAsync: () => Promise<D>;
|
|
98
|
-
mutate: (data: D | ((data: D) => D)) => void;
|
|
99
106
|
cancel: () => void;
|
|
100
107
|
}
|
|
101
108
|
|
|
@@ -108,11 +115,17 @@ declare type ThrottleOptionsType = {
|
|
|
108
115
|
|
|
109
116
|
declare type Timeout = ReturnType<typeof setTimeout>;
|
|
110
117
|
|
|
111
|
-
export declare function useRequest<D, P extends any[] = any[]>(service: ServiceType<D, P>, options
|
|
118
|
+
export declare function useRequest<D extends object, K extends keyof D, P extends any[] = any[]>(service: ServiceType<D, P>, options: BaseOptions<D, P, K> & {
|
|
119
|
+
resKey: K;
|
|
120
|
+
}): UseRequestReturnType<D, P, K>;
|
|
121
|
+
|
|
122
|
+
export declare function useRequest<D extends object, P extends any[] = any[]>(service: ServiceType<D, P>, options?: BaseOptions<D, P, "data">): UseRequestReturnType<D, P, "data">;
|
|
123
|
+
|
|
124
|
+
export declare function useRequest<D, P extends any[] = any[]>(service: ServiceType<D, P>, options?: BaseOptions<D, P, never>): UseRequestReturnType<D, P, never>;
|
|
112
125
|
|
|
113
126
|
export declare function useRequest<D, P extends any[] = any[], O = {}>(service: ServiceType<D, P>, options: BaseOptions<D, P> & O, plugins: Plugin_2<D, P, O>[]): UseRequestReturnType<D, P>;
|
|
114
127
|
|
|
115
|
-
declare interface UseRequestReturnType<D, P extends any[]> extends ToRefs<IState<D, P>> {
|
|
128
|
+
declare interface UseRequestReturnType<D, P extends any[], K = "data"> extends ToRefs<IState<D, P, K>> {
|
|
116
129
|
run: (...args: P) => void;
|
|
117
130
|
cancel: () => void;
|
|
118
131
|
refresh: () => void;
|
|
@@ -120,7 +133,6 @@ declare interface UseRequestReturnType<D, P extends any[]> extends ToRefs<IState
|
|
|
120
133
|
abort: () => void;
|
|
121
134
|
refreshAsync: () => Promise<D>;
|
|
122
135
|
clearCache: (key?: string) => void;
|
|
123
|
-
mutate: (data: D | ((data: D) => D)) => void;
|
|
124
136
|
}
|
|
125
137
|
|
|
126
138
|
export { }
|
package/dist/vue3-request.es.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { reactive as
|
|
2
|
-
const b = (e) => e,
|
|
3
|
-
function
|
|
4
|
-
return
|
|
1
|
+
import { reactive as D, onMounted as H, onUnmounted as F, toRefs as W, ref as R, computed as $, toValue as d, watch as B, watchEffect as L, onWatcherCleanup as N } from "vue";
|
|
2
|
+
const b = (e) => e, P = (e) => e == null, T = typeof window > "u";
|
|
3
|
+
function O(e) {
|
|
4
|
+
return P(e) ? e : typeof e == "number" && !isNaN(e);
|
|
5
5
|
}
|
|
6
6
|
function p(e, t) {
|
|
7
|
-
return
|
|
7
|
+
return O(e) === void 0 ? {
|
|
8
8
|
is: !1,
|
|
9
9
|
value: 0
|
|
10
|
-
} :
|
|
10
|
+
} : O(e) ? t && e === -1 ? {
|
|
11
11
|
is: !0,
|
|
12
12
|
value: -1
|
|
13
13
|
} : {
|
|
@@ -18,27 +18,28 @@ function p(e, t) {
|
|
|
18
18
|
value: 0
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
|
-
const
|
|
21
|
+
const z = (e, t) => {
|
|
22
22
|
let n = t;
|
|
23
|
-
for (let
|
|
24
|
-
const r = e[
|
|
23
|
+
for (let i = e.length; i-- > 0; ) {
|
|
24
|
+
const r = e[i];
|
|
25
25
|
n = r(n);
|
|
26
26
|
}
|
|
27
27
|
return n?.();
|
|
28
28
|
}, A = () => new Promise(() => {
|
|
29
|
-
}),
|
|
30
|
-
let
|
|
31
|
-
const
|
|
32
|
-
u && (
|
|
33
|
-
|
|
34
|
-
},
|
|
35
|
-
}, V = (e) =>
|
|
36
|
-
e && typeof e == "string" ?
|
|
29
|
+
}), C = /* @__PURE__ */ new Map(), U = (e, t, { data: n, params: i, time: r }) => {
|
|
30
|
+
let s;
|
|
31
|
+
const a = V(e), { is: u, value: c } = p(t, !0);
|
|
32
|
+
u && (a?.timer && clearTimeout(a.timer), c !== -1 && (s = setTimeout(() => {
|
|
33
|
+
_(e);
|
|
34
|
+
}, c)), C.set(e, { data: n, params: i, time: r, timer: s }));
|
|
35
|
+
}, V = (e) => C.get(e), _ = (e) => {
|
|
36
|
+
e && typeof e == "string" ? C.delete(e) : C.clear();
|
|
37
37
|
};
|
|
38
|
-
class
|
|
38
|
+
class J {
|
|
39
39
|
constructor(t, n) {
|
|
40
|
-
this.service = t, this.options = n, this.state =
|
|
40
|
+
this.service = t, this.options = n, this.state = D({
|
|
41
41
|
data: void 0,
|
|
42
|
+
res: void 0,
|
|
42
43
|
error: void 0,
|
|
43
44
|
params: n?.defaultParams || [],
|
|
44
45
|
loading: !1,
|
|
@@ -53,59 +54,57 @@ class X {
|
|
|
53
54
|
abort = () => {
|
|
54
55
|
};
|
|
55
56
|
setState = (t) => {
|
|
56
|
-
Object.assign(this.state, t)
|
|
57
|
+
if (Object.assign(this.state, t), t.data && typeof t.data == "object" && !P(t.data)) {
|
|
58
|
+
const n = this.options.resKey || "data";
|
|
59
|
+
Reflect.has(t.data, n) && (this.state.res = t.data[n]);
|
|
60
|
+
}
|
|
57
61
|
};
|
|
58
62
|
executePlugin = (t, ...n) => {
|
|
59
63
|
if (t === "onRequest")
|
|
60
64
|
return {
|
|
61
|
-
servicePromise:
|
|
65
|
+
servicePromise: z(
|
|
62
66
|
this.pluginImpls.map((r) => r.onRequest).filter(Boolean),
|
|
63
67
|
n[0]
|
|
64
68
|
)
|
|
65
69
|
};
|
|
66
70
|
{
|
|
67
|
-
const
|
|
68
|
-
return Object.assign({}, ...
|
|
71
|
+
const i = this.pluginImpls.map((r) => r[t]?.(...n)).filter(Boolean);
|
|
72
|
+
return Object.assign({}, ...i);
|
|
69
73
|
}
|
|
70
74
|
};
|
|
71
|
-
// 设置加载状态
|
|
72
75
|
loading = (t) => {
|
|
73
76
|
this.setState({ loading: t, isFinished: !t });
|
|
74
77
|
};
|
|
75
|
-
// 请求完成
|
|
76
78
|
onFinished = () => {
|
|
77
79
|
this.executePlugin(
|
|
78
80
|
"onFinally",
|
|
79
|
-
this.state.params
|
|
80
|
-
this.state.data,
|
|
81
|
-
this.state.error
|
|
81
|
+
this.state.params
|
|
82
|
+
// this.state.data,
|
|
83
|
+
// this.state.error
|
|
82
84
|
), this.loading(!1), this.options.onFinally?.(
|
|
83
|
-
this.state.params
|
|
84
|
-
this.state.data,
|
|
85
|
-
this.state.error
|
|
85
|
+
this.state.params
|
|
86
|
+
// this.state.data,
|
|
87
|
+
// this.state.error
|
|
86
88
|
);
|
|
87
89
|
};
|
|
88
90
|
runAsync = async (...t) => {
|
|
89
|
-
const n = ++this.currentRequestId, { isReturn:
|
|
91
|
+
const n = ++this.currentRequestId, { isReturn: i, isReady: r, ...s } = this.executePlugin(
|
|
90
92
|
"onBefore",
|
|
91
93
|
t
|
|
92
94
|
);
|
|
93
95
|
if (!r)
|
|
94
96
|
return A();
|
|
95
|
-
if (this.setState({ params: t, ...
|
|
96
|
-
return this.loading(!1),
|
|
97
|
+
if (this.setState({ params: t, ...s }), this.loading(!0), i)
|
|
98
|
+
return this.loading(!1), s.data;
|
|
97
99
|
this.options.onBefore?.(t);
|
|
98
100
|
try {
|
|
99
|
-
const
|
|
100
|
-
let { servicePromise: u } = this.executePlugin("onRequest",
|
|
101
|
-
u || (u =
|
|
102
|
-
|
|
103
|
-
return n !== this.currentRequestId ? A() : (this.setState({ data: o, error: void 0 }), this.executePlugin("onSuccess", o, t), this.options.onSuccess?.(o, t), this.onFinished(), o);
|
|
104
|
-
} catch (i) {
|
|
101
|
+
const a = () => this.service(...t);
|
|
102
|
+
let { servicePromise: u } = this.executePlugin("onRequest", a);
|
|
103
|
+
return u || (u = a()), n !== this.currentRequestId ? A() : (this.setState({ data: await u, error: void 0 }), this.executePlugin("onSuccess", t), this.options.onSuccess?.(t), this.onFinished(), this.state.data);
|
|
104
|
+
} catch (a) {
|
|
105
105
|
if (n !== this.currentRequestId)
|
|
106
106
|
return A();
|
|
107
|
-
|
|
108
|
-
throw this.setState({ data: void 0, error: u }), this.executePlugin("onError", u, t), this.options.onError?.(u, t), this.onFinished(), u;
|
|
107
|
+
throw this.setState({ data: void 0, res: void 0, error: a }), this.executePlugin("onError", t), this.options.onError?.(t), this.onFinished(), this.state.error;
|
|
109
108
|
}
|
|
110
109
|
};
|
|
111
110
|
run = (...t) => {
|
|
@@ -117,42 +116,38 @@ class X {
|
|
|
117
116
|
this.run(...this.state.params);
|
|
118
117
|
};
|
|
119
118
|
refreshAsync = async () => await this.runAsync(...this.state.params);
|
|
120
|
-
mutate = (t) => {
|
|
121
|
-
J(t) ? t(this.state.data) : this.setState({ data: t }), this.executePlugin("onMutate", this.state.data);
|
|
122
|
-
};
|
|
123
119
|
cancel = () => {
|
|
124
120
|
this.executePlugin("onCancel"), this.currentRequestId++, this.loading(!1);
|
|
125
121
|
};
|
|
126
122
|
}
|
|
127
|
-
function
|
|
128
|
-
const
|
|
123
|
+
function Q(e, t, n) {
|
|
124
|
+
const i = {
|
|
129
125
|
manual: !1,
|
|
130
126
|
...t
|
|
131
|
-
}, r = new
|
|
127
|
+
}, r = new J(e, i);
|
|
132
128
|
r.pluginImpls = n.map(
|
|
133
|
-
(
|
|
134
|
-
),
|
|
135
|
-
if (!
|
|
136
|
-
const
|
|
137
|
-
r.run(...
|
|
129
|
+
(h) => h(r, i)
|
|
130
|
+
), H(() => {
|
|
131
|
+
if (!i.manual) {
|
|
132
|
+
const h = r.state.params;
|
|
133
|
+
r.run(...h);
|
|
138
134
|
}
|
|
139
|
-
}),
|
|
140
|
-
const { run:
|
|
135
|
+
}), F(r.cancel);
|
|
136
|
+
const { run: s, cancel: a, refresh: u, runAsync: c, refreshAsync: v, abort: o } = r;
|
|
141
137
|
return {
|
|
142
|
-
|
|
143
|
-
run:
|
|
144
|
-
cancel:
|
|
138
|
+
...W(r.state),
|
|
139
|
+
run: s,
|
|
140
|
+
cancel: a,
|
|
145
141
|
refresh: u,
|
|
146
|
-
runAsync:
|
|
147
|
-
|
|
148
|
-
abort: c,
|
|
142
|
+
runAsync: c,
|
|
143
|
+
abort: o,
|
|
149
144
|
refreshAsync: v,
|
|
150
|
-
clearCache:
|
|
145
|
+
clearCache: _
|
|
151
146
|
};
|
|
152
147
|
}
|
|
153
|
-
const
|
|
148
|
+
const X = b((e, { abortPrevious: t = !0 }) => {
|
|
154
149
|
let n = null;
|
|
155
|
-
const
|
|
150
|
+
const i = () => {
|
|
156
151
|
n = new AbortController(), e.setState({
|
|
157
152
|
signal: n.signal,
|
|
158
153
|
isAborted: n.signal.aborted
|
|
@@ -164,189 +159,177 @@ const Z = b((e, { abortPrevious: t = !0 }) => {
|
|
|
164
159
|
}));
|
|
165
160
|
}, {
|
|
166
161
|
onBefore: () => {
|
|
167
|
-
typeof t == "boolean" && t && e.abort(),
|
|
162
|
+
typeof t == "boolean" && t && e.abort(), i();
|
|
168
163
|
},
|
|
169
164
|
onRequest: (r) => () => r(),
|
|
170
165
|
onCancel: () => {
|
|
171
166
|
e.abort(), n = null;
|
|
172
167
|
}
|
|
173
168
|
};
|
|
174
|
-
}), y = /* @__PURE__ */ new Map(),
|
|
175
|
-
y.has(e) && y.get(e)?.forEach((
|
|
176
|
-
},
|
|
169
|
+
}), y = /* @__PURE__ */ new Map(), Y = (e, { data: t, params: n, time: i }) => {
|
|
170
|
+
y.has(e) && y.get(e)?.forEach((s) => s({ data: t, params: n, time: i }));
|
|
171
|
+
}, q = (e, t) => (y.has(e) ? y.get(e)?.add(t) : y.set(e, /* @__PURE__ */ new Set()), () => {
|
|
177
172
|
const n = y.get(e);
|
|
178
173
|
n?.delete(t), n?.size === 0 && y.delete(e);
|
|
179
|
-
}),
|
|
180
|
-
|
|
174
|
+
}), S = /* @__PURE__ */ new Map(), Z = (e, t) => {
|
|
175
|
+
S.set(e, t), t.then(() => {
|
|
181
176
|
}).catch(() => {
|
|
182
177
|
}).finally(() => {
|
|
183
|
-
|
|
178
|
+
I(e);
|
|
184
179
|
});
|
|
185
|
-
},
|
|
186
|
-
e && typeof e == "string" ?
|
|
187
|
-
},
|
|
180
|
+
}, G = (e) => S.get(e), I = (e) => {
|
|
181
|
+
e && typeof e == "string" ? S.delete(e) : S.clear();
|
|
182
|
+
}, k = b(
|
|
188
183
|
(e, {
|
|
189
184
|
cacheKey: t,
|
|
190
185
|
cacheTime: n = 3e5,
|
|
191
|
-
staleTime:
|
|
186
|
+
staleTime: i = 0,
|
|
192
187
|
setCache: r,
|
|
193
|
-
getCache:
|
|
188
|
+
getCache: s
|
|
194
189
|
}) => {
|
|
195
|
-
const
|
|
190
|
+
const a = R(null);
|
|
196
191
|
let u = null;
|
|
197
|
-
const { is:
|
|
198
|
-
if (!
|
|
192
|
+
const { is: c, value: v } = p(i, !0);
|
|
193
|
+
if (!c)
|
|
199
194
|
return {};
|
|
200
|
-
const
|
|
201
|
-
r ? r(f, l) :
|
|
202
|
-
}, m = (f) =>
|
|
203
|
-
function
|
|
204
|
-
const f =
|
|
195
|
+
const o = typeof t == "function" ? t : () => t, h = (f, l) => {
|
|
196
|
+
r ? r(f, l) : U(f, n, l), Y(f, l);
|
|
197
|
+
}, m = (f) => s ? s(f) : V(f);
|
|
198
|
+
function w() {
|
|
199
|
+
const f = o();
|
|
205
200
|
if (!f) return;
|
|
206
201
|
const l = m(f);
|
|
207
202
|
l && Reflect.has(l, "data") && e.setState({
|
|
208
203
|
data: l.data,
|
|
209
204
|
params: l.params
|
|
210
|
-
}),
|
|
205
|
+
}), a.value = q(f, (g) => {
|
|
211
206
|
e.setState({
|
|
212
|
-
data:
|
|
207
|
+
data: g.data
|
|
213
208
|
});
|
|
214
209
|
});
|
|
215
210
|
}
|
|
216
|
-
return
|
|
217
|
-
|
|
211
|
+
return w(), F(() => {
|
|
212
|
+
a.value?.();
|
|
218
213
|
}), {
|
|
219
214
|
onBefore: (f) => {
|
|
220
|
-
const l =
|
|
215
|
+
const l = o(f);
|
|
221
216
|
if (!l) return null;
|
|
222
|
-
const
|
|
223
|
-
return !
|
|
224
|
-
data:
|
|
217
|
+
const g = m(l);
|
|
218
|
+
return !g || !Reflect.has(g, "data") ? {} : v === -1 || Date.now() - g.time < v ? {
|
|
219
|
+
data: g.data,
|
|
225
220
|
error: void 0,
|
|
226
221
|
isReturn: !0
|
|
227
222
|
} : {
|
|
228
|
-
data:
|
|
223
|
+
data: g.data,
|
|
229
224
|
error: void 0
|
|
230
225
|
};
|
|
231
226
|
},
|
|
232
227
|
onRequest: (f) => {
|
|
233
|
-
const l =
|
|
228
|
+
const l = o(e.state.params);
|
|
234
229
|
if (!l) return f;
|
|
235
|
-
let
|
|
236
|
-
return
|
|
230
|
+
let g = G(l);
|
|
231
|
+
return g && g !== u ? () => g : (g = f(), u = g, Z(l, g), () => g);
|
|
237
232
|
},
|
|
238
|
-
onSuccess: (f
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
data:
|
|
242
|
-
params:
|
|
233
|
+
onSuccess: (f) => {
|
|
234
|
+
const l = o(f);
|
|
235
|
+
l && (a.value?.(), h(l, {
|
|
236
|
+
data: e.state.data,
|
|
237
|
+
params: f,
|
|
243
238
|
time: Date.now()
|
|
244
|
-
}),
|
|
239
|
+
}), a.value = q(l, (g) => {
|
|
245
240
|
e.setState({
|
|
246
|
-
data:
|
|
247
|
-
});
|
|
248
|
-
}));
|
|
249
|
-
},
|
|
250
|
-
onMutate(f) {
|
|
251
|
-
const l = c(e.state.params);
|
|
252
|
-
l && (i.value?.(), g(l, {
|
|
253
|
-
data: f,
|
|
254
|
-
params: e.state.params,
|
|
255
|
-
time: Date.now()
|
|
256
|
-
}), i.value = E(l, (d) => {
|
|
257
|
-
e.setState({
|
|
258
|
-
data: d.data
|
|
241
|
+
data: g.data
|
|
259
242
|
});
|
|
260
243
|
}));
|
|
261
244
|
}
|
|
262
245
|
};
|
|
263
246
|
}
|
|
264
|
-
),
|
|
265
|
-
const
|
|
247
|
+
), K = b((e, { errorRetryCount: t = 0, errorRetryInterval: n }) => {
|
|
248
|
+
const i = R(), r = R(0), s = R(!1), a = $(
|
|
266
249
|
() => 1e3 * Math.pow(2, r.value)
|
|
267
250
|
), u = () => {
|
|
268
|
-
let
|
|
251
|
+
let o = null;
|
|
269
252
|
r.value++;
|
|
270
|
-
const { value:
|
|
271
|
-
if (m ||
|
|
272
|
-
let { is: f, value: l } = p(
|
|
273
|
-
f || (l =
|
|
274
|
-
|
|
253
|
+
const { value: h } = p(d(t), !0), m = h === -1, w = r.value <= h;
|
|
254
|
+
if (m || w) {
|
|
255
|
+
let { is: f, value: l } = p(d(n));
|
|
256
|
+
f || (l = d(Math.min(a.value, 3e4))), o = setTimeout(() => {
|
|
257
|
+
s.value = !0, e.refresh();
|
|
275
258
|
}, l);
|
|
276
259
|
}
|
|
277
260
|
return () => {
|
|
278
|
-
|
|
261
|
+
o && clearTimeout(o);
|
|
279
262
|
};
|
|
280
|
-
},
|
|
281
|
-
|
|
282
|
-
}, { is: v } = p(
|
|
263
|
+
}, c = () => {
|
|
264
|
+
i.value?.();
|
|
265
|
+
}, { is: v } = p(d(t), !0);
|
|
283
266
|
return v ? {
|
|
284
267
|
onBefore: () => {
|
|
285
|
-
|
|
268
|
+
s.value || (r.value = 0), s.value = !1, c();
|
|
286
269
|
},
|
|
287
270
|
onSuccess: () => {
|
|
288
271
|
r.value = 0;
|
|
289
272
|
},
|
|
290
273
|
onError: () => {
|
|
291
|
-
|
|
274
|
+
i.value = u();
|
|
292
275
|
},
|
|
293
276
|
onCancel: () => {
|
|
294
|
-
r.value = 0,
|
|
277
|
+
r.value = 0, c();
|
|
295
278
|
}
|
|
296
279
|
} : {};
|
|
297
|
-
}),
|
|
298
|
-
|
|
299
|
-
}),
|
|
300
|
-
|
|
280
|
+
}), M = () => T || P(window.document?.visibilityState) ? !0 : document.visibilityState === "visible", E = /* @__PURE__ */ new Set(), ee = (e) => (E.add(e), () => {
|
|
281
|
+
E.delete(e);
|
|
282
|
+
}), te = () => {
|
|
283
|
+
M() && E.forEach((e) => {
|
|
301
284
|
e();
|
|
302
285
|
});
|
|
303
286
|
};
|
|
304
|
-
!T && window?.addEventListener && window.addEventListener("visibilitychange",
|
|
305
|
-
const
|
|
306
|
-
(e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCount:
|
|
307
|
-
const r =
|
|
287
|
+
!T && window?.addEventListener && window.addEventListener("visibilitychange", te, !1);
|
|
288
|
+
const ne = b(
|
|
289
|
+
(e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCount: i }) => {
|
|
290
|
+
const r = R(null), s = R(), a = () => {
|
|
308
291
|
let u;
|
|
309
|
-
const { value:
|
|
310
|
-
|
|
292
|
+
const { value: c } = p(
|
|
293
|
+
d(i),
|
|
311
294
|
!0
|
|
312
295
|
);
|
|
313
|
-
if (e.state.error &&
|
|
314
|
-
const { is: v, value:
|
|
315
|
-
|
|
296
|
+
if (e.state.error && c !== 0) return;
|
|
297
|
+
const { is: v, value: o } = p(
|
|
298
|
+
d(t)
|
|
316
299
|
);
|
|
317
300
|
return v ? (u = setTimeout(() => {
|
|
318
|
-
!
|
|
319
|
-
},
|
|
301
|
+
!d(n) && !M() ? r.value = ee(e.refresh) : e.refresh();
|
|
302
|
+
}, o), () => {
|
|
320
303
|
u && clearTimeout(u), r.value?.();
|
|
321
304
|
}) : void 0;
|
|
322
305
|
};
|
|
323
|
-
return
|
|
324
|
-
[() =>
|
|
306
|
+
return B(
|
|
307
|
+
[() => d(t), () => d(n)],
|
|
325
308
|
() => {
|
|
326
|
-
|
|
309
|
+
s.value?.(), s.value = a();
|
|
327
310
|
}
|
|
328
|
-
),
|
|
311
|
+
), F(() => {
|
|
329
312
|
r.value?.();
|
|
330
313
|
}), {
|
|
331
314
|
onBefore: () => {
|
|
332
|
-
|
|
315
|
+
s.value?.();
|
|
333
316
|
},
|
|
334
317
|
onCancel: () => {
|
|
335
|
-
|
|
318
|
+
s.value?.();
|
|
336
319
|
},
|
|
337
320
|
onFinally: () => {
|
|
338
|
-
|
|
321
|
+
s.value = a();
|
|
339
322
|
}
|
|
340
323
|
};
|
|
341
324
|
}
|
|
342
|
-
),
|
|
343
|
-
(e, { manual: t, refreshDeps: n, refreshDepsAction:
|
|
344
|
-
if (
|
|
325
|
+
), re = b(
|
|
326
|
+
(e, { manual: t, refreshDeps: n, refreshDepsAction: i }) => {
|
|
327
|
+
if (P(n) || typeof n != "object" && typeof n != "function")
|
|
345
328
|
return {};
|
|
346
|
-
const r =
|
|
329
|
+
const r = B(
|
|
347
330
|
n,
|
|
348
331
|
() => {
|
|
349
|
-
t || (
|
|
332
|
+
t || (i ? i() : e.refresh());
|
|
350
333
|
},
|
|
351
334
|
{
|
|
352
335
|
deep: !0
|
|
@@ -358,175 +341,175 @@ const se = b(
|
|
|
358
341
|
}
|
|
359
342
|
};
|
|
360
343
|
}
|
|
361
|
-
),
|
|
362
|
-
|
|
363
|
-
}),
|
|
364
|
-
!
|
|
344
|
+
), ie = () => (!T && window.navigator?.onLine) ?? !0, x = /* @__PURE__ */ new Set(), se = (e) => (x.add(e), () => {
|
|
345
|
+
x.delete(e);
|
|
346
|
+
}), j = () => {
|
|
347
|
+
!M() || !ie() || x.forEach((e) => {
|
|
365
348
|
e();
|
|
366
349
|
});
|
|
367
350
|
};
|
|
368
|
-
!T && window?.addEventListener && (window.addEventListener("visibilitychange",
|
|
369
|
-
const
|
|
351
|
+
!T && window?.addEventListener && (window.addEventListener("visibilitychange", j, !1), window.addEventListener("focus", j, !1));
|
|
352
|
+
const ae = b(
|
|
370
353
|
(e, { refocusTimespan: t = 5e3, refreshOnWindowFocus: n = !1 }) => {
|
|
371
|
-
const
|
|
372
|
-
if (!r ||
|
|
354
|
+
const i = R(), { is: r } = p(d(t));
|
|
355
|
+
if (!r || P(d(n)) || typeof d(n) != "boolean")
|
|
373
356
|
return {};
|
|
374
|
-
const
|
|
375
|
-
let
|
|
357
|
+
const s = (a, u) => {
|
|
358
|
+
let c = !1;
|
|
376
359
|
return () => {
|
|
377
|
-
|
|
378
|
-
|
|
360
|
+
c || (c = !0, a(), setTimeout(() => {
|
|
361
|
+
c = !1;
|
|
379
362
|
}, u));
|
|
380
363
|
};
|
|
381
364
|
};
|
|
382
365
|
return L(() => {
|
|
383
|
-
|
|
384
|
-
|
|
366
|
+
i.value?.(), d(n) && (i.value = se(
|
|
367
|
+
s(e.refresh, d(t))
|
|
385
368
|
));
|
|
386
|
-
}),
|
|
387
|
-
|
|
369
|
+
}), F(() => {
|
|
370
|
+
i.value?.();
|
|
388
371
|
}), {};
|
|
389
372
|
}
|
|
390
|
-
),
|
|
391
|
-
(e, { manual: t, ready: n = !0, defaultParams:
|
|
392
|
-
const r =
|
|
393
|
-
() =>
|
|
394
|
-
(
|
|
395
|
-
!t &&
|
|
373
|
+
), ue = b(
|
|
374
|
+
(e, { manual: t, ready: n = !0, defaultParams: i = [] }) => {
|
|
375
|
+
const r = B(
|
|
376
|
+
() => d(n),
|
|
377
|
+
(s) => {
|
|
378
|
+
!t && s && e.run(...i);
|
|
396
379
|
},
|
|
397
380
|
{
|
|
398
381
|
flush: "sync"
|
|
399
382
|
}
|
|
400
383
|
);
|
|
401
384
|
return {
|
|
402
|
-
onBefore: () => ({ isReady:
|
|
385
|
+
onBefore: () => ({ isReady: d(n) }),
|
|
403
386
|
onCancel: () => {
|
|
404
387
|
r();
|
|
405
388
|
}
|
|
406
389
|
};
|
|
407
390
|
}
|
|
408
391
|
);
|
|
409
|
-
function
|
|
410
|
-
var
|
|
411
|
-
|
|
412
|
-
},
|
|
413
|
-
|
|
392
|
+
function oe(e, t, n) {
|
|
393
|
+
var i = null, r = null, s = typeof n == "boolean" ? { leading: n, trailing: !n } : Object.assign({ leading: !1, trailing: !0 }, n), a = !1, u = null, c = s.leading, v = s.trailing, o = function() {
|
|
394
|
+
i = null, r = null;
|
|
395
|
+
}, h = function() {
|
|
396
|
+
a = !0, e.apply(r, i), o();
|
|
414
397
|
}, m = function() {
|
|
415
|
-
if (
|
|
416
|
-
|
|
398
|
+
if (c === !0 && (u = null), a) {
|
|
399
|
+
o();
|
|
417
400
|
return;
|
|
418
401
|
}
|
|
419
402
|
if (v === !0) {
|
|
420
|
-
|
|
403
|
+
h();
|
|
421
404
|
return;
|
|
422
405
|
}
|
|
423
|
-
|
|
424
|
-
},
|
|
406
|
+
o();
|
|
407
|
+
}, w = function() {
|
|
425
408
|
var l = u !== null;
|
|
426
|
-
return l && clearTimeout(u),
|
|
409
|
+
return l && clearTimeout(u), o(), u = null, a = !1, l;
|
|
427
410
|
}, f = function() {
|
|
428
|
-
|
|
411
|
+
a = !1, i = arguments, r = this, u === null ? c === !0 && h() : clearTimeout(u), u = setTimeout(m, t);
|
|
429
412
|
};
|
|
430
|
-
return f.cancel =
|
|
413
|
+
return f.cancel = w, f;
|
|
431
414
|
}
|
|
432
|
-
const
|
|
433
|
-
(e, { debounceWait: t, debounceOptions: n, manual:
|
|
434
|
-
const r =
|
|
435
|
-
let
|
|
436
|
-
const
|
|
437
|
-
return
|
|
438
|
-
const { is: u, value:
|
|
415
|
+
const le = b(
|
|
416
|
+
(e, { debounceWait: t, debounceOptions: n, manual: i }) => {
|
|
417
|
+
const r = R(!1);
|
|
418
|
+
let s = null;
|
|
419
|
+
const a = e.runAsync;
|
|
420
|
+
return i || (r.value = !0), L(() => {
|
|
421
|
+
const { is: u, value: c } = p(d(t));
|
|
439
422
|
if (!u) return;
|
|
440
|
-
const v =
|
|
441
|
-
|
|
442
|
-
(
|
|
443
|
-
|
|
423
|
+
const v = d(n);
|
|
424
|
+
s = oe(
|
|
425
|
+
(o) => o(),
|
|
426
|
+
c,
|
|
444
427
|
v
|
|
445
|
-
), e.runAsync = (...
|
|
446
|
-
r.value ? (r.value = !1,
|
|
447
|
-
|
|
428
|
+
), e.runAsync = (...o) => new Promise((h, m) => {
|
|
429
|
+
r.value ? (r.value = !1, a(...o).then(h).catch(m)) : s(() => {
|
|
430
|
+
a(...o).then(h).catch(m);
|
|
448
431
|
});
|
|
449
432
|
}), N(() => {
|
|
450
|
-
|
|
433
|
+
s?.cancel(), e.runAsync = a;
|
|
451
434
|
});
|
|
452
435
|
}), {
|
|
453
436
|
onCancel: () => {
|
|
454
|
-
|
|
437
|
+
s?.cancel();
|
|
455
438
|
}
|
|
456
439
|
};
|
|
457
440
|
}
|
|
458
441
|
);
|
|
459
|
-
function
|
|
460
|
-
var
|
|
461
|
-
|
|
462
|
-
},
|
|
463
|
-
|
|
442
|
+
function ce(e, t, n) {
|
|
443
|
+
var i = null, r = null, s = !1, a = null, u = Object.assign({ leading: !0, trailing: !0 }, n), c = u.leading, v = u.trailing, o = function() {
|
|
444
|
+
i = null, r = null;
|
|
445
|
+
}, h = function() {
|
|
446
|
+
s = !0, e.apply(r, i), a = setTimeout(m, t), o();
|
|
464
447
|
}, m = function() {
|
|
465
|
-
if (
|
|
466
|
-
|
|
448
|
+
if (a = null, s) {
|
|
449
|
+
o();
|
|
467
450
|
return;
|
|
468
451
|
}
|
|
469
452
|
if (v === !0) {
|
|
470
|
-
|
|
453
|
+
h();
|
|
471
454
|
return;
|
|
472
455
|
}
|
|
473
|
-
|
|
474
|
-
},
|
|
475
|
-
var l =
|
|
476
|
-
return l && clearTimeout(
|
|
456
|
+
o();
|
|
457
|
+
}, w = function() {
|
|
458
|
+
var l = a !== null;
|
|
459
|
+
return l && clearTimeout(a), o(), a = null, s = !1, l;
|
|
477
460
|
}, f = function() {
|
|
478
|
-
if (
|
|
479
|
-
|
|
461
|
+
if (i = arguments, r = this, s = !1, a === null && c === !0) {
|
|
462
|
+
h();
|
|
480
463
|
return;
|
|
481
464
|
}
|
|
482
|
-
v === !0 && (
|
|
465
|
+
v === !0 && (a = setTimeout(m, t));
|
|
483
466
|
};
|
|
484
|
-
return f.cancel =
|
|
467
|
+
return f.cancel = w, f;
|
|
485
468
|
}
|
|
486
|
-
const
|
|
469
|
+
const fe = b(
|
|
487
470
|
(e, { throttleWait: t, throttleOptions: n }) => {
|
|
488
|
-
let
|
|
471
|
+
let i = null;
|
|
489
472
|
const r = e.runAsync;
|
|
490
473
|
return L(() => {
|
|
491
|
-
const { is:
|
|
492
|
-
if (!
|
|
493
|
-
const u =
|
|
494
|
-
|
|
495
|
-
(
|
|
496
|
-
|
|
474
|
+
const { is: s, value: a } = p(d(t));
|
|
475
|
+
if (!s) return;
|
|
476
|
+
const u = d(n);
|
|
477
|
+
i = ce(
|
|
478
|
+
(c) => c(),
|
|
479
|
+
a,
|
|
497
480
|
u
|
|
498
|
-
), e.runAsync = (...
|
|
499
|
-
|
|
500
|
-
r(...
|
|
481
|
+
), e.runAsync = (...c) => new Promise((v, o) => {
|
|
482
|
+
i(() => {
|
|
483
|
+
r(...c).then(v).catch(o);
|
|
501
484
|
});
|
|
502
485
|
}), N(() => {
|
|
503
|
-
|
|
486
|
+
i?.cancel(), e.runAsync = r;
|
|
504
487
|
});
|
|
505
488
|
}), {
|
|
506
489
|
onCancel: () => {
|
|
507
|
-
|
|
490
|
+
i?.cancel();
|
|
508
491
|
}
|
|
509
492
|
};
|
|
510
493
|
}
|
|
511
|
-
),
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
494
|
+
), de = [
|
|
495
|
+
K,
|
|
496
|
+
ne,
|
|
497
|
+
re,
|
|
498
|
+
ae,
|
|
499
|
+
X,
|
|
500
|
+
ue,
|
|
517
501
|
le,
|
|
518
502
|
fe,
|
|
519
|
-
|
|
520
|
-
ee
|
|
503
|
+
k
|
|
521
504
|
];
|
|
522
|
-
function
|
|
523
|
-
return
|
|
505
|
+
function ve(e, t, n) {
|
|
506
|
+
return Q(e, t || {}, [
|
|
524
507
|
...n || [],
|
|
525
|
-
...
|
|
508
|
+
...de
|
|
526
509
|
]);
|
|
527
510
|
}
|
|
528
511
|
export {
|
|
529
|
-
|
|
512
|
+
_ as clearCache,
|
|
530
513
|
b as definePlugin,
|
|
531
|
-
|
|
514
|
+
ve as useRequest
|
|
532
515
|
};
|
package/dist/vue3-request.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(w,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],i):(w=typeof globalThis<"u"?globalThis:w||self,i(w.Vue3Request={},w.Vue))})(this,function(w,i){"use strict";const p=e=>e,P=e=>e==null,C=typeof window>"u";function
|
|
1
|
+
(function(w,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],i):(w=typeof globalThis<"u"?globalThis:w||self,i(w.Vue3Request={},w.Vue))})(this,function(w,i){"use strict";const p=e=>e,P=e=>e==null,C=typeof window>"u";function q(e){return P(e)?e:typeof e=="number"&&!isNaN(e)}function b(e,t){return q(e)===void 0?{is:!1,value:0}:q(e)?t&&e===-1?{is:!0,value:-1}:{is:!0,value:e===0||e&&e<0?0:e}:{is:!1,value:0}}const O=(e,t)=>{let n=t;for(let s=e.length;s-- >0;){const r=e[s];n=r(n)}return n?.()},T=()=>new Promise(()=>{}),V=new Map,j=(e,t,{data:n,params:s,time:r})=>{let a;const o=B(e),{is:l,value:f}=b(t,!0);l&&(o?.timer&&clearTimeout(o.timer),f!==-1&&(a=setTimeout(()=>{F(e)},f)),V.set(e,{data:n,params:s,time:r,timer:a}))},B=e=>V.get(e),F=e=>{e&&typeof e=="string"?V.delete(e):V.clear()};class N{constructor(t,n){this.service=t,this.options=n,this.state=i.reactive({data:void 0,res:void 0,error:void 0,params:n?.defaultParams||[],loading:!1,isFinished:!1,isAborted:!1,signal:new AbortController().signal})}currentRequestId=0;pluginImpls=[];state;abort=()=>{};setState=t=>{if(Object.assign(this.state,t),t.data&&typeof t.data=="object"&&!P(t.data)){const n=this.options.resKey||"data";Reflect.has(t.data,n)&&(this.state.res=t.data[n])}};executePlugin=(t,...n)=>{if(t==="onRequest")return{servicePromise:O(this.pluginImpls.map(r=>r.onRequest).filter(Boolean),n[0])};{const s=this.pluginImpls.map(r=>r[t]?.(...n)).filter(Boolean);return Object.assign({},...s)}};loading=t=>{this.setState({loading:t,isFinished:!t})};onFinished=()=>{this.executePlugin("onFinally",this.state.params),this.loading(!1),this.options.onFinally?.(this.state.params)};runAsync=async(...t)=>{const n=++this.currentRequestId,{isReturn:s,isReady:r,...a}=this.executePlugin("onBefore",t);if(!r)return T();if(this.setState({params:t,...a}),this.loading(!0),s)return this.loading(!1),a.data;this.options.onBefore?.(t);try{const o=()=>this.service(...t);let{servicePromise:l}=this.executePlugin("onRequest",o);return l||(l=o()),n!==this.currentRequestId?T():(this.setState({data:await l,error:void 0}),this.executePlugin("onSuccess",t),this.options.onSuccess?.(t),this.onFinished(),this.state.data)}catch(o){if(n!==this.currentRequestId)return T();throw this.setState({data:void 0,res:void 0,error:o}),this.executePlugin("onError",t),this.options.onError?.(t),this.onFinished(),this.state.error}};run=(...t)=>{this.runAsync(...t).catch(n=>{this.options.onError})};refresh=()=>{this.run(...this.state.params)};refreshAsync=async()=>await this.runAsync(...this.state.params);cancel=()=>{this.executePlugin("onCancel"),this.currentRequestId++,this.loading(!1)}}function U(e,t,n){const s={manual:!1,...t},r=new N(e,s);r.pluginImpls=n.map(h=>h(r,s)),i.onMounted(()=>{if(!s.manual){const h=r.state.params;r.run(...h)}}),i.onUnmounted(r.cancel);const{run:a,cancel:o,refresh:l,runAsync:f,refreshAsync:g,abort:u}=r;return{...i.toRefs(r.state),run:a,cancel:o,refresh:l,runAsync:f,abort:u,refreshAsync:g,clearCache:F}}const W=p((e,{abortPrevious:t=!0})=>{let n=null;const s=()=>{n=new AbortController,e.setState({signal:n.signal,isAborted:n.signal.aborted})};return e.abort=()=>{n&&!n.signal.aborted&&!e.state.isFinished&&(n.abort(),e.setState({isAborted:n.signal.aborted}))},{onBefore:()=>{typeof t=="boolean"&&t&&e.abort(),s()},onRequest:r=>()=>r(),onCancel:()=>{e.abort(),n=null}}}),R=new Map,_=(e,{data:t,params:n,time:s})=>{R.has(e)&&R.get(e)?.forEach(a=>a({data:t,params:n,time:s}))},L=(e,t)=>(R.has(e)?R.get(e)?.add(t):R.set(e,new Set),()=>{const n=R.get(e);n?.delete(t),n?.size===0&&R.delete(e)}),S=new Map,D=(e,t)=>{S.set(e,t),t.then(()=>{}).catch(()=>{}).finally(()=>{$(e)})},H=e=>S.get(e),$=e=>{e&&typeof e=="string"?S.delete(e):S.clear()},z=p((e,{cacheKey:t,cacheTime:n=3e5,staleTime:s=0,setCache:r,getCache:a})=>{const o=i.ref(null);let l=null;const{is:f,value:g}=b(s,!0);if(!f)return{};const u=typeof t=="function"?t:()=>t,h=(d,c)=>{r?r(d,c):j(d,n,c),_(d,c)},v=d=>a?a(d):B(d);function y(){const d=u();if(!d)return;const c=v(d);c&&Reflect.has(c,"data")&&e.setState({data:c.data,params:c.params}),o.value=L(d,m=>{e.setState({data:m.data})})}return y(),i.onUnmounted(()=>{o.value?.()}),{onBefore:d=>{const c=u(d);if(!c)return null;const m=v(c);return!m||!Reflect.has(m,"data")?{}:g===-1||Date.now()-m.time<g?{data:m.data,error:void 0,isReturn:!0}:{data:m.data,error:void 0}},onRequest:d=>{const c=u(e.state.params);if(!c)return d;let m=H(c);return m&&m!==l?()=>m:(m=d(),l=m,D(c,m),()=>m)},onSuccess:d=>{const c=u(d);c&&(o.value?.(),h(c,{data:e.state.data,params:d,time:Date.now()}),o.value=L(c,m=>{e.setState({data:m.data})}))}}}),J=p((e,{errorRetryCount:t=0,errorRetryInterval:n})=>{const s=i.ref(),r=i.ref(0),a=i.ref(!1),o=i.computed(()=>1e3*Math.pow(2,r.value)),l=()=>{let u=null;r.value++;const{value:h}=b(i.toValue(t),!0),v=h===-1,y=r.value<=h;if(v||y){let{is:d,value:c}=b(i.toValue(n));d||(c=i.toValue(Math.min(o.value,3e4))),u=setTimeout(()=>{a.value=!0,e.refresh()},c)}return()=>{u&&clearTimeout(u)}},f=()=>{s.value?.()},{is:g}=b(i.toValue(t),!0);return g?{onBefore:()=>{a.value||(r.value=0),a.value=!1,f()},onSuccess:()=>{r.value=0},onError:()=>{s.value=l()},onCancel:()=>{r.value=0,f()}}:{}}),A=()=>C||P(window.document?.visibilityState)?!0:document.visibilityState==="visible",E=new Set,Q=e=>(E.add(e),()=>{E.delete(e)}),X=()=>{A()&&E.forEach(e=>{e()})};!C&&window?.addEventListener&&window.addEventListener("visibilitychange",X,!1);const Y=p((e,{pollingInterval:t,pollingWhenHidden:n=!0,errorRetryCount:s})=>{const r=i.ref(null),a=i.ref(),o=()=>{let l;const{value:f}=b(i.toValue(s),!0);if(e.state.error&&f!==0)return;const{is:g,value:u}=b(i.toValue(t));return g?(l=setTimeout(()=>{!i.toValue(n)&&!A()?r.value=Q(e.refresh):e.refresh()},u),()=>{l&&clearTimeout(l),r.value?.()}):void 0};return i.watch([()=>i.toValue(t),()=>i.toValue(n)],()=>{a.value?.(),a.value=o()}),i.onUnmounted(()=>{r.value?.()}),{onBefore:()=>{a.value?.()},onCancel:()=>{a.value?.()},onFinally:()=>{a.value=o()}}}),Z=p((e,{manual:t,refreshDeps:n,refreshDepsAction:s})=>{if(P(n)||typeof n!="object"&&typeof n!="function")return{};const r=i.watch(n,()=>{t||(s?s():e.refresh())},{deep:!0});return{onCancel:()=>{r()}}}),G=()=>(!C&&window.navigator?.onLine)??!0,x=new Set,I=e=>(x.add(e),()=>{x.delete(e)}),M=()=>{!A()||!G()||x.forEach(e=>{e()})};!C&&window?.addEventListener&&(window.addEventListener("visibilitychange",M,!1),window.addEventListener("focus",M,!1));const k=p((e,{refocusTimespan:t=5e3,refreshOnWindowFocus:n=!1})=>{const s=i.ref(),{is:r}=b(i.toValue(t));if(!r||P(i.toValue(n))||typeof i.toValue(n)!="boolean")return{};const a=(o,l)=>{let f=!1;return()=>{f||(f=!0,o(),setTimeout(()=>{f=!1},l))}};return i.watchEffect(()=>{s.value?.(),i.toValue(n)&&(s.value=I(a(e.refresh,i.toValue(t))))}),i.onUnmounted(()=>{s.value?.()}),{}}),K=p((e,{manual:t,ready:n=!0,defaultParams:s=[]})=>{const r=i.watch(()=>i.toValue(n),a=>{!t&&a&&e.run(...s)},{flush:"sync"});return{onBefore:()=>({isReady:i.toValue(n)}),onCancel:()=>{r()}}});function ee(e,t,n){var s=null,r=null,a=typeof n=="boolean"?{leading:n,trailing:!n}:Object.assign({leading:!1,trailing:!0},n),o=!1,l=null,f=a.leading,g=a.trailing,u=function(){s=null,r=null},h=function(){o=!0,e.apply(r,s),u()},v=function(){if(f===!0&&(l=null),o){u();return}if(g===!0){h();return}u()},y=function(){var c=l!==null;return c&&clearTimeout(l),u(),l=null,o=!1,c},d=function(){o=!1,s=arguments,r=this,l===null?f===!0&&h():clearTimeout(l),l=setTimeout(v,t)};return d.cancel=y,d}const te=p((e,{debounceWait:t,debounceOptions:n,manual:s})=>{const r=i.ref(!1);let a=null;const o=e.runAsync;return s||(r.value=!0),i.watchEffect(()=>{const{is:l,value:f}=b(i.toValue(t));if(!l)return;const g=i.toValue(n);a=ee(u=>u(),f,g),e.runAsync=(...u)=>new Promise((h,v)=>{r.value?(r.value=!1,o(...u).then(h).catch(v)):a(()=>{o(...u).then(h).catch(v)})}),i.onWatcherCleanup(()=>{a?.cancel(),e.runAsync=o})}),{onCancel:()=>{a?.cancel()}}});function ne(e,t,n){var s=null,r=null,a=!1,o=null,l=Object.assign({leading:!0,trailing:!0},n),f=l.leading,g=l.trailing,u=function(){s=null,r=null},h=function(){a=!0,e.apply(r,s),o=setTimeout(v,t),u()},v=function(){if(o=null,a){u();return}if(g===!0){h();return}u()},y=function(){var c=o!==null;return c&&clearTimeout(o),u(),o=null,a=!1,c},d=function(){if(s=arguments,r=this,a=!1,o===null&&f===!0){h();return}g===!0&&(o=setTimeout(v,t))};return d.cancel=y,d}const re=p((e,{throttleWait:t,throttleOptions:n})=>{let s=null;const r=e.runAsync;return i.watchEffect(()=>{const{is:a,value:o}=b(i.toValue(t));if(!a)return;const l=i.toValue(n);s=ne(f=>f(),o,l),e.runAsync=(...f)=>new Promise((g,u)=>{s(()=>{r(...f).then(g).catch(u)})}),i.onWatcherCleanup(()=>{s?.cancel(),e.runAsync=r})}),{onCancel:()=>{s?.cancel()}}}),ie=[J,Y,Z,k,W,K,te,re,z];function se(e,t,n){return U(e,t||{},[...n||[],...ie])}w.clearCache=F,w.definePlugin=p,w.useRequest=se,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED