@taicode/common-base 3.7.4 → 3.7.7

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.
Files changed (167) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +7 -8
  3. package/output/batching-buffer/batching-buffer.d.ts +0 -133
  4. package/output/batching-buffer/batching-buffer.d.ts.map +0 -1
  5. package/output/batching-buffer/batching-buffer.js +0 -264
  6. package/output/batching-buffer/batching-buffer.test.d.ts +0 -2
  7. package/output/batching-buffer/batching-buffer.test.d.ts.map +0 -1
  8. package/output/batching-buffer/batching-buffer.test.js +0 -358
  9. package/output/batching-buffer/index.d.ts +0 -3
  10. package/output/batching-buffer/index.d.ts.map +0 -1
  11. package/output/batching-buffer/index.js +0 -1
  12. package/output/catch/catch.d.ts +0 -46
  13. package/output/catch/catch.d.ts.map +0 -1
  14. package/output/catch/catch.js +0 -48
  15. package/output/catch/catch.test.d.ts +0 -2
  16. package/output/catch/catch.test.d.ts.map +0 -1
  17. package/output/catch/catch.test.js +0 -43
  18. package/output/catch/index.d.ts +0 -2
  19. package/output/catch/index.d.ts.map +0 -1
  20. package/output/catch/index.js +0 -1
  21. package/output/color/color.d.ts +0 -6
  22. package/output/color/color.d.ts.map +0 -1
  23. package/output/color/color.js +0 -56
  24. package/output/color/color.test.d.ts +0 -2
  25. package/output/color/color.test.d.ts.map +0 -1
  26. package/output/color/color.test.js +0 -110
  27. package/output/color/index.d.ts +0 -2
  28. package/output/color/index.d.ts.map +0 -1
  29. package/output/color/index.js +0 -1
  30. package/output/debounce/debounce.d.ts +0 -7
  31. package/output/debounce/debounce.d.ts.map +0 -1
  32. package/output/debounce/debounce.js +0 -36
  33. package/output/debounce/debounce.test.d.ts +0 -2
  34. package/output/debounce/debounce.test.d.ts.map +0 -1
  35. package/output/debounce/debounce.test.js +0 -135
  36. package/output/debounce/index.d.ts +0 -2
  37. package/output/debounce/index.d.ts.map +0 -1
  38. package/output/debounce/index.js +0 -1
  39. package/output/disposer/disposer.d.ts +0 -8
  40. package/output/disposer/disposer.d.ts.map +0 -1
  41. package/output/disposer/disposer.js +0 -21
  42. package/output/disposer/disposer.test.d.ts +0 -2
  43. package/output/disposer/disposer.test.d.ts.map +0 -1
  44. package/output/disposer/disposer.test.js +0 -216
  45. package/output/disposer/index.d.ts +0 -2
  46. package/output/disposer/index.d.ts.map +0 -1
  47. package/output/disposer/index.js +0 -1
  48. package/output/error/error.d.ts +0 -128
  49. package/output/error/error.d.ts.map +0 -1
  50. package/output/error/error.js +0 -141
  51. package/output/error/error.test.d.ts +0 -2
  52. package/output/error/error.test.d.ts.map +0 -1
  53. package/output/error/error.test.js +0 -420
  54. package/output/error/index.d.ts +0 -3
  55. package/output/error/index.d.ts.map +0 -1
  56. package/output/error/index.js +0 -1
  57. package/output/event-emitter/event-emitter.d.ts +0 -33
  58. package/output/event-emitter/event-emitter.d.ts.map +0 -1
  59. package/output/event-emitter/event-emitter.js +0 -66
  60. package/output/event-emitter/event-emitter.test.d.ts +0 -2
  61. package/output/event-emitter/event-emitter.test.d.ts.map +0 -1
  62. package/output/event-emitter/event-emitter.test.js +0 -177
  63. package/output/event-emitter/index.d.ts +0 -2
  64. package/output/event-emitter/index.d.ts.map +0 -1
  65. package/output/event-emitter/index.js +0 -1
  66. package/output/flow-queue/flow-queue.d.ts +0 -486
  67. package/output/flow-queue/flow-queue.d.ts.map +0 -1
  68. package/output/flow-queue/flow-queue.js +0 -654
  69. package/output/flow-queue/flow-queue.test.d.ts +0 -2
  70. package/output/flow-queue/flow-queue.test.d.ts.map +0 -1
  71. package/output/flow-queue/flow-queue.test.js +0 -1205
  72. package/output/flow-queue/index.d.ts +0 -3
  73. package/output/flow-queue/index.d.ts.map +0 -1
  74. package/output/flow-queue/index.js +0 -3
  75. package/output/index.d.ts +0 -18
  76. package/output/index.d.ts.map +0 -1
  77. package/output/index.js +0 -26
  78. package/output/logger/formatter.d.ts +0 -70
  79. package/output/logger/formatter.d.ts.map +0 -1
  80. package/output/logger/formatter.js +0 -206
  81. package/output/logger/formatter.test.d.ts +0 -2
  82. package/output/logger/formatter.test.d.ts.map +0 -1
  83. package/output/logger/formatter.test.js +0 -296
  84. package/output/logger/index.d.ts +0 -4
  85. package/output/logger/index.d.ts.map +0 -1
  86. package/output/logger/index.js +0 -3
  87. package/output/logger/logger.d.ts +0 -64
  88. package/output/logger/logger.d.ts.map +0 -1
  89. package/output/logger/logger.js +0 -179
  90. package/output/logger/logger.test.d.ts +0 -2
  91. package/output/logger/logger.test.d.ts.map +0 -1
  92. package/output/logger/logger.test.js +0 -299
  93. package/output/logger/transport.d.ts +0 -124
  94. package/output/logger/transport.d.ts.map +0 -1
  95. package/output/logger/transport.js +0 -153
  96. package/output/logger/transport.test.d.ts +0 -2
  97. package/output/logger/transport.test.d.ts.map +0 -1
  98. package/output/logger/transport.test.js +0 -309
  99. package/output/lru-cache/index.d.ts +0 -2
  100. package/output/lru-cache/index.d.ts.map +0 -1
  101. package/output/lru-cache/index.js +0 -1
  102. package/output/lru-cache/lru-cache.d.ts +0 -22
  103. package/output/lru-cache/lru-cache.d.ts.map +0 -1
  104. package/output/lru-cache/lru-cache.js +0 -147
  105. package/output/lru-cache/lru-cache.test.d.ts +0 -2
  106. package/output/lru-cache/lru-cache.test.d.ts.map +0 -1
  107. package/output/lru-cache/lru-cache.test.js +0 -239
  108. package/output/number/index.d.ts +0 -2
  109. package/output/number/index.d.ts.map +0 -1
  110. package/output/number/index.js +0 -1
  111. package/output/number/number.d.ts +0 -2
  112. package/output/number/number.d.ts.map +0 -1
  113. package/output/number/number.js +0 -23
  114. package/output/number/number.test.d.ts +0 -2
  115. package/output/number/number.test.d.ts.map +0 -1
  116. package/output/number/number.test.js +0 -50
  117. package/output/object/index.d.ts +0 -2
  118. package/output/object/index.d.ts.map +0 -1
  119. package/output/object/index.js +0 -1
  120. package/output/object/object.d.ts +0 -7
  121. package/output/object/object.d.ts.map +0 -1
  122. package/output/object/object.js +0 -27
  123. package/output/object/object.test.d.ts +0 -2
  124. package/output/object/object.test.d.ts.map +0 -1
  125. package/output/object/object.test.js +0 -42
  126. package/output/ring-cache/index.d.ts +0 -6
  127. package/output/ring-cache/index.d.ts.map +0 -1
  128. package/output/ring-cache/index.js +0 -5
  129. package/output/ring-cache/ring-cache.d.ts +0 -125
  130. package/output/ring-cache/ring-cache.d.ts.map +0 -1
  131. package/output/ring-cache/ring-cache.js +0 -197
  132. package/output/ring-cache/ring-cache.test.d.ts +0 -2
  133. package/output/ring-cache/ring-cache.test.d.ts.map +0 -1
  134. package/output/ring-cache/ring-cache.test.js +0 -196
  135. package/output/service/index.d.ts +0 -2
  136. package/output/service/index.d.ts.map +0 -1
  137. package/output/service/index.js +0 -1
  138. package/output/service/service.d.ts +0 -6
  139. package/output/service/service.d.ts.map +0 -1
  140. package/output/service/service.js +0 -3
  141. package/output/string/index.d.ts +0 -2
  142. package/output/string/index.d.ts.map +0 -1
  143. package/output/string/index.js +0 -1
  144. package/output/string/string.d.ts +0 -3
  145. package/output/string/string.d.ts.map +0 -1
  146. package/output/string/string.js +0 -9
  147. package/output/string/string.test.d.ts +0 -2
  148. package/output/string/string.test.d.ts.map +0 -1
  149. package/output/string/string.test.js +0 -77
  150. package/output/throttle/index.d.ts +0 -2
  151. package/output/throttle/index.d.ts.map +0 -1
  152. package/output/throttle/index.js +0 -1
  153. package/output/throttle/throttle.d.ts +0 -12
  154. package/output/throttle/throttle.d.ts.map +0 -1
  155. package/output/throttle/throttle.js +0 -55
  156. package/output/throttle/throttle.test.d.ts +0 -2
  157. package/output/throttle/throttle.test.d.ts.map +0 -1
  158. package/output/throttle/throttle.test.js +0 -177
  159. package/output/ttl-cache/index.d.ts +0 -2
  160. package/output/ttl-cache/index.d.ts.map +0 -1
  161. package/output/ttl-cache/index.js +0 -1
  162. package/output/ttl-cache/ttl-cache.d.ts +0 -148
  163. package/output/ttl-cache/ttl-cache.d.ts.map +0 -1
  164. package/output/ttl-cache/ttl-cache.js +0 -313
  165. package/output/ttl-cache/ttl-cache.test.d.ts +0 -2
  166. package/output/ttl-cache/ttl-cache.test.d.ts.map +0 -1
  167. package/output/ttl-cache/ttl-cache.test.js +0 -222
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 TaicodeLabs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taicode/common-base",
3
- "version": "3.7.4",
3
+ "version": "3.7.7",
4
4
  "author": "Alain",
5
5
  "license": "ISC",
6
6
  "description": "",
@@ -20,17 +20,16 @@
20
20
  "default": "./output/*/index.js"
21
21
  }
22
22
  },
23
- "scripts": {
24
- "test": "vitest run",
25
- "build": "node ../../node_modules/typescript/bin/tsc -p tsconfig.json",
26
- "prepublishOnly": "npm run build",
27
- "dev": "node ../../node_modules/typescript/bin/tsc -p tsconfig.json --watch"
28
- },
29
23
  "files": [
30
24
  "output"
31
25
  ],
32
26
  "devDependencies": {
33
27
  "vitest": "^3.2.4",
34
28
  "mobx": "^6.15.0"
29
+ },
30
+ "scripts": {
31
+ "test": "vitest run",
32
+ "build": "node ../../node_modules/typescript/bin/tsc -p tsconfig.json",
33
+ "dev": "node ../../node_modules/typescript/bin/tsc -p tsconfig.json --watch"
35
34
  }
36
- }
35
+ }
@@ -1,133 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1,264 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=batching-buffer.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"batching-buffer.test.d.ts","sourceRoot":"","sources":["../../source/batching-buffer/batching-buffer.test.ts"],"names":[],"mappings":""}