modern-ts 0.8.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 (261) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +223 -0
  3. package/README.zh-CN.md +223 -0
  4. package/dist/chunks/Errors-BWEYu-AB.cjs +2 -0
  5. package/dist/chunks/Errors-BWEYu-AB.cjs.map +1 -0
  6. package/dist/chunks/Errors-DwUAvFIF.mjs +63 -0
  7. package/dist/chunks/Errors-DwUAvFIF.mjs.map +1 -0
  8. package/dist/chunks/Math-CGEH1ClN.mjs +68 -0
  9. package/dist/chunks/Math-CGEH1ClN.mjs.map +1 -0
  10. package/dist/chunks/Math-CWZVxSF-.cjs +2 -0
  11. package/dist/chunks/Math-CWZVxSF-.cjs.map +1 -0
  12. package/dist/chunks/base-B9k8Lqvf.mjs +56 -0
  13. package/dist/chunks/base-B9k8Lqvf.mjs.map +1 -0
  14. package/dist/chunks/base-BTXL1y_I.cjs +2 -0
  15. package/dist/chunks/base-BTXL1y_I.cjs.map +1 -0
  16. package/dist/chunks/base-BpECeKwU.mjs +19 -0
  17. package/dist/chunks/base-BpECeKwU.mjs.map +1 -0
  18. package/dist/chunks/base-BvHqpP_s.cjs +2 -0
  19. package/dist/chunks/base-BvHqpP_s.cjs.map +1 -0
  20. package/dist/chunks/base-K-RHLZ-S.mjs +46 -0
  21. package/dist/chunks/base-K-RHLZ-S.mjs.map +1 -0
  22. package/dist/chunks/base-r6kn0B0D.cjs +2 -0
  23. package/dist/chunks/base-r6kn0B0D.cjs.map +1 -0
  24. package/dist/chunks/combine-async-DcG-3p8i.cjs +2 -0
  25. package/dist/chunks/combine-async-DcG-3p8i.cjs.map +1 -0
  26. package/dist/chunks/combine-async-Dcg4ya1R.mjs +236 -0
  27. package/dist/chunks/combine-async-Dcg4ya1R.mjs.map +1 -0
  28. package/dist/chunks/delay-CG3cD7bk.cjs +2 -0
  29. package/dist/chunks/delay-CG3cD7bk.cjs.map +1 -0
  30. package/dist/chunks/delay-CKIXpP71.mjs +32 -0
  31. package/dist/chunks/delay-CKIXpP71.mjs.map +1 -0
  32. package/dist/chunks/helper-BVabbpmX.mjs +165 -0
  33. package/dist/chunks/helper-BVabbpmX.mjs.map +1 -0
  34. package/dist/chunks/helper-tsqZMRHW.cjs +3 -0
  35. package/dist/chunks/helper-tsqZMRHW.cjs.map +1 -0
  36. package/dist/chunks/local-8JVtLN0O.mjs +10 -0
  37. package/dist/chunks/local-8JVtLN0O.mjs.map +1 -0
  38. package/dist/chunks/local-Da4yU-nE.cjs +2 -0
  39. package/dist/chunks/local-Da4yU-nE.cjs.map +1 -0
  40. package/dist/chunks/types-B7X_LBjM.cjs +2 -0
  41. package/dist/chunks/types-B7X_LBjM.cjs.map +1 -0
  42. package/dist/chunks/types-DE2SnSbz.mjs +5 -0
  43. package/dist/chunks/types-DE2SnSbz.mjs.map +1 -0
  44. package/dist/index.cjs +2 -0
  45. package/dist/index.cjs.map +1 -0
  46. package/dist/index.mjs +759 -0
  47. package/dist/index.mjs.map +1 -0
  48. package/dist/subpath/Arr.cjs +2 -0
  49. package/dist/subpath/Arr.cjs.map +1 -0
  50. package/dist/subpath/Arr.mjs +539 -0
  51. package/dist/subpath/Arr.mjs.map +1 -0
  52. package/dist/subpath/Concurrent.cjs +15 -0
  53. package/dist/subpath/Concurrent.cjs.map +1 -0
  54. package/dist/subpath/Concurrent.mjs +4044 -0
  55. package/dist/subpath/Concurrent.mjs.map +1 -0
  56. package/dist/subpath/FetchQ.cjs +2 -0
  57. package/dist/subpath/FetchQ.cjs.map +1 -0
  58. package/dist/subpath/FetchQ.mjs +519 -0
  59. package/dist/subpath/FetchQ.mjs.map +1 -0
  60. package/dist/subpath/Fit.cjs +2 -0
  61. package/dist/subpath/Fit.cjs.map +1 -0
  62. package/dist/subpath/Fit.mjs +914 -0
  63. package/dist/subpath/Fit.mjs.map +1 -0
  64. package/dist/subpath/Lazy.cjs +2 -0
  65. package/dist/subpath/Lazy.cjs.map +1 -0
  66. package/dist/subpath/Lazy.mjs +72 -0
  67. package/dist/subpath/Lazy.mjs.map +1 -0
  68. package/dist/subpath/Maps.cjs +2 -0
  69. package/dist/subpath/Maps.cjs.map +1 -0
  70. package/dist/subpath/Maps.mjs +123 -0
  71. package/dist/subpath/Maps.mjs.map +1 -0
  72. package/dist/subpath/Maybe.cjs +2 -0
  73. package/dist/subpath/Maybe.cjs.map +1 -0
  74. package/dist/subpath/Maybe.mjs +329 -0
  75. package/dist/subpath/Maybe.mjs.map +1 -0
  76. package/dist/subpath/Reactive.cjs +2 -0
  77. package/dist/subpath/Reactive.cjs.map +1 -0
  78. package/dist/subpath/Reactive.mjs +1204 -0
  79. package/dist/subpath/Reactive.mjs.map +1 -0
  80. package/dist/subpath/Reader.cjs +2 -0
  81. package/dist/subpath/Reader.cjs.map +1 -0
  82. package/dist/subpath/Reader.mjs +75 -0
  83. package/dist/subpath/Reader.mjs.map +1 -0
  84. package/dist/subpath/ReaderT.cjs +2 -0
  85. package/dist/subpath/ReaderT.cjs.map +1 -0
  86. package/dist/subpath/ReaderT.mjs +148 -0
  87. package/dist/subpath/ReaderT.mjs.map +1 -0
  88. package/dist/subpath/Resource.cjs +2 -0
  89. package/dist/subpath/Resource.cjs.map +1 -0
  90. package/dist/subpath/Resource.mjs +150 -0
  91. package/dist/subpath/Resource.mjs.map +1 -0
  92. package/dist/subpath/Result.cjs +2 -0
  93. package/dist/subpath/Result.cjs.map +1 -0
  94. package/dist/subpath/Result.mjs +337 -0
  95. package/dist/subpath/Result.mjs.map +1 -0
  96. package/dist/subpath/Sets.cjs +2 -0
  97. package/dist/subpath/Sets.cjs.map +1 -0
  98. package/dist/subpath/Sets.mjs +92 -0
  99. package/dist/subpath/Sets.mjs.map +1 -0
  100. package/dist/subpath/Str.cjs +2 -0
  101. package/dist/subpath/Str.cjs.map +1 -0
  102. package/dist/subpath/Str.mjs +114 -0
  103. package/dist/subpath/Str.mjs.map +1 -0
  104. package/dist/subpath/TxScope.cjs +2 -0
  105. package/dist/subpath/TxScope.cjs.map +1 -0
  106. package/dist/subpath/TxScope.mjs +138 -0
  107. package/dist/subpath/TxScope.mjs.map +1 -0
  108. package/dist/subpath/TypeTool.cjs +2 -0
  109. package/dist/subpath/TypeTool.cjs.map +1 -0
  110. package/dist/subpath/TypeTool.mjs +9 -0
  111. package/dist/subpath/TypeTool.mjs.map +1 -0
  112. package/dist/subpath/Utils.cjs +2 -0
  113. package/dist/subpath/Utils.cjs.map +1 -0
  114. package/dist/subpath/Utils.mjs +835 -0
  115. package/dist/subpath/Utils.mjs.map +1 -0
  116. package/dist/subpath/VirtualTime.cjs +2 -0
  117. package/dist/subpath/VirtualTime.cjs.map +1 -0
  118. package/dist/subpath/VirtualTime.mjs +1186 -0
  119. package/dist/subpath/VirtualTime.mjs.map +1 -0
  120. package/dist/types/Arr.d.ts +2 -0
  121. package/dist/types/Concurrent/Lock/condition_variable.d.ts +138 -0
  122. package/dist/types/Concurrent/Lock/mutex.d.ts +185 -0
  123. package/dist/types/Concurrent/Lock/rwlock.d.ts +253 -0
  124. package/dist/types/Concurrent/Lock/semaphore.d.ts +208 -0
  125. package/dist/types/Concurrent/Valve/__export__.d.ts +5 -0
  126. package/dist/types/Concurrent/Valve/circuit-breaker.d.ts +146 -0
  127. package/dist/types/Concurrent/Valve/leaky-bucket.d.ts +115 -0
  128. package/dist/types/Concurrent/Valve/sliding-window.d.ts +91 -0
  129. package/dist/types/Concurrent/Valve/token-bucket.d.ts +68 -0
  130. package/dist/types/Concurrent/__export__.d.ts +15 -0
  131. package/dist/types/Concurrent/barrier.d.ts +255 -0
  132. package/dist/types/Concurrent/channel.d.ts +239 -0
  133. package/dist/types/Concurrent/delay.d.ts +42 -0
  134. package/dist/types/Concurrent/ext/map.d.ts +82 -0
  135. package/dist/types/Concurrent/ext/other.d.ts +113 -0
  136. package/dist/types/Concurrent/ext/race.d.ts +31 -0
  137. package/dist/types/Concurrent/ext/some.d.ts +64 -0
  138. package/dist/types/Concurrent/limiter.d.ts +131 -0
  139. package/dist/types/Concurrent/scheduler.d.ts +171 -0
  140. package/dist/types/Concurrent/task-scope.d.ts +282 -0
  141. package/dist/types/Concurrent.d.ts +2 -0
  142. package/dist/types/Errors.d.ts +66 -0
  143. package/dist/types/FetchQ.d.ts +2 -0
  144. package/dist/types/Fit/__export__.d.ts +5 -0
  145. package/dist/types/Fit/base.d.ts +325 -0
  146. package/dist/types/Fit/parser.d.ts +110 -0
  147. package/dist/types/Fit/standard-schema.d.ts +127 -0
  148. package/dist/types/Fit/sugar.d.ts +274 -0
  149. package/dist/types/Fit/tool.d.ts +585 -0
  150. package/dist/types/Fit.d.ts +2 -0
  151. package/dist/types/Lazy.d.ts +2 -0
  152. package/dist/types/Maps.d.ts +2 -0
  153. package/dist/types/Maybe/__export__.d.ts +6 -0
  154. package/dist/types/Maybe/base.d.ts +62 -0
  155. package/dist/types/Maybe/consumers-async.d.ts +58 -0
  156. package/dist/types/Maybe/consumers.d.ts +63 -0
  157. package/dist/types/Maybe/operators-async.d.ts +145 -0
  158. package/dist/types/Maybe/operators.d.ts +177 -0
  159. package/dist/types/Maybe/types.d.ts +12 -0
  160. package/dist/types/Maybe.d.ts +2 -0
  161. package/dist/types/MockClock/Timeline.d.ts +392 -0
  162. package/dist/types/MockClock/TimerHandle.d.ts +159 -0
  163. package/dist/types/MockClock/TimerHeap.d.ts +21 -0
  164. package/dist/types/MockClock/VirtualTimeManager.d.ts +23 -0
  165. package/dist/types/MockClock/__export__.d.ts +104 -0
  166. package/dist/types/MockClock/hooks/__export__.d.ts +9 -0
  167. package/dist/types/MockClock/hooks/hookAbortSignal.d.ts +3 -0
  168. package/dist/types/MockClock/hooks/hookDate.d.ts +3 -0
  169. package/dist/types/MockClock/hooks/hookIntl.d.ts +3 -0
  170. package/dist/types/MockClock/hooks/hookMessageChannel.d.ts +3 -0
  171. package/dist/types/MockClock/hooks/hookMicrotasks.d.ts +3 -0
  172. package/dist/types/MockClock/hooks/hookOs.d.ts +3 -0
  173. package/dist/types/MockClock/hooks/hookPerformance.d.ts +3 -0
  174. package/dist/types/MockClock/hooks/hookProcess.d.ts +3 -0
  175. package/dist/types/MockClock/hooks/hookTimers.d.ts +3 -0
  176. package/dist/types/MockClock/types.d.ts +103 -0
  177. package/dist/types/Other/FetchQ.d.ts +320 -0
  178. package/dist/types/Other/deque.d.ts +59 -0
  179. package/dist/types/Other/disjointSet.d.ts +53 -0
  180. package/dist/types/Other/heap.d.ts +41 -0
  181. package/dist/types/Other/lazy.d.ts +127 -0
  182. package/dist/types/Other/queue.d.ts +42 -0
  183. package/dist/types/Other/secret.d.ts +111 -0
  184. package/dist/types/Other/stack.d.ts +40 -0
  185. package/dist/types/Reactive/__export__.d.ts +4 -0
  186. package/dist/types/Reactive/event_emitter.d.ts +195 -0
  187. package/dist/types/Reactive/flow-combination.d.ts +74 -0
  188. package/dist/types/Reactive/flow.d.ts +180 -0
  189. package/dist/types/Reactive/pace.d.ts +80 -0
  190. package/dist/types/Reactive.d.ts +2 -0
  191. package/dist/types/Reader/__export-readerT__.d.ts +4 -0
  192. package/dist/types/Reader/__export-reader__.d.ts +4 -0
  193. package/dist/types/Reader/local.d.ts +39 -0
  194. package/dist/types/Reader/reader-async.d.ts +84 -0
  195. package/dist/types/Reader/reader.d.ts +96 -0
  196. package/dist/types/Reader/readerT-async.d.ts +186 -0
  197. package/dist/types/Reader/readerT.d.ts +178 -0
  198. package/dist/types/Reader/types.d.ts +37 -0
  199. package/dist/types/Reader.d.ts +2 -0
  200. package/dist/types/ReaderT.d.ts +2 -0
  201. package/dist/types/Resource/TxScope-async.d.ts +86 -0
  202. package/dist/types/Resource/TxScope.d.ts +85 -0
  203. package/dist/types/Resource/__export-TxScope__.d.ts +2 -0
  204. package/dist/types/Resource/__export-resource__.d.ts +2 -0
  205. package/dist/types/Resource/resource-async.d.ts +79 -0
  206. package/dist/types/Resource/resource.d.ts +78 -0
  207. package/dist/types/Resource/types.d.ts +79 -0
  208. package/dist/types/Resource.d.ts +2 -0
  209. package/dist/types/Result/Consumers/branch-async.d.ts +77 -0
  210. package/dist/types/Result/Consumers/branch.d.ts +70 -0
  211. package/dist/types/Result/Consumers/inspect.d.ts +66 -0
  212. package/dist/types/Result/Operators/Async/combine-async.d.ts +69 -0
  213. package/dist/types/Result/Operators/Async/partition-async.d.ts +29 -0
  214. package/dist/types/Result/Operators/Async/reduce-async.d.ts +15 -0
  215. package/dist/types/Result/Operators/Async/transform-async.d.ts +90 -0
  216. package/dist/types/Result/Operators/combine.d.ts +60 -0
  217. package/dist/types/Result/Operators/flatten.d.ts +26 -0
  218. package/dist/types/Result/Operators/partition.d.ts +25 -0
  219. package/dist/types/Result/Operators/reduce.d.ts +12 -0
  220. package/dist/types/Result/Operators/transform.d.ts +70 -0
  221. package/dist/types/Result/__export__.d.ts +14 -0
  222. package/dist/types/Result/base.d.ts +115 -0
  223. package/dist/types/Result/types.d.ts +34 -0
  224. package/dist/types/Result.d.ts +2 -0
  225. package/dist/types/Sets.d.ts +2 -0
  226. package/dist/types/Str.d.ts +2 -0
  227. package/dist/types/TxScope.d.ts +2 -0
  228. package/dist/types/TypeTool.d.ts +2 -0
  229. package/dist/types/Utils/Array/__export__.d.ts +10 -0
  230. package/dist/types/Utils/Array/filtering.d.ts +89 -0
  231. package/dist/types/Utils/Array/flatten.d.ts +45 -0
  232. package/dist/types/Utils/Array/grouping.d.ts +44 -0
  233. package/dist/types/Utils/Array/iteration.d.ts +86 -0
  234. package/dist/types/Utils/Array/operators.d.ts +62 -0
  235. package/dist/types/Utils/Array/randomization.d.ts +76 -0
  236. package/dist/types/Utils/Array/set.d.ts +108 -0
  237. package/dist/types/Utils/Array/slice.d.ts +189 -0
  238. package/dist/types/Utils/Array/sorting.d.ts +55 -0
  239. package/dist/types/Utils/Array/zip.d.ts +93 -0
  240. package/dist/types/Utils/Functions/__export__.d.ts +5 -0
  241. package/dist/types/Utils/Functions/base.d.ts +157 -0
  242. package/dist/types/Utils/Functions/curry.d.ts +73 -0
  243. package/dist/types/Utils/Functions/debounce.d.ts +240 -0
  244. package/dist/types/Utils/Functions/defer.d.ts +43 -0
  245. package/dist/types/Utils/Functions/pipe.d.ts +205 -0
  246. package/dist/types/Utils/Map.d.ts +284 -0
  247. package/dist/types/Utils/Math.d.ts +225 -0
  248. package/dist/types/Utils/Object/__export__.d.ts +2 -0
  249. package/dist/types/Utils/Object/base.d.ts +131 -0
  250. package/dist/types/Utils/Object/clone.d.ts +81 -0
  251. package/dist/types/Utils/Predicates.d.ts +361 -0
  252. package/dist/types/Utils/Set.d.ts +105 -0
  253. package/dist/types/Utils/String.d.ts +217 -0
  254. package/dist/types/Utils/__export__.d.ts +9 -0
  255. package/dist/types/Utils/type-tool.d.ts +168 -0
  256. package/dist/types/Utils.d.ts +2 -0
  257. package/dist/types/VirtualTime.d.ts +2 -0
  258. package/dist/types/helper.d.ts +71 -0
  259. package/dist/types/index.d.ts +21 -0
  260. package/dist/types/unknown-error.d.ts +98 -0
  261. package/package.json +179 -0
