@spitx/helper 0.0.3 → 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 -0
- package/dist/main.js +90 -0
- package/dist/main.umd.cjs +1 -0
- package/dist/promise/index.d.ts +8 -0
- package/dist/request/index.d.ts +1 -0
- package/{request → dist/request}/loading.d.ts +8 -12
- package/package.json +6 -8
- package/main.cjs +0 -1
- package/main.js +0 -90
- package/main.umd.cjs +0 -1
- package/promise/index.d.ts +0 -7
- package/request/index.d.ts +0 -1
- package/useLoadingDelay/index.d.ts +0 -8
- /package/{error → dist/error}/index.d.ts +0 -0
- /package/{flat → dist/flat}/index.d.ts +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{onlyLast → dist/onlyLast}/index.d.ts +0 -0
- /package/{service → dist/service}/index.d.ts +0 -0
- /package/{string → dist/string}/index.d.ts +0 -0
package/dist/main.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
function v(o) {
|
|
2
|
+
let e;
|
|
3
|
+
const t = new Promise((n) => {
|
|
4
|
+
e = n;
|
|
5
|
+
});
|
|
6
|
+
return o.then((n) => {
|
|
7
|
+
e([null, n]);
|
|
8
|
+
}).catch((n) => {
|
|
9
|
+
e([n, null]);
|
|
10
|
+
}), t;
|
|
11
|
+
}
|
|
12
|
+
function y(o) {
|
|
13
|
+
try {
|
|
14
|
+
return [null, o()];
|
|
15
|
+
} catch (e) {
|
|
16
|
+
return [e, null];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
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
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function c() {
|
|
28
|
+
let o;
|
|
29
|
+
return async function(e) {
|
|
30
|
+
const t = o = Math.random() * 1e7;
|
|
31
|
+
return {
|
|
32
|
+
data: await e,
|
|
33
|
+
isLast: t === o
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function u(o = () => {
|
|
38
|
+
}) {
|
|
39
|
+
let e = () => {
|
|
40
|
+
}, t = () => {
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
promise: new Promise((r, s) => {
|
|
44
|
+
o(r, s), e = r, t = s;
|
|
45
|
+
}),
|
|
46
|
+
resolve: e,
|
|
47
|
+
reject: t
|
|
48
|
+
};
|
|
49
|
+
}
|
|
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;
|
|
56
|
+
}
|
|
57
|
+
const a = /* @__PURE__ */ new WeakMap();
|
|
58
|
+
async function P(o, e) {
|
|
59
|
+
const {
|
|
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;
|
|
81
|
+
}
|
|
82
|
+
export {
|
|
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
|
+
P as withSlowLoading
|
|
90
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
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"})});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type Resolver<T> = (value: T | PromiseLike<T>) => void;
|
|
2
|
+
type withPromiseResolverReturnType<T> = {
|
|
3
|
+
promise: Promise<T>;
|
|
4
|
+
resolve: Resolver<T>;
|
|
5
|
+
reject: Resolver<T>;
|
|
6
|
+
};
|
|
7
|
+
export declare function withPromiseResolver<T = unknown>(cb?: (resolve: Resolver<T>, reject: Resolver<T>) => void): withPromiseResolverReturnType<T>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
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,24 +1,22 @@
|
|
|
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",
|
|
8
8
|
"keywords": [],
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"import": "./main.js",
|
|
12
|
-
"types": "./index.d.ts"
|
|
11
|
+
"import": "./dist/main.js",
|
|
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"
|
|
19
19
|
},
|
|
20
|
-
"types": "./index.d.ts",
|
|
21
|
-
"dependencies": {
|
|
22
|
-
"@spitx/helper": "^0.0.2"
|
|
23
|
-
}
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"dependencies": {}
|
|
24
22
|
}
|
package/main.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function f(e){let n;const r=new Promise(t=>{n=t});return e.then(t=>{n([null,t])}).catch(t=>{n([t,null])}),r}function w(e){try{return[null,e()]}catch(n){return[n,null]}}function m(e,n){const r=[...e];let t;for(;r.length&&(t=r.shift(),!!t);){const o=n(t);Array.isArray(o)&&r.unshift(...o)}}function a(){let e;return async function(n){const r=e=Math.random()*1e7;return{data:await n,isLast:r===e}}}function u(e=()=>{}){let n=()=>{},r=()=>{};return{promise:new Promise((o,s)=>{e(o,s),n=o,r=s}),resolve:n,reject:r}}function g(e){return new Promise(n=>{setTimeout(n,e)})}const i=new WeakMap;async function y(e,n){const{least:r,signal:t=new AbortController}=n;i.has(t)||i.set(t,a());const o=i.get(t)||a(),[s]=await Promise.all([o(e),g(r)]);return s}async function P(e,n){const{delay:r,signal:t=new AbortController,onSlow:o}=n;i.has(t)||i.set(t,a());const s=i.get(t)||a(),{promise:h,resolve:d}=u(),l=Date.now();setTimeout(()=>d(l),r);const c=s(e);return await Promise.race([c,h])===l&&o(),c}function S(e=8){const n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";let r="";for(let t=e;t>0;--t)r+=n[~~(Math.random()*(n.length-1))];return r}exports.flat=m;exports.handleError=f;exports.handleErrorSync=w;exports.onlyLast=a;exports.randomString=S;exports.withMinLoading=y;exports.withPromiseResolver=u;exports.withSlowLoading=P;
|
package/main.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
function d(e) {
|
|
2
|
-
let n;
|
|
3
|
-
const r = new Promise((t) => {
|
|
4
|
-
n = t;
|
|
5
|
-
});
|
|
6
|
-
return e.then((t) => {
|
|
7
|
-
n([null, t]);
|
|
8
|
-
}).catch((t) => {
|
|
9
|
-
n([t, null]);
|
|
10
|
-
}), r;
|
|
11
|
-
}
|
|
12
|
-
function g(e) {
|
|
13
|
-
try {
|
|
14
|
-
return [null, e()];
|
|
15
|
-
} catch (n) {
|
|
16
|
-
return [n, null];
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
function y(e, n) {
|
|
20
|
-
const r = [...e];
|
|
21
|
-
let t;
|
|
22
|
-
for (; r.length && (t = r.shift(), !!t); ) {
|
|
23
|
-
const o = n(t);
|
|
24
|
-
Array.isArray(o) && r.unshift(...o);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
function a() {
|
|
28
|
-
let e;
|
|
29
|
-
return async function(n) {
|
|
30
|
-
const r = e = Math.random() * 1e7;
|
|
31
|
-
return {
|
|
32
|
-
data: await n,
|
|
33
|
-
isLast: r === e
|
|
34
|
-
};
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
function h(e = () => {
|
|
38
|
-
}) {
|
|
39
|
-
let n = () => {
|
|
40
|
-
}, r = () => {
|
|
41
|
-
};
|
|
42
|
-
return {
|
|
43
|
-
promise: new Promise((o, s) => {
|
|
44
|
-
e(o, s), n = o, r = s;
|
|
45
|
-
}),
|
|
46
|
-
resolve: n,
|
|
47
|
-
reject: r
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
function m(e) {
|
|
51
|
-
return new Promise((n) => {
|
|
52
|
-
setTimeout(n, e);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
const i = /* @__PURE__ */ new WeakMap();
|
|
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;
|
|
61
|
-
}
|
|
62
|
-
async function P(e, n) {
|
|
63
|
-
const {
|
|
64
|
-
delay: r,
|
|
65
|
-
signal: t = new AbortController(),
|
|
66
|
-
onSlow: o
|
|
67
|
-
} = n;
|
|
68
|
-
i.has(t) || i.set(t, a());
|
|
69
|
-
const s = i.get(t) || a(), { promise: u, resolve: f } = h(), l = Date.now();
|
|
70
|
-
setTimeout(() => f(l), r);
|
|
71
|
-
const c = s(e);
|
|
72
|
-
return await Promise.race([c, u]) === l && o(), c;
|
|
73
|
-
}
|
|
74
|
-
function v(e = 8) {
|
|
75
|
-
const n = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
76
|
-
let r = "";
|
|
77
|
-
for (let t = e; t > 0; --t)
|
|
78
|
-
r += n[~~(Math.random() * (n.length - 1))];
|
|
79
|
-
return r;
|
|
80
|
-
}
|
|
81
|
-
export {
|
|
82
|
-
y as flat,
|
|
83
|
-
d as handleError,
|
|
84
|
-
g as handleErrorSync,
|
|
85
|
-
a as onlyLast,
|
|
86
|
-
v as randomString,
|
|
87
|
-
p as withMinLoading,
|
|
88
|
-
h as withPromiseResolver,
|
|
89
|
-
P as withSlowLoading
|
|
90
|
-
};
|
package/main.umd.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
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 o=new Promise(t=>{n=t});return e.then(t=>{n([null,t])}).catch(t=>{n([t,null])}),o}function h(e){try{return[null,e()]}catch(n){return[n,null]}}function m(e,n){const o=[...e];let t;for(;o.length&&(t=o.shift(),!!t);){const i=n(t);Array.isArray(i)&&o.unshift(...i)}}function c(){let e;return async function(n){const o=e=Math.random()*1e7;return{data:await n,isLast:o===e}}}function u(e=()=>{}){let n=()=>{},o=()=>{};return{promise:new Promise((i,a)=>{e(i,a),n=i,o=a}),resolve:n,reject:o}}function w(e){return new Promise(n=>{setTimeout(n,e)})}const s=new WeakMap;async function y(e,n){const{least:o,signal:t=new AbortController}=n;s.has(t)||s.set(t,c());const i=s.get(t)||c(),[a]=await Promise.all([i(e),w(o)]);return a}async function g(e,n){const{delay:o,signal:t=new AbortController,onSlow:i}=n;s.has(t)||s.set(t,c());const a=s.get(t)||c(),{promise:S,resolve:v}=u(),f=Date.now();setTimeout(()=>v(f),o);const d=a(e);return await Promise.race([d,S])===f&&i(),d}function P(e=8){const n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";let o="";for(let t=e;t>0;--t)o+=n[~~(Math.random()*(n.length-1))];return o}r.flat=m,r.handleError=l,r.handleErrorSync=h,r.onlyLast=c,r.randomString=P,r.withMinLoading=y,r.withPromiseResolver=u,r.withSlowLoading=g,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
package/promise/index.d.ts
DELETED
package/request/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { withMinLoading, withSlowLoading, } from './loading';
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|