@ls-stack/utils 3.63.0 → 3.66.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.
Files changed (328) hide show
  1. package/dist/{arrayUtils.d.cts → arrayUtils.d.mts} +24 -23
  2. package/dist/arrayUtils.mjs +249 -0
  3. package/dist/assertions-qMxfVhSu.mjs +207 -0
  4. package/dist/{assertions.d.ts → assertions.d.mts} +4 -3
  5. package/dist/assertions.mjs +3 -0
  6. package/dist/asyncQueue.d.mts +497 -0
  7. package/dist/asyncQueue.mjs +757 -0
  8. package/dist/{awaitDebounce.d.cts → awaitDebounce.d.mts} +11 -6
  9. package/dist/awaitDebounce.mjs +54 -0
  10. package/dist/{cache.d.ts → cache.d.mts} +76 -68
  11. package/dist/cache.mjs +355 -0
  12. package/dist/castValues-DfICShCc.mjs +19 -0
  13. package/dist/{castValues.d.cts → castValues.d.mts} +3 -2
  14. package/dist/castValues.mjs +3 -0
  15. package/dist/{concurrentCalls.d.ts → concurrentCalls.d.mts} +74 -65
  16. package/dist/concurrentCalls.mjs +295 -0
  17. package/dist/consoleFmt.d.mts +55 -0
  18. package/dist/consoleFmt.mjs +63 -0
  19. package/dist/conversions-DTmwEMIu.mjs +12 -0
  20. package/dist/conversions.d.mts +4 -0
  21. package/dist/conversions.mjs +3 -0
  22. package/dist/createThrottleController.d.mts +18 -0
  23. package/dist/createThrottleController.mjs +40 -0
  24. package/dist/debounce.d.mts +47 -0
  25. package/dist/debounce.mjs +117 -0
  26. package/dist/dedent.d.mts +74 -0
  27. package/dist/dedent.mjs +80 -0
  28. package/dist/deepEqual-C7EZEixx.mjs +78 -0
  29. package/dist/{deepEqual.d.cts → deepEqual.d.mts} +3 -2
  30. package/dist/deepEqual.mjs +3 -0
  31. package/dist/{deepReplaceValues.d.cts → deepReplaceValues.d.mts} +4 -3
  32. package/dist/deepReplaceValues.mjs +61 -0
  33. package/dist/diffParser.d.mts +63 -0
  34. package/dist/diffParser.mjs +410 -0
  35. package/dist/enhancedMap.d.mts +21 -0
  36. package/dist/enhancedMap.mjs +69 -0
  37. package/dist/exhaustiveMatch.d.mts +10 -0
  38. package/dist/exhaustiveMatch.mjs +48 -0
  39. package/dist/{filterObjectOrArrayKeys.d.cts → filterObjectOrArrayKeys.d.mts} +15 -8
  40. package/dist/filterObjectOrArrayKeys.mjs +497 -0
  41. package/dist/{getAutoIncrementId.d.cts → getAutoIncrementId.d.mts} +9 -5
  42. package/dist/getAutoIncrementId.mjs +53 -0
  43. package/dist/{getCompositeKey.d.cts → getCompositeKey.d.mts} +3 -2
  44. package/dist/getCompositeKey.mjs +50 -0
  45. package/dist/{getValueStableKey.d.cts → getValueStableKey.d.mts} +5 -3
  46. package/dist/getValueStableKey.mjs +17 -0
  47. package/dist/{hash.d.cts → hash.d.mts} +3 -2
  48. package/dist/hash.mjs +28 -0
  49. package/dist/interpolate.d.mts +17 -0
  50. package/dist/interpolate.mjs +28 -0
  51. package/dist/{iteratorUtils.d.cts → iteratorUtils.d.mts} +5 -4
  52. package/dist/iteratorUtils.mjs +39 -0
  53. package/dist/keepPrevIfUnchanged.d.mts +12 -0
  54. package/dist/keepPrevIfUnchanged.mjs +9 -0
  55. package/dist/keyedMap.d.mts +76 -0
  56. package/dist/keyedMap.mjs +139 -0
  57. package/dist/keyedSet.d.mts +77 -0
  58. package/dist/keyedSet.mjs +129 -0
  59. package/dist/{levenshtein.d.cts → levenshtein.d.mts} +3 -2
  60. package/dist/levenshtein.mjs +121 -0
  61. package/dist/main.d.mts +4 -0
  62. package/dist/main.mjs +7 -0
  63. package/dist/matchPath.d.mts +50 -0
  64. package/dist/matchPath.mjs +81 -0
  65. package/dist/mathUtils-BDP1lM_z.mjs +81 -0
  66. package/dist/{mathUtils.d.cts → mathUtils.d.mts} +3 -2
  67. package/dist/mathUtils.mjs +3 -0
  68. package/dist/{mutationUtils.d.cts → mutationUtils.d.mts} +6 -5
  69. package/dist/mutationUtils.mjs +44 -0
  70. package/dist/{objUtils.d.ts → objUtils.d.mts} +8 -6
  71. package/dist/objUtils.mjs +115 -0
  72. package/dist/parallelAsyncCalls.d.mts +83 -0
  73. package/dist/parallelAsyncCalls.mjs +121 -0
  74. package/dist/partialEqual.d.mts +139 -0
  75. package/dist/partialEqual.mjs +1055 -0
  76. package/dist/promiseUtils.d.mts +9 -0
  77. package/dist/promiseUtils.mjs +17 -0
  78. package/dist/regexUtils.d.mts +18 -0
  79. package/dist/regexUtils.mjs +34 -0
  80. package/dist/{retryOnError.d.cts → retryOnError.d.mts} +38 -37
  81. package/dist/retryOnError.mjs +91 -0
  82. package/dist/{runShellCmd.d.ts → runShellCmd.d.mts} +24 -15
  83. package/dist/runShellCmd.mjs +151 -0
  84. package/dist/{safeJson.d.cts → safeJson.d.mts} +3 -2
  85. package/dist/safeJson.mjs +30 -0
  86. package/dist/{saferTyping.d.cts → saferTyping.d.mts} +4 -3
  87. package/dist/saferTyping.mjs +45 -0
  88. package/dist/serializeXML.d.mts +23 -0
  89. package/dist/serializeXML.mjs +74 -0
  90. package/dist/{shallowEqual.d.cts → shallowEqual.d.mts} +3 -2
  91. package/dist/shallowEqual.mjs +54 -0
  92. package/dist/sleep.d.mts +4 -0
  93. package/dist/sleep.mjs +7 -0
  94. package/dist/stringUtils-DjhWOiYn.mjs +113 -0
  95. package/dist/{stringUtils.d.cts → stringUtils.d.mts} +3 -2
  96. package/dist/stringUtils.mjs +3 -0
  97. package/dist/{testUtils.d.ts → testUtils.d.mts} +83 -52
  98. package/dist/testUtils.mjs +310 -0
  99. package/dist/{throttle.d.ts → throttle.d.mts} +18 -17
  100. package/dist/throttle.mjs +102 -0
  101. package/dist/time-sr2lhQRw.mjs +67 -0
  102. package/dist/{time.d.ts → time.d.mts} +8 -7
  103. package/dist/time.mjs +3 -0
  104. package/dist/{timers.d.cts → timers.d.mts} +22 -13
  105. package/dist/timers.mjs +220 -0
  106. package/dist/{tsResult.d.cts → tsResult.d.mts} +52 -48
  107. package/dist/tsResult.mjs +142 -0
  108. package/dist/typeGuards-B1mzA-Rz.mjs +128 -0
  109. package/dist/{typeGuards.d.cts → typeGuards.d.mts} +3 -2
  110. package/dist/typeGuards.mjs +3 -0
  111. package/dist/{typeUtils.d.ts → typeUtils.d.mts} +13 -34
  112. package/dist/typeUtils.mjs +1 -0
  113. package/dist/{typedStrings.d.cts → typedStrings.d.mts} +5 -4
  114. package/dist/typedStrings.mjs +131 -0
  115. package/dist/typingFnUtils-Bb8drgKF.mjs +101 -0
  116. package/dist/{typingFnUtils.d.cts → typingFnUtils.d.mts} +13 -22
  117. package/dist/typingFnUtils.mjs +3 -0
  118. package/dist/{typingTestUtils.d.cts → typingTestUtils.d.mts} +11 -15
  119. package/dist/typingTestUtils.mjs +80 -0
  120. package/dist/typingUtils.d.mts +20 -0
  121. package/dist/typingUtils.mjs +1 -0
  122. package/dist/yamlStringify.d.mts +17 -0
  123. package/dist/yamlStringify.mjs +189 -0
  124. package/package.json +65 -234
  125. package/dist/arrayUtils.cjs +0 -229
  126. package/dist/arrayUtils.d.ts +0 -171
  127. package/dist/arrayUtils.js +0 -42
  128. package/dist/assertions.cjs +0 -107
  129. package/dist/assertions.d.cts +0 -192
  130. package/dist/assertions.js +0 -25
  131. package/dist/asyncQueue.cjs +0 -672
  132. package/dist/asyncQueue.d.cts +0 -488
  133. package/dist/asyncQueue.d.ts +0 -488
  134. package/dist/asyncQueue.js +0 -631
  135. package/dist/awaitDebounce.cjs +0 -106
  136. package/dist/awaitDebounce.d.ts +0 -41
  137. package/dist/awaitDebounce.js +0 -28
  138. package/dist/cache.cjs +0 -367
  139. package/dist/cache.d.cts +0 -228
  140. package/dist/cache.js +0 -19
  141. package/dist/castValues.cjs +0 -50
  142. package/dist/castValues.d.ts +0 -4
  143. package/dist/castValues.js +0 -8
  144. package/dist/chunk-5DZT3Z5Z.js +0 -8
  145. package/dist/chunk-6FBIEPWU.js +0 -96
  146. package/dist/chunk-6FIBVC2P.js +0 -56
  147. package/dist/chunk-7CQPOM5I.js +0 -100
  148. package/dist/chunk-B6DNOZCP.js +0 -369
  149. package/dist/chunk-BM4PYVOX.js +0 -109
  150. package/dist/chunk-C2SVCIWE.js +0 -57
  151. package/dist/chunk-CCUPDGSZ.js +0 -132
  152. package/dist/chunk-DBOWTYR4.js +0 -49
  153. package/dist/chunk-DFXNVEH6.js +0 -14
  154. package/dist/chunk-DX2524CZ.js +0 -314
  155. package/dist/chunk-GMJTLFM6.js +0 -60
  156. package/dist/chunk-IATIXMCE.js +0 -20
  157. package/dist/chunk-II4R3VVX.js +0 -25
  158. package/dist/chunk-JF2MDHOJ.js +0 -40
  159. package/dist/chunk-JQFUKJU5.js +0 -71
  160. package/dist/chunk-MI4UE2PQ.js +0 -561
  161. package/dist/chunk-PUKVXYYL.js +0 -52
  162. package/dist/chunk-QQS7I7ZL.js +0 -16
  163. package/dist/chunk-VAAMRG4K.js +0 -20
  164. package/dist/chunk-WFQJUJTC.js +0 -182
  165. package/dist/chunk-ZXIKIA5B.js +0 -178
  166. package/dist/concurrentCalls.cjs +0 -406
  167. package/dist/concurrentCalls.d.cts +0 -116
  168. package/dist/concurrentCalls.js +0 -346
  169. package/dist/consoleFmt.cjs +0 -85
  170. package/dist/consoleFmt.d.cts +0 -54
  171. package/dist/consoleFmt.d.ts +0 -54
  172. package/dist/consoleFmt.js +0 -60
  173. package/dist/conversions.cjs +0 -44
  174. package/dist/conversions.d.cts +0 -3
  175. package/dist/conversions.d.ts +0 -3
  176. package/dist/conversions.js +0 -6
  177. package/dist/createThrottleController.cjs +0 -193
  178. package/dist/createThrottleController.d.cts +0 -13
  179. package/dist/createThrottleController.d.ts +0 -13
  180. package/dist/createThrottleController.js +0 -61
  181. package/dist/debounce.cjs +0 -157
  182. package/dist/debounce.d.cts +0 -46
  183. package/dist/debounce.d.ts +0 -46
  184. package/dist/debounce.js +0 -8
  185. package/dist/dedent.cjs +0 -104
  186. package/dist/dedent.d.cts +0 -73
  187. package/dist/dedent.d.ts +0 -73
  188. package/dist/dedent.js +0 -79
  189. package/dist/deepEqual.cjs +0 -96
  190. package/dist/deepEqual.d.ts +0 -21
  191. package/dist/deepEqual.js +0 -8
  192. package/dist/deepReplaceValues.cjs +0 -87
  193. package/dist/deepReplaceValues.d.ts +0 -27
  194. package/dist/deepReplaceValues.js +0 -7
  195. package/dist/enhancedMap.cjs +0 -131
  196. package/dist/enhancedMap.d.cts +0 -20
  197. package/dist/enhancedMap.d.ts +0 -20
  198. package/dist/enhancedMap.js +0 -10
  199. package/dist/exhaustiveMatch.cjs +0 -66
  200. package/dist/exhaustiveMatch.d.cts +0 -9
  201. package/dist/exhaustiveMatch.d.ts +0 -9
  202. package/dist/exhaustiveMatch.js +0 -40
  203. package/dist/filterObjectOrArrayKeys.cjs +0 -619
  204. package/dist/filterObjectOrArrayKeys.d.ts +0 -88
  205. package/dist/filterObjectOrArrayKeys.js +0 -9
  206. package/dist/getAutoIncrementId.cjs +0 -44
  207. package/dist/getAutoIncrementId.d.ts +0 -46
  208. package/dist/getAutoIncrementId.js +0 -18
  209. package/dist/getCompositeKey.cjs +0 -86
  210. package/dist/getCompositeKey.d.ts +0 -11
  211. package/dist/getCompositeKey.js +0 -8
  212. package/dist/getValueStableKey.cjs +0 -89
  213. package/dist/getValueStableKey.d.ts +0 -15
  214. package/dist/getValueStableKey.js +0 -11
  215. package/dist/hash.cjs +0 -57
  216. package/dist/hash.d.ts +0 -7
  217. package/dist/hash.js +0 -32
  218. package/dist/interpolate.cjs +0 -88
  219. package/dist/interpolate.d.cts +0 -11
  220. package/dist/interpolate.d.ts +0 -11
  221. package/dist/interpolate.js +0 -46
  222. package/dist/iteratorUtils.cjs +0 -73
  223. package/dist/iteratorUtils.d.ts +0 -10
  224. package/dist/iteratorUtils.js +0 -44
  225. package/dist/keepPrevIfUnchanged.cjs +0 -102
  226. package/dist/keepPrevIfUnchanged.d.cts +0 -7
  227. package/dist/keepPrevIfUnchanged.d.ts +0 -7
  228. package/dist/keepPrevIfUnchanged.js +0 -7
  229. package/dist/levenshtein.cjs +0 -180
  230. package/dist/levenshtein.d.ts +0 -5
  231. package/dist/levenshtein.js +0 -153
  232. package/dist/main.cjs +0 -32
  233. package/dist/main.d.cts +0 -3
  234. package/dist/main.d.ts +0 -3
  235. package/dist/main.js +0 -7
  236. package/dist/matchPath.cjs +0 -155
  237. package/dist/matchPath.d.cts +0 -53
  238. package/dist/matchPath.d.ts +0 -53
  239. package/dist/matchPath.js +0 -108
  240. package/dist/mathUtils.cjs +0 -81
  241. package/dist/mathUtils.d.ts +0 -54
  242. package/dist/mathUtils.js +0 -22
  243. package/dist/mutationUtils.cjs +0 -153
  244. package/dist/mutationUtils.d.ts +0 -15
  245. package/dist/mutationUtils.js +0 -55
  246. package/dist/objUtils.cjs +0 -242
  247. package/dist/objUtils.d.cts +0 -28
  248. package/dist/objUtils.js +0 -38
  249. package/dist/parallelAsyncCalls.cjs +0 -162
  250. package/dist/parallelAsyncCalls.d.cts +0 -82
  251. package/dist/parallelAsyncCalls.d.ts +0 -82
  252. package/dist/parallelAsyncCalls.js +0 -126
  253. package/dist/partialEqual.cjs +0 -1196
  254. package/dist/partialEqual.d.cts +0 -141
  255. package/dist/partialEqual.d.ts +0 -141
  256. package/dist/partialEqual.js +0 -1168
  257. package/dist/promiseUtils.cjs +0 -38
  258. package/dist/promiseUtils.d.cts +0 -8
  259. package/dist/promiseUtils.d.ts +0 -8
  260. package/dist/promiseUtils.js +0 -6
  261. package/dist/regexUtils.cjs +0 -60
  262. package/dist/regexUtils.d.cts +0 -17
  263. package/dist/regexUtils.d.ts +0 -17
  264. package/dist/regexUtils.js +0 -33
  265. package/dist/retryOnError.cjs +0 -130
  266. package/dist/retryOnError.d.ts +0 -83
  267. package/dist/retryOnError.js +0 -101
  268. package/dist/runShellCmd.cjs +0 -127
  269. package/dist/runShellCmd.d.cts +0 -90
  270. package/dist/runShellCmd.js +0 -98
  271. package/dist/safeJson.cjs +0 -45
  272. package/dist/safeJson.d.ts +0 -16
  273. package/dist/safeJson.js +0 -8
  274. package/dist/saferTyping.cjs +0 -52
  275. package/dist/saferTyping.d.ts +0 -47
  276. package/dist/saferTyping.js +0 -23
  277. package/dist/serializeXML.cjs +0 -154
  278. package/dist/serializeXML.d.cts +0 -22
  279. package/dist/serializeXML.d.ts +0 -22
  280. package/dist/serializeXML.js +0 -116
  281. package/dist/shallowEqual.cjs +0 -88
  282. package/dist/shallowEqual.d.ts +0 -4
  283. package/dist/shallowEqual.js +0 -63
  284. package/dist/sleep.cjs +0 -32
  285. package/dist/sleep.d.cts +0 -3
  286. package/dist/sleep.d.ts +0 -3
  287. package/dist/sleep.js +0 -6
  288. package/dist/stringUtils.cjs +0 -155
  289. package/dist/stringUtils.d.ts +0 -55
  290. package/dist/stringUtils.js +0 -50
  291. package/dist/testUtils.cjs +0 -1490
  292. package/dist/testUtils.d.cts +0 -133
  293. package/dist/testUtils.js +0 -359
  294. package/dist/throttle.cjs +0 -282
  295. package/dist/throttle.d.cts +0 -98
  296. package/dist/throttle.js +0 -38
  297. package/dist/time.cjs +0 -152
  298. package/dist/time.d.cts +0 -25
  299. package/dist/time.js +0 -38
  300. package/dist/timers.cjs +0 -194
  301. package/dist/timers.d.ts +0 -121
  302. package/dist/timers.js +0 -156
  303. package/dist/tsResult.cjs +0 -226
  304. package/dist/tsResult.d.ts +0 -114
  305. package/dist/tsResult.js +0 -180
  306. package/dist/typeGuards.cjs +0 -70
  307. package/dist/typeGuards.d.ts +0 -111
  308. package/dist/typeGuards.js +0 -18
  309. package/dist/typeUtils.cjs +0 -18
  310. package/dist/typeUtils.d.cts +0 -61
  311. package/dist/typeUtils.js +0 -0
  312. package/dist/typedStrings.cjs +0 -90
  313. package/dist/typedStrings.d.ts +0 -163
  314. package/dist/typedStrings.js +0 -57
  315. package/dist/typingFnUtils.cjs +0 -96
  316. package/dist/typingFnUtils.d.ts +0 -100
  317. package/dist/typingFnUtils.js +0 -30
  318. package/dist/typingTestUtils.cjs +0 -52
  319. package/dist/typingTestUtils.d.ts +0 -79
  320. package/dist/typingTestUtils.js +0 -27
  321. package/dist/typingUtils.cjs +0 -18
  322. package/dist/typingUtils.d.cts +0 -35
  323. package/dist/typingUtils.d.ts +0 -35
  324. package/dist/typingUtils.js +0 -0
  325. package/dist/yamlStringify.cjs +0 -423
  326. package/dist/yamlStringify.d.cts +0 -10
  327. package/dist/yamlStringify.d.ts +0 -10
  328. package/dist/yamlStringify.js +0 -9
