vue3-request 1.0.24 → 1.0.25
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 +258 -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()),this.setState({data:await o,error:void 0}),n!==this.currentRequestId?V():(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()), this.setState({ data: await u, error: void 0 }), n !== this.currentRequestId ? A() : (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,47 @@ class X {
|
|
|
117
116
|
this.run(...this.state.params);
|
|
118
117
|
};
|
|
119
118
|
refreshAsync = async () => await this.runAsync(...this.state.params);
|
|
120
|
-
mutate = (
|
|
121
|
-
|
|
122
|
-
};
|
|
119
|
+
// mutate = (data: D | ((data: D) => D)) => {
|
|
120
|
+
// if (isFunction(data)) {
|
|
121
|
+
// this.setState({ data: data(this.state.data!) });
|
|
122
|
+
// } else {
|
|
123
|
+
// this.setState({ data });
|
|
124
|
+
// }
|
|
125
|
+
// this.executePlugin("onMutate", this.state.data);
|
|
126
|
+
// };
|
|
123
127
|
cancel = () => {
|
|
124
128
|
this.executePlugin("onCancel"), this.currentRequestId++, this.loading(!1);
|
|
125
129
|
};
|
|
126
130
|
}
|
|
127
|
-
function
|
|
128
|
-
const
|
|
131
|
+
function Q(e, t, n) {
|
|
132
|
+
const i = {
|
|
129
133
|
manual: !1,
|
|
130
134
|
...t
|
|
131
|
-
}, r = new
|
|
135
|
+
}, r = new J(e, i);
|
|
132
136
|
r.pluginImpls = n.map(
|
|
133
|
-
(
|
|
134
|
-
),
|
|
135
|
-
if (!
|
|
136
|
-
const
|
|
137
|
-
r.run(...
|
|
137
|
+
(h) => h(r, i)
|
|
138
|
+
), H(() => {
|
|
139
|
+
if (!i.manual) {
|
|
140
|
+
const h = r.state.params;
|
|
141
|
+
r.run(...h);
|
|
138
142
|
}
|
|
139
|
-
}),
|
|
140
|
-
const { run:
|
|
143
|
+
}), F(r.cancel);
|
|
144
|
+
const { run: s, cancel: a, refresh: u, runAsync: c, refreshAsync: v, abort: o } = r;
|
|
141
145
|
return {
|
|
142
|
-
|
|
143
|
-
run:
|
|
144
|
-
cancel:
|
|
146
|
+
...W(r.state),
|
|
147
|
+
run: s,
|
|
148
|
+
cancel: a,
|
|
145
149
|
refresh: u,
|
|
146
|
-
runAsync:
|
|
147
|
-
mutate
|
|
148
|
-
abort:
|
|
150
|
+
runAsync: c,
|
|
151
|
+
// mutate,
|
|
152
|
+
abort: o,
|
|
149
153
|
refreshAsync: v,
|
|
150
|
-
clearCache:
|
|
154
|
+
clearCache: _
|
|
151
155
|
};
|
|
152
156
|
}
|
|
153
|
-
const
|
|
157
|
+
const X = b((e, { abortPrevious: t = !0 }) => {
|
|
154
158
|
let n = null;
|
|
155
|
-
const
|
|
159
|
+
const i = () => {
|
|
156
160
|
n = new AbortController(), e.setState({
|
|
157
161
|
signal: n.signal,
|
|
158
162
|
isAborted: n.signal.aborted
|
|
@@ -164,189 +168,192 @@ const Z = b((e, { abortPrevious: t = !0 }) => {
|
|
|
164
168
|
}));
|
|
165
169
|
}, {
|
|
166
170
|
onBefore: () => {
|
|
167
|
-
typeof t == "boolean" && t && e.abort(),
|
|
171
|
+
typeof t == "boolean" && t && e.abort(), i();
|
|
168
172
|
},
|
|
169
173
|
onRequest: (r) => () => r(),
|
|
170
174
|
onCancel: () => {
|
|
171
175
|
e.abort(), n = null;
|
|
172
176
|
}
|
|
173
177
|
};
|
|
174
|
-
}), y = /* @__PURE__ */ new Map(),
|
|
175
|
-
y.has(e) && y.get(e)?.forEach((
|
|
176
|
-
},
|
|
178
|
+
}), y = /* @__PURE__ */ new Map(), Y = (e, { data: t, params: n, time: i }) => {
|
|
179
|
+
y.has(e) && y.get(e)?.forEach((s) => s({ data: t, params: n, time: i }));
|
|
180
|
+
}, q = (e, t) => (y.has(e) ? y.get(e)?.add(t) : y.set(e, /* @__PURE__ */ new Set()), () => {
|
|
177
181
|
const n = y.get(e);
|
|
178
182
|
n?.delete(t), n?.size === 0 && y.delete(e);
|
|
179
|
-
}),
|
|
180
|
-
|
|
183
|
+
}), S = /* @__PURE__ */ new Map(), Z = (e, t) => {
|
|
184
|
+
S.set(e, t), t.then(() => {
|
|
181
185
|
}).catch(() => {
|
|
182
186
|
}).finally(() => {
|
|
183
|
-
|
|
187
|
+
I(e);
|
|
184
188
|
});
|
|
185
|
-
},
|
|
186
|
-
e && typeof e == "string" ?
|
|
187
|
-
},
|
|
189
|
+
}, G = (e) => S.get(e), I = (e) => {
|
|
190
|
+
e && typeof e == "string" ? S.delete(e) : S.clear();
|
|
191
|
+
}, k = b(
|
|
188
192
|
(e, {
|
|
189
193
|
cacheKey: t,
|
|
190
194
|
cacheTime: n = 3e5,
|
|
191
|
-
staleTime:
|
|
195
|
+
staleTime: i = 0,
|
|
192
196
|
setCache: r,
|
|
193
|
-
getCache:
|
|
197
|
+
getCache: s
|
|
194
198
|
}) => {
|
|
195
|
-
const
|
|
199
|
+
const a = R(null);
|
|
196
200
|
let u = null;
|
|
197
|
-
const { is:
|
|
198
|
-
if (!
|
|
201
|
+
const { is: c, value: v } = p(i, !0);
|
|
202
|
+
if (!c)
|
|
199
203
|
return {};
|
|
200
|
-
const
|
|
201
|
-
r ? r(f, l) :
|
|
202
|
-
}, m = (f) =>
|
|
203
|
-
function
|
|
204
|
-
const f =
|
|
204
|
+
const o = typeof t == "function" ? t : () => t, h = (f, l) => {
|
|
205
|
+
r ? r(f, l) : U(f, n, l), Y(f, l);
|
|
206
|
+
}, m = (f) => s ? s(f) : V(f);
|
|
207
|
+
function w() {
|
|
208
|
+
const f = o();
|
|
205
209
|
if (!f) return;
|
|
206
210
|
const l = m(f);
|
|
207
211
|
l && Reflect.has(l, "data") && e.setState({
|
|
208
212
|
data: l.data,
|
|
209
213
|
params: l.params
|
|
210
|
-
}),
|
|
214
|
+
}), a.value = q(f, (g) => {
|
|
211
215
|
e.setState({
|
|
212
|
-
data:
|
|
216
|
+
data: g.data
|
|
213
217
|
});
|
|
214
218
|
});
|
|
215
219
|
}
|
|
216
|
-
return
|
|
217
|
-
|
|
220
|
+
return w(), F(() => {
|
|
221
|
+
a.value?.();
|
|
218
222
|
}), {
|
|
219
223
|
onBefore: (f) => {
|
|
220
|
-
const l =
|
|
224
|
+
const l = o(f);
|
|
221
225
|
if (!l) return null;
|
|
222
|
-
const
|
|
223
|
-
return !
|
|
224
|
-
data:
|
|
226
|
+
const g = m(l);
|
|
227
|
+
return !g || !Reflect.has(g, "data") ? {} : v === -1 || Date.now() - g.time < v ? {
|
|
228
|
+
data: g.data,
|
|
225
229
|
error: void 0,
|
|
226
230
|
isReturn: !0
|
|
227
231
|
} : {
|
|
228
|
-
data:
|
|
232
|
+
data: g.data,
|
|
229
233
|
error: void 0
|
|
230
234
|
};
|
|
231
235
|
},
|
|
232
236
|
onRequest: (f) => {
|
|
233
|
-
const l =
|
|
237
|
+
const l = o(e.state.params);
|
|
234
238
|
if (!l) return f;
|
|
235
|
-
let
|
|
236
|
-
return
|
|
239
|
+
let g = G(l);
|
|
240
|
+
return g && g !== u ? () => g : (g = f(), u = g, Z(l, g), () => g);
|
|
237
241
|
},
|
|
238
|
-
onSuccess: (f
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
data:
|
|
242
|
-
params:
|
|
242
|
+
onSuccess: (f) => {
|
|
243
|
+
const l = o(f);
|
|
244
|
+
l && (a.value?.(), h(l, {
|
|
245
|
+
data: e.state.data,
|
|
246
|
+
params: f,
|
|
243
247
|
time: Date.now()
|
|
244
|
-
}),
|
|
248
|
+
}), a.value = q(l, (g) => {
|
|
245
249
|
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
|
|
250
|
+
data: g.data
|
|
259
251
|
});
|
|
260
252
|
}));
|
|
261
253
|
}
|
|
254
|
+
// onMutate(data) {
|
|
255
|
+
// const cacheKey = _getCacheKey(requestInstance.state.params);
|
|
256
|
+
// if (!cacheKey) return;
|
|
257
|
+
// unSubscribe.value?.();
|
|
258
|
+
// _setCache(cacheKey, {
|
|
259
|
+
// data,
|
|
260
|
+
// params: requestInstance.state.params,
|
|
261
|
+
// time: Date.now(),
|
|
262
|
+
// });
|
|
263
|
+
// unSubscribe.value = on(cacheKey, (cacheData) => {
|
|
264
|
+
// requestInstance.setState({
|
|
265
|
+
// data: cacheData.data,
|
|
266
|
+
// });
|
|
267
|
+
// });
|
|
268
|
+
// },
|
|
262
269
|
};
|
|
263
270
|
}
|
|
264
|
-
),
|
|
265
|
-
const
|
|
271
|
+
), K = b((e, { errorRetryCount: t = 0, errorRetryInterval: n }) => {
|
|
272
|
+
const i = R(), r = R(0), s = R(!1), a = $(
|
|
266
273
|
() => 1e3 * Math.pow(2, r.value)
|
|
267
274
|
), u = () => {
|
|
268
|
-
let
|
|
275
|
+
let o = null;
|
|
269
276
|
r.value++;
|
|
270
|
-
const { value:
|
|
271
|
-
if (m ||
|
|
272
|
-
let { is: f, value: l } = p(
|
|
273
|
-
f || (l =
|
|
274
|
-
|
|
277
|
+
const { value: h } = p(d(t), !0), m = h === -1, w = r.value <= h;
|
|
278
|
+
if (m || w) {
|
|
279
|
+
let { is: f, value: l } = p(d(n));
|
|
280
|
+
f || (l = d(Math.min(a.value, 3e4))), o = setTimeout(() => {
|
|
281
|
+
s.value = !0, e.refresh();
|
|
275
282
|
}, l);
|
|
276
283
|
}
|
|
277
284
|
return () => {
|
|
278
|
-
|
|
285
|
+
o && clearTimeout(o);
|
|
279
286
|
};
|
|
280
|
-
},
|
|
281
|
-
|
|
282
|
-
}, { is: v } = p(
|
|
287
|
+
}, c = () => {
|
|
288
|
+
i.value?.();
|
|
289
|
+
}, { is: v } = p(d(t), !0);
|
|
283
290
|
return v ? {
|
|
284
291
|
onBefore: () => {
|
|
285
|
-
|
|
292
|
+
s.value || (r.value = 0), s.value = !1, c();
|
|
286
293
|
},
|
|
287
294
|
onSuccess: () => {
|
|
288
295
|
r.value = 0;
|
|
289
296
|
},
|
|
290
297
|
onError: () => {
|
|
291
|
-
|
|
298
|
+
i.value = u();
|
|
292
299
|
},
|
|
293
300
|
onCancel: () => {
|
|
294
|
-
r.value = 0,
|
|
301
|
+
r.value = 0, c();
|
|
295
302
|
}
|
|
296
303
|
} : {};
|
|
297
|
-
}),
|
|
298
|
-
|
|
299
|
-
}),
|
|
300
|
-
|
|
304
|
+
}), M = () => T || P(window.document?.visibilityState) ? !0 : document.visibilityState === "visible", E = /* @__PURE__ */ new Set(), ee = (e) => (E.add(e), () => {
|
|
305
|
+
E.delete(e);
|
|
306
|
+
}), te = () => {
|
|
307
|
+
M() && E.forEach((e) => {
|
|
301
308
|
e();
|
|
302
309
|
});
|
|
303
310
|
};
|
|
304
|
-
!T && window?.addEventListener && window.addEventListener("visibilitychange",
|
|
305
|
-
const
|
|
306
|
-
(e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCount:
|
|
307
|
-
const r =
|
|
311
|
+
!T && window?.addEventListener && window.addEventListener("visibilitychange", te, !1);
|
|
312
|
+
const ne = b(
|
|
313
|
+
(e, { pollingInterval: t, pollingWhenHidden: n = !0, errorRetryCount: i }) => {
|
|
314
|
+
const r = R(null), s = R(), a = () => {
|
|
308
315
|
let u;
|
|
309
|
-
const { value:
|
|
310
|
-
|
|
316
|
+
const { value: c } = p(
|
|
317
|
+
d(i),
|
|
311
318
|
!0
|
|
312
319
|
);
|
|
313
|
-
if (e.state.error &&
|
|
314
|
-
const { is: v, value:
|
|
315
|
-
|
|
320
|
+
if (e.state.error && c !== 0) return;
|
|
321
|
+
const { is: v, value: o } = p(
|
|
322
|
+
d(t)
|
|
316
323
|
);
|
|
317
324
|
return v ? (u = setTimeout(() => {
|
|
318
|
-
!
|
|
319
|
-
},
|
|
325
|
+
!d(n) && !M() ? r.value = ee(e.refresh) : e.refresh();
|
|
326
|
+
}, o), () => {
|
|
320
327
|
u && clearTimeout(u), r.value?.();
|
|
321
328
|
}) : void 0;
|
|
322
329
|
};
|
|
323
|
-
return
|
|
324
|
-
[() =>
|
|
330
|
+
return B(
|
|
331
|
+
[() => d(t), () => d(n)],
|
|
325
332
|
() => {
|
|
326
|
-
|
|
333
|
+
s.value?.(), s.value = a();
|
|
327
334
|
}
|
|
328
|
-
),
|
|
335
|
+
), F(() => {
|
|
329
336
|
r.value?.();
|
|
330
337
|
}), {
|
|
331
338
|
onBefore: () => {
|
|
332
|
-
|
|
339
|
+
s.value?.();
|
|
333
340
|
},
|
|
334
341
|
onCancel: () => {
|
|
335
|
-
|
|
342
|
+
s.value?.();
|
|
336
343
|
},
|
|
337
344
|
onFinally: () => {
|
|
338
|
-
|
|
345
|
+
s.value = a();
|
|
339
346
|
}
|
|
340
347
|
};
|
|
341
348
|
}
|
|
342
|
-
),
|
|
343
|
-
(e, { manual: t, refreshDeps: n, refreshDepsAction:
|
|
344
|
-
if (
|
|
349
|
+
), re = b(
|
|
350
|
+
(e, { manual: t, refreshDeps: n, refreshDepsAction: i }) => {
|
|
351
|
+
if (P(n) || typeof n != "object" && typeof n != "function")
|
|
345
352
|
return {};
|
|
346
|
-
const r =
|
|
353
|
+
const r = B(
|
|
347
354
|
n,
|
|
348
355
|
() => {
|
|
349
|
-
t || (
|
|
356
|
+
t || (i ? i() : e.refresh());
|
|
350
357
|
},
|
|
351
358
|
{
|
|
352
359
|
deep: !0
|
|
@@ -358,175 +365,175 @@ const se = b(
|
|
|
358
365
|
}
|
|
359
366
|
};
|
|
360
367
|
}
|
|
361
|
-
),
|
|
362
|
-
|
|
363
|
-
}),
|
|
364
|
-
!
|
|
368
|
+
), ie = () => (!T && window.navigator?.onLine) ?? !0, x = /* @__PURE__ */ new Set(), se = (e) => (x.add(e), () => {
|
|
369
|
+
x.delete(e);
|
|
370
|
+
}), j = () => {
|
|
371
|
+
!M() || !ie() || x.forEach((e) => {
|
|
365
372
|
e();
|
|
366
373
|
});
|
|
367
374
|
};
|
|
368
|
-
!T && window?.addEventListener && (window.addEventListener("visibilitychange",
|
|
369
|
-
const
|
|
375
|
+
!T && window?.addEventListener && (window.addEventListener("visibilitychange", j, !1), window.addEventListener("focus", j, !1));
|
|
376
|
+
const ae = b(
|
|
370
377
|
(e, { refocusTimespan: t = 5e3, refreshOnWindowFocus: n = !1 }) => {
|
|
371
|
-
const
|
|
372
|
-
if (!r ||
|
|
378
|
+
const i = R(), { is: r } = p(d(t));
|
|
379
|
+
if (!r || P(d(n)) || typeof d(n) != "boolean")
|
|
373
380
|
return {};
|
|
374
|
-
const
|
|
375
|
-
let
|
|
381
|
+
const s = (a, u) => {
|
|
382
|
+
let c = !1;
|
|
376
383
|
return () => {
|
|
377
|
-
|
|
378
|
-
|
|
384
|
+
c || (c = !0, a(), setTimeout(() => {
|
|
385
|
+
c = !1;
|
|
379
386
|
}, u));
|
|
380
387
|
};
|
|
381
388
|
};
|
|
382
389
|
return L(() => {
|
|
383
|
-
|
|
384
|
-
|
|
390
|
+
i.value?.(), d(n) && (i.value = se(
|
|
391
|
+
s(e.refresh, d(t))
|
|
385
392
|
));
|
|
386
|
-
}),
|
|
387
|
-
|
|
393
|
+
}), F(() => {
|
|
394
|
+
i.value?.();
|
|
388
395
|
}), {};
|
|
389
396
|
}
|
|
390
|
-
),
|
|
391
|
-
(e, { manual: t, ready: n = !0, defaultParams:
|
|
392
|
-
const r =
|
|
393
|
-
() =>
|
|
394
|
-
(
|
|
395
|
-
!t &&
|
|
397
|
+
), ue = b(
|
|
398
|
+
(e, { manual: t, ready: n = !0, defaultParams: i = [] }) => {
|
|
399
|
+
const r = B(
|
|
400
|
+
() => d(n),
|
|
401
|
+
(s) => {
|
|
402
|
+
!t && s && e.run(...i);
|
|
396
403
|
},
|
|
397
404
|
{
|
|
398
405
|
flush: "sync"
|
|
399
406
|
}
|
|
400
407
|
);
|
|
401
408
|
return {
|
|
402
|
-
onBefore: () => ({ isReady:
|
|
409
|
+
onBefore: () => ({ isReady: d(n) }),
|
|
403
410
|
onCancel: () => {
|
|
404
411
|
r();
|
|
405
412
|
}
|
|
406
413
|
};
|
|
407
414
|
}
|
|
408
415
|
);
|
|
409
|
-
function
|
|
410
|
-
var
|
|
411
|
-
|
|
412
|
-
},
|
|
413
|
-
|
|
416
|
+
function oe(e, t, n) {
|
|
417
|
+
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() {
|
|
418
|
+
i = null, r = null;
|
|
419
|
+
}, h = function() {
|
|
420
|
+
a = !0, e.apply(r, i), o();
|
|
414
421
|
}, m = function() {
|
|
415
|
-
if (
|
|
416
|
-
|
|
422
|
+
if (c === !0 && (u = null), a) {
|
|
423
|
+
o();
|
|
417
424
|
return;
|
|
418
425
|
}
|
|
419
426
|
if (v === !0) {
|
|
420
|
-
|
|
427
|
+
h();
|
|
421
428
|
return;
|
|
422
429
|
}
|
|
423
|
-
|
|
424
|
-
},
|
|
430
|
+
o();
|
|
431
|
+
}, w = function() {
|
|
425
432
|
var l = u !== null;
|
|
426
|
-
return l && clearTimeout(u),
|
|
433
|
+
return l && clearTimeout(u), o(), u = null, a = !1, l;
|
|
427
434
|
}, f = function() {
|
|
428
|
-
|
|
435
|
+
a = !1, i = arguments, r = this, u === null ? c === !0 && h() : clearTimeout(u), u = setTimeout(m, t);
|
|
429
436
|
};
|
|
430
|
-
return f.cancel =
|
|
437
|
+
return f.cancel = w, f;
|
|
431
438
|
}
|
|
432
|
-
const
|
|
433
|
-
(e, { debounceWait: t, debounceOptions: n, manual:
|
|
434
|
-
const r =
|
|
435
|
-
let
|
|
436
|
-
const
|
|
437
|
-
return
|
|
438
|
-
const { is: u, value:
|
|
439
|
+
const le = b(
|
|
440
|
+
(e, { debounceWait: t, debounceOptions: n, manual: i }) => {
|
|
441
|
+
const r = R(!1);
|
|
442
|
+
let s = null;
|
|
443
|
+
const a = e.runAsync;
|
|
444
|
+
return i || (r.value = !0), L(() => {
|
|
445
|
+
const { is: u, value: c } = p(d(t));
|
|
439
446
|
if (!u) return;
|
|
440
|
-
const v =
|
|
441
|
-
|
|
442
|
-
(
|
|
443
|
-
|
|
447
|
+
const v = d(n);
|
|
448
|
+
s = oe(
|
|
449
|
+
(o) => o(),
|
|
450
|
+
c,
|
|
444
451
|
v
|
|
445
|
-
), e.runAsync = (...
|
|
446
|
-
r.value ? (r.value = !1,
|
|
447
|
-
|
|
452
|
+
), e.runAsync = (...o) => new Promise((h, m) => {
|
|
453
|
+
r.value ? (r.value = !1, a(...o).then(h).catch(m)) : s(() => {
|
|
454
|
+
a(...o).then(h).catch(m);
|
|
448
455
|
});
|
|
449
456
|
}), N(() => {
|
|
450
|
-
|
|
457
|
+
s?.cancel(), e.runAsync = a;
|
|
451
458
|
});
|
|
452
459
|
}), {
|
|
453
460
|
onCancel: () => {
|
|
454
|
-
|
|
461
|
+
s?.cancel();
|
|
455
462
|
}
|
|
456
463
|
};
|
|
457
464
|
}
|
|
458
465
|
);
|
|
459
|
-
function
|
|
460
|
-
var
|
|
461
|
-
|
|
462
|
-
},
|
|
463
|
-
|
|
466
|
+
function ce(e, t, n) {
|
|
467
|
+
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() {
|
|
468
|
+
i = null, r = null;
|
|
469
|
+
}, h = function() {
|
|
470
|
+
s = !0, e.apply(r, i), a = setTimeout(m, t), o();
|
|
464
471
|
}, m = function() {
|
|
465
|
-
if (
|
|
466
|
-
|
|
472
|
+
if (a = null, s) {
|
|
473
|
+
o();
|
|
467
474
|
return;
|
|
468
475
|
}
|
|
469
476
|
if (v === !0) {
|
|
470
|
-
|
|
477
|
+
h();
|
|
471
478
|
return;
|
|
472
479
|
}
|
|
473
|
-
|
|
474
|
-
},
|
|
475
|
-
var l =
|
|
476
|
-
return l && clearTimeout(
|
|
480
|
+
o();
|
|
481
|
+
}, w = function() {
|
|
482
|
+
var l = a !== null;
|
|
483
|
+
return l && clearTimeout(a), o(), a = null, s = !1, l;
|
|
477
484
|
}, f = function() {
|
|
478
|
-
if (
|
|
479
|
-
|
|
485
|
+
if (i = arguments, r = this, s = !1, a === null && c === !0) {
|
|
486
|
+
h();
|
|
480
487
|
return;
|
|
481
488
|
}
|
|
482
|
-
v === !0 && (
|
|
489
|
+
v === !0 && (a = setTimeout(m, t));
|
|
483
490
|
};
|
|
484
|
-
return f.cancel =
|
|
491
|
+
return f.cancel = w, f;
|
|
485
492
|
}
|
|
486
|
-
const
|
|
493
|
+
const fe = b(
|
|
487
494
|
(e, { throttleWait: t, throttleOptions: n }) => {
|
|
488
|
-
let
|
|
495
|
+
let i = null;
|
|
489
496
|
const r = e.runAsync;
|
|
490
497
|
return L(() => {
|
|
491
|
-
const { is:
|
|
492
|
-
if (!
|
|
493
|
-
const u =
|
|
494
|
-
|
|
495
|
-
(
|
|
496
|
-
|
|
498
|
+
const { is: s, value: a } = p(d(t));
|
|
499
|
+
if (!s) return;
|
|
500
|
+
const u = d(n);
|
|
501
|
+
i = ce(
|
|
502
|
+
(c) => c(),
|
|
503
|
+
a,
|
|
497
504
|
u
|
|
498
|
-
), e.runAsync = (...
|
|
499
|
-
|
|
500
|
-
r(...
|
|
505
|
+
), e.runAsync = (...c) => new Promise((v, o) => {
|
|
506
|
+
i(() => {
|
|
507
|
+
r(...c).then(v).catch(o);
|
|
501
508
|
});
|
|
502
509
|
}), N(() => {
|
|
503
|
-
|
|
510
|
+
i?.cancel(), e.runAsync = r;
|
|
504
511
|
});
|
|
505
512
|
}), {
|
|
506
513
|
onCancel: () => {
|
|
507
|
-
|
|
514
|
+
i?.cancel();
|
|
508
515
|
}
|
|
509
516
|
};
|
|
510
517
|
}
|
|
511
|
-
),
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
518
|
+
), de = [
|
|
519
|
+
K,
|
|
520
|
+
ne,
|
|
521
|
+
re,
|
|
522
|
+
ae,
|
|
523
|
+
X,
|
|
524
|
+
ue,
|
|
517
525
|
le,
|
|
518
526
|
fe,
|
|
519
|
-
|
|
520
|
-
ee
|
|
527
|
+
k
|
|
521
528
|
];
|
|
522
|
-
function
|
|
523
|
-
return
|
|
529
|
+
function ve(e, t, n) {
|
|
530
|
+
return Q(e, t || {}, [
|
|
524
531
|
...n || [],
|
|
525
|
-
...
|
|
532
|
+
...de
|
|
526
533
|
]);
|
|
527
534
|
}
|
|
528
535
|
export {
|
|
529
|
-
|
|
536
|
+
_ as clearCache,
|
|
530
537
|
b as definePlugin,
|
|
531
|
-
|
|
538
|
+
ve as useRequest
|
|
532
539
|
};
|
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()),this.setState({data:await l,error:void 0}),n!==this.currentRequestId?T():(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