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