@spitx/helper 0.0.6 → 0.0.8
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/dist/main.cjs +1 -1
- package/dist/main.js +62 -66
- package/dist/main.umd.cjs +1 -1
- package/dist/request/loading.d.ts +0 -35
- package/package.json +1 -1
package/dist/main.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function d(t){let n;const r=new Promise(e=>{n=e});return t.then(e=>{n([null,e])}).catch(e=>{n([e,null])}),r}function h(t){try{return[null,t()]}catch(n){return[n,null]}}function f(t,n){const r=[...t];let e;for(;r.length&&(e=r.shift(),!!e);){const o=n(e);Array.isArray(o)&&r.unshift(...o)}}function l(){let t;return async function(n){const r=t=Math.random()*1e7;return{data:await n,isLast:r===t}}}function w(t=()=>{}){let n=()=>{},r=()=>{};return{promise:new Promise((o,a)=>{t(o,a),n=o,r=a}),resolve:n,reject:r}}function u(t=8){const n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";let r="";for(let e=t;e>0;--e)r+=n[~~(Math.random()*(n.length-1))];return r}function c(t,n){return new Promise(r=>{setTimeout(()=>r(n),t)})}const i=new WeakMap;async function m(t,n){const{loadingDelay:r,loadingKeep:e,signal:o=new AbortController,onSlow:a}=n;i.has(o)||i.set(o,l());const s=u();return await Promise.race([t,c(r,s)])===s&&(a(),await Promise.all([t,c(e)])),t}async function g(t,n){const r=n.signal||new AbortController;return await(i.get(r)||l())(m(t,n))}exports.flat=f;exports.handleError=d;exports.handleErrorSync=h;exports.onlyLast=l;exports.randomString=u;exports.withPromiseResolver=w;exports.withSlowLoading=g;
|
package/dist/main.js
CHANGED
|
@@ -1,90 +1,86 @@
|
|
|
1
|
-
function
|
|
2
|
-
let
|
|
3
|
-
const
|
|
4
|
-
|
|
1
|
+
function h(t) {
|
|
2
|
+
let n;
|
|
3
|
+
const r = new Promise((e) => {
|
|
4
|
+
n = e;
|
|
5
5
|
});
|
|
6
|
-
return
|
|
7
|
-
|
|
8
|
-
}).catch((
|
|
9
|
-
|
|
10
|
-
}),
|
|
6
|
+
return t.then((e) => {
|
|
7
|
+
n([null, e]);
|
|
8
|
+
}).catch((e) => {
|
|
9
|
+
n([e, null]);
|
|
10
|
+
}), r;
|
|
11
11
|
}
|
|
12
|
-
function
|
|
12
|
+
function w(t) {
|
|
13
13
|
try {
|
|
14
|
-
return [null,
|
|
15
|
-
} catch (
|
|
16
|
-
return [
|
|
14
|
+
return [null, t()];
|
|
15
|
+
} catch (n) {
|
|
16
|
+
return [n, null];
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
function
|
|
20
|
-
const
|
|
21
|
-
let
|
|
22
|
-
for (;
|
|
23
|
-
const
|
|
24
|
-
Array.isArray(
|
|
19
|
+
function m(t, n) {
|
|
20
|
+
const r = [...t];
|
|
21
|
+
let e;
|
|
22
|
+
for (; r.length && (e = r.shift(), !!e); ) {
|
|
23
|
+
const o = n(e);
|
|
24
|
+
Array.isArray(o) && r.unshift(...o);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
function c() {
|
|
28
|
-
let
|
|
29
|
-
return async function(
|
|
30
|
-
const
|
|
28
|
+
let t;
|
|
29
|
+
return async function(n) {
|
|
30
|
+
const r = t = Math.random() * 1e7;
|
|
31
31
|
return {
|
|
32
|
-
data: await
|
|
33
|
-
isLast:
|
|
32
|
+
data: await n,
|
|
33
|
+
isLast: r === t
|
|
34
34
|
};
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
|
-
function
|
|
37
|
+
function g(t = () => {
|
|
38
38
|
}) {
|
|
39
|
-
let
|
|
40
|
-
},
|
|
39
|
+
let n = () => {
|
|
40
|
+
}, r = () => {
|
|
41
41
|
};
|
|
42
42
|
return {
|
|
43
|
-
promise: new Promise((
|
|
44
|
-
o
|
|
43
|
+
promise: new Promise((o, a) => {
|
|
44
|
+
t(o, a), n = o, r = a;
|
|
45
45
|
}),
|
|
46
|
-
resolve:
|
|
47
|
-
reject:
|
|
46
|
+
resolve: n,
|
|
47
|
+
reject: r
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
|
-
function
|
|
51
|
-
const
|
|
52
|
-
let
|
|
53
|
-
for (let
|
|
54
|
-
|
|
55
|
-
return
|
|
50
|
+
function u(t = 8) {
|
|
51
|
+
const n = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
52
|
+
let r = "";
|
|
53
|
+
for (let e = t; e > 0; --e)
|
|
54
|
+
r += n[~~(Math.random() * (n.length - 1))];
|
|
55
|
+
return r;
|
|
56
56
|
}
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
function l(t, n) {
|
|
58
|
+
return new Promise((r) => {
|
|
59
|
+
setTimeout(() => r(n), t);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const i = /* @__PURE__ */ new WeakMap();
|
|
63
|
+
async function f(t, n) {
|
|
59
64
|
const {
|
|
60
|
-
loadingDelay:
|
|
61
|
-
loadingKeep:
|
|
62
|
-
signal:
|
|
63
|
-
onSlow:
|
|
64
|
-
} =
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (await Promise.race([i, f]) === l) {
|
|
73
|
-
s();
|
|
74
|
-
const {
|
|
75
|
-
promise: m,
|
|
76
|
-
resolve: g
|
|
77
|
-
} = u();
|
|
78
|
-
setTimeout(() => g(!0), n), await Promise.all([i, m]);
|
|
79
|
-
}
|
|
80
|
-
return i;
|
|
65
|
+
loadingDelay: r,
|
|
66
|
+
loadingKeep: e,
|
|
67
|
+
signal: o = new AbortController(),
|
|
68
|
+
onSlow: a
|
|
69
|
+
} = n;
|
|
70
|
+
i.has(o) || i.set(o, c());
|
|
71
|
+
const s = u();
|
|
72
|
+
return await Promise.race([t, l(r, s)]) === s && (a(), await Promise.all([t, l(e)])), t;
|
|
73
|
+
}
|
|
74
|
+
async function y(t, n) {
|
|
75
|
+
const r = n.signal || new AbortController();
|
|
76
|
+
return await (i.get(r) || c())(f(t, n));
|
|
81
77
|
}
|
|
82
78
|
export {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
79
|
+
m as flat,
|
|
80
|
+
h as handleError,
|
|
81
|
+
w as handleErrorSync,
|
|
86
82
|
c as onlyLast,
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
83
|
+
u as randomString,
|
|
84
|
+
g as withPromiseResolver,
|
|
85
|
+
y as withSlowLoading
|
|
90
86
|
};
|
package/dist/main.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(r,
|
|
1
|
+
(function(r,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(r=typeof globalThis<"u"?globalThis:r||self,l(r.main={}))})(this,function(r){"use strict";function l(e){let n;const t=new Promise(o=>{n=o});return e.then(o=>{n([null,o])}).catch(o=>{n([o,null])}),t}function h(e){try{return[null,e()]}catch(n){return[n,null]}}function m(e,n){const t=[...e];let o;for(;t.length&&(o=t.shift(),!!o);){const i=n(o);Array.isArray(i)&&t.unshift(...i)}}function s(){let e;return async function(n){const t=e=Math.random()*1e7;return{data:await n,isLast:t===e}}}function w(e=()=>{}){let n=()=>{},t=()=>{};return{promise:new Promise((i,a)=>{e(i,a),n=i,t=a}),resolve:n,reject:t}}function u(e=8){const n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";let t="";for(let o=e;o>0;--o)t+=n[~~(Math.random()*(n.length-1))];return t}function d(e,n){return new Promise(t=>{setTimeout(()=>t(n),e)})}const c=new WeakMap;async function g(e,n){const{loadingDelay:t,loadingKeep:o,signal:i=new AbortController,onSlow:a}=n;c.has(i)||c.set(i,s());const f=u();return await Promise.race([e,d(t,f)])===f&&(a(),await Promise.all([e,d(o)])),e}async function y(e,n){const t=n.signal||new AbortController;return await(c.get(t)||s())(g(e,n))}r.flat=m,r.handleError=l,r.handleErrorSync=h,r.onlyLast=s,r.randomString=u,r.withPromiseResolver=w,r.withSlowLoading=y,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -1,38 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 对Promise做如下处理
|
|
3
|
-
* - 对请求都保持至少delay时长的loading
|
|
4
|
-
* - 处理竞态请求
|
|
5
|
-
* @param {AbortController} option.signal 用于标识竞态请求
|
|
6
|
-
* @example
|
|
7
|
-
* const signal = new AbortController()
|
|
8
|
-
* const { data, isLast} = leastLoading(request, {
|
|
9
|
-
* delay: 200,
|
|
10
|
-
* signal: signal,
|
|
11
|
-
* })
|
|
12
|
-
* if (!isLast) return
|
|
13
|
-
* doSomething(data)
|
|
14
|
-
*/
|
|
15
|
-
/**
|
|
16
|
-
* 对 Promise 做如下处理
|
|
17
|
-
* - 处理竞态请求
|
|
18
|
-
* - 请求时长 < loadingDelay 时,不做任何处理
|
|
19
|
-
* - 请求时长 > delay 且 为竞态请求的最后一个时,触发最后一个请求的onSlow函数
|
|
20
|
-
* - 并且至少loadingKeep时间后返回请求结果
|
|
21
|
-
* @param {AbortController} option.signal 用于标识竞态请求
|
|
22
|
-
* @example
|
|
23
|
-
* const signal = new AbortController()
|
|
24
|
-
* const loading = ref(false)
|
|
25
|
-
* const { data, isLast} = withSlowLoading(request, {
|
|
26
|
-
* loadingDelay: 200,
|
|
27
|
-
* loadingKeep: 200,
|
|
28
|
-
* signal: signal,
|
|
29
|
-
* onSlow: () => {
|
|
30
|
-
* loading.value = true
|
|
31
|
-
* }
|
|
32
|
-
* })
|
|
33
|
-
* if (!isLast) return
|
|
34
|
-
* doSomething(data)
|
|
35
|
-
*/
|
|
36
1
|
export declare function withSlowLoading<T>(p: Promise<T>, option: {
|
|
37
2
|
loadingDelay: number;
|
|
38
3
|
loadingKeep: number;
|