@net-vert/core 0.0.7 → 0.0.9
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 +37 -5
- package/dist/index.js +195 -88
- package/dist/index.umd.cjs +1 -1
- package/package.json +5 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
|
+
import { TaskQueue } from 'id-queue';
|
|
2
|
+
|
|
1
3
|
declare interface BasicCredentials {
|
|
2
4
|
username: string;
|
|
3
5
|
password: string;
|
|
4
6
|
}
|
|
5
7
|
|
|
8
|
+
declare class ConcurrentPool {
|
|
9
|
+
parallelCount: number;
|
|
10
|
+
tasks: TaskItemList;
|
|
11
|
+
runningCount: number;
|
|
12
|
+
constructor(parallelCount?: number);
|
|
13
|
+
add(id: string, task: Task): Promise<unknown>;
|
|
14
|
+
remove(id: string): void;
|
|
15
|
+
execute(currentTask: TaskItem): Promise<any>;
|
|
16
|
+
_run(): void;
|
|
17
|
+
}
|
|
18
|
+
|
|
6
19
|
export declare const inject: (requestor: UnifiedRequestor, instanceKey?: string) => void;
|
|
7
20
|
|
|
8
21
|
declare interface RequestConfig<D = any> {
|
|
@@ -31,11 +44,14 @@ export declare const requestExtender: {
|
|
|
31
44
|
}) => number);
|
|
32
45
|
}) => Requestor;
|
|
33
46
|
idempotencyRequestor: (genKey?: (config: UnifiedConfig) => string) => Requestor;
|
|
34
|
-
retryRequestor: (config?:
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}) =>
|
|
47
|
+
retryRequestor: (config?: RetryOptions) => Requestor;
|
|
48
|
+
concurrentPoolRequestor: (config: {
|
|
49
|
+
parallelCount?: number;
|
|
50
|
+
createId?: (config: UnifiedConfig) => string;
|
|
51
|
+
} & RetryOptions) => {
|
|
52
|
+
requestor: Requestor;
|
|
53
|
+
concurrentPool: ConcurrentPool;
|
|
54
|
+
};
|
|
39
55
|
};
|
|
40
56
|
|
|
41
57
|
export declare interface Requestor {
|
|
@@ -46,6 +62,22 @@ export declare interface Requestor {
|
|
|
46
62
|
request<R = any, D = any>(config: UnifiedConfig<D>): Promise<R>;
|
|
47
63
|
}
|
|
48
64
|
|
|
65
|
+
declare type RetryOptions = {
|
|
66
|
+
retries?: number;
|
|
67
|
+
delay?: number | ((attempt: number) => number);
|
|
68
|
+
retryCondition?: (error: any) => boolean;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
declare type Task = () => Promise<any>;
|
|
72
|
+
|
|
73
|
+
declare type TaskItem = {
|
|
74
|
+
task: Task;
|
|
75
|
+
resolve: (value: any) => any;
|
|
76
|
+
reject: (value: any) => any;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
declare type TaskItemList = TaskQueue<TaskItem>;
|
|
80
|
+
|
|
49
81
|
export declare type UnifiedConfig<D = any> = RequestConfig<D> & {
|
|
50
82
|
url: string;
|
|
51
83
|
method: keyof Requestor;
|
package/dist/index.js
CHANGED
|
@@ -1,77 +1,77 @@
|
|
|
1
1
|
const y = {
|
|
2
|
-
get: (
|
|
3
|
-
url:
|
|
2
|
+
get: (r, e) => ({
|
|
3
|
+
url: r,
|
|
4
4
|
method: "get",
|
|
5
5
|
...e,
|
|
6
6
|
params: e == null ? void 0 : e.params
|
|
7
7
|
}),
|
|
8
|
-
post: (
|
|
9
|
-
url:
|
|
8
|
+
post: (r, e, t) => ({
|
|
9
|
+
url: r,
|
|
10
10
|
method: "post",
|
|
11
11
|
data: e,
|
|
12
|
-
headers: { "Content-Type": "application/json", ...
|
|
13
|
-
...
|
|
12
|
+
headers: { "Content-Type": "application/json", ...t == null ? void 0 : t.headers },
|
|
13
|
+
...t
|
|
14
14
|
}),
|
|
15
|
-
delete: (
|
|
16
|
-
url:
|
|
15
|
+
delete: (r, e) => ({
|
|
16
|
+
url: r,
|
|
17
17
|
method: "delete",
|
|
18
18
|
...e
|
|
19
19
|
}),
|
|
20
|
-
put: (
|
|
21
|
-
url:
|
|
20
|
+
put: (r, e, t) => ({
|
|
21
|
+
url: r,
|
|
22
22
|
method: "put",
|
|
23
23
|
data: e,
|
|
24
|
-
headers: { "Content-Type": "application/json", ...
|
|
25
|
-
...
|
|
24
|
+
headers: { "Content-Type": "application/json", ...t == null ? void 0 : t.headers },
|
|
25
|
+
...t
|
|
26
26
|
}),
|
|
27
|
-
request: (
|
|
27
|
+
request: (r) => r
|
|
28
28
|
};
|
|
29
|
-
function
|
|
29
|
+
function k(r) {
|
|
30
30
|
const e = {};
|
|
31
31
|
return Object.keys(y).forEach(
|
|
32
|
-
(
|
|
33
|
-
e[
|
|
34
|
-
const
|
|
35
|
-
return
|
|
32
|
+
(t) => {
|
|
33
|
+
e[t] = (...s) => {
|
|
34
|
+
const n = y[t](...s);
|
|
35
|
+
return r(n);
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
38
|
), {
|
|
39
39
|
...e,
|
|
40
|
-
request:
|
|
40
|
+
request: r
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
|
-
const P = "default",
|
|
44
|
-
|
|
45
|
-
},
|
|
46
|
-
const e =
|
|
47
|
-
if (!e) throw new Error(`Requestor实例 ${
|
|
43
|
+
const P = "default", w = /* @__PURE__ */ new Map(), L = (r, e = P) => {
|
|
44
|
+
w.set(e, k(r));
|
|
45
|
+
}, C = (r = P) => {
|
|
46
|
+
const e = w.get(r);
|
|
47
|
+
if (!e) throw new Error(`Requestor实例 ${r} 未注册`);
|
|
48
48
|
return e;
|
|
49
49
|
};
|
|
50
|
-
class
|
|
50
|
+
class M {
|
|
51
51
|
// 是否存在
|
|
52
52
|
has(e) {
|
|
53
53
|
return !!localStorage.getItem(e);
|
|
54
54
|
}
|
|
55
55
|
// 获取缓存
|
|
56
56
|
get(e) {
|
|
57
|
-
const
|
|
58
|
-
if (
|
|
57
|
+
const t = localStorage.getItem(e);
|
|
58
|
+
if (t)
|
|
59
59
|
try {
|
|
60
|
-
return JSON.parse(
|
|
60
|
+
return JSON.parse(t);
|
|
61
61
|
} catch (s) {
|
|
62
62
|
console.error("Error parsing cached data", s);
|
|
63
63
|
return;
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
// 设置缓存
|
|
67
|
-
set(e,
|
|
67
|
+
set(e, t) {
|
|
68
68
|
try {
|
|
69
|
-
const s = JSON.stringify(
|
|
69
|
+
const s = JSON.stringify(t);
|
|
70
70
|
localStorage.setItem(e, s);
|
|
71
71
|
} catch (s) {
|
|
72
72
|
console.error("Error saving data to localStorage", s);
|
|
73
73
|
}
|
|
74
|
-
return
|
|
74
|
+
return t;
|
|
75
75
|
}
|
|
76
76
|
// 删除缓存
|
|
77
77
|
delete(e) {
|
|
@@ -82,90 +82,197 @@ class E {
|
|
|
82
82
|
localStorage.clear();
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
-
const T = new
|
|
86
|
-
const
|
|
85
|
+
const T = new M(), E = /* @__PURE__ */ new Map(), j = (r) => r ? T : E, I = () => {
|
|
86
|
+
const r = /* @__PURE__ */ new Map();
|
|
87
87
|
return {
|
|
88
|
-
getPromise: (o) =>
|
|
89
|
-
setPromise: (o,
|
|
90
|
-
|
|
88
|
+
getPromise: (o) => r.get(o),
|
|
89
|
+
setPromise: (o, l) => {
|
|
90
|
+
r.set(o, l);
|
|
91
91
|
},
|
|
92
92
|
delPromise: (o) => {
|
|
93
|
-
|
|
93
|
+
r.delete(o);
|
|
94
94
|
},
|
|
95
95
|
clearCache: () => {
|
|
96
|
-
|
|
96
|
+
r.clear();
|
|
97
97
|
}
|
|
98
98
|
};
|
|
99
|
-
},
|
|
100
|
-
key: (
|
|
99
|
+
}, b = {
|
|
100
|
+
key: (r) => r.url,
|
|
101
101
|
persist: !1,
|
|
102
102
|
cacheTime: 1 / 0
|
|
103
|
-
},
|
|
104
|
-
value:
|
|
103
|
+
}, x = (r, e) => ({
|
|
104
|
+
value: r,
|
|
105
105
|
expiresAt: Date.now() + e
|
|
106
|
-
}),
|
|
107
|
-
const e = { ...
|
|
108
|
-
get(
|
|
109
|
-
return (...
|
|
110
|
-
const h = y[
|
|
111
|
-
if (
|
|
112
|
-
return console.log(`===> 已存在该请求: ${
|
|
113
|
-
const
|
|
114
|
-
if (
|
|
115
|
-
return
|
|
106
|
+
}), z = (r) => Date.now() > r.expiresAt, R = (r = {}) => {
|
|
107
|
+
const e = { ...b, ...r }, t = j(e.persist), { getPromise: s, setPromise: n, delPromise: o } = I(), l = {
|
|
108
|
+
get(c, i) {
|
|
109
|
+
return (...u) => {
|
|
110
|
+
const h = y[i](...u), a = e.key(h), m = s(a);
|
|
111
|
+
if (m)
|
|
112
|
+
return console.log(`===> 已存在该请求: ${a}`), m;
|
|
113
|
+
const f = t.get(a);
|
|
114
|
+
if (f && !z(f))
|
|
115
|
+
return f.value;
|
|
116
116
|
{
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
e.cacheTime({ config: h, response:
|
|
117
|
+
const v = Reflect.apply(c[i], c, u).then((d) => (typeof e.cacheTime == "number" ? t.set(a, x(d, e.cacheTime)) : t.set(
|
|
118
|
+
a,
|
|
119
|
+
x(
|
|
120
|
+
d,
|
|
121
|
+
e.cacheTime({ config: h, response: d })
|
|
122
122
|
)
|
|
123
|
-
), console.log("===>cache", "成功",
|
|
124
|
-
o(
|
|
123
|
+
), console.log("===>cache", "成功", a, d), d)).finally(() => {
|
|
124
|
+
o(a);
|
|
125
125
|
});
|
|
126
|
-
return a
|
|
126
|
+
return n(a, v), v;
|
|
127
127
|
}
|
|
128
128
|
};
|
|
129
129
|
}
|
|
130
130
|
};
|
|
131
|
-
return new Proxy(
|
|
132
|
-
},
|
|
133
|
-
const { method: e, url:
|
|
134
|
-
return [e,
|
|
135
|
-
},
|
|
136
|
-
key: (e) =>
|
|
131
|
+
return new Proxy(C(), l);
|
|
132
|
+
}, O = (r) => {
|
|
133
|
+
const { method: e, url: t, params: s, data: n } = r;
|
|
134
|
+
return [e, t, JSON.stringify(s), JSON.stringify(n)].join("|");
|
|
135
|
+
}, D = (r) => R({
|
|
136
|
+
key: (e) => r ? r(e) : O(e),
|
|
137
137
|
persist: !1
|
|
138
|
-
}),
|
|
138
|
+
}), _ = {
|
|
139
139
|
retries: 3,
|
|
140
|
-
delay:
|
|
140
|
+
delay: 0,
|
|
141
141
|
retryCondition: () => !0
|
|
142
|
-
},
|
|
143
|
-
const { retries: e, delay:
|
|
144
|
-
get(o,
|
|
145
|
-
return (...
|
|
146
|
-
let
|
|
147
|
-
const
|
|
148
|
-
if (
|
|
149
|
-
|
|
150
|
-
const h = typeof
|
|
151
|
-
return new Promise((
|
|
152
|
-
setTimeout(() =>
|
|
142
|
+
}, S = (r = {}) => {
|
|
143
|
+
const { retries: e, delay: t, retryCondition: s } = { ..._, ...r }, n = {
|
|
144
|
+
get(o, l) {
|
|
145
|
+
return (...c) => {
|
|
146
|
+
let i = 0;
|
|
147
|
+
const p = () => Reflect.apply(o[l], o, c).catch((u) => {
|
|
148
|
+
if (i < e && s(u)) {
|
|
149
|
+
i++;
|
|
150
|
+
const h = typeof t == "function" ? t(i) : t;
|
|
151
|
+
return new Promise((a) => {
|
|
152
|
+
setTimeout(() => a(p()), h);
|
|
153
153
|
});
|
|
154
154
|
}
|
|
155
|
-
return Promise.reject(
|
|
155
|
+
return Promise.reject(u);
|
|
156
156
|
});
|
|
157
|
-
return
|
|
157
|
+
return p();
|
|
158
158
|
};
|
|
159
159
|
}
|
|
160
160
|
};
|
|
161
|
-
return new Proxy(
|
|
162
|
-
}
|
|
161
|
+
return new Proxy(C(), n);
|
|
162
|
+
};
|
|
163
|
+
var A = Object.defineProperty, J = (r, e, t) => e in r ? A(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, q = (r, e, t) => J(r, typeof e != "symbol" ? e + "" : e, t);
|
|
164
|
+
class N {
|
|
165
|
+
// 尾部指针(最新加入的任务)
|
|
166
|
+
constructor(e) {
|
|
167
|
+
if (q(this, "map"), q(this, "head"), q(this, "tail"), this.map = /* @__PURE__ */ new Map(), this.head = null, this.tail = null, e)
|
|
168
|
+
for (const [t, s] of e)
|
|
169
|
+
this.enqueue(t, s);
|
|
170
|
+
}
|
|
171
|
+
// 添加任务(O(1))
|
|
172
|
+
enqueue(e, t) {
|
|
173
|
+
this.has(e) && this.remove(e);
|
|
174
|
+
const s = { id: e, data: t, next: null, prev: this.tail };
|
|
175
|
+
this.tail ? this.tail.next = s : this.head = s, this.tail = s, this.map.set(e, s);
|
|
176
|
+
}
|
|
177
|
+
// 取出最早的任务(O(1))
|
|
178
|
+
dequeue() {
|
|
179
|
+
if (!this.head) return null;
|
|
180
|
+
const e = this.head;
|
|
181
|
+
return this.head = e.next, this.head ? this.head.prev = null : this.tail = null, this.map.delete(e.id), e.data;
|
|
182
|
+
}
|
|
183
|
+
// 通过 ID 删除任务(O(1))
|
|
184
|
+
remove(e) {
|
|
185
|
+
const t = this.map.get(e);
|
|
186
|
+
return t ? (t.prev ? t.prev.next = t.next : this.head = t.next, t.next ? t.next.prev = t.prev : this.tail = t.prev, this.map.delete(e), !0) : !1;
|
|
187
|
+
}
|
|
188
|
+
// 通过 ID 获取任务(O(1))
|
|
189
|
+
getTask(e) {
|
|
190
|
+
var t;
|
|
191
|
+
return (t = this.map.get(e)) == null ? void 0 : t.data;
|
|
192
|
+
}
|
|
193
|
+
// 通过id更新任务
|
|
194
|
+
update(e, t) {
|
|
195
|
+
const s = this.map.get(e);
|
|
196
|
+
return s ? (s.data = t, !0) : !1;
|
|
197
|
+
}
|
|
198
|
+
has(e) {
|
|
199
|
+
return this.map.has(e);
|
|
200
|
+
}
|
|
201
|
+
peek() {
|
|
202
|
+
var e;
|
|
203
|
+
return ((e = this.head) == null ? void 0 : e.data) ?? null;
|
|
204
|
+
}
|
|
205
|
+
clear() {
|
|
206
|
+
this.map.clear(), this.head = null, this.tail = null;
|
|
207
|
+
}
|
|
208
|
+
// 获取当前队列大小
|
|
209
|
+
get size() {
|
|
210
|
+
return this.map.size;
|
|
211
|
+
}
|
|
212
|
+
get queue() {
|
|
213
|
+
const e = [];
|
|
214
|
+
let t = this.head;
|
|
215
|
+
for (; t; )
|
|
216
|
+
e.push(t.data), t = t.next;
|
|
217
|
+
return e;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
class $ {
|
|
221
|
+
constructor(e = 4) {
|
|
222
|
+
this.parallelCount = e, this.tasks = new N(), this.runningCount = 0;
|
|
223
|
+
}
|
|
224
|
+
// 加入
|
|
225
|
+
add(e, t) {
|
|
226
|
+
return console.log("poolinset", e, t), new Promise((s, n) => {
|
|
227
|
+
this.tasks.enqueue(e, {
|
|
228
|
+
task: t,
|
|
229
|
+
resolve: s,
|
|
230
|
+
reject: n
|
|
231
|
+
}), this._run();
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
// 删除
|
|
235
|
+
remove(e) {
|
|
236
|
+
this.tasks.remove(e);
|
|
237
|
+
}
|
|
238
|
+
execute(e) {
|
|
239
|
+
const { task: t, resolve: s, reject: n } = e;
|
|
240
|
+
return t().then(s).catch(n).finally(() => {
|
|
241
|
+
this.runningCount--, this._run();
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
_run() {
|
|
245
|
+
for (; this.runningCount < this.parallelCount && this.tasks.size > 0; ) {
|
|
246
|
+
const e = this.tasks.dequeue();
|
|
247
|
+
this.runningCount++, this.execute(e);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
const H = {
|
|
252
|
+
parallelCount: 4,
|
|
253
|
+
retries: 0,
|
|
254
|
+
createId: () => `${Date.now()}_${Math.random().toString().slice(2, 8)}`
|
|
255
|
+
}, F = (r) => {
|
|
256
|
+
const e = { ...H, ...r }, { parallelCount: t, createId: s, ...n } = e, o = new $(t), l = n.retries > 0 ? S(n) : null, g = {
|
|
257
|
+
get(c, i) {
|
|
258
|
+
return (...u) => {
|
|
259
|
+
const h = y[i](...u), a = s(h), m = () => l ? l.request(h) : Reflect.apply(c[i], c, u);
|
|
260
|
+
return o.add(a, m);
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
return {
|
|
265
|
+
requestor: new Proxy(C(), g),
|
|
266
|
+
concurrentPool: o
|
|
267
|
+
};
|
|
268
|
+
}, U = {
|
|
163
269
|
cacheRequestor: R,
|
|
164
|
-
idempotencyRequestor:
|
|
165
|
-
retryRequestor:
|
|
270
|
+
idempotencyRequestor: D,
|
|
271
|
+
retryRequestor: S,
|
|
272
|
+
concurrentPoolRequestor: F
|
|
166
273
|
};
|
|
167
274
|
export {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
275
|
+
L as inject,
|
|
276
|
+
U as requestExtender,
|
|
277
|
+
C as useRequestor
|
|
171
278
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(c
|
|
1
|
+
(function(u,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(u=typeof globalThis<"u"?globalThis:u||self,c(u["net-vert/core"]={}))})(this,function(u){"use strict";const c={get:(r,e)=>({url:r,method:"get",...e,params:e==null?void 0:e.params}),post:(r,e,t)=>({url:r,method:"post",data:e,headers:{"Content-Type":"application/json",...t==null?void 0:t.headers},...t}),delete:(r,e)=>({url:r,method:"delete",...e}),put:(r,e,t)=>({url:r,method:"put",data:e,headers:{"Content-Type":"application/json",...t==null?void 0:t.headers},...t}),request:r=>r};function T(r){const e={};return Object.keys(c).forEach(t=>{e[t]=(...s)=>{const n=c[t](...s);return r(n)}}),{...e,request:r}}const P="default",x=new Map,k=(r,e=P)=>{x.set(e,T(r))},f=(r=P)=>{const e=x.get(r);if(!e)throw new Error(`Requestor实例 ${r} 未注册`);return e};class j{has(e){return!!localStorage.getItem(e)}get(e){const t=localStorage.getItem(e);if(t)try{return JSON.parse(t)}catch(s){console.error("Error parsing cached data",s);return}}set(e,t){try{const s=JSON.stringify(t);localStorage.setItem(e,s)}catch(s){console.error("Error saving data to localStorage",s)}return t}delete(e){localStorage.removeItem(e)}clear(){localStorage.clear()}}const b=new j,E=new Map,I=r=>r?b:E,O=()=>{const r=new Map;return{getPromise:o=>r.get(o),setPromise:(o,l)=>{r.set(o,l)},delPromise:o=>{r.delete(o)},clearCache:()=>{r.clear()}}},z={key:r=>r.url,persist:!1,cacheTime:1/0},w=(r,e)=>({value:r,expiresAt:Date.now()+e}),D=r=>Date.now()>r.expiresAt,R=(r={})=>{const e={...z,...r},t=I(e.persist),{getPromise:s,setPromise:n,delPromise:o}=O(),l={get(d,i){return(...h)=>{const p=c[i](...h),a=e.key(p),g=s(a);if(g)return console.log(`===> 已存在该请求: ${a}`),g;const v=t.get(a);if(v&&!D(v))return v.value;{const M=Reflect.apply(d[i],d,h).then(m=>(typeof e.cacheTime=="number"?t.set(a,w(m,e.cacheTime)):t.set(a,w(m,e.cacheTime({config:p,response:m}))),console.log("===>cache","成功",a,m),m)).finally(()=>{o(a)});return n(a,M),M}}}};return new Proxy(f(),l)},_=r=>{const{method:e,url:t,params:s,data:n}=r;return[e,t,JSON.stringify(s),JSON.stringify(n)].join("|")},A=r=>R({key:e=>r?r(e):_(e),persist:!1}),J={retries:3,delay:0,retryCondition:()=>!0},S=(r={})=>{const{retries:e,delay:t,retryCondition:s}={...J,...r},n={get(o,l){return(...d)=>{let i=0;const y=()=>Reflect.apply(o[l],o,d).catch(h=>{if(i<e&&s(h)){i++;const p=typeof t=="function"?t(i):t;return new Promise(a=>{setTimeout(()=>a(y()),p)})}return Promise.reject(h)});return y()}}};return new Proxy(f(),n)};var N=Object.defineProperty,$=(r,e,t)=>e in r?N(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,q=(r,e,t)=>$(r,typeof e!="symbol"?e+"":e,t);class H{constructor(e){if(q(this,"map"),q(this,"head"),q(this,"tail"),this.map=new Map,this.head=null,this.tail=null,e)for(const[t,s]of e)this.enqueue(t,s)}enqueue(e,t){this.has(e)&&this.remove(e);const s={id:e,data:t,next:null,prev:this.tail};this.tail?this.tail.next=s:this.head=s,this.tail=s,this.map.set(e,s)}dequeue(){if(!this.head)return null;const e=this.head;return this.head=e.next,this.head?this.head.prev=null:this.tail=null,this.map.delete(e.id),e.data}remove(e){const t=this.map.get(e);return t?(t.prev?t.prev.next=t.next:this.head=t.next,t.next?t.next.prev=t.prev:this.tail=t.prev,this.map.delete(e),!0):!1}getTask(e){var t;return(t=this.map.get(e))==null?void 0:t.data}update(e,t){const s=this.map.get(e);return s?(s.data=t,!0):!1}has(e){return this.map.has(e)}peek(){var e;return((e=this.head)==null?void 0:e.data)??null}clear(){this.map.clear(),this.head=null,this.tail=null}get size(){return this.map.size}get queue(){const e=[];let t=this.head;for(;t;)e.push(t.data),t=t.next;return e}}class F{constructor(e=4){this.parallelCount=e,this.tasks=new H,this.runningCount=0}add(e,t){return console.log("poolinset",e,t),new Promise((s,n)=>{this.tasks.enqueue(e,{task:t,resolve:s,reject:n}),this._run()})}remove(e){this.tasks.remove(e)}execute(e){const{task:t,resolve:s,reject:n}=e;return t().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 L={parallelCount:4,retries:0,createId:()=>`${Date.now()}_${Math.random().toString().slice(2,8)}`},U={cacheRequestor:R,idempotencyRequestor:A,retryRequestor:S,concurrentPoolRequestor:r=>{const e={...L,...r},{parallelCount:t,createId:s,...n}=e,o=new F(t),l=n.retries>0?S(n):null,C={get(d,i){return(...h)=>{const p=c[i](...h),a=s(p),g=()=>l?l.request(p):Reflect.apply(d[i],d,h);return o.add(a,g)}}};return{requestor:new Proxy(f(),C),concurrentPool:o}}};u.inject=k,u.requestExtender=U,u.useRequestor=f,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@net-vert/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"description": "Dependency Inversion Network Library with Type-Safe Injection.",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"type": "module",
|
|
@@ -25,7 +25,11 @@
|
|
|
25
25
|
"publishConfig": {
|
|
26
26
|
"access": "public"
|
|
27
27
|
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"id-queue": "^1.0.9"
|
|
30
|
+
},
|
|
28
31
|
"scripts": {
|
|
32
|
+
"test": "vitest",
|
|
29
33
|
"build": "run-p type-check build-only",
|
|
30
34
|
"build-only": "vite build",
|
|
31
35
|
"type-check": "tsc --noEmit"
|