@net-vert/core 0.1.2 → 0.2.0

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/index.d.ts CHANGED
@@ -1,5 +1,19 @@
1
1
  import { TaskQueue } from 'id-queue';
2
2
 
3
+ declare type CachedData = {
4
+ value: any;
5
+ expiresAt: number;
6
+ };
7
+
8
+ declare type CacheRequestor<P extends boolean, S extends boolean> = {
9
+ key?: (config: UnifiedConfig) => string;
10
+ duration?: Duration;
11
+ sync?: S;
12
+ persist?: P;
13
+ name?: [P, S] extends [true, false] ? string : undefined;
14
+ isValid?: S extends true ? (params: IsValidParams) => boolean : (params: IsValidParams) => boolean | Promise<boolean>;
15
+ };
16
+
3
17
  declare class ConcurrentPool {
4
18
  parallelCount: number;
5
19
  tasks: TaskItemList;
@@ -11,8 +25,20 @@ declare class ConcurrentPool {
11
25
  _run(): void;
12
26
  }
13
27
 
28
+ declare type Duration = number | (({ key, config, response }: {
29
+ key: string;
30
+ config: UnifiedConfig;
31
+ response: any;
32
+ }) => number);
33
+
14
34
  export declare const inject: (requestor: UnifiedRequestor, instanceKey?: string) => void;
15
35
 
36
+ declare type IsValidParams = {
37
+ key: string;
38
+ config: UnifiedConfig;
39
+ cachedData: CachedData;
40
+ };
41
+
16
42
  declare interface RequestConfig<D = any> {
17
43
  url?: string;
18
44
  method?: keyof Requestor;
@@ -28,25 +54,32 @@ declare interface RequestConfig<D = any> {
28
54
  }
29
55
 
30
56
  export declare const requestExtender: {
31
- cacheRequestor: (config?: {
32
- key?: (config: UnifiedConfig) => string;
33
- persist?: boolean;
34
- duration?: number | (({ key, config, response }: {
35
- key: string;
36
- config: UnifiedConfig;
37
- response: any;
38
- }) => number);
39
- isValid?: (params: {
40
- key: string;
41
- config: UnifiedConfig;
42
- cachedData: {
43
- value: any;
44
- expiresAt: number;
45
- };
46
- }) => boolean | Promise<boolean>;
47
- }) => Requestor;
57
+ cacheRequestor: <P extends boolean = false, S extends boolean = false>(config?: CacheRequestor<P, S>) => {
58
+ requestor: Requestor;
59
+ store: {
60
+ has(key: string): boolean;
61
+ get<T>(key: string): T | undefined;
62
+ set<T>(key: string, value: T): T;
63
+ remove(key: string): void;
64
+ clear(): void;
65
+ } | {
66
+ has: (key: string) => Promise<boolean>;
67
+ get: <T>(key: string) => Promise<T | null>;
68
+ set: <T>(key: string, value: T) => Promise<T>;
69
+ remove: (key: string) => Promise<void>;
70
+ clear: () => Promise<void>;
71
+ } | {
72
+ has: (key: string) => boolean;
73
+ get: (key: string) => any;
74
+ set: <T>(key: string, value: T) => Map<string, any>;
75
+ remove: (key: string) => boolean;
76
+ clear: () => void;
77
+ };
78
+ };
48
79
  idempotencyRequestor: (genKey?: (config: UnifiedConfig) => string) => Requestor;
49
- retryRequestor: (config?: RetryOptions) => Requestor;
80
+ retryRequestor: (config?: RetryOptions) => {
81
+ requestor: Requestor;
82
+ };
50
83
  concurrentPoolRequestor: (config?: {
51
84
  parallelCount?: number;
52
85
  createId?: (config: UnifiedConfig) => string;
@@ -54,6 +87,12 @@ export declare const requestExtender: {
54
87
  requestor: Requestor;
55
88
  concurrentPool: ConcurrentPool;
56
89
  };
90
+ syncRequestor: (config?: {
91
+ persist?: false;
92
+ sync?: true;
93
+ } & CacheRequestor<false, true>) => {
94
+ requestor: Requestor;
95
+ };
57
96
  };
58
97
 
59
98
  export declare interface Requestor {
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import h from "localforage";
2
- import { TaskQueue as I } from "id-queue";
3
- const f = {
1
+ import D from "localforage";
2
+ import { TaskQueue as E } from "id-queue";
3
+ const k = {
4
4
  get: (t, e) => ({
5
5
  url: t,
6
6
  method: "get",
@@ -30,11 +30,11 @@ const f = {
30
30
  };
31
31
  function j(t) {
32
32
  const e = {};
33
- return Object.keys(f).forEach(
33
+ return Object.keys(k).forEach(
34
34
  (r) => {
35
- e[r] = (...o) => {
36
- const n = f[r](...o);
37
- return t(n);
35
+ e[r] = (...n) => {
36
+ const o = k[r](...n);
37
+ return t(o);
38
38
  };
39
39
  }
40
40
  ), {
@@ -42,39 +42,75 @@ function j(t) {
42
42
  request: t
43
43
  };
44
44
  }
45
- const R = "default", k = /* @__PURE__ */ new Map(), U = (t, e = R) => {
46
- k.set(e, j(t));
47
- }, g = (t = R) => {
48
- const e = k.get(t);
45
+ const M = "default", z = () => `${Date.now()}_${Math.random().toString().slice(2, 8)}`, I = /* @__PURE__ */ new Map(), V = (t, e = M) => {
46
+ I.set(e, j(t));
47
+ }, v = (t = M) => {
48
+ const e = I.get(t);
49
49
  if (!e) throw new Error(`Requestor实例 ${t} 未注册`);
50
50
  return e;
51
51
  };
52
- class E {
53
- constructor(e) {
54
- e && h.config(e);
52
+ class O {
53
+ has(e) {
54
+ return !!this.get(e);
55
55
  }
56
- async has(e) {
57
- return await h.getItem(e) !== null;
56
+ // 获取缓存
57
+ get(e) {
58
+ const r = localStorage.getItem(e);
59
+ if (r)
60
+ try {
61
+ return JSON.parse(r);
62
+ } catch (n) {
63
+ console.error("Error parsing cached data", n);
64
+ return;
65
+ }
58
66
  }
59
- async get(e) {
67
+ // 设置缓存
68
+ set(e, r) {
60
69
  try {
61
- return await h.getItem(e) ?? void 0;
62
- } catch (r) {
63
- console.error("Error getting data", r);
64
- return;
70
+ const n = JSON.stringify(r);
71
+ localStorage.setItem(e, n);
72
+ } catch (n) {
73
+ console.error("Error saving data to localStorage", n);
65
74
  }
75
+ return r;
66
76
  }
67
- async set(e, r) {
68
- return await h.setItem(e, r), r;
69
- }
70
- async delete(e) {
71
- await h.removeItem(e);
77
+ // 删除缓存
78
+ remove(e) {
79
+ localStorage.removeItem(e);
72
80
  }
73
- async clear() {
74
- await h.clear();
81
+ // 清空所有缓存
82
+ clear() {
83
+ localStorage.clear();
75
84
  }
76
85
  }
77
- const S = new E(), T = /* @__PURE__ */ new Map(), $ = (t) => t ? S : T, D = () => {
86
+ const T = new O(), H = ({ persist: t, name: e, sync: r }) => {
87
+ if (t && r)
88
+ return T;
89
+ if (t) {
90
+ const n = D.createInstance({
91
+ name: e
92
+ // driver: sync
93
+ // ?undefined
94
+ // :localforage.LOCALSTORAGE,
95
+ });
96
+ return {
97
+ has: (o) => n.keys().then((s) => s.includes(o)),
98
+ get: (o) => n.getItem(o),
99
+ set: (o, s) => n.setItem(o, s),
100
+ remove: (o) => n.removeItem(o),
101
+ clear: () => n.clear()
102
+ };
103
+ } else {
104
+ const n = /* @__PURE__ */ new Map();
105
+ return {
106
+ has: (o) => n.has(o),
107
+ get: (o) => n.get(o),
108
+ set: (o, s) => n.set(o, s),
109
+ remove: (o) => n.delete(o),
110
+ clear: () => n.clear()
111
+ };
112
+ }
113
+ }, U = () => {
78
114
  const t = /* @__PURE__ */ new Map();
79
115
  return {
80
116
  getPromise: (s) => t.get(s),
@@ -91,90 +127,130 @@ const S = new E(), T = /* @__PURE__ */ new Map(), $ = (t) => t ? S : T, D = () =
91
127
  }, _ = (t, e) => ({
92
128
  value: t,
93
129
  expiresAt: Date.now() + e
94
- }), z = (t) => t.expiresAt > Date.now(), A = {
130
+ }), x = (t) => t.expiresAt > Date.now(), A = {
95
131
  key: (t) => t.url,
96
132
  persist: !1,
97
- duration: 1 / 0
98
- }, x = (t) => {
99
- const e = { ...A, ...t }, r = $(e.persist), { getPromise: o, setPromise: n, delPromise: s } = D(), i = {
100
- get(d, c) {
101
- return async (...u) => {
102
- const l = f[c](...u), a = e.key(l), p = o(a);
103
- if (p)
104
- return p;
105
- const C = await r.get(a);
106
- let w = !1;
107
- if (C && z(C)) {
108
- try {
109
- w = e.isValid ? await e.isValid({
110
- key: a,
111
- config: l,
112
- cachedData: C
113
- }) : !0;
114
- } catch (m) {
115
- console.error(`校验异常 ${a}`, m);
116
- }
117
- w || r.delete(a);
118
- }
119
- if (w)
120
- return C.value;
121
- const P = Reflect.apply(d[c], d, u).then(async (m) => {
122
- const v = typeof e.duration == "number" ? e.duration : e.duration({
123
- key: a,
124
- config: l,
125
- response: m
133
+ duration: 1 / 0,
134
+ sync: !1
135
+ }, S = (t) => {
136
+ const e = { ...A, ...t }, { name: r, persist: n, sync: o } = e, s = H({ persist: n, name: r, sync: o }), { getPromise: i, setPromise: P, delPromise: f } = U();
137
+ function c(a, d) {
138
+ const { isValid: l } = e, h = s.get(a);
139
+ let g = !1;
140
+ if (h && x(h)) {
141
+ try {
142
+ g = (l == null ? void 0 : l({
143
+ key: a,
144
+ config: d,
145
+ cachedData: h
146
+ })) ?? !0;
147
+ } catch (u) {
148
+ console.error(`校验异常 ${a}`, u);
149
+ }
150
+ !g && s.remove(a);
151
+ }
152
+ return { shouldUseCache: g, cachedData: h };
153
+ }
154
+ async function w(a, d) {
155
+ const { isValid: l } = e, h = await s.get(a);
156
+ let g = !1;
157
+ if (h && x(h)) {
158
+ try {
159
+ g = await (l == null ? void 0 : l({
160
+ key: a,
161
+ config: d,
162
+ cachedData: h
163
+ })) ?? !0;
164
+ } catch (u) {
165
+ console.error(`校验异常 ${a}`, u);
166
+ }
167
+ !g && s.remove(a);
168
+ }
169
+ return { shouldUseCache: g, cachedData: h };
170
+ }
171
+ const p = {
172
+ get(a, d) {
173
+ function l(u, m, ...y) {
174
+ const C = Reflect.apply(a[d], a, y).then(async (q) => {
175
+ const R = typeof e.duration == "number" ? e.duration : e.duration({
176
+ key: u,
177
+ config: m,
178
+ response: q
126
179
  });
127
- return r.set(a, _(m, v)), m;
180
+ return s.set(u, _(q, R)), q;
128
181
  }).finally(() => {
129
- s(a);
182
+ f(u);
130
183
  });
131
- return n(a, P), P;
184
+ return P(u, C), C;
185
+ }
186
+ return o ? (...u) => {
187
+ const m = k[d](...u), y = e.key(m), C = i(y);
188
+ if (C)
189
+ return C;
190
+ const { shouldUseCache: q, cachedData: R } = c(y, m);
191
+ return q ? R.value : l(y, m, ...u);
192
+ } : async (...u) => {
193
+ const m = k[d](...u), y = e.key(m), C = i(y);
194
+ if (C)
195
+ return C;
196
+ const { shouldUseCache: q, cachedData: R } = await w(y, m);
197
+ return q ? R.value : l(y, m, ...u);
132
198
  };
133
199
  }
134
200
  };
135
- return new Proxy(g(), i);
136
- }, O = (t) => {
137
- const { method: e, url: r, params: o, data: n } = t;
138
- return [e, r, JSON.stringify(o), JSON.stringify(n)].join("|");
139
- }, H = (t) => x({
140
- key: (e) => t ? t(e) : O(e),
141
- persist: !1
142
- }), V = {
201
+ return {
202
+ requestor: new Proxy(v(), p),
203
+ store: s
204
+ };
205
+ }, J = (t) => {
206
+ const { method: e, url: r, params: n, data: o } = t;
207
+ return [e, r, JSON.stringify(n), JSON.stringify(o)].join("|");
208
+ }, N = (t) => {
209
+ const {
210
+ requestor: e
211
+ } = S({
212
+ key: (r) => t ? t(r) : J(r),
213
+ persist: !1
214
+ });
215
+ return e;
216
+ }, b = {
143
217
  retries: 3,
144
218
  delay: 0,
145
219
  retryCondition: () => !0
146
- }, M = (t) => {
147
- const { retries: e, delay: r, retryCondition: o } = { ...V, ...t }, n = {
220
+ }, $ = (t) => {
221
+ const { retries: e, delay: r, retryCondition: n } = { ...b, ...t }, o = {
148
222
  get(s, i) {
149
- return (...d) => {
223
+ return (...f) => {
150
224
  let c = 0;
151
- const y = () => Reflect.apply(s[i], s, d).catch((u) => {
152
- if (c < e && o(u)) {
225
+ const w = () => Reflect.apply(s[i], s, f).catch((p) => {
226
+ if (c < e && n(p)) {
153
227
  c++;
154
- const l = typeof r == "function" ? r(c) : r;
155
- return new Promise((a) => {
156
- setTimeout(() => a(y()), l);
228
+ const a = typeof r == "function" ? r(c) : r;
229
+ return new Promise((d) => {
230
+ setTimeout(() => d(w()), a);
157
231
  });
158
232
  }
159
- return Promise.reject(u);
233
+ return Promise.reject(p);
160
234
  });
161
- return y();
235
+ return w();
162
236
  };
163
237
  }
164
238
  };
165
- return new Proxy(g(), n);
239
+ return {
240
+ requestor: new Proxy(v(), o)
241
+ };
166
242
  };
167
- class b {
243
+ class F {
168
244
  constructor(e = 4) {
169
- this.parallelCount = e, this.tasks = new I(), this.runningCount = 0;
245
+ this.parallelCount = e, this.tasks = new E(), this.runningCount = 0;
170
246
  }
171
247
  // 加入
172
248
  add(e, r) {
173
- return console.log("poolinset", e, r), new Promise((o, n) => {
249
+ return new Promise((n, o) => {
174
250
  this.tasks.enqueue(e, {
175
251
  task: r,
176
- resolve: o,
177
- reject: n
252
+ resolve: n,
253
+ reject: o
178
254
  }), this._run();
179
255
  });
180
256
  }
@@ -183,8 +259,8 @@ class b {
183
259
  this.tasks.remove(e);
184
260
  }
185
261
  execute(e) {
186
- const { task: r, resolve: o, reject: n } = e;
187
- return r().then(o).catch(n).finally(() => {
262
+ const { task: r, resolve: n, reject: o } = e;
263
+ return r().then(n).catch(o).finally(() => {
188
264
  this.runningCount--, this._run();
189
265
  });
190
266
  }
@@ -195,31 +271,55 @@ class b {
195
271
  }
196
272
  }
197
273
  }
198
- const F = {
274
+ const L = {
199
275
  parallelCount: 4,
200
276
  retries: 0,
201
- createId: () => `${Date.now()}_${Math.random().toString().slice(2, 8)}`
202
- }, J = (t) => {
203
- const e = { ...F, ...t }, { parallelCount: r, createId: o, ...n } = e, s = new b(r), i = n.retries > 0 ? M(n) : null, q = {
204
- get(d, c) {
205
- return (...u) => {
206
- const l = f[c](...u), a = o(l), p = () => i ? i.request(l) : Reflect.apply(d[c], d, u);
207
- return s.add(a, p);
277
+ createId: () => z()
278
+ }, Q = (t) => {
279
+ const e = { ...L, ...t }, { parallelCount: r, createId: n, ...o } = e, s = new F(r), { requestor: i = null } = o.retries > 0 ? $(o) : {}, P = {
280
+ get(f, c) {
281
+ return (...p) => {
282
+ const a = k[c](...p), d = n(a), l = () => i ? Reflect.apply(i[c], i, p) : Reflect.apply(f[c], f, p);
283
+ return s.add(d, l);
208
284
  };
209
285
  }
210
286
  };
211
287
  return {
212
- requestor: new Proxy(g(), q),
288
+ requestor: new Proxy(v(), P),
213
289
  concurrentPool: s
214
290
  };
215
- }, Q = {
216
- cacheRequestor: x,
217
- idempotencyRequestor: H,
218
- retryRequestor: M,
219
- concurrentPoolRequestor: J
291
+ }, W = {
292
+ persist: !1,
293
+ sync: !0
294
+ }, Y = (t) => {
295
+ const e = { ...W, ...t }, { ...r } = e, {
296
+ requestor: n
297
+ } = S(r), o = {
298
+ get(s, i) {
299
+ return (...f) => {
300
+ try {
301
+ const c = Reflect.apply(s[i], s, f);
302
+ if (c instanceof Promise)
303
+ throw c;
304
+ return c;
305
+ } catch (c) {
306
+ throw c;
307
+ }
308
+ };
309
+ }
310
+ };
311
+ return {
312
+ requestor: new Proxy(n, o)
313
+ };
314
+ }, X = {
315
+ cacheRequestor: S,
316
+ idempotencyRequestor: N,
317
+ retryRequestor: $,
318
+ concurrentPoolRequestor: Q,
319
+ syncRequestor: Y
220
320
  };
221
321
  export {
222
- U as inject,
223
- Q as requestExtender,
224
- g as useRequestor
322
+ V as inject,
323
+ X as requestExtender,
324
+ v as useRequestor
225
325
  };
@@ -1 +1 @@
1
- (function(u,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("localforage"),require("id-queue")):typeof define=="function"&&define.amd?define(["exports","localforage","id-queue"],a):(u=typeof globalThis<"u"?globalThis:u||self,a(u["net-vert/core"]={},u.localforage,u.idQueue))})(this,function(u,a,j){"use strict";const p={get:(t,e)=>({url:t,method:"get",...e,params:e==null?void 0:e.params}),post:(t,e,r)=>({url:t,method:"post",data:e,headers:{"Content-Type":"application/json",...r==null?void 0:r.headers},...r}),delete:(t,e)=>({url:t,method:"delete",...e}),put:(t,e,r)=>({url:t,method:"put",data:e,headers:{"Content-Type":"application/json",...r==null?void 0:r.headers},...r}),request:t=>t};function T(t){const e={};return Object.keys(p).forEach(r=>{e[r]=(...s)=>{const n=p[r](...s);return t(n)}}),{...e,request:t}}const R="default",k=new Map,I=(t,e=R)=>{k.set(e,T(t))},f=(t=R)=>{const e=k.get(t);if(!e)throw new Error(`Requestor实例 ${t} 未注册`);return e};class S{constructor(e){e&&a.config(e)}async has(e){return await a.getItem(e)!==null}async get(e){try{return await a.getItem(e)??void 0}catch(r){console.error("Error getting data",r);return}}async set(e,r){return await a.setItem(e,r),r}async delete(e){await a.removeItem(e)}async clear(){await a.clear()}}const E=new S,$=new Map,D=t=>t?E:$,O=()=>{const t=new Map;return{getPromise:o=>t.get(o),setPromise:(o,l)=>{t.set(o,l)},delPromise:o=>{t.delete(o)},clearCache:()=>{t.clear()}}},_=(t,e)=>({value:t,expiresAt:Date.now()+e}),z=t=>t.expiresAt>Date.now(),A={key:t=>t.url,persist:!1,duration:1/0},v=t=>{const e={...A,...t},r=D(e.persist),{getPromise:s,setPromise:n,delPromise:o}=O(),l={get(y,c){return async(...d)=>{const h=p[c](...d),i=e.key(h),C=s(i);if(C)return C;const w=await r.get(i);let g=!1;if(w&&z(w)){try{g=e.isValid?await e.isValid({key:i,config:h,cachedData:w}):!0}catch(m){console.error(`校验异常 ${i}`,m)}g||r.delete(i)}if(g)return w.value;const M=Reflect.apply(y[c],y,d).then(async m=>{const N=typeof e.duration=="number"?e.duration:e.duration({key:i,config:h,response:m});return r.set(i,_(m,N)),m}).finally(()=>{o(i)});return n(i,M),M}}};return new Proxy(f(),l)},b=t=>{const{method:e,url:r,params:s,data:n}=t;return[e,r,JSON.stringify(s),JSON.stringify(n)].join("|")},H=t=>v({key:e=>t?t(e):b(e),persist:!1}),V={retries:3,delay:0,retryCondition:()=>!0},x=t=>{const{retries:e,delay:r,retryCondition:s}={...V,...t},n={get(o,l){return(...y)=>{let c=0;const q=()=>Reflect.apply(o[l],o,y).catch(d=>{if(c<e&&s(d)){c++;const h=typeof r=="function"?r(c):r;return new Promise(i=>{setTimeout(()=>i(q()),h)})}return Promise.reject(d)});return q()}}};return new Proxy(f(),n)};class F{constructor(e=4){this.parallelCount=e,this.tasks=new j.TaskQueue,this.runningCount=0}add(e,r){return console.log("poolinset",e,r),new Promise((s,n)=>{this.tasks.enqueue(e,{task:r,resolve:s,reject:n}),this._run()})}remove(e){this.tasks.remove(e)}execute(e){const{task:r,resolve:s,reject:n}=e;return r().then(s).catch(n).finally(()=>{this.runningCount--,this._run()})}_run(){for(;this.runningCount<this.parallelCount&&this.tasks.size>0;){const e=this.tasks.dequeue();this.runningCount++,this.execute(e)}}}const J={parallelCount:4,retries:0,createId:()=>`${Date.now()}_${Math.random().toString().slice(2,8)}`},L={cacheRequestor:v,idempotencyRequestor:H,retryRequestor:x,concurrentPoolRequestor:t=>{const e={...J,...t},{parallelCount:r,createId:s,...n}=e,o=new F(r),l=n.retries>0?x(n):null,P={get(y,c){return(...d)=>{const h=p[c](...d),i=s(h),C=()=>l?l.request(h):Reflect.apply(y[c],y,d);return o.add(i,C)}}};return{requestor:new Proxy(f(),P),concurrentPool:o}}};u.inject=I,u.requestExtender=L,u.useRequestor=f,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
1
+ (function(h,w){typeof exports=="object"&&typeof module<"u"?w(exports,require("localforage"),require("id-queue")):typeof define=="function"&&define.amd?define(["exports","localforage","id-queue"],w):(h=typeof globalThis<"u"?globalThis:h||self,w(h["net-vert/core"]={},h.localforage,h.idQueue))})(this,function(h,w,D){"use strict";const R={get:(t,e)=>({url:t,method:"get",...e,params:e==null?void 0:e.params}),post:(t,e,r)=>({url:t,method:"post",data:e,headers:{"Content-Type":"application/json",...r==null?void 0:r.headers},...r}),delete:(t,e)=>({url:t,method:"delete",...e}),put:(t,e,r)=>({url:t,method:"put",data:e,headers:{"Content-Type":"application/json",...r==null?void 0:r.headers},...r}),request:t=>t};function E(t){const e={};return Object.keys(R).forEach(r=>{e[r]=(...n)=>{const o=R[r](...n);return t(o)}}),{...e,request:t}}const I="default",O=()=>`${Date.now()}_${Math.random().toString().slice(2,8)}`,j=new Map,z=(t,e=I)=>{j.set(e,E(t))},M=(t=I)=>{const e=j.get(t);if(!e)throw new Error(`Requestor实例 ${t} 未注册`);return e};class H{has(e){return!!this.get(e)}get(e){const r=localStorage.getItem(e);if(r)try{return JSON.parse(r)}catch(n){console.error("Error parsing cached data",n);return}}set(e,r){try{const n=JSON.stringify(r);localStorage.setItem(e,n)}catch(n){console.error("Error saving data to localStorage",n)}return r}remove(e){localStorage.removeItem(e)}clear(){localStorage.clear()}}const U=new H,_=({persist:t,name:e,sync:r})=>{if(t&&r)return U;if(t){const n=w.createInstance({name:e});return{has:o=>n.keys().then(s=>s.includes(o)),get:o=>n.getItem(o),set:(o,s)=>n.setItem(o,s),remove:o=>n.removeItem(o),clear:()=>n.clear()}}else{const n=new Map;return{has:o=>n.has(o),get:o=>n.get(o),set:(o,s)=>n.set(o,s),remove:o=>n.delete(o),clear:()=>n.clear()}}},A=()=>{const t=new Map;return{getPromise:s=>t.get(s),setPromise:(s,i)=>{t.set(s,i)},delPromise:s=>{t.delete(s)},clearCache:()=>{t.clear()}}},J=(t,e)=>({value:t,expiresAt:Date.now()+e}),T=t=>t.expiresAt>Date.now(),N={key:t=>t.url,persist:!1,duration:1/0,sync:!1},x=t=>{const e={...N,...t},{name:r,persist:n,sync:o}=e,s=_({persist:n,name:r,sync:o}),{getPromise:i,setPromise:S,delPromise:p}=A();function a(c,d){const{isValid:l}=e,m=s.get(c);let q=!1;if(m&&T(m)){try{q=(l==null?void 0:l({key:c,config:d,cachedData:m}))??!0}catch(u){console.error(`校验异常 ${c}`,u)}!q&&s.remove(c)}return{shouldUseCache:q,cachedData:m}}async function k(c,d){const{isValid:l}=e,m=await s.get(c);let q=!1;if(m&&T(m)){try{q=await(l==null?void 0:l({key:c,config:d,cachedData:m}))??!0}catch(u){console.error(`校验异常 ${c}`,u)}!q&&s.remove(c)}return{shouldUseCache:q,cachedData:m}}const g={get(c,d){function l(u,f,...y){const C=Reflect.apply(c[d],c,y).then(async P=>{const v=typeof e.duration=="number"?e.duration:e.duration({key:u,config:f,response:P});return s.set(u,J(P,v)),P}).finally(()=>{p(u)});return S(u,C),C}return o?(...u)=>{const f=R[d](...u),y=e.key(f),C=i(y);if(C)return C;const{shouldUseCache:P,cachedData:v}=a(y,f);return P?v.value:l(y,f,...u)}:async(...u)=>{const f=R[d](...u),y=e.key(f),C=i(y);if(C)return C;const{shouldUseCache:P,cachedData:v}=await k(y,f);return P?v.value:l(y,f,...u)}}};return{requestor:new Proxy(M(),g),store:s}},b=t=>{const{method:e,url:r,params:n,data:o}=t;return[e,r,JSON.stringify(n),JSON.stringify(o)].join("|")},Q=t=>{const{requestor:e}=x({key:r=>t?t(r):b(r),persist:!1});return e},F={retries:3,delay:0,retryCondition:()=>!0},$=t=>{const{retries:e,delay:r,retryCondition:n}={...F,...t},o={get(s,i){return(...p)=>{let a=0;const k=()=>Reflect.apply(s[i],s,p).catch(g=>{if(a<e&&n(g)){a++;const c=typeof r=="function"?r(a):r;return new Promise(d=>{setTimeout(()=>d(k()),c)})}return Promise.reject(g)});return k()}}};return{requestor:new Proxy(M(),o)}};class L{constructor(e=4){this.parallelCount=e,this.tasks=new D.TaskQueue,this.runningCount=0}add(e,r){return new Promise((n,o)=>{this.tasks.enqueue(e,{task:r,resolve:n,reject:o}),this._run()})}remove(e){this.tasks.remove(e)}execute(e){const{task:r,resolve:n,reject:o}=e;return r().then(n).catch(o).finally(()=>{this.runningCount--,this._run()})}_run(){for(;this.runningCount<this.parallelCount&&this.tasks.size>0;){const e=this.tasks.dequeue();this.runningCount++,this.execute(e)}}}const W={parallelCount:4,retries:0,createId:()=>O()},Y=t=>{const e={...W,...t},{parallelCount:r,createId:n,...o}=e,s=new L(r),{requestor:i=null}=o.retries>0?$(o):{},S={get(p,a){return(...g)=>{const c=R[a](...g),d=n(c),l=()=>i?Reflect.apply(i[a],i,g):Reflect.apply(p[a],p,g);return s.add(d,l)}}};return{requestor:new Proxy(M(),S),concurrentPool:s}},B={persist:!1,sync:!0},G={cacheRequestor:x,idempotencyRequestor:Q,retryRequestor:$,concurrentPoolRequestor:Y,syncRequestor:t=>{const e={...B,...t},{...r}=e,{requestor:n}=x(r),o={get(s,i){return(...p)=>{try{const a=Reflect.apply(s[i],s,p);if(a instanceof Promise)throw a;return a}catch(a){throw a}}}};return{requestor:new Proxy(n,o)}}};h.inject=z,h.requestExtender=G,h.useRequestor=M,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@net-vert/core",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "Dependency Inversion Network Library with Type-Safe Injection.",
5
5
  "main": "dist/index",
6
6
  "type": "module",