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