@@ -0,0 +1,127 @@
1
+ import { AsyncResult, Result } from '../Result/types';
2
+ import { UnknownError } from '../unknown-error';
3
+ /**
4
+ * @typeParam T - The success value type.
5
+ */
6
+ export type Lazy<T> = () => T;
7
+ /**
8
+ * @typeParam T - The success value type.
9
+ */
10
+ export type LazyAsync<T> = (signal?: AbortSignal) => Promise<T>;
11
+ /**
12
+ * Wraps a concrete value into a synchronous Lazy function.
13
+ * @typeParam T - The type of the value.
14
+ * @param {T} value - The value to be lazily returned.
15
+ * @returns {Lazy<T>} A Lazy function that always returns the value.
16
+ */
17
+ export declare const wrap: <T>(value: T) => Lazy<T>;
18
+ /**
19
+ * Executes a synchronous Lazy function immediately.
20
+ * @typeParam T - The return type of the function.
21
+ * @param {Lazy<T>} fn - The Lazy function to execute.
22
+ * @returns {T} The computed result of the Lazy function.
23
+ */
24
+ export declare const run: <T>(fn: Lazy<T>) => T;
25
+ /**
26
+ * Wraps a concrete value into an asynchronous LazyAsync function.
27
+ * @typeParam T - The type of the value.
28
+ * @param {T} value - The value to be lazily returned.
29
+ * @returns {LazyAsync<T>} A LazyAsync function that asynchronously returns the value.
30
+ */
31
+ export declare const wrapAsync: <T>(value: T) => LazyAsync<T>;
32
+ /**
33
+ * Executes an asynchronous LazyAsync function immediately with optional abort signal.
34
+ * @typeParam T - The return type of the promise.
35
+ * @param {LazyAsync<T>} fn - The LazyAsync function to execute.
36
+ * @param {AbortSignal} [signal] - Optional AbortSignal to abort the operation.
37
+ * @returns {Promise<T>} The asynchronous computed result.
38
+ */
39
+ export declare const runAsync: <T>(fn: LazyAsync<T>, signal?: AbortSignal) => Promise<T>;
40
+ /**
41
+ * Executes a Lazy function only once and caches the result (Memoization).
42
+ * @typeParam T - The type of the computed value.
43
+ * @param {Lazy<T>} fn - The Lazy function to memoize.
44
+ * @returns {Lazy<T>} The memoized Lazy function.
45
+ */
46
+ export declare function Memoized<T>(fn: Lazy<T>): Lazy<T>;
47
+ /**
48
+ * Executes a LazyAsync function only once and caches the resulting Promise.
49
+ * Accepts AbortSignal which is passed to the underlying function on first call.
50
+ * Clears cache on abort to allow retries.
51
+ * @typeParam T - The type of the computed value.
52
+ * @param {LazyAsync<T>} fn - The LazyAsync function to memoize.
53
+ * @returns {LazyAsync<T>} The memoized LazyAsync function.
54
+ */
55
+ export declare function AsyncMemoized<T>(fn: LazyAsync<T>): LazyAsync<T>;
56
+ /**
57
+ * Executes a Lazy function within a try-catch block and ensures cleanup runs.
58
+ * Captures synchronous exceptions and wraps the result in Result.
59
+ * @typeParam T - The type of the success value.
60
+ * @param {Lazy<T>} fn - The Lazy function to execute.
61
+ * @param {() => void} [cleaner] - Optional synchronous cleanup function.
62
+ * @returns {Result<T, UnknownError>} Ok(T) on success, or Err(UnknownError) on exception.
63
+ */
64
+ export declare function runWithCleanup<T>(fn: Lazy<T>, cleaner?: () => void): Result<T, UnknownError>;
65
+ /**
66
+ * Executes LazyAsync with cleanup and abort support.
67
+ * @typeParam T - The type of the success value.
68
+ * @param {LazyAsync<T>} fn - The LazyAsync function to execute.
69
+ * @param {() => void | Promise<void>} [cleaner] - Optional cleanup function.
70
+ * @param {AbortSignal} [signal] - Optional AbortSignal to abort the operation. ✨ 新增参数
71
+ * @returns {AsyncResult<T, UnknownError>}
72
+ */
73
+ export declare function runAsyncWithCleanup<T>(fn: LazyAsync<T>, cleaner?: () => void | Promise<void>, signal?: AbortSignal): AsyncResult<T, UnknownError>;
74
+ /**
75
+ * Maps the result of a Lazy function to a new Lazy function (Functor map).
76
+ * @typeParam T - The input value type.
77
+ * @typeParam U - The output value type.
78
+ * @param {Lazy<T>} fn - The input Lazy function.
79
+ * @param {(value: T) => U} mapper - The synchronous mapping function.
80
+ * @returns {Lazy<U>} The composed Lazy function.
81
+ */
82
+ export declare const map: <T, U>(fn: Lazy<T>, mapper: (value: T) => U) => Lazy<U>;
83
+ /**
84
+ * Maps async LazyAsync result with abort support.
85
+ * @typeParam T - The input value type.
86
+ * @typeParam U - The output value type.
87
+ * @param {LazyAsync<T>} fn - The input LazyAsync function.
88
+ * @param {(value: T) => U | Promise<U>} mapper - Mapping function.
89
+ * @returns {LazyAsync<U>} Composed LazyAsync function.
90
+ */
91
+ export declare const mapAsync: <T, U>(fn: LazyAsync<T>, mapper: (value: T) => U | Promise<U>) => LazyAsync<U>;
92
+ /**
93
+ * Monadic bind operation (flatMap) for Lazy. Composes two Lazy functions.
94
+ * @typeParam T - The input value type.
95
+ * @typeParam U - The output value type.
96
+ * @param {Lazy<T>} fn - The initial Lazy function.
97
+ * @param {(value: T) => Lazy<U>} mapper - Maps the result T to a new Lazy<U>.
98
+ * @returns {Lazy<U>} The flattened composed Lazy function.
99
+ */
100
+ export declare const andThen: <T, U>(fn: Lazy<T>, mapper: (value: T) => Lazy<U>) => Lazy<U>;
101
+ /**
102
+ * Async monadic bind (flatMap) with abort support.
103
+ * @typeParam T - The input value type.
104
+ * @typeParam U - The output value type.
105
+ * @param {LazyAsync<T>} fn - Initial LazyAsync function.
106
+ * @param {(value: T) => LazyAsync<U>} mapper - Maps to new LazyAsync.
107
+ * @returns {LazyAsync<U>} Flattened composed function.
108
+ */
109
+ export declare const andThenAsync: <T, U>(fn: LazyAsync<T>, mapper: (value: T) => LazyAsync<U>) => LazyAsync<U>;
110
+ /**
111
+ * Combines the results of two Lazy functions into a tuple.
112
+ * @typeParam T - The type of the first value.
113
+ * @typeParam U - The type of the second value.
114
+ * @param {Lazy<T>} fn1 - The first Lazy function.
115
+ * @param {Lazy<U>} fn2 - The second Lazy function.
116
+ * @returns {Lazy<[T, U]>} A Lazy function returning the tuple [T, U].
117
+ */
118
+ export declare const zip: <T, U>(fn1: Lazy<T>, fn2: Lazy<U>) => Lazy<[T, U]>;
119
+ /**
120
+ * Concurrent execution of two LazyAsync with shared abort signal.
121
+ * @typeParam T - First value type.
122
+ * @typeParam U - Second value type.
123
+ * @param {LazyAsync<T>} fn1 - First LazyAsync function.
124
+ * @param {LazyAsync<U>} fn2 - Second LazyAsync function.
125
+ * @returns {LazyAsync<[T, U]>} Tuple result function.
126
+ */
127
+ export declare const zipAsync: <T, U>(fn1: LazyAsync<T>, fn2: LazyAsync<U>) => LazyAsync<[T, U]>;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * A FIFO (First-In-First-Out) queue implementation
3
+ * Uses circular buffer optimization to avoid O(n) shift operations
4
+ * @template T The type of elements stored in the queue
5
+ * @note It is recommended not to store `null` values in this queue, as `null` is used
6
+ * by `peek()` and `dequeue()` to indicate an empty queue.
7
+ * Storing `null` makes it impossible to distinguish between "queue is empty" and "actual null value stored".
8
+ */
9
+ export declare class Queue<T> {
10
+ private queue_data;
11
+ private head_idx;
12
+ /**
13
+ * Returns the number of elements in the queue
14
+ */
15
+ get size(): number;
16
+ /**
17
+ * Checks if the queue is empty
18
+ */
19
+ isEmpty(): boolean;
20
+ /**
21
+ * Returns the front element without removing it
22
+ * @returns The front element, or null if queue is empty
23
+ */
24
+ peek(): T | null;
25
+ /**
26
+ * Adds an element to the back of the queue
27
+ */
28
+ enqueue(item: T): void;
29
+ /**
30
+ * Removes and returns the front element from the queue
31
+ * @returns The front element, or null if queue is empty
32
+ */
33
+ dequeue(): T | null;
34
+ /**
35
+ * Removes all elements from the queue
36
+ */
37
+ clear(): void;
38
+ /**
39
+ * Converts the queue to an array (front element at index 0)
40
+ */
41
+ toArray(): T[];
42
+ }
@@ -0,0 +1,111 @@
1
+ /**
2
+ * A unique Symbol used to represent the state of a Secret instance when its original
3
+ * sensitive value has been destroyed or erased from memory.
4
+ * @internal
5
+ */
6
+ export declare const EMPTY: unique symbol;
7
+ export declare class SecretError extends Error {
8
+ constructor(message: string);
9
+ }
10
+ /**
11
+ * A secure container class for sensitive values (T).
12
+ *
13
+ * The class uses private fields, controlled access, and disposal mechanisms
14
+ * to protect data from accidental exposure in logs, serialization, and memory dumps.
15
+ *
16
+ * @template T The type of the sensitive value being protected.
17
+ * @implements {Disposable} (Implicitly, for TypeScript 5.2+ `using` support)
18
+ */
19
+ export declare class Secret<T> {
20
+ #private;
21
+ /**
22
+ * Creates a new Secret container for the given sensitive value.
23
+ *
24
+ * @param {T} value The sensitive value (e.g., an API key or password) to be secured.
25
+ */
26
+ constructor(value: T);
27
+ /**
28
+ * A factory method to create a new Secret instance.
29
+ *
30
+ * @template T The type of the sensitive value.
31
+ * @param {T} value The sensitive value to be wrapped.
32
+ * @returns {Secret<T>} A new Secret instance containing the value.
33
+ */
34
+ static make<T>(value: T): Secret<T>;
35
+ /**
36
+ * Reveals the original sensitive value stored within the Secret container.
37
+ * This is the only intended method to retrieve the protected data.
38
+ *
39
+ * @template T The type of the value being revealed.
40
+ * @param {Secret<T>} secret The Secret instance to reveal the value from.
41
+ * @returns {T} The original sensitive value.
42
+ * @throws {Error} If the Secret value has already been destroyed (`destroy` or `using` block exited).
43
+ */
44
+ static reveal<T>(secret: Secret<T>): T;
45
+ /**
46
+ * Destroys the sensitive value by overwriting its memory location with the `Empty` Symbol.
47
+ * This minimizes the duration the sensitive data resides in memory.
48
+ *
49
+ * After destruction, `reveal()` will throw an error.
50
+ *
51
+ * @template T The type of the value being destroyed.
52
+ * @param {Secret<T>} secret The Secret instance to destroy.
53
+ */
54
+ static destroy<T>(secret: Secret<T>): void;
55
+ /**
56
+ * Executes a callback function with the revealed sensitive value and ensures the secret
57
+ * is destroyed immediately after the callback completes, regardless of success or failure.
58
+ *
59
+ * @template T The type of the sensitive value.
60
+ * @template R The return type of the callback function.
61
+ * @param {Secret<T>} secret The Secret instance to operate on.
62
+ * @param {(value: T) => R} callback The function to execute with the revealed value.
63
+ * @returns {R} The result of the callback function.
64
+ */
65
+ static dispose<T, R>(secret: Secret<T>, callback: (value: T) => R): R;
66
+ /**
67
+ * Executes an asynchronous callback function with the revealed sensitive value and ensures the secret
68
+ * is destroyed immediately after the callback completes, regardless of success or failure.
69
+ *
70
+ * @template T The type of the sensitive value.
71
+ * @template R The resolved type of the callback function's Promise.
72
+ * @param {Secret<T>} secret The Secret instance to operate on.
73
+ * @param {(value: T) => Promise<R>} callback The asynchronous function to execute with the revealed value.
74
+ * @returns {Promise<R>} A Promise that resolves to the result of the asynchronous callback.
75
+ */
76
+ static disposeAsync<T, R>(secret: Secret<T>, callback: (value: T) => Promise<R>): Promise<R>;
77
+ /**
78
+ * Prevents accidental serialization of the secret value via `JSON.stringify()`.
79
+ * @throws {SecretError} Always throws an error to prevent serialization.
80
+ */
81
+ toJSON(): void;
82
+ /**
83
+ * Prevents accidental string conversion of the secret value.
84
+ * @throws {SecretError} Always throws an error to prevent conversion.
85
+ */
86
+ toString(): void;
87
+ /**
88
+ * Prevents accidental primitive conversion of the secret value.
89
+ * @throws {SecretError} Always throws an error to prevent conversion.
90
+ */
91
+ valueOf(): void;
92
+ /**
93
+ * Prevents accidental primitive conversion of the secret value, respecting the `hint`.
94
+ * @param {'string' | 'number' | 'default'} hint The desired conversion hint.
95
+ * @throws {SecretError} Always throws an error to prevent conversion.
96
+ */
97
+ [Symbol.toPrimitive](hint: 'string' | 'number' | 'default'): void;
98
+ /**
99
+ * Prevents accidental inspection via `Object.prototype.toString.call()`.
100
+ * @throws {SecretError} Always throws an error to prevent inspection.
101
+ */
102
+ get [Symbol.toStringTag](): void;
103
+ /**
104
+ * Implements the `[Symbol.dispose]` method, making the Secret compatible with
105
+ * the TypeScript 5.2+ `using` declaration for automatic resource management.
106
+ *
107
+ * When a `using` block exits, this method is called, which internally calls `destroy`.
108
+ * @internal
109
+ */
110
+ [Symbol.dispose](): void;
111
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * A LIFO (Last-In-First-Out) stack implementation
3
+ * @template T The type of elements stored in the stack
4
+ * @note It is recommended not to store `null` values in this stack, as `null` is used
5
+ * by `peek()` and `pop()` to indicate an empty stack.
6
+ * Storing `null` makes it impossible to distinguish between "stack is empty" and "actual null value stored".
7
+ */
8
+ export declare class Stack<T> {
9
+ private stack_data;
10
+ /**
11
+ * Returns the number of elements in the stack
12
+ */
13
+ get size(): number;
14
+ /**
15
+ * Checks if the stack is empty
16
+ */
17
+ isEmpty(): boolean;
18
+ /**
19
+ * Returns the top element without removing it
20
+ * @returns The top element, or null if stack is empty
21
+ */
22
+ peek(): T | null;
23
+ /**
24
+ * Pushes an element onto the top of the stack
25
+ */
26
+ push(item: T): void;
27
+ /**
28
+ * Removes and returns the top element from the stack
29
+ * @returns The top element, or null if stack is empty
30
+ */
31
+ pop(): T | null;
32
+ /**
33
+ * Removes all elements from the stack
34
+ */
35
+ clear(): void;
36
+ /**
37
+ * Converts the stack to an array (top element at the end)
38
+ */
39
+ toArray(): T[];
40
+ }
@@ -0,0 +1,4 @@
1
+ export * from './flow';
2
+ export * from './flow-combination';
3
+ export * from './pace';
4
+ export * from './event_emitter';
@@ -0,0 +1,195 @@
1
+ type Callback<T extends Record<string, unknown[]>, K extends keyof T> = (...args: T[K]) => void;
2
+ /**
3
+ * A type-safe event emitter that supports synchronous and asynchronous event handling.
4
+ *
5
+ * Features:
6
+ * - Full TypeScript type safety with event map support
7
+ * - Special events: `newListener`, `removeListener`, `error`
8
+ * - Supports prepend/once/async patterns
9
+ * - Deferred listener modification during emit (prevents iteration issues)
10
+ * - Optimized storage: single function -> list transition
11
+ *
12
+ * @template T - Event map where keys are event names and values are argument tuples
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * interface MyEvents {
17
+ * message: [text: string, from: string];
18
+ * data: [payload: unknown];
19
+ * error: [err: Error];
20
+ * }
21
+ *
22
+ * const emitter = new EventEmitter<MyEvents>();
23
+ * emitter.on('message', (text, from) => console.log(`${from}: ${text}`));
24
+ * emitter.emit('message', 'Hello', 'Alice');
25
+ * ```
26
+ */
27
+ export declare class EventEmitter<T extends Record<PropertyKey, unknown[]>> {
28
+ private events;
29
+ private emitting_depth;
30
+ private pending_removals_set;
31
+ private pending_removals_list;
32
+ private pending_additions;
33
+ /**
34
+ * Creates a new EventEmitter instance.
35
+ */
36
+ constructor();
37
+ private emitRemoveListener;
38
+ /**
39
+ * Registers an event listener that will be invoked each time the event is emitted.
40
+ *
41
+ * Triggers `newListener` event before adding the listener.
42
+ * During an active emit, additions are deferred until the emit completes.
43
+ *
44
+ * @template K - Event name key
45
+ * @param event - The event name to listen for
46
+ * @param callback - The listener function to register
47
+ * @returns This EventEmitter instance for chaining
48
+ */
49
+ on<K extends keyof T>(event: K, callback: Callback<T, K>): this;
50
+ /**
51
+ * Removes a previously registered event listener.
52
+ *
53
+ * Triggers `removeListener` event after removing the listener.
54
+ * During an active emit, removals are deferred until the emit completes.
55
+ *
56
+ * @template K - Event name key
57
+ * @param event - The event name to remove the listener from
58
+ * @param callback - The listener function to remove
59
+ * @returns This EventEmitter instance for chaining
60
+ */
61
+ off<K extends keyof T>(event: K, callback: Callback<T, K>): this;
62
+ private findWrappedFn;
63
+ private removeFromList;
64
+ /**
65
+ * Emits an event, invoking all registered listeners with the provided arguments.
66
+ *
67
+ * Listeners are invoked in order: prepend listeners (reverse order) then normal listeners.
68
+ * If no listeners exist and the event is `error`, the error is thrown.
69
+ * Listener errors are caught and forwarded to `error` event handlers.
70
+ *
71
+ * @template K - Event name key
72
+ * @param event - The event name to emit
73
+ * @param args - Arguments to pass to the listeners
74
+ * @returns `true` if all listeners completed without errors, `false` otherwise
75
+ * @throws The error argument if emitting `error` with no listeners
76
+ */
77
+ emit<K extends keyof T>(event: K, ...args: T[K]): boolean;
78
+ private handleEmitError;
79
+ private isPendingRemoval;
80
+ private flushPendingOperations;
81
+ /**
82
+ * Registers a one-time listener that removes itself after first invocation.
83
+ *
84
+ * @template K - Event name key
85
+ * @param event - The event name to listen for
86
+ * @param callback - The listener function to register
87
+ * @returns This EventEmitter instance for chaining
88
+ */
89
+ once<K extends keyof T>(event: K, callback: Callback<T, K>): this;
90
+ /**
91
+ * Registers a listener that will be invoked before other listeners.
92
+ *
93
+ * Prepend listeners are invoked in reverse order of registration (LIFO).
94
+ * Triggers `newListener` event before adding the listener.
95
+ *
96
+ * @template K - Event name key
97
+ * @param event - The event name to listen for
98
+ * @param callback - The listener function to register
99
+ * @returns This EventEmitter instance for chaining
100
+ */
101
+ prependListener<K extends keyof T>(event: K, callback: Callback<T, K>): this;
102
+ /**
103
+ * Registers a one-time prepend listener.
104
+ *
105
+ * Combines `prependListener` and `once` behavior.
106
+ *
107
+ * @template K - Event name key
108
+ * @param event - The event name to listen for
109
+ * @param callback - The listener function to register
110
+ * @returns This EventEmitter instance for chaining
111
+ */
112
+ prependOnceListener<K extends keyof T>(event: K, callback: Callback<T, K>): this;
113
+ /**
114
+ * Returns the number of listeners registered for the specified event.
115
+ *
116
+ * @param event - The event name to query
117
+ * @returns The count of registered listeners
118
+ */
119
+ listenerCount(event: keyof T): number;
120
+ /**
121
+ * Returns a copy of the listeners array for the specified event.
122
+ *
123
+ * @template K - Event name key
124
+ * @param event - The event name to query
125
+ * @returns Array of registered listener functions
126
+ */
127
+ listeners<K extends keyof T>(event: K): Callback<T, K>[];
128
+ /**
129
+ * Returns an array of event names that have registered listeners.
130
+ *
131
+ * @returns Array of event names with at least one listener
132
+ */
133
+ eventNames(): (keyof T)[];
134
+ /**
135
+ * Removes all listeners for a specific event, or all listeners if no event is specified.
136
+ *
137
+ * Triggers `removeListener` event for each removed listener.
138
+ * During an active emit, removals are deferred until the emit completes.
139
+ *
140
+ * @param event - Optional event name to remove listeners from
141
+ * @returns This EventEmitter instance for chaining
142
+ */
143
+ removeAllListeners(event?: keyof T): this;
144
+ /**
145
+ * Asynchronously emits an event, awaiting all listener results.
146
+ *
147
+ * All listeners are executed in parallel using `Promise.all`.
148
+ * Listener errors are collected and thrown as `AggregateError` if multiple,
149
+ * or re-thrown directly if only one error occurred.
150
+ *
151
+ * @template K - Event name key
152
+ * @param event - The event name to emit
153
+ * @param args - Arguments to pass to the listeners
154
+ * @returns Promise resolving to `true` if all listeners succeeded
155
+ * @throws The error argument if emitting `error` with no listeners
156
+ * @throws AggregateError if multiple listeners threw errors
157
+ */
158
+ emitAsync<K extends keyof T>(event: K, ...args: T[K]): Promise<boolean>;
159
+ private handleAsyncError;
160
+ /**
161
+ * Returns a promise that resolves when the specified event is emitted.
162
+ *
163
+ * Supports timeout, filter function, and AbortSignal for cancellation.
164
+ *
165
+ * @template K - Event name key
166
+ * @param event - The event name to wait for
167
+ * @param options - Optional configuration
168
+ * @param options.timeout - Maximum time to wait in milliseconds
169
+ * @param options.filter - Function to filter which emissions to accept
170
+ * @param options.signal - AbortSignal for cancellation
171
+ * @returns Promise resolving to the event arguments tuple
172
+ * @throws Error on timeout
173
+ * @throws DOMException when aborted via signal
174
+ */
175
+ waitFor<K extends keyof T>(event: K, options?: {
176
+ timeout?: number;
177
+ filter?: (...args: T[K]) => boolean;
178
+ signal?: AbortSignal;
179
+ }): Promise<T[K]>;
180
+ /**
181
+ * Alias for `waitFor` without filter support.
182
+ *
183
+ * @template K - Event name key
184
+ * @param event - The event name to wait for
185
+ * @param options - Optional configuration
186
+ * @param options.timeout - Maximum time to wait in milliseconds
187
+ * @param options.signal - AbortSignal for cancellation
188
+ * @returns Promise resolving to the event arguments tuple
189
+ */
190
+ onceAsync<K extends keyof T>(event: K, options?: {
191
+ timeout?: number;
192
+ signal?: AbortSignal;
193
+ }): Promise<T[K]>;
194
+ }
195
+ export {};
@@ -0,0 +1,74 @@
1
+ import { Flow, Observer, Unsubscribable } from './flow';
2
+ type FlowInputValue<T> = T extends FlowInput<infer R> ? R : never;
3
+ type FlowInputTuple<T extends readonly FlowInput<unknown>[]> = {
4
+ [K in keyof T]: FlowInputValue<T[K]>;
5
+ };
6
+ type PrependTuple<T, R extends readonly unknown[]> = [T, ...R];
7
+ interface FlowInput<T> {
8
+ subscribe(observer: Observer<T>): Unsubscribable;
9
+ }
10
+ /**
11
+ * Merges multiple Flow sources into a single Flow that emits values
12
+ * from all sources concurrently. Completes when all sources complete.
13
+ * @template T The value type emitted by the sources
14
+ * @param sources Flow sources to merge
15
+ * @returns A Flow emitting values from all sources as they arrive
16
+ */
17
+ export declare function mergeFlow<T>(...sources: [FlowInput<T>, ...FlowInput<T>[]]): Flow<T>;
18
+ /**
19
+ * Concatenates multiple Flow sources sequentially, subscribing to each
20
+ * source only after the previous one completes.
21
+ * @template T The value type emitted by the sources
22
+ * @param sources Flow sources to concatenate
23
+ * @returns A Flow emitting values from each source in order
24
+ */
25
+ export declare function concatFlow<T>(...sources: [FlowInput<T>, ...FlowInput<T>[]]): Flow<T>;
26
+ /**
27
+ * Races multiple Flow sources, emitting the first value, error, or completion
28
+ * from any source and immediately unsubscribing from all others.
29
+ * @template T The value type emitted by the sources
30
+ * @param sources Flow sources to race
31
+ * @returns A Flow mirroring the first source to emit
32
+ */
33
+ export declare function raceFlow<T>(...sources: [FlowInput<T>, ...FlowInput<T>[]]): Flow<T>;
34
+ /**
35
+ * Combines multiple Flow sources to emit tuples of the latest values
36
+ * from all sources whenever any source emits. Waits for all sources
37
+ * to emit at least once before emitting.
38
+ * @template T A tuple type of Flow sources
39
+ * @param sources Flow sources to combine
40
+ * @returns A Flow emitting tuples of the latest values from all sources
41
+ */
42
+ export declare function combineLatestFlow<T extends readonly [FlowInput<unknown>, ...FlowInput<unknown>[]]>(...sources: T): Flow<FlowInputTuple<T>>;
43
+ /**
44
+ * Combines a source Flow with other Flows, emitting a tuple of the source value
45
+ * and the latest values from other sources whenever the source emits.
46
+ * Waits for all other sources to emit at least once.
47
+ * @template T The value type emitted by the main source
48
+ * @template R A tuple type of other Flow sources
49
+ * @param source The main Flow that triggers emissions
50
+ * @param others Other Flow sources to combine with
51
+ * @returns A Flow emitting tuples with the source value prepended to other values
52
+ */
53
+ export declare function withLatestFromFlow<T, R extends readonly [FlowInput<unknown>, ...FlowInput<unknown>[]]>(source: FlowInput<T>, ...others: R): Flow<PrependTuple<T, FlowInputTuple<R>>>;
54
+ /**
55
+ * Waits for all Flow sources to complete, then emits a tuple of their last values.
56
+ * Emits once when all sources complete. Completes without emitting if any
57
+ * source completes without emitting.
58
+ * @template T A tuple type of Flow sources
59
+ * @param sources Flow sources to join
60
+ * @returns A Flow emitting a single tuple of last values when all sources complete
61
+ */
62
+ export declare function forkJoinFlow<T extends readonly [FlowInput<unknown>, ...FlowInput<unknown>[]]>(...sources: T): Flow<FlowInputTuple<T>>;
63
+ /**
64
+ * Zips multiple Flow sources by index, emitting tuples containing the nth
65
+ * value from each source. Completes when any source completes and its
66
+ * buffer is empty.
67
+ * @template T A tuple type of Flow sources
68
+ * @param sources Flow sources to zip
69
+ * @returns A Flow emitting tuples of values at the same index from each source
70
+ */
71
+ export declare function zipFlow<T extends readonly [FlowInput<unknown>, ...FlowInput<unknown>[]]>(...sources: T): Flow<{
72
+ [K in keyof T]: T[K] extends FlowInput<infer R> ? R : never;
73
+ }>;
74
+ export {};