@@ -1,672 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/asyncQueue.ts
21
- var asyncQueue_exports = {};
22
- __export(asyncQueue_exports, {
23
- createAsyncQueue: () => createAsyncQueue,
24
- createAsyncQueueWithMeta: () => createAsyncQueueWithMeta
25
- });
26
- module.exports = __toCommonJS(asyncQueue_exports);
27
- var import_evtmitter = require("evtmitter");
28
- var import_t_result = require("t-result");
29
-
30
- // src/promiseUtils.ts
31
- function defer() {
32
- let resolve;
33
- let reject;
34
- const promise = new Promise((_resolve, _reject) => {
35
- resolve = _resolve;
36
- reject = _reject;
37
- });
38
- return { resolve, reject, promise };
39
- }
40
-
41
- // src/time.ts
42
- var MINUTE_AS_MS = 60 * 1e3;
43
- var HOUR_AS_MS = 60 * MINUTE_AS_MS;
44
- var DAY_AS_MS = 24 * HOUR_AS_MS;
45
- var WEEK_AS_MS = 7 * DAY_AS_MS;
46
- var MONTH_AS_MS = 30 * DAY_AS_MS;
47
- var YEAR_AS_MS = 365 * DAY_AS_MS;
48
- var HOUR_AS_SECS = 60 * 60;
49
- var DAY_AS_SECS = 24 * HOUR_AS_SECS;
50
- var WEEK_AS_SECS = 7 * DAY_AS_SECS;
51
- var MONTH_AS_SECS = 30 * DAY_AS_SECS;
52
- var YEAR_AS_SECS = 365 * DAY_AS_SECS;
53
- function durationObjToMs(durationObj) {
54
- return (durationObj.hours ?? 0) * HOUR_AS_MS + (durationObj.minutes ?? 0) * MINUTE_AS_MS + (durationObj.seconds ?? 0) * 1e3 + (durationObj.ms ?? 0) + (durationObj.days ?? 0) * DAY_AS_MS;
55
- }
56
-
57
- // src/asyncQueue.ts
58
- var AsyncQueue = class _AsyncQueue {
59
- #queue = [];
60
- #pending = 0;
61
- #size = 0;
62
- #concurrency;
63
- #completed = 0;
64
- #failed = 0;
65
- #idleResolvers = [];
66
- #sizeLessThanWaiters = [];
67
- /**
68
- * Event emitter for tracking task lifecycle
69
- *
70
- * @example
71
- * Listening to Events
72
- * ```typescript
73
- * const queue = createAsyncQueue<string>();
74
- *
75
- * queue.events.on('start', (event) => {
76
- * console.log('Task started:', event.payload.meta);
77
- * });
78
- *
79
- * queue.events.on('complete', (event) => {
80
- * console.log('Task completed:', event.payload.value);
81
- * });
82
- *
83
- * queue.events.on('error', (event) => {
84
- * console.error('Task failed:', event.payload.error);
85
- * });
86
- * ```
87
- */
88
- events = (0, import_evtmitter.evtmitter)();
89
- #signal;
90
- #taskTimeout;
91
- #stopped = false;
92
- #paused = false;
93
- #started = false;
94
- #stopOnError = false;
95
- #rejectPendingOnError = false;
96
- #autoStart = true;
97
- #stoppedReason;
98
- #rateLimit;
99
- #taskExecutionTimes = [];
100
- #rateLimitTimeouts = /* @__PURE__ */ new Set();
101
- /** Array of all task failures with metadata for debugging and analysis */
102
- failures = [];
103
- /** Array of all task completions with metadata for debugging and analysis */
104
- completions = [];
105
- constructor({
106
- concurrency = 1,
107
- signal,
108
- timeout: taskTimeout,
109
- stopOnError = false,
110
- rejectPendingOnError = false,
111
- autoStart = true,
112
- rateLimit
113
- } = {}) {
114
- this.#concurrency = concurrency;
115
- this.#signal = signal;
116
- this.#taskTimeout = taskTimeout;
117
- this.#stopOnError = stopOnError;
118
- this.#rejectPendingOnError = rejectPendingOnError;
119
- this.#autoStart = autoStart;
120
- this.#started = autoStart;
121
- this.#rateLimit = rateLimit;
122
- this.events.on("error", (e) => {
123
- this.failures.push(e.payload);
124
- });
125
- this.events.on("complete", (e) => {
126
- this.completions.push(e.payload);
127
- });
128
- }
129
- #getRateLimitIntervalMs() {
130
- if (!this.#rateLimit) return 0;
131
- return typeof this.#rateLimit.interval === "number" ? this.#rateLimit.interval : durationObjToMs(this.#rateLimit.interval);
132
- }
133
- #cleanupExpiredExecutionTimes(now) {
134
- if (!this.#rateLimit) return;
135
- const intervalMs = this.#getRateLimitIntervalMs();
136
- const cutoff = now - intervalMs;
137
- this.#taskExecutionTimes = this.#taskExecutionTimes.filter(
138
- (time) => time > cutoff
139
- );
140
- }
141
- #isRateLimited() {
142
- if (!this.#rateLimit) return false;
143
- const now = Date.now();
144
- this.#cleanupExpiredExecutionTimes(now);
145
- return this.#taskExecutionTimes.length >= this.#rateLimit.maxTasks;
146
- }
147
- #getRateLimitDelay() {
148
- if (!this.#rateLimit || this.#taskExecutionTimes.length === 0) return 0;
149
- const oldestExecution = this.#taskExecutionTimes[0];
150
- if (oldestExecution === void 0) return 0;
151
- const intervalMs = this.#getRateLimitIntervalMs();
152
- const timeUntilSlotOpens = oldestExecution + intervalMs - Date.now();
153
- return Math.max(0, timeUntilSlotOpens);
154
- }
155
- #recordTaskExecution() {
156
- if (!this.#rateLimit) return;
157
- const now = Date.now();
158
- this.#taskExecutionTimes.push(now);
159
- this.#cleanupExpiredExecutionTimes(now);
160
- }
161
- #enqueue(task) {
162
- this.#queue.push(task);
163
- this.#size++;
164
- }
165
- static #createTimeoutSignal(ms) {
166
- const controller = new AbortController();
167
- const id = setTimeout(() => {
168
- controller.abort(
169
- new DOMException(
170
- "The operation was aborted due to timeout",
171
- "TimeoutError"
172
- )
173
- );
174
- }, ms);
175
- controller.signal.addEventListener(
176
- "abort",
177
- () => {
178
- clearTimeout(id);
179
- },
180
- { once: true }
181
- );
182
- return controller.signal;
183
- }
184
- // removed: onEmpty-related waiters
185
- #resolveSizeLessThanWaiters() {
186
- if (this.#sizeLessThanWaiters.length === 0) return;
187
- const remaining = [];
188
- for (const waiter of this.#sizeLessThanWaiters) {
189
- if (this.#size < waiter.limit) {
190
- waiter.resolve();
191
- } else {
192
- remaining.push(waiter);
193
- }
194
- }
195
- this.#sizeLessThanWaiters = remaining;
196
- }
197
- /**
198
- * Add a task that returns a Result to the queue
199
- *
200
- * Use this method when your task function already returns a Result type. For
201
- * functions that throw errors or return plain values, use `resultifyAdd`
202
- * instead.
203
- *
204
- * @example
205
- * ```typescript
206
- * const queue = createAsyncQueue<string>();
207
- *
208
- * const result = await queue.add(async () => {
209
- * try {
210
- * const data = await fetchData();
211
- * return Result.ok(data);
212
- * } catch (error) {
213
- * return Result.err(error);
214
- * }
215
- * });
216
- *
217
- * if (result.ok) {
218
- * console.log('Success:', result.value);
219
- * } else {
220
- * console.log('Error:', result.error);
221
- * }
222
- * ```;
223
- *
224
- * @param fn - Task function that returns a Result
225
- * @param options - Optional configuration for this task
226
- * @returns Promise that resolves with the task result
227
- */
228
- async add(fn, options) {
229
- if (this.#signal?.aborted) {
230
- return import_t_result.Result.err(
231
- this.#signal.reason instanceof Error ? this.#signal.reason : new DOMException("This operation was aborted", "AbortError")
232
- );
233
- }
234
- if (this.#stopped) {
235
- return import_t_result.Result.err(
236
- this.#stoppedReason ?? new Error("Queue has been stopped")
237
- );
238
- }
239
- const deferred = defer();
240
- const taskTimeout = this.#taskTimeout ?? options?.timeout;
241
- const task = {
242
- run: async (ctx) => {
243
- return fn(ctx);
244
- },
245
- resolve: deferred.resolve,
246
- reject: deferred.reject,
247
- signal: options?.signal,
248
- meta: options?.meta,
249
- timeout: taskTimeout
250
- };
251
- this.#enqueue(task);
252
- if (this.#autoStart && this.#started) {
253
- this.#processQueue();
254
- }
255
- const r = await deferred.promise;
256
- if (options?.onComplete) {
257
- r.onOk(options.onComplete);
258
- }
259
- if (options?.onError) {
260
- r.onErr(options.onError);
261
- }
262
- return r;
263
- }
264
- /**
265
- * Add a task that returns a plain value or throws errors to the queue
266
- *
267
- * This is the most commonly used method. It automatically wraps your function
268
- * to handle errors and convert them to Result types.
269
- *
270
- * @example
271
- * Basic Usage
272
- * ```typescript
273
- * const queue = createAsyncQueue<string>();
274
- *
275
- * queue.resultifyAdd(async () => {
276
- * const response = await fetch('/api/data');
277
- * return response.json();
278
- * }).then(result => {
279
- * if (result.ok) {
280
- * console.log('Data:', result.value);
281
- * } else {
282
- * console.error('Failed:', result.error);
283
- * }
284
- * });
285
- * ```
286
- *
287
- * @example
288
- * With Callbacks
289
- * ```typescript
290
- * queue.resultifyAdd(
291
- * async () => processData(),
292
- * {
293
- * onComplete: (data) => console.log('Processed:', data),
294
- * onError: (error) => console.error('Failed:', error),
295
- * timeout: 5000
296
- * }
297
- * );
298
- * ```
299
- *
300
- * @param fn - Task function that returns a value or throws
301
- * @param options - Optional configuration for this task
302
- * @returns Promise that resolves with the task result wrapped in Result
303
- */
304
- resultifyAdd(fn, options) {
305
- return this.add(
306
- (ctx) => (0, import_t_result.resultify)(async () => {
307
- return fn(ctx);
308
- }),
309
- options
310
- );
311
- }
312
- async #processQueue() {
313
- if (this.#signal?.aborted) {
314
- this.clear();
315
- return;
316
- }
317
- if (this.#stopped || this.#paused || !this.#started) {
318
- return;
319
- }
320
- if (this.#pending >= this.#concurrency || this.#queue.length === 0) {
321
- return;
322
- }
323
- if (this.#isRateLimited()) {
324
- const delay = this.#getRateLimitDelay();
325
- if (delay > 0) {
326
- const timeoutId = setTimeout(() => {
327
- this.#rateLimitTimeouts.delete(timeoutId);
328
- this.#processQueue();
329
- }, delay);
330
- this.#rateLimitTimeouts.add(timeoutId);
331
- return;
332
- }
333
- }
334
- const task = this.#queue.shift();
335
- if (!task) {
336
- return;
337
- }
338
- this.#pending++;
339
- this.#size--;
340
- this.#resolveSizeLessThanWaiters();
341
- this.#recordTaskExecution();
342
- const signals = [];
343
- if (task.signal) {
344
- signals.push(task.signal);
345
- }
346
- if (this.#signal) {
347
- signals.push(this.#signal);
348
- }
349
- if (task.timeout !== void 0) {
350
- signals.push(_AsyncQueue.#createTimeoutSignal(task.timeout));
351
- }
352
- const signal = signals.length > 1 ? AbortSignal.any(signals) : signals[0];
353
- let abortListener;
354
- try {
355
- if (signal?.aborted) {
356
- const error = signal.reason instanceof Error ? signal.reason : new DOMException("This operation was aborted", "AbortError");
357
- throw error;
358
- }
359
- const signalAbortPromise = new Promise((_, reject) => {
360
- if (signal) {
361
- abortListener = () => {
362
- const reason = signal.reason;
363
- const err = reason instanceof Error ? reason : new DOMException("This operation was aborted", "AbortError");
364
- setTimeout(() => {
365
- reject(err);
366
- }, 0);
367
- };
368
- signal.addEventListener("abort", abortListener, { once: true });
369
- }
370
- });
371
- const taskRunPromise = task.run({ signal, meta: task.meta });
372
- this.events.emit("start", { meta: task.meta });
373
- const result = await Promise.race([taskRunPromise, signalAbortPromise]);
374
- if ((0, import_t_result.isResult)(result)) {
375
- task.resolve(result);
376
- if (!result.ok) {
377
- this.#failed++;
378
- this.events.emit("error", {
379
- meta: task.meta,
380
- error: result.error
381
- });
382
- this.#stopOnErrorAndRejectPending((0, import_t_result.unknownToError)(result.error));
383
- } else {
384
- this.#completed++;
385
- this.events.emit("complete", {
386
- meta: task.meta,
387
- value: result.value
388
- });
389
- }
390
- } else {
391
- const error = new Error("Response not a Result");
392
- task.resolve(import_t_result.Result.err(error));
393
- this.#failed++;
394
- this.events.emit("error", {
395
- meta: task.meta,
396
- error
397
- });
398
- this.#stopOnErrorAndRejectPending(error);
399
- }
400
- } catch (error) {
401
- const processedError = (0, import_t_result.unknownToError)(error);
402
- task.resolve(import_t_result.Result.err(processedError));
403
- this.#failed++;
404
- this.events.emit("error", {
405
- meta: task.meta,
406
- error: processedError
407
- });
408
- this.#stopOnErrorAndRejectPending(processedError);
409
- } finally {
410
- if (signal && abortListener) {
411
- signal.removeEventListener("abort", abortListener);
412
- }
413
- this.#pending--;
414
- this.#processQueue();
415
- if (this.#pending === 0 && this.#size === 0 && this.#rateLimitTimeouts.size === 0) {
416
- this.#resolveIdleWaiters();
417
- }
418
- }
419
- }
420
- #resolveIdleWaiters() {
421
- while (this.#idleResolvers.length > 0) {
422
- const resolve = this.#idleResolvers.shift();
423
- if (resolve) {
424
- resolve();
425
- }
426
- }
427
- }
428
- #stopOnErrorAndRejectPending(error) {
429
- if (!this.#stopOnError) {
430
- return;
431
- }
432
- this.#stopped = true;
433
- this.#stoppedReason = error;
434
- if (this.#rejectPendingOnError) {
435
- while (this.#queue.length > 0) {
436
- const task = this.#queue.shift();
437
- if (task) {
438
- task.resolve(import_t_result.Result.err(error));
439
- }
440
- }
441
- this.#size = 0;
442
- this.#resolveSizeLessThanWaiters();
443
- }
444
- this.#resolveIdleWaiters();
445
- }
446
- /**
447
- * Wait for the queue to become idle (no pending tasks, no queued tasks, and
448
- * no rate-limit timers)
449
- *
450
- * This method resolves when:
451
- *
452
- * - All tasks have completed (success or failure)
453
- * - The queue is stopped due to error (stopOnError), even with remaining tasks
454
- * - There are no queued tasks, no running tasks, and no pending rate-limit
455
- * timers
456
- *
457
- * @example
458
- * ```typescript
459
- * const queue = createAsyncQueue<string>();
460
- *
461
- * // Add multiple tasks
462
- * for (let i = 0; i < 10; i++) {
463
- * queue.resultifyAdd(async () => `task ${i}`);
464
- * }
465
- *
466
- * // Wait for all tasks to complete
467
- * await queue.onIdle();
468
- *
469
- * console.log(`Completed: ${queue.completed}, Failed: ${queue.failed}`);
470
- * ```
471
- *
472
- * @returns Promise that resolves when the queue is idle
473
- */
474
- async onIdle() {
475
- if (this.#stopped || this.#pending === 0 && this.#size === 0 && this.#rateLimitTimeouts.size === 0) {
476
- return Promise.resolve();
477
- }
478
- return new Promise((resolve) => {
479
- this.#idleResolvers.push(resolve);
480
- });
481
- }
482
- // removed: onEmpty()
483
- /**
484
- * Wait until the queued task count is below a limit
485
- *
486
- * Resolves immediately if `size < limit` at the moment of calling. This only
487
- * considers queued (not yet started) tasks; running tasks are tracked by
488
- * `pending`.
489
- *
490
- * @param limit Threshold that `size` must be below to resolve
491
- */
492
- onSizeLessThan(limit) {
493
- if (this.#size < limit) {
494
- return Promise.resolve();
495
- }
496
- return new Promise((resolve) => {
497
- this.#sizeLessThanWaiters.push({ limit, resolve });
498
- });
499
- }
500
- /**
501
- * Clear all queued tasks (does not affect currently running tasks)
502
- *
503
- * This removes all tasks waiting in the queue but allows currently executing
504
- * tasks to complete normally.
505
- *
506
- * @example
507
- * ```typescript
508
- * const queue = createAsyncQueue({ concurrency: 1 });
509
- *
510
- * // Add multiple tasks
511
- * queue.resultifyAdd(async () => longRunningTask()); // Will start immediately
512
- * queue.resultifyAdd(async () => task2()); // Queued
513
- * queue.resultifyAdd(async () => task3()); // Queued
514
- *
515
- * // Clear remaining queued tasks
516
- * queue.clear();
517
- *
518
- * // Only the first task will complete
519
- * await queue.onIdle();
520
- * ```;
521
- */
522
- clear() {
523
- this.#queue = [];
524
- this.#size = 0;
525
- for (const timeoutId of this.#rateLimitTimeouts) {
526
- clearTimeout(timeoutId);
527
- }
528
- this.#rateLimitTimeouts.clear();
529
- if (this.#pending === 0) {
530
- this.#resolveIdleWaiters();
531
- }
532
- this.#resolveSizeLessThanWaiters();
533
- }
534
- /** Number of tasks that have completed successfully */
535
- get completed() {
536
- return this.#completed;
537
- }
538
- /** Number of tasks that have failed */
539
- get failed() {
540
- return this.#failed;
541
- }
542
- /** Number of tasks currently being processed */
543
- get pending() {
544
- return this.#pending;
545
- }
546
- /** Number of tasks waiting in the queue to be processed */
547
- get size() {
548
- return this.#size;
549
- }
550
- /**
551
- * Manually start processing tasks (only needed if autoStart: false)
552
- *
553
- * @example
554
- * ```typescript
555
- * const queue = createAsyncQueue({ autoStart: false });
556
- *
557
- * // Add tasks without starting processing
558
- * queue.resultifyAdd(async () => 'task1');
559
- * queue.resultifyAdd(async () => 'task2');
560
- *
561
- * // Start processing when ready
562
- * queue.start();
563
- * await queue.onIdle();
564
- * ```;
565
- */
566
- start() {
567
- if (this.#stopped) {
568
- return;
569
- }
570
- this.#started = true;
571
- this.#processQueue();
572
- }
573
- /**
574
- * Pause processing new tasks (currently running tasks continue)
575
- *
576
- * @example
577
- * ```typescript
578
- * const queue = createAsyncQueue();
579
- *
580
- * // Start some tasks
581
- * queue.resultifyAdd(async () => longRunningTask1());
582
- * queue.resultifyAdd(async () => longRunningTask2());
583
- *
584
- * // Pause before more tasks are picked up
585
- * queue.pause();
586
- *
587
- * // Later, resume processing
588
- * queue.resume();
589
- * ```;
590
- */
591
- pause() {
592
- this.#paused = true;
593
- }
594
- /** Resume processing tasks after pause */
595
- resume() {
596
- this.#paused = false;
597
- if (this.#started && !this.#stopped) {
598
- this.#processQueue();
599
- }
600
- }
601
- /**
602
- * Reset the queue after being stopped, allowing new tasks to be processed
603
- *
604
- * This clears the stopped state and error reason, and resumes processing any
605
- * remaining queued tasks if autoStart was enabled.
606
- *
607
- * @example
608
- * ```typescript
609
- * const queue = createAsyncQueue({ stopOnError: true });
610
- *
611
- * // Add tasks that will cause the queue to stop
612
- * queue.resultifyAdd(async () => { throw new Error('fail'); });
613
- * queue.resultifyAdd(async () => 'remaining task');
614
- *
615
- * await queue.onIdle();
616
- *
617
- * if (queue.isStopped) {
618
- * console.log(`Queue stopped, ${queue.size} tasks remaining`);
619
- *
620
- * // Reset and process remaining tasks
621
- * queue.reset();
622
- * await queue.onIdle();
623
- * }
624
- * ```
625
- */
626
- reset() {
627
- this.#stopped = false;
628
- this.#stoppedReason = void 0;
629
- if (this.#autoStart) {
630
- this.#started = true;
631
- this.#processQueue();
632
- }
633
- }
634
- /** Whether the queue is stopped due to an error */
635
- get isStopped() {
636
- return this.#stopped;
637
- }
638
- /** Whether the queue is currently paused */
639
- get isPaused() {
640
- return this.#paused;
641
- }
642
- /** Whether the queue has been started (relevant for autoStart: false) */
643
- get isStarted() {
644
- return this.#started;
645
- }
646
- /** The error that caused the queue to stop (if any) */
647
- get stoppedReason() {
648
- return this.#stoppedReason;
649
- }
650
- };
651
- var AsyncQueueWithMeta = class extends AsyncQueue {
652
- constructor(options) {
653
- super(options);
654
- }
655
- add(fn, options) {
656
- return super.add(fn, options);
657
- }
658
- resultifyAdd(fn, options) {
659
- return super.resultifyAdd(fn, options);
660
- }
661
- };
662
- function createAsyncQueue(options) {
663
- return new AsyncQueue(options);
664
- }
665
- function createAsyncQueueWithMeta(options) {
666
- return new AsyncQueueWithMeta(options);
667
- }
668
- // Annotate the CommonJS export names for ESM import in node:
669
- 0 && (module.exports = {
670
- createAsyncQueue,
671
- createAsyncQueueWithMeta
672
- });