@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 CHANGED
@@ -1 +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;
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 v(o) {
2
- let e;
3
- const t = new Promise((n) => {
4
- e = n;
1
+ function h(t) {
2
+ let n;
3
+ const r = new Promise((e) => {
4
+ n = e;
5
5
  });
6
- return o.then((n) => {
7
- e([null, n]);
8
- }).catch((n) => {
9
- e([n, null]);
10
- }), t;
6
+ return t.then((e) => {
7
+ n([null, e]);
8
+ }).catch((e) => {
9
+ n([e, null]);
10
+ }), r;
11
11
  }
12
- function y(o) {
12
+ function w(t) {
13
13
  try {
14
- return [null, o()];
15
- } catch (e) {
16
- return [e, null];
14
+ return [null, t()];
15
+ } catch (n) {
16
+ return [n, null];
17
17
  }
18
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);
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 o;
29
- return async function(e) {
30
- const t = o = Math.random() * 1e7;
28
+ let t;
29
+ return async function(n) {
30
+ const r = t = Math.random() * 1e7;
31
31
  return {
32
- data: await e,
33
- isLast: t === o
32
+ data: await n,
33
+ isLast: r === t
34
34
  };
35
35
  };
36
36
  }
37
- function u(o = () => {
37
+ function g(t = () => {
38
38
  }) {
39
- let e = () => {
40
- }, t = () => {
39
+ let n = () => {
40
+ }, r = () => {
41
41
  };
42
42
  return {
43
- promise: new Promise((r, s) => {
44
- o(r, s), e = r, t = s;
43
+ promise: new Promise((o, a) => {
44
+ t(o, a), n = o, r = a;
45
45
  }),
46
- resolve: e,
47
- reject: t
46
+ resolve: n,
47
+ reject: r
48
48
  };
49
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;
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
- const a = /* @__PURE__ */ new WeakMap();
58
- async function P(o, e) {
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: 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;
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
- L as flat,
84
- v as handleError,
85
- y as handleErrorSync,
79
+ m as flat,
80
+ h as handleError,
81
+ w as handleErrorSync,
86
82
  c as onlyLast,
87
- w as randomString,
88
- u as withPromiseResolver,
89
- P as withSlowLoading
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,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"})});
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;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@spitx/helper",
3
3
  "type": "module",
4
- "version": "0.0.6",
4
+ "version": "0.0.8",
5
5
  "description": "helper module",
6
6
  "author": "spitx",
7
7
  "license": "ISC",