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,148 @@
1
+ import { O as o, E as u } from "../chunks/base-K-RHLZ-S.mjs";
2
+ import { m as v, a as i, b as x, c as T, d as h, e as O, f as d, g, h as M, i as P } from "../chunks/combine-async-Dcg4ya1R.mjs";
3
+ import { i as y } from "../chunks/base-BpECeKwU.mjs";
4
+ import { l as R } from "../chunks/local-8JVtLN0O.mjs";
5
+ const S = (r) => (e) => o(r), j = () => (r) => o(r), q = (r) => (e) => u(r), N = (r) => (e) => r, m = (r, e) => (t) => y(r) ? o(r) : u(e), z = m, p = (r, e) => (t) => {
6
+ const n = r(t);
7
+ return v(n, e);
8
+ }, B = (r, e) => (t) => {
9
+ const n = r(t);
10
+ return i(n, (s) => e(s)(t));
11
+ }, l = (r, e) => (t) => {
12
+ const n = r(t), s = e(t);
13
+ return x(n, s);
14
+ }, A = (r) => (e, t) => {
15
+ const n = p(e, (s) => (c) => r(s, c));
16
+ return l(n, t);
17
+ }, C = (r) => (e, t, n) => {
18
+ const s = A(
19
+ (c, a) => (_) => r(c, a, _)
20
+ )(e, t);
21
+ return l(s, n);
22
+ }, D = (r, e) => (t) => {
23
+ const n = r(t);
24
+ return i(n, (s) => {
25
+ const a = e(s)(t);
26
+ return a.ok ? o(s) : a;
27
+ });
28
+ }, F = (r, e) => (t) => {
29
+ const n = r(t);
30
+ return T(n, e);
31
+ }, G = (r, e) => (t) => {
32
+ const n = r(t);
33
+ if (n.ok)
34
+ return n;
35
+ const s = n.error;
36
+ return e(s)(t);
37
+ }, b = (r) => (e) => (t) => {
38
+ const n = e.map((s) => r(s)(t));
39
+ return h(n);
40
+ }, H = (r) => b((e) => e)(r), I = (r) => async (e) => o(r), J = () => async (r) => o(r), K = (r) => async (e) => u(r), L = (r) => async (e) => r, w = (r, e) => async (t) => y(r) ? o(r) : u(e), Q = w, E = (r, e) => async (t) => {
41
+ const n = await r(t);
42
+ return O(n, e);
43
+ }, U = (r, e) => async (t) => {
44
+ const n = await r(t);
45
+ return d(n, async (s) => await e(s)(t));
46
+ }, f = (r, e) => async (t) => {
47
+ const n = r(t), s = e(t), [c, a] = await Promise.all([n, s]);
48
+ return g(c, a);
49
+ }, $ = (r) => (e, t) => {
50
+ const n = E(e, (s) => (c) => r(s, c));
51
+ return f(n, t);
52
+ }, V = (r) => (e, t, n) => {
53
+ const s = $((c, a) => (_) => r(c, a, _))(
54
+ e,
55
+ t
56
+ );
57
+ return f(s, n);
58
+ }, W = (r, e) => async (t) => {
59
+ const n = await r(t);
60
+ return d(n, async (s) => {
61
+ const a = await e(s)(t);
62
+ return a.ok ? o(s) : a;
63
+ });
64
+ }, X = (r, e) => async (t) => {
65
+ const n = await r(t);
66
+ return M(n, e);
67
+ }, Y = (r, e) => async (t) => {
68
+ const n = await r(t);
69
+ if (n.ok)
70
+ return n;
71
+ const s = n.error;
72
+ return await e(s)(t);
73
+ }, k = (r) => (e) => async (t) => {
74
+ const n = e.map((c) => r(c)(t)), s = await Promise.all(n);
75
+ return P(s);
76
+ }, Z = (r) => k((e) => e)(r), sr = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
77
+ __proto__: null,
78
+ andThen: B,
79
+ andThenAsync: U,
80
+ ap: l,
81
+ apAsync: f,
82
+ ask: j,
83
+ askAsync: J,
84
+ fail: q,
85
+ failAsync: K,
86
+ fromMaybe: m,
87
+ fromMaybeAsync: w,
88
+ fromNullable: z,
89
+ fromNullableAsync: Q,
90
+ fromResult: N,
91
+ fromResultAsync: L,
92
+ liftA2: A,
93
+ liftA2Async: $,
94
+ liftA3: C,
95
+ liftA3Async: V,
96
+ local: R,
97
+ map: p,
98
+ mapAsync: E,
99
+ mapErr: F,
100
+ mapErrAsync: X,
101
+ of: S,
102
+ ofAsync: I,
103
+ orElse: G,
104
+ orElseAsync: Y,
105
+ sequence: H,
106
+ sequenceAsync: Z,
107
+ tap: D,
108
+ tapAsync: W,
109
+ traverse: b,
110
+ traverseAsync: k
111
+ }, Symbol.toStringTag, { value: "Module" }));
112
+ export {
113
+ sr as _,
114
+ B as andThen,
115
+ U as andThenAsync,
116
+ l as ap,
117
+ f as apAsync,
118
+ j as ask,
119
+ J as askAsync,
120
+ q as fail,
121
+ K as failAsync,
122
+ m as fromMaybe,
123
+ w as fromMaybeAsync,
124
+ z as fromNullable,
125
+ Q as fromNullableAsync,
126
+ N as fromResult,
127
+ L as fromResultAsync,
128
+ A as liftA2,
129
+ $ as liftA2Async,
130
+ C as liftA3,
131
+ V as liftA3Async,
132
+ R as local,
133
+ p as map,
134
+ E as mapAsync,
135
+ F as mapErr,
136
+ X as mapErrAsync,
137
+ S as of,
138
+ I as ofAsync,
139
+ G as orElse,
140
+ Y as orElseAsync,
141
+ H as sequence,
142
+ Z as sequenceAsync,
143
+ D as tap,
144
+ W as tapAsync,
145
+ b as traverse,
146
+ k as traverseAsync
147
+ };
148
+ //# sourceMappingURL=ReaderT.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReaderT.mjs","sources":["../../src/Reader/readerT.ts","../../src/Reader/readerT-async.ts"],"sourcesContent":["// ========================================\r\n// ./src/Reader/readerT.ts\r\n// ========================================\r\nimport type {ReaderT} from './types';\r\nimport {Ok, Err} from '../Result/base';\r\nimport type {Result} from '../Result/types';\r\nimport {\r\n map as mapResult,\r\n andThen as andThenResult,\r\n mapErr as mapErrResult,\r\n} from '../Result/Operators/transform';\r\nimport {ap as apResult, all as allResult} from '../Result/Operators/combine';\r\nimport {type Maybe, isSome} from '../Maybe/__export__';\r\n\r\n/**\r\n * Lifts a pure value (A) into the ReaderT context as a successful result.\r\n * This is the 'return' or 'pure' operation for ReaderT.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the value to wrap.\r\n * @param a - The value to be wrapped in ReaderT.\r\n * @returns ReaderT<R, E, A> that always returns Ok(a).\r\n */\r\nexport const of = <R, E, A>(a: A): ReaderT<R, E, A> => {\r\n return (_env: R) => Ok(a);\r\n};\r\n\r\n/**\r\n * Retrieves the environment (R) as a successful result.\r\n * This is the 'ask' operation in ReaderT context.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @returns ReaderT<R, E, R> that returns Ok(env).\r\n */\r\nexport const ask = <R, E>(): ReaderT<R, E, R> => {\r\n return (env: R) => Ok(env);\r\n};\r\n\r\n/**\r\n * Creates a ReaderT that immediately fails with the given error (E).\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type parameter (not used, but required for type consistency).\r\n * @param e - The error value.\r\n * @returns ReaderT<R, E, A> that always returns Err(e).\r\n */\r\nexport const fail = <R, E, A>(e: E): ReaderT<R, E, A> => {\r\n return (_env: R) => Err(e);\r\n};\r\n\r\n/**\r\n * Lifts an existing Result into the ReaderT context.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The success value type.\r\n * @param result - The Result<A, E> to wrap.\r\n * @returns ReaderT<R, E, A> that always returns the wrapped Result.\r\n */\r\nexport const fromResult = <R, E, A>(result: Result<A, E>): ReaderT<R, E, A> => {\r\n return (_env: R) => result;\r\n};\r\n\r\n/**\r\n * Converts a Maybe value into a ReaderT.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The success value type.\r\n * @param maybe - The Maybe<A> value.\r\n * @param on_none_error - The error to use if Maybe is None.\r\n * @returns ReaderT<R, E, A> that returns Ok(A) if Some, otherwise Err(on_none_error).\r\n */\r\nexport const fromMaybe = <R, E, A>(\r\n maybe: Maybe<A>,\r\n on_none_error: E,\r\n): ReaderT<R, E, A> => {\r\n return (_env: R) => {\r\n if (isSome(maybe)) {\r\n return Ok(maybe);\r\n }\r\n return Err(on_none_error);\r\n };\r\n};\r\n\r\n/**\r\n * Converts a nullable value into a ReaderT.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The success value type.\r\n * @param maybe - The nullable value (A | null | undefined).\r\n * @param on_none_error - The error to use if the value is null or undefined.\r\n * @returns ReaderT<R, E, A> that returns Ok(A) if value exists, otherwise Err(on_none_error).\r\n */\r\nexport const fromNullable = fromMaybe;\r\n\r\n/**\r\n * Applies a pure function (A) => B to the success value inside the ReaderT.\r\n * This is the 'map' operation (Functor) for ReaderT.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the input value.\r\n * @typeParam B - The type of the output value.\r\n * @param reader - The source ReaderT<R, E, A>.\r\n * @param f_ab - The mapping function (A) => B.\r\n * @returns ReaderT<R, E, B> with the transformed success value.\r\n */\r\nexport const map = <R, E, A, B>(\r\n reader: ReaderT<R, E, A>,\r\n f_ab: (a: A) => B,\r\n): ReaderT<R, E, B> => {\r\n return (env: R) => {\r\n const result_a = reader(env);\r\n return mapResult(result_a, f_ab);\r\n };\r\n};\r\n\r\n/**\r\n * Chains two ReaderT computations. The result of the first reader (A) is used to\r\n * produce the second reader (ReaderT<R, E, B>). This is the 'flatMap' or 'bind' operation (Monad).\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the input value of the first reader.\r\n * @typeParam B - The type of the final output value.\r\n * @param reader - The initial ReaderT<R, E, A>.\r\n * @param f_next - A function that takes the result of 'reader' and returns the next ReaderT<R, E, B>.\r\n * @returns ReaderT<R, E, B> containing the result of the chained computation.\r\n */\r\nexport const andThen = <R, E, A, B>(\r\n reader: ReaderT<R, E, A>,\r\n f_next: (a: A) => ReaderT<R, E, B>,\r\n): ReaderT<R, E, B> => {\r\n return (env: R) => {\r\n const result_a = reader(env);\r\n return andThenResult(result_a, (a: A) => {\r\n const next_reader = f_next(a);\r\n return next_reader(env);\r\n });\r\n };\r\n};\r\n\r\n/**\r\n * Applies a function contained within a ReaderT (R, E, (A) => B) to a value\r\n * contained within another ReaderT (R, E, A). This is the 'ap' operation (Applicative Functor).\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the input value.\r\n * @typeParam B - The type of the output value.\r\n * @param reader_fab - ReaderT containing the function (A) => B.\r\n * @param reader_a - ReaderT containing the input value A.\r\n * @returns ReaderT<R, E, B> containing the applied result.\r\n */\r\nexport const ap = <R, E, A, B>(\r\n reader_fab: ReaderT<R, E, (a: A) => B>,\r\n reader_a: ReaderT<R, E, A>,\r\n): ReaderT<R, E, B> => {\r\n return (env: R) => {\r\n const result_fab = reader_fab(env);\r\n const result_a = reader_a(env);\r\n return apResult(result_fab, result_a);\r\n };\r\n};\r\n\r\n/**\r\n * Lifts a binary function (A, B) => C into the ReaderT context.\r\n * It combines two ReaderTs using the Applicative 'ap' operation.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the first input value.\r\n * @typeParam B - The type of the second input value.\r\n * @typeParam C - The type of the output value.\r\n * @param f_abc - The binary function (A, B) => C.\r\n * @returns A function that takes two ReaderTs and returns a ReaderT<R, E, C>.\r\n */\r\nexport const liftA2 =\r\n <R, E, A, B, C>(f_abc: (a: A, b: B) => C) =>\r\n (\r\n reader_a: ReaderT<R, E, A>,\r\n reader_b: ReaderT<R, E, B>,\r\n ): ReaderT<R, E, C> => {\r\n const reader_f_bc = map(reader_a, (a: A) => (b: B) => f_abc(a, b));\r\n return ap(reader_f_bc, reader_b);\r\n };\r\n\r\n/**\r\n * Lifts a ternary function (A, B, C) => D into the ReaderT context.\r\n * It combines three ReaderTs using the Applicative 'ap' operation.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the first input value.\r\n * @typeParam B - The type of the second input value.\r\n * @typeParam C - The type of the third input value.\r\n * @typeParam D - The type of the output value.\r\n * @param f_abcd - The ternary function (A, B, C) => D.\r\n * @returns A function that takes three ReaderTs and returns a ReaderT<R, E, D>.\r\n */\r\nexport const liftA3 =\r\n <R, E, A, B, C, D>(f_abcd: (a: A, b: B, c: C) => D) =>\r\n (\r\n reader_a: ReaderT<R, E, A>,\r\n reader_b: ReaderT<R, E, B>,\r\n reader_c: ReaderT<R, E, C>,\r\n ): ReaderT<R, E, D> => {\r\n const reader_f_cd = liftA2<R, E, A, B, (c: C) => D>(\r\n (a: A, b: B) => (c: C) => f_abcd(a, b, c),\r\n )(reader_a, reader_b);\r\n\r\n return ap(reader_f_cd, reader_c);\r\n };\r\n\r\n/**\r\n * Performs a side effect using the result of the first ReaderT, and then returns\r\n * the original result. The side effect can fail (return Err), in which case the\r\n * entire computation fails with that error.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the original value to be passed through.\r\n * @param reader - The source ReaderT<R, E, A>.\r\n * @param f - A function that takes A and returns a ReaderT<R, E, unknown> (the side effect).\r\n * @returns ReaderT<R, E, A> that returns the original success value or the side effect's error.\r\n */\r\nexport const tap = <R, E, A>(\r\n reader: ReaderT<R, E, A>,\r\n f: (a: A) => ReaderT<R, E, unknown>,\r\n): ReaderT<R, E, A> => {\r\n return (env: R) => {\r\n const result_a = reader(env);\r\n\r\n return andThenResult(result_a, (a: A) => {\r\n const side_effect_reader = f(a);\r\n const side_effect_result = side_effect_reader(env);\r\n\r\n if (!side_effect_result.ok) {\r\n return side_effect_result;\r\n }\r\n return Ok(a);\r\n });\r\n };\r\n};\r\n\r\n/**\r\n * Transforms the error type (E) of a ReaderT using a mapping function.\r\n * This is the 'mapError' operation for ReaderT.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The original Error type.\r\n * @typeParam A - The success value type.\r\n * @typeParam F - The new Error type.\r\n * @param reader - The source ReaderT<R, E, A>.\r\n * @param fn_fe - The function (E) => F to map the error.\r\n * @returns ReaderT<R, F, A> with the transformed error type.\r\n */\r\nexport const mapErr = <R, E, A, F>(\r\n reader: ReaderT<R, E, A>,\r\n fn_fe: (e: E) => F,\r\n): ReaderT<R, F, A> => {\r\n return (env: R) => {\r\n const result_a = reader(env);\r\n return mapErrResult(result_a, fn_fe);\r\n };\r\n};\r\n\r\n/**\r\n * Provides an alternative ReaderT to execute if the primary ReaderT fails.\r\n * This allows for error recovery or fallback behavior.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The primary error type.\r\n * @typeParam A - The success value type.\r\n * @typeParam F - The alternative error type.\r\n * @param reader - The primary ReaderT<R, E, A>.\r\n * @param alternative_reader - A function that takes the error E and returns a recovery ReaderT<R, F, A>.\r\n * @returns ReaderT<R, F, A> that is either the success of the primary reader, or the result of the alternative reader.\r\n */\r\nexport const orElse = <R, E, A, F>(\r\n reader: ReaderT<R, E, A>,\r\n alternative_reader: (e: E) => ReaderT<R, F, A>,\r\n): ReaderT<R, F, A> => {\r\n return (env: R) => {\r\n const result_a = reader(env);\r\n if (result_a.ok) {\r\n return result_a as Result<A, F>;\r\n }\r\n const error = result_a.error;\r\n const recovered_reader = alternative_reader(error);\r\n return recovered_reader(env);\r\n };\r\n};\r\n\r\n/**\r\n * Maps a list of items (T[]) to ReaderT computations and collects the results (A[])\r\n * into a single ReaderT. This is the 'traverse' operation.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam T - The type of input items.\r\n * @typeParam A - The type of output values.\r\n * @param f_ta - The function (T) => ReaderT<R, E, A> to apply to each item.\r\n * @param items - The list of items T to process.\r\n * @returns ReaderT<R, E, A[]> that contains an array of results A or the first error E.\r\n */\r\nexport const traverse =\r\n <R, E, T, A>(f_ta: (t: T) => ReaderT<R, E, A>) =>\r\n (items: readonly T[]): ReaderT<R, E, A[]> => {\r\n return (env: R) => {\r\n const results: Result<A, E>[] = items.map((item) => {\r\n const reader = f_ta(item);\r\n return reader(env);\r\n });\r\n return allResult(results);\r\n };\r\n };\r\n\r\n/**\r\n * Sequences a list of ReaderT computations into a single ReaderT.\r\n * This is a specialization of 'traverse' where the function is the identity.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The success value type.\r\n * @param readers - The list of ReaderT<R, E, A> computations.\r\n * @returns ReaderT<R, E, A[]> that contains an array of results A or the first error E.\r\n */\r\nexport const sequence = <R, E, A>(\r\n readers: readonly ReaderT<R, E, A>[],\r\n): ReaderT<R, E, A[]> => {\r\n return traverse((reader: ReaderT<R, E, A>) => reader)(readers);\r\n};\r\n","// ========================================\r\n// ./src/Reader/readerT-async.ts\r\n// ========================================\r\n/* eslint-disable @typescript-eslint/require-await */\r\n\r\nimport type {AsyncReaderT} from './types';\r\nimport {Ok, Err} from '../Result/base';\r\nimport type {Result} from '../Result/types';\r\nimport {\r\n mapAsync as mapResultAsync,\r\n andThenAsync as andThenResultAsync,\r\n mapErrAsync as mapErrResultAsync,\r\n} from '../Result/Operators/Async/transform-async';\r\nimport {\r\n apAsync as apResultAsync,\r\n allAsync as allResultAsync,\r\n} from '../Result/Operators/Async/combine-async';\r\nimport {type Maybe, isSome} from '../Maybe/__export__';\r\nimport {UnknownError} from '../unknown-error';\r\n\r\n/**\r\n * Creates an AsyncReaderT that ignores the environment (R) and resolves immediately\r\n * with a successful Result containing the given value (A). This is the 'return' or 'pure' operation.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the value to wrap.\r\n * @param a - The value to be wrapped in AsyncReaderT.\r\n * @returns AsyncReaderT<R, E, A> that resolves to Ok(a).\r\n */\r\nexport const ofAsync = <R, E, A>(a: A): AsyncReaderT<R, E, A> => {\r\n return async (_env: R) => Ok(a);\r\n};\r\n\r\n/**\r\n * Creates an AsyncReaderT that retrieves the environment (R) and resolves with it\r\n * in a successful Result. This is the 'ask' operation in ReaderT Monad.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @returns AsyncReaderT<R, E, R> that resolves to Ok(env).\r\n */\r\nexport const askAsync = <R, E>(): AsyncReaderT<R, E, R> => {\r\n return async (env: R) => Ok(env);\r\n};\r\n\r\n/**\r\n * Creates an AsyncReaderT that always fails with the given error (E), ignoring the environment.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type parameter for the value (unused, for consistency).\r\n * @param e - The error value to wrap in a failed Result.\r\n * @returns AsyncReaderT<R, E, A> that resolves to Err(e).\r\n */\r\nexport const failAsync = <R, E, A>(e: E): AsyncReaderT<R, E, A> => {\r\n return async (_env: R) => Err(e);\r\n};\r\n\r\n/**\r\n * Lifts a Result<A, E> into the AsyncReaderT context, ignoring the environment.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the value.\r\n * @param result - The Result to lift.\r\n * @returns AsyncReaderT<R, E | UnknownError, A> that resolves to the given result.\r\n */\r\nexport const fromResultAsync = <R, E, A>(\r\n result: Result<A, E>,\r\n): AsyncReaderT<R, E, A> => {\r\n return async (_env: R) => result;\r\n};\r\n\r\n/**\r\n * Lifts a Maybe<A> into the AsyncReaderT context, converting it to a Result.\r\n * If the Maybe is Some, returns Ok with the value; if None, returns Err with the given error.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the value.\r\n * @param maybe - The Maybe value to convert.\r\n * @param on_none_error - The error to use if the Maybe is None.\r\n * @returns AsyncReaderT<R, E | UnknownError, A> that resolves to Ok(a) or Err(on_none_error).\r\n */\r\nexport const fromMaybeAsync = <R, E, A>(\r\n maybe: Maybe<A>,\r\n on_none_error: E,\r\n): AsyncReaderT<R, E | UnknownError, A> => {\r\n return async (_env: R) => {\r\n if (isSome(maybe)) {\r\n return Ok(maybe);\r\n }\r\n return Err(on_none_error);\r\n };\r\n};\r\n\r\n/**\r\n * Lifts a nullable value (A | null | undefined) into the AsyncReaderT context.\r\n * If the value is not null or undefined, returns Ok with the value; otherwise returns Err.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the value.\r\n * @param maybe - The nullable value to convert.\r\n * @param on_none_error - The error to use if the value is null or undefined.\r\n * @returns AsyncReaderT<R, E | UnknownError, A> that resolves accordingly.\r\n */\r\nexport const fromNullableAsync = fromMaybeAsync;\r\n\r\n/**\r\n * Applies a function (A) => B to the successful value inside the AsyncReaderT,\r\n * maintaining the environment context and propagating errors. This is the 'map' operation.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the input value.\r\n * @typeParam B - The type of the output value.\r\n * @param reader - The source AsyncReaderT<R, E, A>.\r\n * @param fn - The mapping function (A) => B.\r\n * @returns AsyncReaderT<R, E | UnknownError, B> containing the mapped value or the original error.\r\n */\r\nexport const mapAsync = <R, E, A, B>(\r\n reader: AsyncReaderT<R, E, A>,\r\n fn: (a: A) => B,\r\n): AsyncReaderT<R, E | UnknownError | DOMException, B> => {\r\n return async (env: R) => {\r\n const result_a = await reader(env);\r\n return mapResultAsync(result_a, fn);\r\n };\r\n};\r\n\r\n/**\r\n * Chains two AsyncReaderT computations. The successful result of the first reader (A) is used to\r\n * produce the second reader (AsyncReaderT<R, E, B>). Errors are propagated.\r\n * This is the 'flatMap' or 'bind' operation (Monad).\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the input value of the first reader.\r\n * @typeParam B - The type of the final output value.\r\n * @param reader - The initial AsyncReaderT<R, E, A>.\r\n * @param f_next - A function that takes the successful result of 'reader' and returns the next AsyncReaderT<R, E, B>.\r\n * @returns AsyncReaderT<R, E | UnknownError, B> containing the final result or an error.\r\n */\r\nexport const andThenAsync = <R, E, A, B>(\r\n reader: AsyncReaderT<R, E, A>,\r\n f_next: (a: A, signal?: AbortSignal) => AsyncReaderT<R, E, B>,\r\n): AsyncReaderT<R, E | UnknownError | DOMException, B> => {\r\n return async (env: R) => {\r\n const result_a = await reader(env);\r\n return andThenResultAsync(result_a, async (a: A) => {\r\n const next_reader = f_next(a);\r\n return await next_reader(env);\r\n });\r\n };\r\n};\r\n\r\n/**\r\n * Applies a function contained within an AsyncReaderT (R, (A) => B) to a value\r\n * contained within another AsyncReaderT (R, A), preserving the environment and propagating errors.\r\n * This is the 'ap' operation (Applicative Functor).\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the input value.\r\n * @typeParam B - The type of the output value.\r\n * @param reader_fab - AsyncReaderT containing the function (A) => B.\r\n * @param reader_a - AsyncReaderT containing the input value A.\r\n * @returns AsyncReaderT<R, E | UnknownError, B> containing the result of applying the function or an error.\r\n */\r\nexport const apAsync = <R, E, A, B>(\r\n reader_fab: AsyncReaderT<R, E, (a: A) => B>,\r\n reader_a: AsyncReaderT<R, E, A>,\r\n): AsyncReaderT<R, E | UnknownError | DOMException, B> => {\r\n return async (env: R) => {\r\n const promise_fab = reader_fab(env);\r\n const promise_a = reader_a(env);\r\n\r\n const [result_fab, result_a] = await Promise.all([promise_fab, promise_a]);\r\n\r\n return apResultAsync(result_fab, result_a);\r\n };\r\n};\r\n\r\n/**\r\n * Asynchronously lifts a binary function (A, B) => C into the AsyncReaderT context.\r\n * It combines two AsyncReaderTs using the Applicative 'ap' operation, propagating errors.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the first input value.\r\n * @typeParam B - The type of the second input value.\r\n * @typeParam C - The type of the output value.\r\n * @param f_abc - The binary function (A, B) => C.\r\n * @returns A function that takes two AsyncReaderTs and returns an AsyncReaderT<R, E | UnknownError, C>.\r\n */\r\nexport const liftA2Async =\r\n <A, B, C>(f_abc: (a: A, b: B) => C) =>\r\n <R, E>(\r\n reader_a: AsyncReaderT<R, E, A>,\r\n reader_b: AsyncReaderT<R, E, B>,\r\n ): AsyncReaderT<R, E | UnknownError | DOMException, C> => {\r\n const reader_f_bc = mapAsync(reader_a, (a: A) => (b: B) => f_abc(a, b));\r\n return apAsync(reader_f_bc, reader_b);\r\n };\r\n\r\n/**\r\n * Asynchronously lifts a ternary function (A, B, C) => D into the AsyncReaderT context.\r\n * It combines three AsyncReaderTs using the Applicative 'ap' operation, propagating errors.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the first input value.\r\n * @typeParam B - The type of the second input value.\r\n * @typeParam C - The type of the third input value.\r\n * @typeParam D - The type of the output value.\r\n * @param f_abcd - The ternary function (A, B, C) => D.\r\n * @returns A function that takes three AsyncReaderTs and returns an AsyncReaderT<R, E | UnknownError, D>.\r\n */\r\nexport const liftA3Async =\r\n <A, B, C, D>(f_abcd: (a: A, b: B, c: C) => D) =>\r\n <R, E>(\r\n reader_a: AsyncReaderT<R, E, A>,\r\n reader_b: AsyncReaderT<R, E, B>,\r\n reader_c: AsyncReaderT<R, E, C>,\r\n ): AsyncReaderT<R, E | UnknownError | DOMException, D> => {\r\n const reader_f_cd = liftA2Async((a: A, b: B) => (c: C) => f_abcd(a, b, c))(\r\n reader_a,\r\n reader_b,\r\n );\r\n return apAsync(reader_f_cd, reader_c);\r\n };\r\n\r\n/**\r\n * Performs a side effect using the successful result of the AsyncReaderT, and then returns\r\n * the original result, ignoring the side effect's return value. If the side effect fails,\r\n * the error is propagated.\r\n * Note: The side effect function must return an AsyncReaderT.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The type of the original value to be passed through.\r\n * @param reader - The source AsyncReaderT<R, E, A>.\r\n * @param f - A function that takes A and returns an AsyncReaderT<R, E, unknown> (the side effect).\r\n * @returns AsyncReaderT<R, E | UnknownError, A> containing the original value or an error.\r\n */\r\nexport const tapAsync = <R, E, A>(\r\n reader: AsyncReaderT<R, E, A>,\r\n f: (a: A) => AsyncReaderT<R, E, unknown>,\r\n): AsyncReaderT<R, E | UnknownError | DOMException, A> => {\r\n return async (env: R) => {\r\n const result_a = await reader(env);\r\n\r\n return andThenResultAsync(result_a, async (a: A) => {\r\n const side_effect_reader = f(a);\r\n const side_effect_result = await side_effect_reader(env);\r\n\r\n if (!side_effect_result.ok) {\r\n return side_effect_result;\r\n }\r\n return Ok(a);\r\n });\r\n };\r\n};\r\n\r\n/**\r\n * Transforms the error value inside a failed AsyncReaderT using the given function.\r\n * Successful results are passed through unchanged.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The original Error type.\r\n * @typeParam A - The value type.\r\n * @typeParam F - The new Error type.\r\n * @param reader - The source AsyncReaderT<R, E, A>.\r\n * @param fn_fe - The error transformation function (E) => Promise<F> | F.\r\n * @returns AsyncReaderT<R, F | UnknownError, A> with transformed error or original success.\r\n */\r\nexport const mapErrAsync = <R, E, A, F>(\r\n reader: AsyncReaderT<R, E, A>,\r\n fn_fe: (e: E) => Promise<F> | F,\r\n): AsyncReaderT<R, F | UnknownError | DOMException, A> => {\r\n return async (env: R) => {\r\n const result_a = await reader(env);\r\n return mapErrResultAsync(result_a, fn_fe);\r\n };\r\n};\r\n\r\n/**\r\n * Recovers from an error in the AsyncReaderT by executing an alternative reader.\r\n * If the original reader succeeds, its result is returned; otherwise, the error is\r\n * passed to the alternative function which returns a new reader to try.\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The original Error type.\r\n * @typeParam A - The value type.\r\n * @typeParam F - The alternative Error type.\r\n * @param reader - The original AsyncReaderT<R, E, A>.\r\n * @param alternative_reader - A function that takes the error and returns an alternative AsyncReaderT<R, F, A>.\r\n * @returns AsyncReaderT<R, F | UnknownError, A> with either the original success or the alternative result.\r\n */\r\nexport const orElseAsync = <R, E, A, F>(\r\n reader: AsyncReaderT<R, E, A>,\r\n alternative_reader: (e: E) => AsyncReaderT<R, F, A>,\r\n): AsyncReaderT<R, F | UnknownError, A> => {\r\n return async (env: R) => {\r\n const result_a = await reader(env);\r\n\r\n if (result_a.ok) {\r\n return result_a;\r\n }\r\n\r\n const error = result_a.error;\r\n const recovered_reader = alternative_reader(error);\r\n return await recovered_reader(env);\r\n };\r\n};\r\n\r\n/**\r\n * Transforms a list of items (T) into an AsyncReaderT that processes each item\r\n * with the given function and collects all results into an array.\r\n * All items are processed in parallel (using Promise.all).\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam T - The type of input items.\r\n * @typeParam A - The type of output values.\r\n * @param f_ta - A function that transforms each item T into an AsyncReaderT<R, E, A>.\r\n * @returns A function that takes an array of T and returns an AsyncReaderT<R, E | UnknownError, A[]>\r\n * containing all successful results, or the first error encountered.\r\n */\r\nexport const traverseAsync =\r\n <R, E, T, A>(f_ta: (t: T) => AsyncReaderT<R, E, A>) =>\r\n (\r\n items: readonly T[],\r\n ): AsyncReaderT<R, E | UnknownError | DOMException, A[]> => {\r\n return async (env: R) => {\r\n const promises: Promise<Result<A, E>>[] = items.map((item) => {\r\n const reader = f_ta(item);\r\n return reader(env);\r\n });\r\n\r\n const results: Result<A, E>[] = await Promise.all(promises);\r\n\r\n return allResultAsync(results);\r\n };\r\n };\r\n\r\n/**\r\n * Converts a list of AsyncReaderTs into a single AsyncReaderT that contains an array of results.\r\n * All readers are executed in parallel (using Promise.all).\r\n * @typeParam R - The Environment type.\r\n * @typeParam E - The Error type.\r\n * @typeParam A - The value type.\r\n * @param readers - An array of AsyncReaderT<R, E, A> to sequence.\r\n * @returns AsyncReaderT<R, E | UnknownError, A[]> containing all successful results, or the first error.\r\n */\r\nexport const sequenceAsync = <R, E, A>(\r\n readers: readonly AsyncReaderT<R, E, A>[],\r\n): AsyncReaderT<R, E | UnknownError | DOMException, A[]> => {\r\n return traverseAsync((reader: AsyncReaderT<R, E, A>) => reader)(readers);\r\n};\r\n"],"names":["of","a","_env","Ok","ask","env","fail","e","Err","fromResult","result","fromMaybe","maybe","on_none_error","isSome","fromNullable","map","reader","f_ab","result_a","mapResult","andThen","f_next","andThenResult","ap","reader_fab","reader_a","result_fab","apResult","liftA2","f_abc","reader_b","reader_f_bc","b","liftA3","f_abcd","reader_c","reader_f_cd","c","tap","f","side_effect_result","mapErr","fn_fe","mapErrResult","orElse","alternative_reader","error","traverse","f_ta","items","results","item","allResult","sequence","readers","ofAsync","askAsync","failAsync","fromResultAsync","fromMaybeAsync","fromNullableAsync","mapAsync","fn","mapResultAsync","andThenAsync","andThenResultAsync","apAsync","promise_fab","promise_a","apResultAsync","liftA2Async","liftA3Async","tapAsync","mapErrAsync","mapErrResultAsync","orElseAsync","traverseAsync","promises","allResultAsync","sequenceAsync"],"mappings":";;;;AAuBO,MAAMA,IAAK,CAAUC,MACnB,CAACC,MAAYC,EAAGF,CAAC,GAUbG,IAAM,MACV,CAACC,MAAWF,EAAGE,CAAG,GAWdC,IAAO,CAAUC,MACrB,CAACL,MAAYM,EAAID,CAAC,GAWdE,IAAa,CAAUC,MAC3B,CAACR,MAAYQ,GAYTC,IAAY,CACvBC,GACAC,MAEO,CAACX,MACFY,EAAOF,CAAK,IACPT,EAAGS,CAAK,IAEVJ,EAAIK,CAAa,GAafE,IAAeJ,GAafK,IAAM,CACjBC,GACAC,MAEO,CAACb,MAAW;AACjB,QAAMc,IAAWF,EAAOZ,CAAG;AAC3B,SAAOe,EAAUD,GAAUD,CAAI;AACjC,GAcWG,IAAU,CACrBJ,GACAK,MAEO,CAACjB,MAAW;AACjB,QAAMc,IAAWF,EAAOZ,CAAG;AAC3B,SAAOkB,EAAcJ,GAAU,CAAClB,MACVqB,EAAOrB,CAAC,EACTI,CAAG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Module"});const i=require("../chunks/types-B7X_LBjM.cjs"),d=require("../chunks/Errors-BWEYu-AB.cjs"),o=Symbol("Data"),f=Symbol("Cleanup"),u=Symbol("Commit"),y=Symbol("Rollback");class b{[o];[f];[u];[y];constructor(e,s,r,c){this[o]=e,this[f]=s,this[u]=r,this[y]=c}static open(e,s,r,c){return new b(e,s,r,c)}[Symbol.dispose](){this[o]!==i.NONE&&(this[y]&&this[y](this[o]),this[f]&&this[f](this[o]),this[o]=i.NONE)}}function w(t){if(t[o]===i.NONE)throw new d.UseAfterFreeError("Resource already closed/committed/rolled back.");return t[y]=void 0,t[u]&&t[u](t[o]),t[Symbol.dispose](),t}function N(t){if(t[o]===i.NONE)throw new d.UseAfterFreeError("Resource already closed/committed/rolled back.");const e=t[y];return t[y]=void 0,e&&e(t[o]),t[Symbol.dispose](),t}function A(t,e){if(t[o]===i.NONE)throw new d.UseAfterFreeError("Resource already disposed.");return e(t[o])}function E(t,e){if(t[o]===i.NONE)throw new d.UseAfterFreeError("Resource already disposed.");return e(t[o]),t}function _(t,e,s,r,c){const a=t;return a[o]=e,a[f]=s,a[u]=r,a[y]=c,a}const n=Symbol("Data"),m=Symbol("AsyncCleanup"),h=Symbol("AsyncCommit"),l=Symbol("AsyncRollback");class p{[n];[m];[h];[l];constructor(e,s,r,c){this[n]=e,this[m]=s,this[h]=r,this[l]=c}static open(e,s,r,c){return new p(e,s,r,c)}async[Symbol.asyncDispose](){this[n]!==i.NONE&&(this[l]&&await this[l](this[n]),this[m]&&await this[m](this[n]),this[n]=i.NONE)}}async function O(t){if(t[n]===i.NONE)throw new d.UseAfterFreeError("Resource already closed/committed/rolled back.");return t[h]&&await t[h](t[n]),t[l]=void 0,await t[Symbol.asyncDispose](),t}async function S(t){if(t[n]===i.NONE)throw new d.UseAfterFreeError("Resource already closed/committed/rolled back.");const e=t[l];return t[l]=void 0,e&&await e(t[n]),t[l]=void 0,await t[Symbol.asyncDispose](),t}async function k(t,e){if(t[n]===i.NONE)throw new d.UseAfterFreeError("Resource already disposed.");return await e(t[n])}async function R(t,e){if(t[n]===i.NONE)throw new d.UseAfterFreeError("Resource already disposed.");return await e(t[n]),t}function F(t,e,s,r,c){const a=t;return a[n]=e,a[m]=s,a[h]=r,a[l]=c,a}const U=Object.freeze(Object.defineProperty({__proto__:null,AsyncTxScope:p,TxScope:b,commit:w,commitAsync:O,exec:E,execAsync:R,reopen:_,reopenAsync:F,rollback:N,rollbackAsync:S,use:A,useAsync:k},Symbol.toStringTag,{value:"Module"}));exports.AsyncTxScope=p;exports.TxScope=b;exports.__exportResource__=U;exports.commit=w;exports.commitAsync=O;exports.exec=E;exports.execAsync=R;exports.reopen=_;exports.reopenAsync=F;exports.rollback=N;exports.rollbackAsync=S;exports.use=A;exports.useAsync=k;
2
+ //# sourceMappingURL=Resource.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Resource.cjs","sources":["../../src/Resource/TxScope.ts","../../src/Resource/TxScope-async.ts"],"sourcesContent":["// ========================================\r\n// ./src/Resource/TxScope.ts\r\n// ========================================\r\n/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */\r\n\r\nimport {\r\n type StateOpened,\r\n type StateCommit,\r\n type StateRollback,\r\n type Cleaner,\r\n type CommitUp,\r\n type RollbackUp,\r\n type NoneType,\r\n NONE,\r\n} from './types';\r\nimport {UseAfterFreeError} from '../Errors';\r\n\r\n// ------ 私有符号定义 ------\r\nconst _data = Symbol('Data');\r\nconst _cleanup = Symbol('Cleanup');\r\nconst _commitup = Symbol('Commit');\r\nconst _rollbackup = Symbol('Rollback');\r\n\r\n/**\r\n * Synchronous transaction scope management class.\r\n * @typeParam T - The type of the transaction data\r\n * @typeParam S - The state type (opened, commit, or rollback)\r\n */\r\nexport class TxScope<T, S extends StateOpened | StateCommit | StateRollback> {\r\n declare private state: S;\r\n public [_data]: T | NoneType;\r\n public [_cleanup]: Cleaner<T> | undefined;\r\n public [_commitup]: CommitUp<T> | undefined;\r\n public [_rollbackup]: RollbackUp<T> | undefined;\r\n\r\n // ------ 构造函数 ------\r\n private constructor(\r\n data: T,\r\n cleanup_fn?: Cleaner<T>,\r\n commitup_fn?: CommitUp<T>,\r\n rollbackup_fn?: RollbackUp<T>,\r\n ) {\r\n this[_data] = data;\r\n this[_cleanup] = cleanup_fn;\r\n this[_commitup] = commitup_fn;\r\n this[_rollbackup] = rollbackup_fn;\r\n }\r\n\r\n /**\r\n * Creates a new opened synchronous transaction scope.\r\n * @typeParam D - The type of the transaction data\r\n * @param data - The transaction data\r\n * @param cleaner - Optional synchronous cleanup function\r\n * @param commiter - Optional synchronous commit function\r\n * @param rollbacker - Optional synchronous rollback function\r\n * @returns A new opened synchronous transaction scope\r\n */\r\n static open<D>(\r\n data: D,\r\n cleaner?: Cleaner<D>,\r\n commiter?: CommitUp<D>,\r\n rollbacker?: RollbackUp<D>,\r\n ): TxScope<D, StateOpened> {\r\n return new TxScope(data, cleaner, commiter, rollbacker);\r\n }\r\n\r\n /**\r\n * Synchronously disposes the transaction scope (cleanup with rollback).\r\n */\r\n [Symbol.dispose](this: TxScope<T, StateOpened>): void {\r\n if (this[_data] === NONE) {\r\n return;\r\n }\r\n // 如果事务未被 commit/rollback 清理,则在 dispose 时执行回滚。\r\n if (this[_rollbackup]) {\r\n this[_rollbackup](this[_data] as T);\r\n }\r\n if (this[_cleanup]) {\r\n this[_cleanup](this[_data] as T);\r\n }\r\n this[_data] = NONE;\r\n }\r\n}\r\n\r\n/**\r\n * Union type for synchronous transaction scope states.\r\n * @typeParam T - The type of the transaction data\r\n */\r\nexport type TxScopeType<T> =\r\n | TxScope<T, StateOpened>\r\n | TxScope<T, StateCommit>\r\n | TxScope<T, StateRollback>;\r\n\r\n/**\r\n * Commits a synchronous transaction scope.\r\n * @typeParam T - The type of the transaction data\r\n * @param resource - The opened synchronous transaction scope\r\n * @returns The committed transaction scope\r\n * @throws Error if resource is already disposed/committed/rolled back\r\n */\r\nexport function commit<T>(\r\n resource: TxScope<T, StateOpened>,\r\n): TxScope<T, StateCommit> {\r\n if (resource[_data] === NONE) {\r\n throw new UseAfterFreeError(\r\n 'Resource already closed/committed/rolled back.',\r\n );\r\n }\r\n resource[_rollbackup] = undefined;\r\n if (resource[_commitup]) {\r\n resource[_commitup](resource[_data] as T);\r\n }\r\n resource[Symbol.dispose]();\r\n return resource as unknown as TxScope<T, StateCommit>;\r\n}\r\n\r\n/**\r\n * Rolls back a synchronous transaction scope.\r\n * @typeParam T - The type of the transaction data\r\n * @param resource - The opened synchronous transaction scope\r\n * @returns The rolled back transaction scope\r\n * @throws Error if resource is already disposed/committed/rolled back\r\n */\r\nexport function rollback<T>(\r\n resource: TxScope<T, StateOpened>,\r\n): TxScope<T, StateRollback> {\r\n if (resource[_data] === NONE) {\r\n throw new UseAfterFreeError(\r\n 'Resource already closed/committed/rolled back.',\r\n );\r\n }\r\n const fn = resource[_rollbackup];\r\n resource[_rollbackup] = undefined;\r\n if (fn) {\r\n fn(resource[_data] as T); // 即使这里失败了,回滚函数已经是undefined了,不会二次错误\r\n }\r\n resource[Symbol.dispose]();\r\n return resource as unknown as TxScope<T, StateRollback>;\r\n}\r\n\r\n/**\r\n * Uses a synchronous transaction scope with a callback function.\r\n * @typeParam T - The type of the transaction data\r\n * @typeParam R - The return type of the callback\r\n * @param TxScope - The opened synchronous transaction scope\r\n * @param fn - Function to process the transaction data\r\n * @returns The result of the callback\r\n * @throws Error if transaction scope is already disposed\r\n */\r\nexport function use<T, R>(\r\n TxScope: TxScope<T, StateOpened>,\r\n fn: (data: T) => R,\r\n): R {\r\n if (TxScope[_data] === NONE) {\r\n throw new UseAfterFreeError('Resource already disposed.');\r\n }\r\n return fn(TxScope[_data] as T);\r\n}\r\n\r\n/**\r\n * Performs a side effect on a synchronous transaction scope and returns it.\r\n * @typeParam T - The type of the transaction data\r\n * @typeParam R - The return type of the side effect function\r\n * @param TxScope - The opened synchronous transaction scope\r\n * @param fn - Side effect function to execute\r\n * @returns The same transaction scope after executing the side effect\r\n * @throws Error if transaction scope is already disposed\r\n */\r\nexport function exec<T, R>(\r\n TxScope: TxScope<T, StateOpened>,\r\n fn: (data: T) => R,\r\n): TxScope<T, StateOpened> {\r\n if (TxScope[_data] === NONE) {\r\n throw new UseAfterFreeError('Resource already disposed.');\r\n }\r\n fn(TxScope[_data] as T);\r\n return TxScope;\r\n}\r\n\r\n/**\r\n * Reopens a committed or rolled back synchronous transaction scope with new data.\r\n * @typeParam T - The type of the transaction data\r\n * @param TxScope - The committed or rolled back synchronous transaction scope\r\n * @param data - New data for the transaction scope\r\n * @param cleaner - Optional new cleanup function\r\n * @param commiter - Optional new commit function\r\n * @param rollbacker - Optional new rollback function\r\n * @returns The reopened synchronous transaction scope\r\n */\r\nexport function reopen<T>(\r\n TxScope: TxScope<T, StateCommit> | TxScope<T, StateRollback>,\r\n data: T,\r\n cleaner?: Cleaner<T>,\r\n commiter?: CommitUp<T>,\r\n rollbacker?: RollbackUp<T>,\r\n): TxScope<T, StateOpened> {\r\n const r = TxScope as unknown as TxScope<T, StateOpened>;\r\n r[_data] = data;\r\n r[_cleanup] = cleaner;\r\n r[_commitup] = commiter;\r\n r[_rollbackup] = rollbacker;\r\n return r;\r\n}\r\n","// ========================================\r\n// ./src/Resource/TxScope-async.ts\r\n// ========================================\r\n/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */\r\n\r\nimport {\r\n type StateOpened,\r\n type StateCommit,\r\n type StateRollback,\r\n type AnyCleaner,\r\n type AsyncCommitUp,\r\n type AnyRollbackUp,\r\n type NoneType,\r\n NONE,\r\n} from './types';\r\nimport {UseAfterFreeError} from '../Errors';\r\n\r\n// ------ 私有符号定义 ------\r\nconst _data = Symbol('Data');\r\nconst _cleanup = Symbol('AsyncCleanup');\r\nconst _commitup = Symbol('AsyncCommit');\r\nconst _rollbackup = Symbol('AsyncRollback');\r\n\r\n/**\r\n * Asynchronous transaction scope management class.\r\n * @typeParam T - The type of the transaction data\r\n * @typeParam S - The state type (opened, commit, or rollback)\r\n */\r\nexport class AsyncTxScope<\r\n T,\r\n S extends StateOpened | StateCommit | StateRollback,\r\n> {\r\n declare private state: S;\r\n public [_data]: T | NoneType;\r\n public [_cleanup]: AnyCleaner<T> | undefined;\r\n public [_commitup]: AsyncCommitUp<T> | undefined;\r\n public [_rollbackup]: AnyRollbackUp<T> | undefined;\r\n\r\n // ------ 构造函数 ------\r\n private constructor(\r\n data: T,\r\n cleanup_fn?: AnyCleaner<T>,\r\n commitup_fn?: AsyncCommitUp<T>,\r\n rollbackup_fn?: AnyRollbackUp<T>,\r\n ) {\r\n this[_data] = data;\r\n this[_cleanup] = cleanup_fn;\r\n this[_commitup] = commitup_fn;\r\n this[_rollbackup] = rollbackup_fn;\r\n }\r\n\r\n /**\r\n * Creates a new opened asynchronous transaction scope.\r\n * @typeParam D - The type of the transaction data\r\n * @param data - The transaction data\r\n * @param cleaner - Optional asynchronous cleanup function\r\n * @param commiter - Optional asynchronous commit function\r\n * @param rollbacker - Optional asynchronous rollback function\r\n * @returns A new opened asynchronous transaction scope\r\n */\r\n static open<D>(\r\n data: D,\r\n cleaner?: AnyCleaner<D>,\r\n commiter?: AsyncCommitUp<D>,\r\n rollbacker?: AnyRollbackUp<D>,\r\n ): AsyncTxScope<D, StateOpened> {\r\n return new AsyncTxScope(data, cleaner, commiter, rollbacker);\r\n }\r\n\r\n /**\r\n * Asynchronously disposes the transaction scope (cleanup with rollback).\r\n * @returns Promise that resolves when disposal is complete\r\n */\r\n async [Symbol.asyncDispose](\r\n this: AsyncTxScope<T, StateOpened>,\r\n ): Promise<void> {\r\n if (this[_data] === NONE) {\r\n return;\r\n }\r\n // 如果事务未被 commit/rollback 清理,则在 dispose 时执行异步回滚。\r\n if (this[_rollbackup]) {\r\n await this[_rollbackup](this[_data] as T); // 强制等待异步回滚\r\n }\r\n if (this[_cleanup]) {\r\n await this[_cleanup](this[_data] as T); // 强制等待异步清理\r\n }\r\n this[_data] = NONE;\r\n }\r\n}\r\n\r\n/**\r\n * Union type for asynchronous transaction scope states.\r\n * @typeParam T - The type of the transaction data\r\n */\r\nexport type AsyncTxScopeType<T> =\r\n | AsyncTxScope<T, StateOpened>\r\n | AsyncTxScope<T, StateCommit>\r\n | AsyncTxScope<T, StateRollback>;\r\n\r\n/**\r\n * Commits an asynchronous transaction scope.\r\n * @typeParam T - The type of the transaction data\r\n * @param resource - The opened asynchronous transaction scope\r\n * @returns Promise with the committed transaction scope\r\n * @throws Error if resource is already disposed/committed/rolled back\r\n */\r\nexport async function commitAsync<T>(\r\n resource: AsyncTxScope<T, StateOpened>,\r\n): Promise<AsyncTxScope<T, StateCommit>> {\r\n if (resource[_data] === NONE) {\r\n throw new UseAfterFreeError(\r\n 'Resource already closed/committed/rolled back.',\r\n );\r\n }\r\n if (resource[_commitup]) {\r\n await resource[_commitup](resource[_data] as T);\r\n }\r\n resource[_rollbackup] = undefined;\r\n await resource[Symbol.asyncDispose]();\r\n return resource as unknown as AsyncTxScope<T, StateCommit>;\r\n}\r\n\r\n/**\r\n * Rolls back an asynchronous transaction scope.\r\n * @typeParam T - The type of the transaction data\r\n * @param resource - The opened asynchronous transaction scope\r\n * @returns Promise with the rolled back transaction scope\r\n * @throws Error if resource is already disposed/committed/rolled back\r\n */\r\nexport async function rollbackAsync<T>(\r\n resource: AsyncTxScope<T, StateOpened>,\r\n): Promise<AsyncTxScope<T, StateRollback>> {\r\n if (resource[_data] === NONE) {\r\n throw new UseAfterFreeError(\r\n 'Resource already closed/committed/rolled back.',\r\n );\r\n }\r\n const fn = resource[_rollbackup];\r\n resource[_rollbackup] = undefined;\r\n if (fn) {\r\n await fn(resource[_data] as T);\r\n }\r\n resource[_rollbackup] = undefined;\r\n await resource[Symbol.asyncDispose]();\r\n return resource as unknown as AsyncTxScope<T, StateRollback>;\r\n}\r\n\r\n/**\r\n * Uses an asynchronous transaction scope with a callback function.\r\n * @typeParam T - The type of the transaction data\r\n * @typeParam R - The return type of the callback\r\n * @param TxScope - The opened asynchronous transaction scope\r\n * @param fn - Function to process the transaction data\r\n * @returns Promise with the result of the callback\r\n * @throws Error if transaction scope is already disposed\r\n */\r\nexport async function useAsync<T, R>(\r\n TxScope: AsyncTxScope<T, StateOpened>,\r\n fn: (data: T) => Promise<R> | R,\r\n): Promise<R> {\r\n if (TxScope[_data] === NONE) {\r\n throw new UseAfterFreeError('Resource already disposed.');\r\n }\r\n return await fn(TxScope[_data] as T);\r\n}\r\n\r\n/**\r\n * Performs a side effect on an asynchronous transaction scope and returns it.\r\n * @typeParam T - The type of the transaction data\r\n * @typeParam R - The return type of the side effect function\r\n * @param TxScope - The opened asynchronous transaction scope\r\n * @param fn - Side effect function to execute\r\n * @returns The same transaction scope after executing the side effect\r\n * @throws Error if transaction scope is already disposed\r\n */\r\nexport async function execAsync<T, R>(\r\n TxScope: AsyncTxScope<T, StateOpened>,\r\n fn: (data: T) => Promise<R> | R,\r\n): Promise<AsyncTxScope<T, StateOpened>> {\r\n if (TxScope[_data] === NONE) {\r\n throw new UseAfterFreeError('Resource already disposed.');\r\n }\r\n await fn(TxScope[_data] as T);\r\n return TxScope;\r\n}\r\n\r\n/**\r\n * Reopens a committed or rolled back asynchronous transaction scope with new data.\r\n * @typeParam T - The type of the transaction data\r\n * @param TxScope - The committed or rolled back asynchronous transaction scope\r\n * @param data - New data for the transaction scope\r\n * @param cleaner - Optional new cleanup function\r\n * @param commiter - Optional new commit function\r\n * @param rollbacker - Optional new rollback function\r\n * @returns The reopened asynchronous transaction scope\r\n */\r\nexport function reopenAsync<T>(\r\n TxScope: AsyncTxScope<T, StateCommit> | AsyncTxScope<T, StateRollback>,\r\n data: T,\r\n cleaner?: AnyCleaner<T>,\r\n commiter?: AsyncCommitUp<T>,\r\n rollbacker?: AnyRollbackUp<T>,\r\n): AsyncTxScope<T, StateOpened> {\r\n const r = TxScope as unknown as AsyncTxScope<T, StateOpened>;\r\n r[_data] = data;\r\n r[_cleanup] = cleaner;\r\n r[_commitup] = commiter;\r\n r[_rollbackup] = rollbacker;\r\n return r;\r\n}\r\n"],"names":["_data","_cleanup","_commitup","_rollbackup","TxScope","data","cleanup_fn","commitup_fn","rollbackup_fn","cleaner","commiter","rollbacker","NONE","commit","resource","UseAfterFreeError","rollback","fn","use","exec","reopen","r","AsyncTxScope","commitAsync","rollbackAsync","useAsync","execAsync","reopenAsync"],"mappings":"2KAkBMA,EAAQ,OAAO,MAAM,EACrBC,EAAW,OAAO,SAAS,EAC3BC,EAAY,OAAO,QAAQ,EAC3BC,EAAc,OAAO,UAAU,EAO9B,MAAMC,CAAgE,CAE3E,CAAQJ,CAAK,EACb,CAAQC,CAAQ,EAChB,CAAQC,CAAS,EACjB,CAAQC,CAAW,EAGX,YACNE,EACAC,EACAC,EACAC,EACA,CACA,KAAKR,CAAK,EAAIK,EACd,KAAKJ,CAAQ,EAAIK,EACjB,KAAKJ,CAAS,EAAIK,EAClB,KAAKJ,CAAW,EAAIK,CACtB,CAWA,OAAO,KACLH,EACAI,EACAC,EACAC,EACyB,CACzB,OAAO,IAAIP,EAAQC,EAAMI,EAASC,EAAUC,CAAU,CACxD,CAKA,CAAC,OAAO,OAAO,GAAuC,CAChD,KAAKX,CAAK,IAAMY,SAIhB,KAAKT,CAAW,GAClB,KAAKA,CAAW,EAAE,KAAKH,CAAK,CAAM,EAEhC,KAAKC,CAAQ,GACf,KAAKA,CAAQ,EAAE,KAAKD,CAAK,CAAM,EAEjC,KAAKA,CAAK,EAAIY,EAAAA,KAChB,CACF,CAkBO,SAASC,EACdC,EACyB,CACzB,GAAIA,EAASd,CAAK,IAAMY,OACtB,MAAM,IAAIG,EAAAA,kBACR,gDAAA,EAGJ,OAAAD,EAASX,CAAW,EAAI,OACpBW,EAASZ,CAAS,GACpBY,EAASZ,CAAS,EAAEY,EAASd,CAAK,CAAM,EAE1Cc,EAAS,OAAO,OAAO,EAAA,EAChBA,CACT,CASO,SAASE,EACdF,EAC2B,CAC3B,GAAIA,EAASd,CAAK,IAAMY,OACtB,MAAM,IAAIG,EAAAA,kBACR,gDAAA,EAGJ,MAAME,EA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
1
+ import { N as s } from "../chunks/types-DE2SnSbz.mjs";
2
+ import { U as m } from "../chunks/Errors-DwUAvFIF.mjs";
3
+ const n = Symbol("Data"), r = Symbol("Cleanup"), h = Symbol("Commit"), y = Symbol("Rollback");
4
+ class w {
5
+ [n];
6
+ [r];
7
+ [h];
8
+ [y];
9
+ // ------ 构造函数 ------
10
+ constructor(i, a, l, c) {
11
+ this[n] = i, this[r] = a, this[h] = l, this[y] = c;
12
+ }
13
+ /**
14
+ * Creates a new opened synchronous transaction scope.
15
+ * @typeParam D - The type of the transaction data
16
+ * @param data - The transaction data
17
+ * @param cleaner - Optional synchronous cleanup function
18
+ * @param commiter - Optional synchronous commit function
19
+ * @param rollbacker - Optional synchronous rollback function
20
+ * @returns A new opened synchronous transaction scope
21
+ */
22
+ static open(i, a, l, c) {
23
+ return new w(i, a, l, c);
24
+ }
25
+ /**
26
+ * Synchronously disposes the transaction scope (cleanup with rollback).
27
+ */
28
+ [Symbol.dispose]() {
29
+ this[n] !== s && (this[y] && this[y](this[n]), this[r] && this[r](this[n]), this[n] = s);
30
+ }
31
+ }
32
+ function p(t) {
33
+ if (t[n] === s)
34
+ throw new m(
35
+ "Resource already closed/committed/rolled back."
36
+ );
37
+ return t[y] = void 0, t[h] && t[h](t[n]), t[Symbol.dispose](), t;
38
+ }
39
+ function _(t) {
40
+ if (t[n] === s)
41
+ throw new m(
42
+ "Resource already closed/committed/rolled back."
43
+ );
44
+ const i = t[y];
45
+ return t[y] = void 0, i && i(t[n]), t[Symbol.dispose](), t;
46
+ }
47
+ function R(t, i) {
48
+ if (t[n] === s)
49
+ throw new m("Resource already disposed.");
50
+ return i(t[n]);
51
+ }
52
+ function k(t, i) {
53
+ if (t[n] === s)
54
+ throw new m("Resource already disposed.");
55
+ return i(t[n]), t;
56
+ }
57
+ function S(t, i, a, l, c) {
58
+ const e = t;
59
+ return e[n] = i, e[r] = a, e[h] = l, e[y] = c, e;
60
+ }
61
+ const o = Symbol("Data"), f = Symbol("AsyncCleanup"), b = Symbol("AsyncCommit"), d = Symbol("AsyncRollback");
62
+ class u {
63
+ [o];
64
+ [f];
65
+ [b];
66
+ [d];
67
+ // ------ 构造函数 ------
68
+ constructor(i, a, l, c) {
69
+ this[o] = i, this[f] = a, this[b] = l, this[d] = c;
70
+ }
71
+ /**
72
+ * Creates a new opened asynchronous transaction scope.
73
+ * @typeParam D - The type of the transaction data
74
+ * @param data - The transaction data
75
+ * @param cleaner - Optional asynchronous cleanup function
76
+ * @param commiter - Optional asynchronous commit function
77
+ * @param rollbacker - Optional asynchronous rollback function
78
+ * @returns A new opened asynchronous transaction scope
79
+ */
80
+ static open(i, a, l, c) {
81
+ return new u(i, a, l, c);
82
+ }
83
+ /**
84
+ * Asynchronously disposes the transaction scope (cleanup with rollback).
85
+ * @returns Promise that resolves when disposal is complete
86
+ */
87
+ async [Symbol.asyncDispose]() {
88
+ this[o] !== s && (this[d] && await this[d](this[o]), this[f] && await this[f](this[o]), this[o] = s);
89
+ }
90
+ }
91
+ async function A(t) {
92
+ if (t[o] === s)
93
+ throw new m(
94
+ "Resource already closed/committed/rolled back."
95
+ );
96
+ return t[b] && await t[b](t[o]), t[d] = void 0, await t[Symbol.asyncDispose](), t;
97
+ }
98
+ async function v(t) {
99
+ if (t[o] === s)
100
+ throw new m(
101
+ "Resource already closed/committed/rolled back."
102
+ );
103
+ const i = t[d];
104
+ return t[d] = void 0, i && await i(t[o]), t[d] = void 0, await t[Symbol.asyncDispose](), t;
105
+ }
106
+ async function D(t, i) {
107
+ if (t[o] === s)
108
+ throw new m("Resource already disposed.");
109
+ return await i(t[o]);
110
+ }
111
+ async function C(t, i) {
112
+ if (t[o] === s)
113
+ throw new m("Resource already disposed.");
114
+ return await i(t[o]), t;
115
+ }
116
+ function $(t, i, a, l, c) {
117
+ const e = t;
118
+ return e[o] = i, e[f] = a, e[b] = l, e[d] = c, e;
119
+ }
120
+ const g = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
121
+ __proto__: null,
122
+ AsyncTxScope: u,
123
+ TxScope: w,
124
+ commit: p,
125
+ commitAsync: A,
126
+ exec: k,
127
+ execAsync: C,
128
+ reopen: S,
129
+ reopenAsync: $,
130
+ rollback: _,
131
+ rollbackAsync: v,
132
+ use: R,
133
+ useAsync: D
134
+ }, Symbol.toStringTag, { value: "Module" }));
135
+ export {
136
+ u as AsyncTxScope,
137
+ w as TxScope,
138
+ g as _,
139
+ p as commit,
140
+ A as commitAsync,
141
+ k as exec,
142
+ C as execAsync,
143
+ S as reopen,
144
+ $ as reopenAsync,
145
+ _ as rollback,
146
+ v as rollbackAsync,
147
+ R as use,
148
+ D as useAsync
149
+ };
150
+ //# sourceMappingURL=Resource.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Resource.mjs","sources":["../../src/Resource/TxScope.ts","../../src/Resource/TxScope-async.ts"],"sourcesContent":["// ========================================\r\n// ./src/Resource/TxScope.ts\r\n// ========================================\r\n/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */\r\n\r\nimport {\r\n type StateOpened,\r\n type StateCommit,\r\n type StateRollback,\r\n type Cleaner,\r\n type CommitUp,\r\n type RollbackUp,\r\n type NoneType,\r\n NONE,\r\n} from './types';\r\nimport {UseAfterFreeError} from '../Errors';\r\n\r\n// ------ 私有符号定义 ------\r\nconst _data = Symbol('Data');\r\nconst _cleanup = Symbol('Cleanup');\r\nconst _commitup = Symbol('Commit');\r\nconst _rollbackup = Symbol('Rollback');\r\n\r\n/**\r\n * Synchronous transaction scope management class.\r\n * @typeParam T - The type of the transaction data\r\n * @typeParam S - The state type (opened, commit, or rollback)\r\n */\r\nexport class TxScope<T, S extends StateOpened | StateCommit | StateRollback> {\r\n declare private state: S;\r\n public [_data]: T | NoneType;\r\n public [_cleanup]: Cleaner<T> | undefined;\r\n public [_commitup]: CommitUp<T> | undefined;\r\n public [_rollbackup]: RollbackUp<T> | undefined;\r\n\r\n // ------ 构造函数 ------\r\n private constructor(\r\n data: T,\r\n cleanup_fn?: Cleaner<T>,\r\n commitup_fn?: CommitUp<T>,\r\n rollbackup_fn?: RollbackUp<T>,\r\n ) {\r\n this[_data] = data;\r\n this[_cleanup] = cleanup_fn;\r\n this[_commitup] = commitup_fn;\r\n this[_rollbackup] = rollbackup_fn;\r\n }\r\n\r\n /**\r\n * Creates a new opened synchronous transaction scope.\r\n * @typeParam D - The type of the transaction data\r\n * @param data - The transaction data\r\n * @param cleaner - Optional synchronous cleanup function\r\n * @param commiter - Optional synchronous commit function\r\n * @param rollbacker - Optional synchronous rollback function\r\n * @returns A new opened synchronous transaction scope\r\n */\r\n static open<D>(\r\n data: D,\r\n cleaner?: Cleaner<D>,\r\n commiter?: CommitUp<D>,\r\n rollbacker?: RollbackUp<D>,\r\n ): TxScope<D, StateOpened> {\r\n return new TxScope(data, cleaner, commiter, rollbacker);\r\n }\r\n\r\n /**\r\n * Synchronously disposes the transaction scope (cleanup with rollback).\r\n */\r\n [Symbol.dispose](this: TxScope<T, StateOpened>): void {\r\n if (this[_data] === NONE) {\r\n return;\r\n }\r\n // 如果事务未被 commit/rollback 清理,则在 dispose 时执行回滚。\r\n if (this[_rollbackup]) {\r\n this[_rollbackup](this[_data] as T);\r\n }\r\n if (this[_cleanup]) {\r\n this[_cleanup](this[_data] as T);\r\n }\r\n this[_data] = NONE;\r\n }\r\n}\r\n\r\n/**\r\n * Union type for synchronous transaction scope states.\r\n * @typeParam T - The type of the transaction data\r\n */\r\nexport type TxScopeType<T> =\r\n | TxScope<T, StateOpened>\r\n | TxScope<T, StateCommit>\r\n | TxScope<T, StateRollback>;\r\n\r\n/**\r\n * Commits a synchronous transaction scope.\r\n * @typeParam T - The type of the transaction data\r\n * @param resource - The opened synchronous transaction scope\r\n * @returns The committed transaction scope\r\n * @throws Error if resource is already disposed/committed/rolled back\r\n */\r\nexport function commit<T>(\r\n resource: TxScope<T, StateOpened>,\r\n): TxScope<T, StateCommit> {\r\n if (resource[_data] === NONE) {\r\n throw new UseAfterFreeError(\r\n 'Resource already closed/committed/rolled back.',\r\n );\r\n }\r\n resource[_rollbackup] = undefined;\r\n if (resource[_commitup]) {\r\n resource[_commitup](resource[_data] as T);\r\n }\r\n resource[Symbol.dispose]();\r\n return resource as unknown as TxScope<T, StateCommit>;\r\n}\r\n\r\n/**\r\n * Rolls back a synchronous transaction scope.\r\n * @typeParam T - The type of the transaction data\r\n * @param resource - The opened synchronous transaction scope\r\n * @returns The rolled back transaction scope\r\n * @throws Error if resource is already disposed/committed/rolled back\r\n */\r\nexport function rollback<T>(\r\n resource: TxScope<T, StateOpened>,\r\n): TxScope<T, StateRollback> {\r\n if (resource[_data] === NONE) {\r\n throw new UseAfterFreeError(\r\n 'Resource already closed/committed/rolled back.',\r\n );\r\n }\r\n const fn = resource[_rollbackup];\r\n resource[_rollbackup] = undefined;\r\n if (fn) {\r\n fn(resource[_data] as T); // 即使这里失败了,回滚函数已经是undefined了,不会二次错误\r\n }\r\n resource[Symbol.dispose]();\r\n return resource as unknown as TxScope<T, StateRollback>;\r\n}\r\n\r\n/**\r\n * Uses a synchronous transaction scope with a callback function.\r\n * @typeParam T - The type of the transaction data\r\n * @typeParam R - The return type of the callback\r\n * @param TxScope - The opened synchronous transaction scope\r\n * @param fn - Function to process the transaction data\r\n * @returns The result of the callback\r\n * @throws Error if transaction scope is already disposed\r\n */\r\nexport function use<T, R>(\r\n TxScope: TxScope<T, StateOpened>,\r\n fn: (data: T) => R,\r\n): R {\r\n if (TxScope[_data] === NONE) {\r\n throw new UseAfterFreeError('Resource already disposed.');\r\n }\r\n return fn(TxScope[_data] as T);\r\n}\r\n\r\n/**\r\n * Performs a side effect on a synchronous transaction scope and returns it.\r\n * @typeParam T - The type of the transaction data\r\n * @typeParam R - The return type of the side effect function\r\n * @param TxScope - The opened synchronous transaction scope\r\n * @param fn - Side effect function to execute\r\n * @returns The same transaction scope after executing the side effect\r\n * @throws Error if transaction scope is already disposed\r\n */\r\nexport function exec<T, R>(\r\n TxScope: TxScope<T, StateOpened>,\r\n fn: (data: T) => R,\r\n): TxScope<T, StateOpened> {\r\n if (TxScope[_data] === NONE) {\r\n throw new UseAfterFreeError('Resource already disposed.');\r\n }\r\n fn(TxScope[_data] as T);\r\n return TxScope;\r\n}\r\n\r\n/**\r\n * Reopens a committed or rolled back synchronous transaction scope with new data.\r\n * @typeParam T - The type of the transaction data\r\n * @param TxScope - The committed or rolled back synchronous transaction scope\r\n * @param data - New data for the transaction scope\r\n * @param cleaner - Optional new cleanup function\r\n * @param commiter - Optional new commit function\r\n * @param rollbacker - Optional new rollback function\r\n * @returns The reopened synchronous transaction scope\r\n */\r\nexport function reopen<T>(\r\n TxScope: TxScope<T, StateCommit> | TxScope<T, StateRollback>,\r\n data: T,\r\n cleaner?: Cleaner<T>,\r\n commiter?: CommitUp<T>,\r\n rollbacker?: RollbackUp<T>,\r\n): TxScope<T, StateOpened> {\r\n const r = TxScope as unknown as TxScope<T, StateOpened>;\r\n r[_data] = data;\r\n r[_cleanup] = cleaner;\r\n r[_commitup] = commiter;\r\n r[_rollbackup] = rollbacker;\r\n return r;\r\n}\r\n","// ========================================\r\n// ./src/Resource/TxScope-async.ts\r\n// ========================================\r\n/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */\r\n\r\nimport {\r\n type StateOpened,\r\n type StateCommit,\r\n type StateRollback,\r\n type AnyCleaner,\r\n type AsyncCommitUp,\r\n type AnyRollbackUp,\r\n type NoneType,\r\n NONE,\r\n} from './types';\r\nimport {UseAfterFreeError} from '../Errors';\r\n\r\n// ------ 私有符号定义 ------\r\nconst _data = Symbol('Data');\r\nconst _cleanup = Symbol('AsyncCleanup');\r\nconst _commitup = Symbol('AsyncCommit');\r\nconst _rollbackup = Symbol('AsyncRollback');\r\n\r\n/**\r\n * Asynchronous transaction scope management class.\r\n * @typeParam T - The type of the transaction data\r\n * @typeParam S - The state type (opened, commit, or rollback)\r\n */\r\nexport class AsyncTxScope<\r\n T,\r\n S extends StateOpened | StateCommit | StateRollback,\r\n> {\r\n declare private state: S;\r\n public [_data]: T | NoneType;\r\n public [_cleanup]: AnyCleaner<T> | undefined;\r\n public [_commitup]: AsyncCommitUp<T> | undefined;\r\n public [_rollbackup]: AnyRollbackUp<T> | undefined;\r\n\r\n // ------ 构造函数 ------\r\n private constructor(\r\n data: T,\r\n cleanup_fn?: AnyCleaner<T>,\r\n commitup_fn?: AsyncCommitUp<T>,\r\n rollbackup_fn?: AnyRollbackUp<T>,\r\n ) {\r\n this[_data] = data;\r\n this[_cleanup] = cleanup_fn;\r\n this[_commitup] = commitup_fn;\r\n this[_rollbackup] = rollbackup_fn;\r\n }\r\n\r\n /**\r\n * Creates a new opened asynchronous transaction scope.\r\n * @typeParam D - The type of the transaction data\r\n * @param data - The transaction data\r\n * @param cleaner - Optional asynchronous cleanup function\r\n * @param commiter - Optional asynchronous commit function\r\n * @param rollbacker - Optional asynchronous rollback function\r\n * @returns A new opened asynchronous transaction scope\r\n */\r\n static open<D>(\r\n data: D,\r\n cleaner?: AnyCleaner<D>,\r\n commiter?: AsyncCommitUp<D>,\r\n rollbacker?: AnyRollbackUp<D>,\r\n ): AsyncTxScope<D, StateOpened> {\r\n return new AsyncTxScope(data, cleaner, commiter, rollbacker);\r\n }\r\n\r\n /**\r\n * Asynchronously disposes the transaction scope (cleanup with rollback).\r\n * @returns Promise that resolves when disposal is complete\r\n */\r\n async [Symbol.asyncDispose](\r\n this: AsyncTxScope<T, StateOpened>,\r\n ): Promise<void> {\r\n if (this[_data] === NONE) {\r\n return;\r\n }\r\n // 如果事务未被 commit/rollback 清理,则在 dispose 时执行异步回滚。\r\n if (this[_rollbackup]) {\r\n await this[_rollbackup](this[_data] as T); // 强制等待异步回滚\r\n }\r\n if (this[_cleanup]) {\r\n await this[_cleanup](this[_data] as T); // 强制等待异步清理\r\n }\r\n this[_data] = NONE;\r\n }\r\n}\r\n\r\n/**\r\n * Union type for asynchronous transaction scope states.\r\n * @typeParam T - The type of the transaction data\r\n */\r\nexport type AsyncTxScopeType<T> =\r\n | AsyncTxScope<T, StateOpened>\r\n | AsyncTxScope<T, StateCommit>\r\n | AsyncTxScope<T, StateRollback>;\r\n\r\n/**\r\n * Commits an asynchronous transaction scope.\r\n * @typeParam T - The type of the transaction data\r\n * @param resource - The opened asynchronous transaction scope\r\n * @returns Promise with the committed transaction scope\r\n * @throws Error if resource is already disposed/committed/rolled back\r\n */\r\nexport async function commitAsync<T>(\r\n resource: AsyncTxScope<T, StateOpened>,\r\n): Promise<AsyncTxScope<T, StateCommit>> {\r\n if (resource[_data] === NONE) {\r\n throw new UseAfterFreeError(\r\n 'Resource already closed/committed/rolled back.',\r\n );\r\n }\r\n if (resource[_commitup]) {\r\n await resource[_commitup](resource[_data] as T);\r\n }\r\n resource[_rollbackup] = undefined;\r\n await resource[Symbol.asyncDispose]();\r\n return resource as unknown as AsyncTxScope<T, StateCommit>;\r\n}\r\n\r\n/**\r\n * Rolls back an asynchronous transaction scope.\r\n * @typeParam T - The type of the transaction data\r\n * @param resource - The opened asynchronous transaction scope\r\n * @returns Promise with the rolled back transaction scope\r\n * @throws Error if resource is already disposed/committed/rolled back\r\n */\r\nexport async function rollbackAsync<T>(\r\n resource: AsyncTxScope<T, StateOpened>,\r\n): Promise<AsyncTxScope<T, StateRollback>> {\r\n if (resource[_data] === NONE) {\r\n throw new UseAfterFreeError(\r\n 'Resource already closed/committed/rolled back.',\r\n );\r\n }\r\n const fn = resource[_rollbackup];\r\n resource[_rollbackup] = undefined;\r\n if (fn) {\r\n await fn(resource[_data] as T);\r\n }\r\n resource[_rollbackup] = undefined;\r\n await resource[Symbol.asyncDispose]();\r\n return resource as unknown as AsyncTxScope<T, StateRollback>;\r\n}\r\n\r\n/**\r\n * Uses an asynchronous transaction scope with a callback function.\r\n * @typeParam T - The type of the transaction data\r\n * @typeParam R - The return type of the callback\r\n * @param TxScope - The opened asynchronous transaction scope\r\n * @param fn - Function to process the transaction data\r\n * @returns Promise with the result of the callback\r\n * @throws Error if transaction scope is already disposed\r\n */\r\nexport async function useAsync<T, R>(\r\n TxScope: AsyncTxScope<T, StateOpened>,\r\n fn: (data: T) => Promise<R> | R,\r\n): Promise<R> {\r\n if (TxScope[_data] === NONE) {\r\n throw new UseAfterFreeError('Resource already disposed.');\r\n }\r\n return await fn(TxScope[_data] as T);\r\n}\r\n\r\n/**\r\n * Performs a side effect on an asynchronous transaction scope and returns it.\r\n * @typeParam T - The type of the transaction data\r\n * @typeParam R - The return type of the side effect function\r\n * @param TxScope - The opened asynchronous transaction scope\r\n * @param fn - Side effect function to execute\r\n * @returns The same transaction scope after executing the side effect\r\n * @throws Error if transaction scope is already disposed\r\n */\r\nexport async function execAsync<T, R>(\r\n TxScope: AsyncTxScope<T, StateOpened>,\r\n fn: (data: T) => Promise<R> | R,\r\n): Promise<AsyncTxScope<T, StateOpened>> {\r\n if (TxScope[_data] === NONE) {\r\n throw new UseAfterFreeError('Resource already disposed.');\r\n }\r\n await fn(TxScope[_data] as T);\r\n return TxScope;\r\n}\r\n\r\n/**\r\n * Reopens a committed or rolled back asynchronous transaction scope with new data.\r\n * @typeParam T - The type of the transaction data\r\n * @param TxScope - The committed or rolled back asynchronous transaction scope\r\n * @param data - New data for the transaction scope\r\n * @param cleaner - Optional new cleanup function\r\n * @param commiter - Optional new commit function\r\n * @param rollbacker - Optional new rollback function\r\n * @returns The reopened asynchronous transaction scope\r\n */\r\nexport function reopenAsync<T>(\r\n TxScope: AsyncTxScope<T, StateCommit> | AsyncTxScope<T, StateRollback>,\r\n data: T,\r\n cleaner?: AnyCleaner<T>,\r\n commiter?: AsyncCommitUp<T>,\r\n rollbacker?: AnyRollbackUp<T>,\r\n): AsyncTxScope<T, StateOpened> {\r\n const r = TxScope as unknown as AsyncTxScope<T, StateOpened>;\r\n r[_data] = data;\r\n r[_cleanup] = cleaner;\r\n r[_commitup] = commiter;\r\n r[_rollbackup] = rollbacker;\r\n return r;\r\n}\r\n"],"names":["_data","_cleanup","_commitup","_rollbackup","TxScope","data","cleanup_fn","commitup_fn","rollbackup_fn","cleaner","commiter","rollbacker","NONE","commit","resource","UseAfterFreeError","rollback","fn","use","exec","reopen","r","AsyncTxScope","commitAsync","rollbackAsync","useAsync","execAsync","reopenAsync"],"mappings":";;AAkBA,MAAMA,IAAQ,OAAO,MAAM,GACrBC,IAAW,OAAO,SAAS,GAC3BC,IAAY,OAAO,QAAQ,GAC3BC,IAAc,OAAO,UAAU;AAO9B,MAAMC,EAAgE;AAAA,EAE3E,CAAQJ,CAAK;AAAA,EACb,CAAQC,CAAQ;AAAA,EAChB,CAAQC,CAAS;AAAA,EACjB,CAAQC,CAAW;AAAA;AAAA,EAGX,YACNE,GACAC,GACAC,GACAC,GACA;AACA,SAAKR,CAAK,IAAIK,GACd,KAAKJ,CAAQ,IAAIK,GACjB,KAAKJ,CAAS,IAAIK,GAClB,KAAKJ,CAAW,IAAIK;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,OAAO,KACLH,GACAI,GACAC,GACAC,GACyB;AACzB,WAAO,IAAIP,EAAQC,GAAMI,GAASC,GAAUC,CAAU;AAAA,EACxD;AAAA;AAAA;AAAA;AAAA,EAKA,CAAC,OAAO,OAAO,IAAuC;AACpD,IAAI,KAAKX,CAAK,MAAMY,MAIhB,KAAKT,CAAW,KAClB,KAAKA,CAAW,EAAE,KAAKH,CAAK,CAAM,GAEhC,KAAKC,CAAQ,KACf,KAAKA,CAAQ,EAAE,KAAKD,CAAK,CAAM,GAEjC,KAAKA,CAAK,IAAIY;AAAA,EAChB;AACF;AAkBO,SAASC,EACdC,GACyB;AACzB,MAAIA,EAASd,CAAK,MAAMY;AACtB,UAAM,IAAIG;AAAA,MACR;AAAA,IAAA;AAGJ,SAAAD,EAASX,CAAW,IAAI,QACpBW,EAASZ,CAAS,KACpBY,EAASZ,CAAS,EAAEY,EAASd,CAAK,CAAM,GAE1Cc,EAAS,OAAO,OAAO,EAAA,GAChBA;AACT;AASO,SAASE,EACdF,GAC2B;AAC3B,M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I,GACAC,GACAC,GAC8B;AAC9B,QAAMU,IAAIjB;AACV,SAAAiB,EAAErB,CAAK,IAAIK,GACXgB,EAAEpB,CAAQ,IAAIQ,GACdY,EAAEnB,CAAS,IAAIQ,GACfW,EAAElB,CAAW,IAAIQ,GACVU;AACT;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("../chunks/base-BTXL1y_I.cjs"),s=require("../chunks/helper-tsqZMRHW.cjs"),e=require("../chunks/combine-async-DcG-3p8i.cjs");function l(n,{fnOk:r=()=>{},fnErr:o=()=>{}}){return n.ok?r(n.value):o(n.error),n}function y(n,r){return n.ok||r(n.error),n}function f(n,r){return n.ok&&r(n.value),n}async function p(n,{fnOk:r=()=>{},fnErr:o=()=>{}},t){if(t?.aborted)return c.Err(s.ensureDOMException(t.reason));const a=await n;try{a.ok?await r(a.value,t):await o(a.error,t)}catch(i){return c.Err(s.wrapError(i))}return a}async function m(n,r,o){if(o?.aborted)return c.Err(s.ensureDOMException(o.reason));const t=await n;if(!t.ok)try{await r(t.error,o)}catch(a){return c.Err(s.wrapError(a))}return t}async function A(n,r,o){if(o?.aborted)return c.Err(s.ensureDOMException(o.reason));const t=await n;if(t.ok)try{await r(t.value,o)}catch(a){return c.Err(s.wrapError(a))}return t}async function E(n,r,o,t){const a=await n;if(a.ok)try{return await s.dynamicAwait(r(a.value,t))}catch(i){return s.UnknownError.from(i)}else try{return await s.dynamicAwait(o(a.error,t))}catch(i){return s.UnknownError.from(i)}}async function k(n,r,o){const t=await n;if(t.ok)try{return await s.dynamicAwait(r(t.value))}catch(a){return s.UnknownError.from(a)}else{if(o)try{return await s.dynamicAwait(o(t.error))}catch(a){return s.UnknownError.from(a)}return}}async function d(n,r,o){const t=await n;if(t.ok){if(o)try{return await s.dynamicAwait(o(t.value))}catch(a){return s.UnknownError.from(a)}return}else try{return await s.dynamicAwait(r(t.error))}catch(a){return s.UnknownError.from(a)}}async function w(n,r,o){const t=await n;if(t.ok)try{return await s.dynamicAwait(o(t.value))}catch(a){return s.UnknownError.from(a)}else try{return await s.dynamicAwait(r(t.error))}catch(a){return s.UnknownError.from(a)}}function h(n,r,o){let t=r;for(const a of n){if(!a.ok)return c.Err(a.error);t=o(t,a.value)}return c.Ok(t)}const O=n=>n.filter(r=>r.ok).map(r=>r.value),_=n=>n.filter(r=>!r.ok).map(r=>r.error);function v(n){const r=[],o=[];for(const t of n)t.ok?r.push(t.value):o.push(t.error);return[r,o]}function x(n){if(c.isOk(n)){const r=n.value;return s.isResultLike(r)?r:n}return n}function z(n){let r=n;for(;c.isOk(r);){const o=r.value;if(!s.isResultLike(o))return r;r=o}return r}async function b(n){let r=await n;for(;c.isOk(r);){let o=r.value;if(s.isPromiseLike(o)&&(o=await o),!s.isResultLike(o))return r;r=o}return r}async function P(n,r,o,t){if(t?.aborted)return c.Err(s.ensureDOMException(t.reason));let a=r;for(const i of n){const u=await c.normalizeToResult(i);if(t?.aborted)return c.Err(s.ensureDOMException(t.reason));if(!u.ok)return c.Err(u.error);try{if(a=await o(a,u.value,t),t?.aborted)return c.Err(s.ensureDOMException(t.reason))}catch(M){return c.Err(s.wrapError(M))}}return c.Ok(a)}async function T(n,r){if(r?.aborted)return Promise.reject(s.ensureDOMException(r.reason));const o=n.map(i=>c.normalizeToResult(i)),t=await Promise.all(o),a=[];for(const i of t){if(r?.aborted)return Promise.reject(s.ensureDOMException(r.reason));i.ok&&a.push(i.value)}return a}async function R(n,r){if(r?.aborted)return Promise.reject(s.ensureDOMException(r.reason));const o=n.map(i=>c.normalizeToResult(i)),t=await Promise.all(o),a=[];for(const i of t){if(r?.aborted)throw r.reason;i.ok||a.push(i.error)}return a}async function B(n,r){if(r?.aborted)return Promise.reject(s.ensureDOMException(r.reason));const o=n.map(u=>c.normalizeToResult(u)),t=await Promise.all(o),a=[],i=[];for(const u of t){if(r?.aborted)throw r.reason;u.ok?a.push(u.value):i.push(u.error)}return[a,i]}const D=Object.freeze(Object.defineProperty({__proto__:null,Err:c.Err,Ok:c.Ok,all:e.all,allAsync:e.allAsync,and:e.and,andAsync:e.andAsync,andThen:e.andThen,andThenAsync:e.andThenAsync,ap:e.ap,apAsync:e.apAsync,collectErr:_,collectErrAsync:R,collectOk:O,collectOkAsync:T,deepFlatten:z,deepFlattenAsync:b,filter:e.filter,filterAsync:e.filterAsync,flatten:x,fromPromise:c.fromPromise,ifErr:e.ifErr,ifErrAsync:d,ifOk:e.ifOk,ifOkAsync:k,isErr:c.isErr,isErrAnd:c.isErrAnd,isOk:c.isOk,isOkAnd:c.isOkAnd,map:e.map,mapAll:e.mapAll,mapAsync:e.mapAsync,mapBoth:e.mapBoth,mapBothAsync:e.mapBothAsync,mapErr:e.mapErr,mapErrAsync:e.mapErrAsync,mapOrElse:e.mapOrElse,mapOrElseAsync:w,match:e.match,matchResultAsync:E,normalizeToResult:c.normalizeToResult,or:e.or,orAsync:e.orAsync,orElse:e.orElse,orElseAsync:e.orElseAsync,partition:v,partitionAsync:B,peekBoth:l,peekBothAsync:p,peekErr:y,peekErrAsync:m,peekOk:f,peekOkAsync:A,recover:e.recover,recoverAsync:e.recoverAsync,reduce:h,reduceAsync:P,safeExecute:c.safeExecute,safeExecuteAsync:c.safeExecuteAsync,sequenceAsync:e.sequenceAsync,toPromise:c.toPromise,zip:e.zip,zipAsync:e.zipAsync},Symbol.toStringTag,{value:"Module"}));exports.Err=c.Err;exports.Ok=c.Ok;exports.fromPromise=c.fromPromise;exports.isErr=c.isErr;exports.isErrAnd=c.isErrAnd;exports.isOk=c.isOk;exports.isOkAnd=c.isOkAnd;exports.normalizeToResult=c.normalizeToResult;exports.safeExecute=c.safeExecute;exports.safeExecuteAsync=c.safeExecuteAsync;exports.toPromise=c.toPromise;exports.all=e.all;exports.allAsync=e.allAsync;exports.and=e.and;exports.andAsync=e.andAsync;exports.andThen=e.andThen;exports.andThenAsync=e.andThenAsync;exports.ap=e.ap;exports.apAsync=e.apAsync;exports.filter=e.filter;exports.filterAsync=e.filterAsync;exports.ifErr=e.ifErr;exports.ifOk=e.ifOk;exports.map=e.map;exports.mapAll=e.mapAll;exports.mapAsync=e.mapAsync;exports.mapBoth=e.mapBoth;exports.mapBothAsync=e.mapBothAsync;exports.mapErr=e.mapErr;exports.mapErrAsync=e.mapErrAsync;exports.mapOrElse=e.mapOrElse;exports.match=e.match;exports.or=e.or;exports.orAsync=e.orAsync;exports.orElse=e.orElse;exports.orElseAsync=e.orElseAsync;exports.recover=e.recover;exports.recoverAsync=e.recoverAsync;exports.sequenceAsync=e.sequenceAsync;exports.zip=e.zip;exports.zipAsync=e.zipAsync;exports.__export__=D;exports.collectErr=_;exports.collectErrAsync=R;exports.collectOk=O;exports.collectOkAsync=T;exports.deepFlatten=z;exports.deepFlattenAsync=b;exports.flatten=x;exports.ifErrAsync=d;exports.ifOkAsync=k;exports.mapOrElseAsync=w;exports.matchResultAsync=E;exports.partition=v;exports.partitionAsync=B;exports.peekBoth=l;exports.peekBothAsync=p;exports.peekErr=y;exports.peekErrAsync=m;exports.peekOk=f;exports.peekOkAsync=A;exports.reduce=h;exports.reduceAsync=P;
2
+ //# sourceMappingURL=Result.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Result.cjs","sources":["../../src/Result/Consumers/inspect.ts","../../src/Result/Consumers/branch-async.ts","../../src/Result/Operators/reduce.ts","../../src/Result/Operators/partition.ts","../../src/Result/Operators/flatten.ts","../../src/Result/Operators/Async/reduce-async.ts","../../src/Result/Operators/Async/partition-async.ts"],"sourcesContent":["// ========================================\r\n// ./src/Result/Consumers/inspect.ts\r\n// ========================================\r\n\r\nimport type {AnyResult, Result} from '../types';\r\nimport {UnknownError, ensureDOMException} from '../../unknown-error';\r\nimport {Err} from '../base';\r\nimport {wrapError} from '../../helper';\r\nimport type {MaybePromise} from '../../Utils/type-tool';\r\n\r\n/**\r\n * Returns the Result as is, regardless of its state. Used for side effects (e.g., logging) on both Success/Failure paths.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param result - The Result to peek\r\n * @param options - Options object containing optional side-effect functions\r\n * @returns The unchanged Result\r\n */\r\nexport function peekBoth<T, E>(\r\n result: Result<T, E>,\r\n {\r\n fnOk = () => {},\r\n fnErr = () => {},\r\n }: {\r\n fnOk?: (value: T) => void;\r\n fnErr?: (error: E) => void;\r\n },\r\n): Result<T, E> {\r\n if (result.ok) {\r\n fnOk(result.value);\r\n } else {\r\n fnErr(result.error);\r\n }\r\n return result;\r\n}\r\n\r\n/**\r\n * Executes the fnErr function as a side effect if the Result is Failure; then returns the Result as is.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param result - The Result to peek\r\n * @param fnErr - The side-effect function to execute on Failure\r\n * @returns The unchanged Result\r\n */\r\nexport function peekErr<T, E>(\r\n result: Result<T, E>,\r\n fnErr: (error: E) => void,\r\n): Result<T, E> {\r\n if (!result.ok) {\r\n fnErr(result.error);\r\n }\r\n return result;\r\n}\r\n\r\n/**\r\n * Executes the fnOk function as a side effect if the Result is Success; then returns the Result as is.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param result - The Result to peek\r\n * @param fnOk - The side-effect function to execute on Success\r\n * @returns The unchanged Result\r\n */\r\nexport function peekOk<T, E>(\r\n result: Result<T, E>,\r\n fnOk: (value: T) => void,\r\n): Result<T, E> {\r\n if (result.ok) {\r\n fnOk(result.value);\r\n }\r\n return result;\r\n}\r\n\r\n/**\r\n * Asynchronously executes side effects for both Success/Failure paths, and returns the AsyncResult unchanged.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param result_promise - The Result or Promise<Result> to peek\r\n * @param options - Options object containing optional side-effect functions\r\n * @param signal - Optional AbortSignal for cancellation\r\n * @returns A Promise containing the original Result, or an UnknownError if a side effect throws\r\n */\r\nexport async function peekBothAsync<T, E>(\r\n result_promise: AnyResult<T, E>,\r\n {\r\n fnOk = () => {},\r\n fnErr = () => {},\r\n }: {\r\n fnOk?: (value: T, signal?: AbortSignal) => MaybePromise<void>;\r\n fnErr?: (error: E, signal?: AbortSignal) => MaybePromise<void>;\r\n },\r\n signal?: AbortSignal,\r\n): Promise<Result<T, E | DOMException | UnknownError>> {\r\n if (signal?.aborted) return Err(ensureDOMException(signal.reason));\r\n const result = await result_promise;\r\n\r\n try {\r\n if (result.ok) {\r\n await fnOk(result.value, signal);\r\n } else {\r\n await fnErr(result.error, signal);\r\n }\r\n } catch (raw_error) {\r\n return Err(wrapError(raw_error));\r\n }\r\n\r\n return result;\r\n}\r\n\r\n/**\r\n * Asynchronously executes fnErr as a side effect if the Result is Failure; then returns the Result unchanged.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param result_promise - The Result or Promise<Result> to peek\r\n * @param fnErr - The side-effect function to execute on Failure\r\n * @param signal - Optional AbortSignal for cancellation\r\n * @returns A Promise containing the original Result, or an UnknownError if fnErr throws\r\n */\r\nexport async function peekErrAsync<T, E>(\r\n result_promise: AnyResult<T, E>,\r\n fnErr: (error: E, signal?: AbortSignal) => MaybePromise<void>,\r\n signal?: AbortSignal,\r\n): Promise<Result<T, E | DOMException | UnknownError>> {\r\n if (signal?.aborted) return Err(ensureDOMException(signal.reason));\r\n const result = await result_promise;\r\n\r\n if (!result.ok) {\r\n try {\r\n await fnErr(result.error, signal);\r\n } catch (raw_error) {\r\n return Err(wrapError(raw_error));\r\n }\r\n }\r\n return result;\r\n}\r\n\r\n/**\r\n * Asynchronously executes fnOk as a side effect if the Result is Success; then returns the Result unchanged.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param result_promise - The Result or Promise<Result> to peek\r\n * @param fnOk - The side-effect function to execute on Success\r\n * @param signal - Optional AbortSignal for cancellation\r\n * @returns A Promise containing the original Result, or an UnknownError if fnOk throws\r\n */\r\nexport async function peekOkAsync<T, E>(\r\n result_promise: AnyResult<T, E>,\r\n fnOk: (value: T, signal?: AbortSignal) => MaybePromise<void>,\r\n signal?: AbortSignal,\r\n): Promise<Result<T, E | DOMException | UnknownError>> {\r\n if (signal?.aborted) return Err(ensureDOMException(signal.reason));\r\n const result = await result_promise;\r\n\r\n if (result.ok) {\r\n try {\r\n await fnOk(result.value, signal);\r\n } catch (raw_error) {\r\n return Err(wrapError(raw_error));\r\n }\r\n }\r\n return result;\r\n}\r\n","// ========================================\r\n// ./src/Result/Consumers/branch-async.ts\r\n// ========================================\r\nimport type {AnyResult} from '../types';\r\nimport {UnknownError} from '../../unknown-error';\r\nimport type {MaybePromise} from '../../Utils/type-tool';\r\nimport {dynamicAwait} from '../../helper';\r\n\r\n/**\r\n * Asynchronously executes different callback functions based on the Result state and returns Promise<R>.\r\n * Catches exceptions thrown by the callbacks.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @typeParam R - The type of the return value\r\n * @param result_promise - The Result or Promise<Result> to match\r\n * @param onOk - The callback function for a successful state (Ok)\r\n * @param onErr - The callback function for a failure state (Err)\r\n * @param signal - Optional AbortSignal for cancellation\r\n * @returns The Promise resolving to the return value of onOk or onErr, or an UnknownError if a callback throws\r\n */\r\nexport async function matchResultAsync<T, E, R>(\r\n result_promise: AnyResult<T, E>,\r\n onOk: (value: T, signal?: AbortSignal) => MaybePromise<R>,\r\n onErr: (error: E, signal?: AbortSignal) => MaybePromise<R>,\r\n signal?: AbortSignal,\r\n): Promise<R | UnknownError> {\r\n const result = await result_promise;\r\n\r\n if (result.ok) {\r\n try {\r\n return await dynamicAwait(onOk(result.value, signal));\r\n } catch (raw_error) {\r\n return UnknownError.from(raw_error);\r\n }\r\n } else {\r\n try {\r\n return await dynamicAwait(onErr(result.error, signal));\r\n } catch (raw_error) {\r\n return UnknownError.from(raw_error);\r\n }\r\n }\r\n}\r\n/**\r\n * Asynchronously executes onOk if the Result is **Ok**; executes the optional onElse if **Err**.\r\n * All exceptions thrown by the callbacks are caught and wrapped as UnknownError.\r\n * @typeParam T - The type of the **Success Value** in the **Ok state**.\r\n * @typeParam E - The type of the **Error** in the **Err state**.\r\n * @typeParam R1 - The final resolved return type of the **onOk** callback.\r\n * @param result_promise - The Result object or Promise<Result> to be checked.\r\n * @param onOk - The callback executed if the Result is Ok. Returns R1 or Promise<R1>.\r\n * @returns A Promise that resolves to the result of onOk (R1), or UnknownError if a callback throws, or **void** if the Result is Err.\r\n */\r\nexport async function ifOkAsync<T, E, R1>(\r\n result_promise: AnyResult<T, E>,\r\n onOk: (value: T) => MaybePromise<R1>,\r\n): Promise<R1 | UnknownError | void>;\r\n\r\n/**\r\n * Asynchronously executes onOk if the Result is **Ok**; executes onElse if **Err**.\r\n * All exceptions thrown by the callbacks are caught and wrapped as UnknownError.\r\n * @typeParam T - The type of the **Success Value** in the **Ok state**.\r\n * @typeParam E - The type of the **Error** in the **Err state**.\r\n * @typeParam R1 - The final resolved return type of the **onOk** callback.\r\n * @typeParam R2 - The final resolved return type of the **onElse** callback.\r\n * @param result_promise - The Result object or Promise<Result> to be checked.\r\n * @param onOk - The callback executed if the Result is Ok. Returns R1 or Promise<R1>.\r\n * @param onElse - The callback executed if the Result is Err. Returns R2 or Promise<R2>.\r\n * @returns A Promise that resolves to the result of onOk or onElse (R1 or R2), or UnknownError if a callback throws.\r\n */\r\nexport async function ifOkAsync<T, E, R1, R2>(\r\n result_promise: AnyResult<T, E>,\r\n onOk: (value: T) => MaybePromise<R1>,\r\n onElse: (error: E) => MaybePromise<R2>,\r\n): Promise<R1 | R2 | UnknownError>;\r\n\r\nexport async function ifOkAsync<T, E, R1, R2 = void>(\r\n result_promise: AnyResult<T, E>,\r\n onOk: (value: T) => MaybePromise<R1>,\r\n onElse?: (error: E) => MaybePromise<R2>,\r\n): Promise<R1 | R2 | UnknownError | void> {\r\n const result = await result_promise;\r\n\r\n if (result.ok) {\r\n try {\r\n return await dynamicAwait(onOk(result.value));\r\n } catch (raw_error) {\r\n return UnknownError.from(raw_error);\r\n }\r\n } else {\r\n if (onElse) {\r\n try {\r\n return await dynamicAwait(onElse(result.error));\r\n } catch (raw_error) {\r\n return UnknownError.from(raw_error);\r\n }\r\n }\r\n return;\r\n }\r\n}\r\n\r\n/**\r\n * Asynchronously executes onErr if the Result is **Err**; executes the optional onElse if **Ok**.\r\n * All exceptions thrown by the callbacks are caught and wrapped as UnknownError.\r\n * @typeParam T - The type of the **Success Value** in the **Ok state**.\r\n * @typeParam E - The type of the **Error** in the **Err state**.\r\n * @typeParam R1 - The final resolved return type of the **onErr** callback.\r\n * @param result_promise - The Result object or Promise<Result> to be checked.\r\n * @param onErr - The callback executed if the Result is Err. Returns R1 or Promise<R1>.\r\n * @returns A Promise that resolves to the result of onErr (R1), or UnknownError if a callback throws, or **void** if the Result is Ok.\r\n */\r\nexport async function ifErrAsync<T, E, R1>(\r\n result_promise: AnyResult<T, E>,\r\n onErr: (error: E) => MaybePromise<R1>,\r\n): Promise<R1 | UnknownError | void>;\r\n\r\n/**\r\n * Asynchronously executes onErr if the Result is **Err**; executes onElse if **Ok**.\r\n * All exceptions thrown by the callbacks are caught and wrapped as UnknownError.\r\n * @typeParam T - The type of the **Success Value** in the **Ok state**.\r\n * @typeParam E - The type of the **Error** in the **Err state**.\r\n * @typeParam R1 - The final resolved return type of the **onErr** callback.\r\n * @typeParam R2 - The final resolved return type of the **onElse** callback.\r\n * @param result_promise - The Result object or Promise<Result> to be checked.\r\n * @param onErr - The callback executed if the Result is Err. Returns R1 or Promise<R1>.\r\n * @param onElse - The callback executed if the Result is Ok. Returns R2 or Promise<R2>.\r\n * @returns A Promise that resolves to the result of onErr or onElse (R1 or R2), or UnknownError if a callback throws.\r\n */\r\nexport async function ifErrAsync<T, E, R1, R2>(\r\n result_promise: AnyResult<T, E>,\r\n onErr: (error: E) => MaybePromise<R1>,\r\n onElse: (value: T) => MaybePromise<R2>,\r\n): Promise<R1 | R2 | UnknownError>;\r\n\r\nexport async function ifErrAsync<T, E, R1, R2 = void>(\r\n result_promise: AnyResult<T, E>,\r\n onErr: (error: E) => MaybePromise<R1>,\r\n onElse?: (value: T) => MaybePromise<R2>,\r\n): Promise<R1 | R2 | UnknownError | void> {\r\n const result = await result_promise;\r\n\r\n if (!result.ok) {\r\n try {\r\n return await dynamicAwait(onErr(result.error));\r\n } catch (raw_error) {\r\n return UnknownError.from(raw_error);\r\n }\r\n } else {\r\n if (onElse) {\r\n try {\r\n return await dynamicAwait(onElse(result.value));\r\n } catch (raw_error) {\r\n return UnknownError.from(raw_error);\r\n }\r\n }\r\n return;\r\n }\r\n}\r\n\r\n/**\r\n * Asynchronously transforms the Result into a single type R.\r\n * Uses mapFn on Success; uses defaultFn on Failure.\r\n * Catches exceptions thrown by the callbacks.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @typeParam R - The type of the final return value\r\n * @param result_promise - The Result or Promise<Result> to process\r\n * @param defaultFn - The function to compute the default value on failure\r\n * @param mapFn - The function to map the success value\r\n * @returns The Promise resolving to the final return value, or an UnknownError if a callback throws\r\n */\r\nexport async function mapOrElseAsync<T, E, R>(\r\n result_promise: AnyResult<T, E>,\r\n defaultFn: (error: E) => MaybePromise<R>,\r\n mapFn: (value: T) => MaybePromise<R>,\r\n): Promise<R | UnknownError> {\r\n const result = await result_promise;\r\n\r\n if (result.ok) {\r\n try {\r\n return await dynamicAwait(mapFn(result.value));\r\n } catch (raw_error) {\r\n return UnknownError.from(raw_error);\r\n }\r\n } else {\r\n try {\r\n return await dynamicAwait(defaultFn(result.error));\r\n } catch (raw_error) {\r\n return UnknownError.from(raw_error);\r\n }\r\n }\r\n}\r\n","// ========================================\r\n// ./src/Result/Operators/reduce.ts\r\n// ========================================\r\nimport type {Result} from '../types';\r\nimport {Ok, Err} from '../base';\r\n\r\n/**\r\n * Reduces multiple Results into a single Result, collecting all success values or the first error (short-circuit).\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @typeParam R - The type of the reduced result\r\n * @param results - The array of Results to reduce\r\n * @param initial - The initial value\r\n * @param reducer - The reduction function\r\n * @returns A Result containing the reduced result\r\n */\r\nexport function reduce<T, E, R>(\r\n results: readonly Result<T, E>[],\r\n initial: R,\r\n reducer: (accumulator: R, value: T) => R,\r\n): Result<R, E> {\r\n let accumulator = initial;\r\n\r\n for (const result of results) {\r\n if (!result.ok) {\r\n return Err(result.error);\r\n }\r\n accumulator = reducer(accumulator, result.value);\r\n }\r\n\r\n return Ok(accumulator);\r\n}\r\n","// ========================================\r\n// ./src/Result/Operators/partition.ts\r\n// ========================================\r\nimport type {Result} from '../types';\r\n\r\n/**\r\n * Collects all success values from an array of Results.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param results - The array of Results\r\n * @returns An array of collected success values\r\n */\r\nexport const collectOk = <T, E>(results: readonly Result<T, E>[]): T[] =>\r\n results\r\n .filter((result): result is {ok: true; value: T} => result.ok)\r\n .map((result) => result.value);\r\n\r\n/**\r\n * Collects all failure errors from an array of Results.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param results - The array of Results\r\n * @returns An array of collected error values\r\n */\r\nexport const collectErr = <T, E>(results: readonly Result<T, E>[]): E[] =>\r\n results\r\n .filter((result): result is {ok: false; error: E} => !result.ok)\r\n .map((result) => result.error);\r\n\r\n/**\r\n * Splits an array of Results into a tuple of success values and error values.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param results - The array of Results\r\n * @returns A tuple containing the array of success values and the array of error values\r\n */\r\nexport function partition<T, E>(results: readonly Result<T, E>[]): [T[], E[]] {\r\n const oks: T[] = [];\r\n const errs: E[] = [];\r\n\r\n for (const result of results) {\r\n if (result.ok) {\r\n oks.push(result.value);\r\n } else {\r\n errs.push(result.error);\r\n }\r\n }\r\n\r\n return [oks, errs];\r\n}\r\n","// ========================================\r\n// ./src/Result/Operators/flatten.ts\r\n// ========================================\r\nimport type {Result} from '../types';\r\nimport {isOk} from '../base';\r\nimport {isPromiseLike, isResultLike} from '../../helper';\r\n\r\n/**\r\n * Unwraps a nested Result into a single layer (Result<T, E>).\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param result - The nested Result to flatten\r\n * @returns The flattened Result\r\n */\r\nexport function flatten<T, E>(\r\n result: Result<T | Result<T, E>, E>,\r\n): Result<T, E> {\r\n if (isOk(result)) {\r\n const inner_value = result.value;\r\n if (isResultLike(inner_value)) {\r\n return inner_value as Result<T, E>;\r\n }\r\n return result as Result<T, E>;\r\n }\r\n return result as Result<T, E>;\r\n}\r\n\r\n/**\r\n * Recursively unwraps a deeply nested Result into a single layer (Result<T, E>).\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param result - The deeply nested Result to flatten\r\n * @returns The flattened Result\r\n */\r\nexport function deepFlatten<T, E>(\r\n result: Result<T | Result<unknown, E>, E>,\r\n): Result<T, E> {\r\n let current_result = result as Result<unknown, E>;\r\n\r\n while (isOk(current_result)) {\r\n const value_to_check = current_result.value;\r\n\r\n if (!isResultLike(value_to_check)) {\r\n return current_result as Result<T, E>;\r\n }\r\n current_result = value_to_check as Result<unknown, E>;\r\n }\r\n return current_result;\r\n}\r\n\r\n/**\r\n * Asynchronously and recursively unwraps a deeply nested Result (including nested Promises within the Result value) into a single layer.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param result_promise - The deeply nested Result or `Promise<Result<T, E>>` to flatten\r\n * @param signal - Optional AbortSignal for cancellation\r\n * @returns The Promise resolving to the flattened Result\r\n */\r\nexport async function deepFlattenAsync<T, E>(\r\n result_promise:\r\n | PromiseLike<Result<T | Result<unknown, E>, E>>\r\n | Result<T | Result<unknown, E>, E>,\r\n): Promise<Result<T, E>> {\r\n let current_result = await result_promise;\r\n\r\n while (isOk(current_result)) {\r\n let value_to_check = current_result.value;\r\n\r\n if (isPromiseLike(value_to_check)) {\r\n value_to_check = await value_to_check;\r\n }\r\n\r\n if (!isResultLike(value_to_check)) {\r\n return current_result as Result<T, E>;\r\n }\r\n current_result = value_to_check as Result<T, E>;\r\n }\r\n return current_result;\r\n}\r\n","// ========================================\r\n// ./src/Result/Operators/Async/reduce-async.ts\r\n// ========================================\r\n\r\nimport type {AnyResult, AsyncResult} from '../../types';\r\nimport {UnknownError, ensureDOMException} from '../../../unknown-error';\r\nimport {Ok, Err, normalizeToResult} from '../../base';\r\nimport {wrapError} from 'src/helper';\r\nimport type {MaybePromise} from '../../../Utils/type-tool';\r\n\r\n/**\r\n * Asynchronously and sequentially reduces multiple AsyncResults into a single Result (short-circuit).\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @typeParam R - The type of the reduced result\r\n * @param resultPromises - The array of AsyncResults to reduce\r\n * @param initial - The initial value\r\n * @param reducer - The reduction function (can be async)\r\n * @param signal - Optional AbortSignal for cancellation\r\n * @returns An AsyncResult containing the reduced result\r\n */\r\nexport async function reduceAsync<T, E, R>(\r\n resultPromises: readonly AnyResult<T, E>[],\r\n initial: R,\r\n reducer: (accumulator: R, value: T, signal?: AbortSignal) => MaybePromise<R>,\r\n signal?: AbortSignal,\r\n): AsyncResult<R, E | UnknownError | DOMException> {\r\n if (signal?.aborted) return Err(ensureDOMException(signal.reason));\r\n\r\n let accumulator = initial;\r\n\r\n for (const resultPromise of resultPromises) {\r\n const result = await normalizeToResult(resultPromise);\r\n if (signal?.aborted) return Err(ensureDOMException(signal.reason));\r\n\r\n if (!result.ok) {\r\n return Err(result.error);\r\n }\r\n\r\n try {\r\n accumulator = await reducer(accumulator, result.value, signal);\r\n if (signal?.aborted) return Err(ensureDOMException(signal.reason));\r\n } catch (rawError) {\r\n return Err(wrapError(rawError));\r\n }\r\n }\r\n\r\n return Ok(accumulator);\r\n}\r\n","// ========================================\r\n// ./src/Result/Operators/Async/partition-async.ts\r\n// ========================================\r\n\r\nimport type {AnyResult} from '../../types';\r\nimport {UnknownError, ensureDOMException} from '../../../unknown-error';\r\nimport {normalizeToResult} from '../../base';\r\n\r\n/**\r\n * Asynchronously collects all success values from an array of AsyncResults.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param results - The array of AsyncResults\r\n * @param signal - Optional AbortSignal for cancellation\r\n * @returns A Promise of the array of collected success values\r\n */\r\nexport async function collectOkAsync<T, E>(\r\n results: readonly AnyResult<T, E>[],\r\n signal?: AbortSignal,\r\n): Promise<T[]> {\r\n if (signal?.aborted) return Promise.reject(ensureDOMException(signal.reason));\r\n\r\n const normalized_promises = results.map((r) => normalizeToResult(r));\r\n const settled_results = await Promise.all(normalized_promises);\r\n\r\n const oks: T[] = [];\r\n for (const result of settled_results) {\r\n if (signal?.aborted)\r\n return Promise.reject(ensureDOMException(signal.reason));\r\n\r\n if (result.ok) {\r\n oks.push(result.value);\r\n }\r\n }\r\n\r\n return oks;\r\n}\r\n\r\n/**\r\n * Asynchronously collects all failure errors from an array of AsyncResults.\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param results - The array of AsyncResults\r\n * @param signal - Optional AbortSignal for cancellation\r\n * @returns A Promise of the array of collected error values\r\n */\r\nexport async function collectErrAsync<T, E>(\r\n results: readonly AnyResult<T, E>[],\r\n signal?: AbortSignal,\r\n): Promise<E[]> {\r\n if (signal?.aborted) return Promise.reject(ensureDOMException(signal.reason));\r\n\r\n const normalized_promises = results.map((r) => normalizeToResult(r));\r\n const settled_results = await Promise.all(normalized_promises);\r\n\r\n const errs: E[] = [];\r\n for (const result of settled_results) {\r\n if (signal?.aborted) throw signal.reason;\r\n\r\n if (!result.ok) {\r\n errs.push(result.error as E);\r\n }\r\n }\r\n\r\n return errs;\r\n}\r\n\r\n/**\r\n * Asynchronously executes the Result array concurrently and partitions it into a tuple of success values and error values (non-short-circuiting).\r\n * @typeParam T - The type of the success value\r\n * @typeParam E - The type of the error\r\n * @param results - The array of AsyncResults\r\n * @param signal - Optional AbortSignal for cancellation\r\n * @returns A Promise of a tuple containing the success value array and the error value array\r\n */\r\nexport async function partitionAsync<T, E>(\r\n results: readonly AnyResult<T, E>[],\r\n signal?: AbortSignal,\r\n): Promise<[T[], Array<E | DOMException | UnknownError>]> {\r\n if (signal?.aborted) return Promise.reject(ensureDOMException(signal.reason));\r\n\r\n const normalized_promises = results.map((r) => normalizeToResult(r));\r\n const settled_results = await Promise.all(normalized_promises);\r\n\r\n const oks: T[] = [];\r\n const errs: Array<E | DOMException | UnknownError> = [];\r\n\r\n for (const result of settled_results) {\r\n if (signal?.aborted) throw signal.reason;\r\n\r\n if (result.ok) {\r\n oks.push(result.value);\r\n } else {\r\n errs.push(result.error);\r\n }\r\n }\r\n\r\n return [oks, errs];\r\n}\r\n"],"names":["peekBoth","result","fnOk","fnErr","peekErr","peekOk","peekBothAsync","result_promise","signal","Err","ensureDOMException","raw_error","wrapError","peekErrAsync","peekOkAsync","matchResultAsync","onOk","onErr","dynamicAwait","UnknownError","ifOkAsync","onElse","ifErrAsync","mapOrElseAsync","defaultFn","mapFn","reduce","results","initial","reducer","accumulator","Ok","collectOk","collectErr","partition","oks","errs","flatten","isOk","inner_value","isResultLike","deepFlatten","current_result","value_to_check","deepFlattenAsync","isPromiseLike","reduceAsync","resultPromises","resultPromise","normalizeToResult","rawError","collectOkAsync","normalized_promises","r","settled_results","collectErrAsync","partitionAsync"],"mappings":"4NAkBO,SAASA,EACdC,EACA,CACE,KAAAC,EAAO,IAAM,CAAC,EACd,MAAAC,EAAQ,IAAM,CAAC,CACjB,EAIc,CACd,OAAIF,EAAO,GACTC,EAAKD,EAAO,KAAK,EAEjBE,EAAMF,EAAO,KAAK,EAEbA,CACT,CAUO,SAASG,EACdH,EACAE,EACc,CACd,OAAKF,EAAO,IACVE,EAAMF,EAAO,KAAK,EAEbA,CACT,CAUO,SAASI,EACdJ,EACAC,EACc,CACd,OAAID,EAAO,IACTC,EAAKD,EAAO,KAAK,EAEZA,CACT,CAWA,eAAsBK,EACpBC,EACA,CACE,KAAAL,EAAO,IAAM,CAAC,EACd,MAAAC,EAAQ,IAAM,CAAC,CACjB,EAIAK,EACqD,CACrD,GAAIA,GAAQ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