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