@taicode/common-base 1.7.0 → 1.7.2
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/output/batching-buffer/batching-buffer.d.ts +133 -0
- package/output/batching-buffer/batching-buffer.d.ts.map +1 -0
- package/output/batching-buffer/batching-buffer.js +264 -0
- package/output/batching-buffer/batching-buffer.test.d.ts +2 -0
- package/output/batching-buffer/batching-buffer.test.d.ts.map +1 -0
- package/output/batching-buffer/batching-buffer.test.js +358 -0
- package/output/batching-buffer/index.d.ts +3 -0
- package/output/batching-buffer/index.d.ts.map +1 -0
- package/output/batching-buffer/index.js +1 -0
- package/output/index.d.ts +3 -0
- package/output/index.d.ts.map +1 -1
- package/output/index.js +4 -0
- package/output/logger/logger.d.ts +3 -3
- package/output/logger/logger.d.ts.map +1 -1
- package/output/logger/logger.js +2 -3
- package/output/logger/logger.test.js +1 -1
- package/output/ttl-cache/index.d.ts +2 -0
- package/output/ttl-cache/index.d.ts.map +1 -0
- package/output/ttl-cache/index.js +1 -0
- package/output/ttl-cache/ttl-cache.d.ts +148 -0
- package/output/ttl-cache/ttl-cache.d.ts.map +1 -0
- package/output/ttl-cache/ttl-cache.js +313 -0
- package/output/ttl-cache/ttl-cache.test.d.ts +2 -0
- package/output/ttl-cache/ttl-cache.test.d.ts.map +1 -0
- package/output/ttl-cache/ttl-cache.test.js +222 -0
- package/package.json +1 -1
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 批处理缓冲区配置选项
|
|
3
|
+
*/
|
|
4
|
+
export interface BatchingBufferOptions<T> {
|
|
5
|
+
/**
|
|
6
|
+
* 批处理模式:批量处理器
|
|
7
|
+
*/
|
|
8
|
+
batchProcessor?: (items: T[]) => Promise<void> | void;
|
|
9
|
+
/**
|
|
10
|
+
* 单个处理模式:单个处理器
|
|
11
|
+
*/
|
|
12
|
+
itemProcessor?: (item: T) => Promise<void> | void;
|
|
13
|
+
/**
|
|
14
|
+
* 缓冲区大小限制,达到此数量时触发处理
|
|
15
|
+
* @default 100
|
|
16
|
+
*/
|
|
17
|
+
bufferSize?: number;
|
|
18
|
+
/**
|
|
19
|
+
* 时间间隔(毫秒),距离上次处理超过此时间时触发处理
|
|
20
|
+
* @default 5000
|
|
21
|
+
*/
|
|
22
|
+
flushInterval?: number;
|
|
23
|
+
/**
|
|
24
|
+
* 是否在添加项目时立即启动定时器
|
|
25
|
+
* @default true
|
|
26
|
+
*/
|
|
27
|
+
autoFlush?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 批处理缓冲区
|
|
31
|
+
*
|
|
32
|
+
* 提供数据暂存和批量/单个处理功能的管理对象。
|
|
33
|
+
* 当缓冲区达到指定数量或距离上次处理达到一定时间时,
|
|
34
|
+
* 自动调用处理方法进行数据消费。
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* // 批量处理模式
|
|
39
|
+
* const buffer = new BatchingBuffer({
|
|
40
|
+
* batchProcessor: async (items) => {
|
|
41
|
+
* console.log('批量处理:', items)
|
|
42
|
+
* await saveToDatabase(items)
|
|
43
|
+
* },
|
|
44
|
+
* bufferSize: 10,
|
|
45
|
+
* flushInterval: 5000
|
|
46
|
+
* })
|
|
47
|
+
*
|
|
48
|
+
* // 单个处理模式
|
|
49
|
+
* const buffer = new BatchingBuffer({
|
|
50
|
+
* itemProcessor: async (item) => {
|
|
51
|
+
* console.log('单个处理:', item)
|
|
52
|
+
* await saveToDatabase([item])
|
|
53
|
+
* },
|
|
54
|
+
* bufferSize: 1,
|
|
55
|
+
* flushInterval: 1000
|
|
56
|
+
* })
|
|
57
|
+
*
|
|
58
|
+
* // 添加数据
|
|
59
|
+
* buffer.add('data1')
|
|
60
|
+
* buffer.add('data2')
|
|
61
|
+
*
|
|
62
|
+
* // 手动刷新
|
|
63
|
+
* await buffer.flush()
|
|
64
|
+
*
|
|
65
|
+
* // 销毁
|
|
66
|
+
* buffer.destroy()
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare class BatchingBuffer<T> {
|
|
70
|
+
private buffer;
|
|
71
|
+
private timer;
|
|
72
|
+
private isDestroyed;
|
|
73
|
+
private lastFlushTime;
|
|
74
|
+
private readonly batchProcessor?;
|
|
75
|
+
private readonly itemProcessor?;
|
|
76
|
+
private readonly bufferSize;
|
|
77
|
+
private readonly flushInterval;
|
|
78
|
+
private readonly autoFlush;
|
|
79
|
+
constructor(options: BatchingBufferOptions<T>);
|
|
80
|
+
/**
|
|
81
|
+
* 添加数据到缓冲区
|
|
82
|
+
* @param item 要添加的数据项
|
|
83
|
+
*/
|
|
84
|
+
add(item: T): void;
|
|
85
|
+
/**
|
|
86
|
+
* 批量添加数据到缓冲区
|
|
87
|
+
* @param items 要添加的数据项数组
|
|
88
|
+
*/
|
|
89
|
+
addBatch(items: T[]): void;
|
|
90
|
+
/**
|
|
91
|
+
* 手动刷新缓冲区,处理所有待处理的数据
|
|
92
|
+
* @returns Promise,在处理完成后 resolve
|
|
93
|
+
*/
|
|
94
|
+
flush(): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* 获取当前缓冲区中的数据数量
|
|
97
|
+
*/
|
|
98
|
+
get size(): number;
|
|
99
|
+
/**
|
|
100
|
+
* 检查缓冲区是否为空
|
|
101
|
+
*/
|
|
102
|
+
get isEmpty(): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* 检查是否已被销毁
|
|
105
|
+
*/
|
|
106
|
+
get destroyed(): boolean;
|
|
107
|
+
/**
|
|
108
|
+
* 获取距离上次刷新的时间(毫秒)
|
|
109
|
+
*/
|
|
110
|
+
get timeSinceLastFlush(): number;
|
|
111
|
+
/**
|
|
112
|
+
* 销毁缓冲区,清理定时器并处理剩余数据
|
|
113
|
+
* @param flushRemaining 是否处理剩余数据,默认为 true
|
|
114
|
+
*/
|
|
115
|
+
destroy(flushRemaining?: boolean): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* 重置定时器
|
|
118
|
+
*/
|
|
119
|
+
private resetTimer;
|
|
120
|
+
/**
|
|
121
|
+
* 清除定时器
|
|
122
|
+
*/
|
|
123
|
+
private clearTimer;
|
|
124
|
+
/**
|
|
125
|
+
* 同步处理缓冲区(用于基于大小的触发)
|
|
126
|
+
*/
|
|
127
|
+
private processBufferSync;
|
|
128
|
+
/**
|
|
129
|
+
* 异步处理缓冲区(用于手动刷新和销毁)
|
|
130
|
+
*/
|
|
131
|
+
private processBufferAsync;
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=batching-buffer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"batching-buffer.d.ts","sourceRoot":"","sources":["../../source/batching-buffer/batching-buffer.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,qBAAqB,CAAC,CAAC;IACtC;;OAEG;IACH,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IAErD;;OAEG;IACH,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IAEjD;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IAEnB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAA;IAEtB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,qBAAa,cAAc,CAAC,CAAC;IAC3B,OAAO,CAAC,MAAM,CAAU;IACxB,OAAO,CAAC,KAAK,CAAmB;IAChC,OAAO,CAAC,WAAW,CAAiB;IACpC,OAAO,CAAC,aAAa,CAAqB;IAC1C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAsC;IACtE,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAmC;IAClE,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAQ;IACnC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAQ;IACtC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;gBAEvB,OAAO,EAAE,qBAAqB,CAAC,CAAC,CAAC;IA0B7C;;;OAGG;IACH,GAAG,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI;IAmBlB;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI;IAyB1B;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;OAEG;IACH,IAAI,OAAO,IAAI,OAAO,CAErB;IAED;;OAEG;IACH,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED;;OAEG;IACH,IAAI,kBAAkB,IAAI,MAAM,CAE/B;IAED;;;OAGG;IACG,OAAO,CAAC,cAAc,GAAE,OAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAgB5D;;OAEG;IACH,OAAO,CAAC,UAAU;IAQlB;;OAEG;IACH,OAAO,CAAC,UAAU;IAOlB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAiDzB;;OAEG;YACW,kBAAkB;CA2BjC"}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 批处理缓冲区
|
|
3
|
+
*
|
|
4
|
+
* 提供数据暂存和批量/单个处理功能的管理对象。
|
|
5
|
+
* 当缓冲区达到指定数量或距离上次处理达到一定时间时,
|
|
6
|
+
* 自动调用处理方法进行数据消费。
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // 批量处理模式
|
|
11
|
+
* const buffer = new BatchingBuffer({
|
|
12
|
+
* batchProcessor: async (items) => {
|
|
13
|
+
* console.log('批量处理:', items)
|
|
14
|
+
* await saveToDatabase(items)
|
|
15
|
+
* },
|
|
16
|
+
* bufferSize: 10,
|
|
17
|
+
* flushInterval: 5000
|
|
18
|
+
* })
|
|
19
|
+
*
|
|
20
|
+
* // 单个处理模式
|
|
21
|
+
* const buffer = new BatchingBuffer({
|
|
22
|
+
* itemProcessor: async (item) => {
|
|
23
|
+
* console.log('单个处理:', item)
|
|
24
|
+
* await saveToDatabase([item])
|
|
25
|
+
* },
|
|
26
|
+
* bufferSize: 1,
|
|
27
|
+
* flushInterval: 1000
|
|
28
|
+
* })
|
|
29
|
+
*
|
|
30
|
+
* // 添加数据
|
|
31
|
+
* buffer.add('data1')
|
|
32
|
+
* buffer.add('data2')
|
|
33
|
+
*
|
|
34
|
+
* // 手动刷新
|
|
35
|
+
* await buffer.flush()
|
|
36
|
+
*
|
|
37
|
+
* // 销毁
|
|
38
|
+
* buffer.destroy()
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export class BatchingBuffer {
|
|
42
|
+
buffer = [];
|
|
43
|
+
timer = null;
|
|
44
|
+
isDestroyed = false;
|
|
45
|
+
lastFlushTime = Date.now();
|
|
46
|
+
batchProcessor;
|
|
47
|
+
itemProcessor;
|
|
48
|
+
bufferSize;
|
|
49
|
+
flushInterval;
|
|
50
|
+
autoFlush;
|
|
51
|
+
constructor(options) {
|
|
52
|
+
// 验证处理器配置
|
|
53
|
+
if (!options.batchProcessor && !options.itemProcessor) {
|
|
54
|
+
throw new Error('必须提供 batchProcessor 或 itemProcessor 其中之一');
|
|
55
|
+
}
|
|
56
|
+
if (options.batchProcessor && options.itemProcessor) {
|
|
57
|
+
throw new Error('只能选择 batchProcessor 或 itemProcessor 中的一个');
|
|
58
|
+
}
|
|
59
|
+
this.batchProcessor = options.batchProcessor;
|
|
60
|
+
this.itemProcessor = options.itemProcessor;
|
|
61
|
+
this.bufferSize = options.bufferSize ?? 100;
|
|
62
|
+
this.flushInterval = options.flushInterval ?? 5000;
|
|
63
|
+
this.autoFlush = options.autoFlush ?? true;
|
|
64
|
+
// 验证配置参数
|
|
65
|
+
if (this.bufferSize <= 0) {
|
|
66
|
+
throw new Error('bufferSize 必须大于 0');
|
|
67
|
+
}
|
|
68
|
+
if (this.flushInterval <= 0) {
|
|
69
|
+
throw new Error('flushInterval 必须大于 0');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 添加数据到缓冲区
|
|
74
|
+
* @param item 要添加的数据项
|
|
75
|
+
*/
|
|
76
|
+
add(item) {
|
|
77
|
+
if (this.isDestroyed) {
|
|
78
|
+
throw new Error('BatchingBuffer 已被销毁,不能添加新数据');
|
|
79
|
+
}
|
|
80
|
+
this.buffer.push(item);
|
|
81
|
+
// 检查是否需要基于大小触发处理
|
|
82
|
+
if (this.buffer.length >= this.bufferSize) {
|
|
83
|
+
this.processBufferSync();
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
// 启动或重置定时器
|
|
87
|
+
if (this.autoFlush) {
|
|
88
|
+
this.resetTimer();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 批量添加数据到缓冲区
|
|
93
|
+
* @param items 要添加的数据项数组
|
|
94
|
+
*/
|
|
95
|
+
addBatch(items) {
|
|
96
|
+
if (this.isDestroyed) {
|
|
97
|
+
throw new Error('BatchingBuffer 已被销毁,不能添加新数据');
|
|
98
|
+
}
|
|
99
|
+
let i = 0;
|
|
100
|
+
while (i < items.length) {
|
|
101
|
+
// 填充缓冲区直到达到大小限制
|
|
102
|
+
while (i < items.length && this.buffer.length < this.bufferSize) {
|
|
103
|
+
this.buffer.push(items[i]);
|
|
104
|
+
i++;
|
|
105
|
+
}
|
|
106
|
+
// 如果缓冲区满了,处理它
|
|
107
|
+
if (this.buffer.length >= this.bufferSize) {
|
|
108
|
+
this.processBufferSync();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// 处理剩余的项目(如果有的话)
|
|
112
|
+
if (this.buffer.length > 0) {
|
|
113
|
+
this.processBufferSync();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 手动刷新缓冲区,处理所有待处理的数据
|
|
118
|
+
* @returns Promise,在处理完成后 resolve
|
|
119
|
+
*/
|
|
120
|
+
async flush() {
|
|
121
|
+
return this.processBufferAsync();
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* 获取当前缓冲区中的数据数量
|
|
125
|
+
*/
|
|
126
|
+
get size() {
|
|
127
|
+
return this.buffer.length;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 检查缓冲区是否为空
|
|
131
|
+
*/
|
|
132
|
+
get isEmpty() {
|
|
133
|
+
return this.buffer.length === 0;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* 检查是否已被销毁
|
|
137
|
+
*/
|
|
138
|
+
get destroyed() {
|
|
139
|
+
return this.isDestroyed;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 获取距离上次刷新的时间(毫秒)
|
|
143
|
+
*/
|
|
144
|
+
get timeSinceLastFlush() {
|
|
145
|
+
return Date.now() - this.lastFlushTime;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* 销毁缓冲区,清理定时器并处理剩余数据
|
|
149
|
+
* @param flushRemaining 是否处理剩余数据,默认为 true
|
|
150
|
+
*/
|
|
151
|
+
async destroy(flushRemaining = true) {
|
|
152
|
+
if (this.isDestroyed) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
this.clearTimer();
|
|
156
|
+
if (flushRemaining && this.buffer.length > 0) {
|
|
157
|
+
await this.processBufferAsync();
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
this.buffer = [];
|
|
161
|
+
}
|
|
162
|
+
this.isDestroyed = true;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* 重置定时器
|
|
166
|
+
*/
|
|
167
|
+
resetTimer() {
|
|
168
|
+
this.clearTimer();
|
|
169
|
+
this.timer = setTimeout(() => {
|
|
170
|
+
this.processBufferSync();
|
|
171
|
+
}, this.flushInterval);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 清除定时器
|
|
175
|
+
*/
|
|
176
|
+
clearTimer() {
|
|
177
|
+
if (this.timer) {
|
|
178
|
+
clearTimeout(this.timer);
|
|
179
|
+
this.timer = null;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* 同步处理缓冲区(用于基于大小的触发)
|
|
184
|
+
*/
|
|
185
|
+
processBufferSync() {
|
|
186
|
+
if (this.buffer.length === 0 || this.isDestroyed) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
this.clearTimer();
|
|
190
|
+
const itemsToProcess = this.buffer.splice(0);
|
|
191
|
+
try {
|
|
192
|
+
if (this.batchProcessor) {
|
|
193
|
+
// 批量处理模式
|
|
194
|
+
const result = this.batchProcessor(itemsToProcess);
|
|
195
|
+
// 如果返回 Promise,处理异步
|
|
196
|
+
if (result && typeof result.then === 'function') {
|
|
197
|
+
result.catch(() => {
|
|
198
|
+
// 处理失败时,将数据重新放回缓冲区开头
|
|
199
|
+
this.buffer.unshift(...itemsToProcess);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
else if (this.itemProcessor) {
|
|
204
|
+
// 单个处理模式 - 同步处理每个项目
|
|
205
|
+
for (let i = 0; i < itemsToProcess.length; i++) {
|
|
206
|
+
const item = itemsToProcess[i];
|
|
207
|
+
try {
|
|
208
|
+
const result = this.itemProcessor(item);
|
|
209
|
+
// 如果返回 Promise,处理异步
|
|
210
|
+
if (result && typeof result.then === 'function') {
|
|
211
|
+
result.catch(() => {
|
|
212
|
+
// 处理失败时,将数据重新放回缓冲区开头
|
|
213
|
+
this.buffer.unshift(item);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
// 同步错误处理 - 只把未处理的项目放回缓冲区
|
|
219
|
+
this.buffer.unshift(...itemsToProcess.slice(i));
|
|
220
|
+
throw error;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
this.lastFlushTime = Date.now();
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
// 批量处理模式的错误处理
|
|
228
|
+
if (this.batchProcessor) {
|
|
229
|
+
this.buffer.unshift(...itemsToProcess);
|
|
230
|
+
}
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* 异步处理缓冲区(用于手动刷新和销毁)
|
|
236
|
+
*/
|
|
237
|
+
async processBufferAsync() {
|
|
238
|
+
if (this.buffer.length === 0) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
this.clearTimer();
|
|
242
|
+
const itemsToProcess = this.buffer.splice(0);
|
|
243
|
+
try {
|
|
244
|
+
if (this.batchProcessor) {
|
|
245
|
+
// 批量处理模式
|
|
246
|
+
await this.batchProcessor(itemsToProcess);
|
|
247
|
+
}
|
|
248
|
+
else if (this.itemProcessor) {
|
|
249
|
+
// 单个处理模式
|
|
250
|
+
for (const item of itemsToProcess) {
|
|
251
|
+
await this.itemProcessor(item);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
this.lastFlushTime = Date.now();
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
// 处理失败时,将数据重新放回缓冲区开头(如果没有被销毁)
|
|
258
|
+
if (!this.isDestroyed) {
|
|
259
|
+
this.buffer.unshift(...itemsToProcess);
|
|
260
|
+
}
|
|
261
|
+
throw error;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"batching-buffer.test.d.ts","sourceRoot":"","sources":["../../source/batching-buffer/batching-buffer.test.ts"],"names":[],"mappings":""}
|