effect 3.10.18 → 3.11.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 (448) hide show
  1. package/dist/cjs/Array.js +206 -0
  2. package/dist/cjs/Array.js.map +1 -1
  3. package/dist/cjs/BigDecimal.js +181 -24
  4. package/dist/cjs/BigDecimal.js.map +1 -1
  5. package/dist/cjs/BigInt.js +54 -0
  6. package/dist/cjs/BigInt.js.map +1 -1
  7. package/dist/cjs/Boolean.js +24 -0
  8. package/dist/cjs/Boolean.js.map +1 -1
  9. package/dist/cjs/Brand.js +4 -0
  10. package/dist/cjs/Brand.js.map +1 -1
  11. package/dist/cjs/Channel.js +44 -4
  12. package/dist/cjs/Channel.js.map +1 -1
  13. package/dist/cjs/Chunk.js +8 -0
  14. package/dist/cjs/Chunk.js.map +1 -1
  15. package/dist/cjs/Config.js +10 -1
  16. package/dist/cjs/Config.js.map +1 -1
  17. package/dist/cjs/Context.js +50 -1
  18. package/dist/cjs/Context.js.map +1 -1
  19. package/dist/cjs/Cron.js +81 -67
  20. package/dist/cjs/Cron.js.map +1 -1
  21. package/dist/cjs/Data.js +14 -0
  22. package/dist/cjs/Data.js.map +1 -1
  23. package/dist/cjs/DateTime.js +178 -664
  24. package/dist/cjs/DateTime.js.map +1 -1
  25. package/dist/cjs/Duration.js +2 -0
  26. package/dist/cjs/Duration.js.map +1 -1
  27. package/dist/cjs/Effect.js +296 -4
  28. package/dist/cjs/Effect.js.map +1 -1
  29. package/dist/cjs/Either.js +38 -2
  30. package/dist/cjs/Either.js.map +1 -1
  31. package/dist/cjs/FiberHandle.js +6 -0
  32. package/dist/cjs/FiberHandle.js.map +1 -1
  33. package/dist/cjs/FiberMap.js +6 -0
  34. package/dist/cjs/FiberMap.js.map +1 -1
  35. package/dist/cjs/FiberSet.js +6 -0
  36. package/dist/cjs/FiberSet.js.map +1 -1
  37. package/dist/cjs/Function.js +34 -0
  38. package/dist/cjs/Function.js.map +1 -1
  39. package/dist/cjs/GlobalValue.js +2 -0
  40. package/dist/cjs/GlobalValue.js.map +1 -1
  41. package/dist/cjs/HashMap.js.map +1 -1
  42. package/dist/cjs/Inspectable.js +8 -4
  43. package/dist/cjs/Inspectable.js.map +1 -1
  44. package/dist/cjs/Iterable.js +18 -0
  45. package/dist/cjs/Iterable.js.map +1 -1
  46. package/dist/cjs/JSONSchema.js.map +1 -1
  47. package/dist/cjs/List.js +4 -0
  48. package/dist/cjs/List.js.map +1 -1
  49. package/dist/cjs/Logger.js +26 -0
  50. package/dist/cjs/Logger.js.map +1 -1
  51. package/dist/cjs/Mailbox.js +2 -0
  52. package/dist/cjs/Mailbox.js.map +1 -1
  53. package/dist/cjs/ManagedRuntime.js +2 -0
  54. package/dist/cjs/ManagedRuntime.js.map +1 -1
  55. package/dist/cjs/Metric.js +10 -0
  56. package/dist/cjs/Metric.js.map +1 -1
  57. package/dist/cjs/Micro.js +1104 -1069
  58. package/dist/cjs/Micro.js.map +1 -1
  59. package/dist/cjs/Number.js +44 -0
  60. package/dist/cjs/Number.js.map +1 -1
  61. package/dist/cjs/Option.js +70 -0
  62. package/dist/cjs/Option.js.map +1 -1
  63. package/dist/cjs/Order.js +2 -0
  64. package/dist/cjs/Order.js.map +1 -1
  65. package/dist/cjs/Ordering.js +4 -0
  66. package/dist/cjs/Ordering.js.map +1 -1
  67. package/dist/cjs/Predicate.js +68 -0
  68. package/dist/cjs/Predicate.js.map +1 -1
  69. package/dist/cjs/Random.js +4 -0
  70. package/dist/cjs/Random.js.map +1 -1
  71. package/dist/cjs/RateLimiter.js +4 -0
  72. package/dist/cjs/RateLimiter.js.map +1 -1
  73. package/dist/cjs/RcMap.js +2 -0
  74. package/dist/cjs/RcMap.js.map +1 -1
  75. package/dist/cjs/RcRef.js +2 -0
  76. package/dist/cjs/RcRef.js.map +1 -1
  77. package/dist/cjs/Record.js +56 -0
  78. package/dist/cjs/Record.js.map +1 -1
  79. package/dist/cjs/Redacted.js +8 -0
  80. package/dist/cjs/Redacted.js.map +1 -1
  81. package/dist/cjs/RegExp.js +4 -0
  82. package/dist/cjs/RegExp.js.map +1 -1
  83. package/dist/cjs/Request.js +4 -0
  84. package/dist/cjs/Request.js.map +1 -1
  85. package/dist/cjs/RequestResolver.js +2 -0
  86. package/dist/cjs/RequestResolver.js.map +1 -1
  87. package/dist/cjs/Runtime.js +6 -0
  88. package/dist/cjs/Runtime.js.map +1 -1
  89. package/dist/cjs/STM.js.map +1 -1
  90. package/dist/cjs/Schema.js +91 -8
  91. package/dist/cjs/Schema.js.map +1 -1
  92. package/dist/cjs/Sink.js +9 -1
  93. package/dist/cjs/Sink.js.map +1 -1
  94. package/dist/cjs/Stream.js +179 -7
  95. package/dist/cjs/Stream.js.map +1 -1
  96. package/dist/cjs/String.js +62 -0
  97. package/dist/cjs/String.js.map +1 -1
  98. package/dist/cjs/Struct.js +12 -0
  99. package/dist/cjs/Struct.js.map +1 -1
  100. package/dist/cjs/Symbol.js +2 -0
  101. package/dist/cjs/Symbol.js.map +1 -1
  102. package/dist/cjs/Trie.js +56 -0
  103. package/dist/cjs/Trie.js.map +1 -1
  104. package/dist/cjs/Tuple.js +18 -0
  105. package/dist/cjs/Tuple.js.map +1 -1
  106. package/dist/cjs/Utils.js +7 -1
  107. package/dist/cjs/Utils.js.map +1 -1
  108. package/dist/cjs/internal/channel/channelExecutor.js +5 -9
  109. package/dist/cjs/internal/channel/channelExecutor.js.map +1 -1
  110. package/dist/cjs/internal/channel.js +156 -130
  111. package/dist/cjs/internal/channel.js.map +1 -1
  112. package/dist/cjs/internal/config.js +13 -4
  113. package/dist/cjs/internal/config.js.map +1 -1
  114. package/dist/cjs/internal/context.js +46 -3
  115. package/dist/cjs/internal/context.js.map +1 -1
  116. package/dist/cjs/internal/dateTime.js +747 -0
  117. package/dist/cjs/internal/dateTime.js.map +1 -0
  118. package/dist/cjs/internal/fiberRuntime.js +34 -11
  119. package/dist/cjs/internal/fiberRuntime.js.map +1 -1
  120. package/dist/cjs/internal/groupBy.js +9 -3
  121. package/dist/cjs/internal/groupBy.js.map +1 -1
  122. package/dist/cjs/internal/layer.js +1 -1
  123. package/dist/cjs/internal/layer.js.map +1 -1
  124. package/dist/cjs/internal/mailbox.js +1 -1
  125. package/dist/cjs/internal/mailbox.js.map +1 -1
  126. package/dist/cjs/internal/sink.js +25 -21
  127. package/dist/cjs/internal/sink.js.map +1 -1
  128. package/dist/cjs/internal/stream.js +70 -71
  129. package/dist/cjs/internal/stream.js.map +1 -1
  130. package/dist/cjs/internal/version.js +1 -1
  131. package/dist/cjs/internal/version.js.map +1 -1
  132. package/dist/dts/Array.d.ts +534 -0
  133. package/dist/dts/Array.d.ts.map +1 -1
  134. package/dist/dts/BigDecimal.d.ts +172 -1
  135. package/dist/dts/BigDecimal.d.ts.map +1 -1
  136. package/dist/dts/BigInt.d.ts +114 -0
  137. package/dist/dts/BigInt.d.ts.map +1 -1
  138. package/dist/dts/Boolean.d.ts +56 -0
  139. package/dist/dts/Boolean.d.ts.map +1 -1
  140. package/dist/dts/Brand.d.ts +6 -0
  141. package/dist/dts/Brand.d.ts.map +1 -1
  142. package/dist/dts/Channel.d.ts +66 -5
  143. package/dist/dts/Channel.d.ts.map +1 -1
  144. package/dist/dts/Chunk.d.ts +30 -0
  145. package/dist/dts/Chunk.d.ts.map +1 -1
  146. package/dist/dts/Config.d.ts +25 -1
  147. package/dist/dts/Config.d.ts.map +1 -1
  148. package/dist/dts/Context.d.ts +155 -0
  149. package/dist/dts/Context.d.ts.map +1 -1
  150. package/dist/dts/Cron.d.ts +21 -6
  151. package/dist/dts/Cron.d.ts.map +1 -1
  152. package/dist/dts/Data.d.ts +26 -0
  153. package/dist/dts/Data.d.ts.map +1 -1
  154. package/dist/dts/DateTime.d.ts +192 -49
  155. package/dist/dts/DateTime.d.ts.map +1 -1
  156. package/dist/dts/Duration.d.ts +2 -0
  157. package/dist/dts/Duration.d.ts.map +1 -1
  158. package/dist/dts/Effect.d.ts +658 -1
  159. package/dist/dts/Effect.d.ts.map +1 -1
  160. package/dist/dts/Either.d.ts +84 -2
  161. package/dist/dts/Either.d.ts.map +1 -1
  162. package/dist/dts/FiberHandle.d.ts +6 -0
  163. package/dist/dts/FiberHandle.d.ts.map +1 -1
  164. package/dist/dts/FiberMap.d.ts +6 -0
  165. package/dist/dts/FiberMap.d.ts.map +1 -1
  166. package/dist/dts/FiberSet.d.ts +6 -0
  167. package/dist/dts/FiberSet.d.ts.map +1 -1
  168. package/dist/dts/Function.d.ts +50 -0
  169. package/dist/dts/Function.d.ts.map +1 -1
  170. package/dist/dts/GlobalValue.d.ts +2 -0
  171. package/dist/dts/GlobalValue.d.ts.map +1 -1
  172. package/dist/dts/HashMap.d.ts +6 -0
  173. package/dist/dts/HashMap.d.ts.map +1 -1
  174. package/dist/dts/Inspectable.d.ts.map +1 -1
  175. package/dist/dts/Iterable.d.ts +26 -0
  176. package/dist/dts/Iterable.d.ts.map +1 -1
  177. package/dist/dts/JSONSchema.d.ts +1 -0
  178. package/dist/dts/JSONSchema.d.ts.map +1 -1
  179. package/dist/dts/List.d.ts +20 -0
  180. package/dist/dts/List.d.ts.map +1 -1
  181. package/dist/dts/Logger.d.ts +34 -0
  182. package/dist/dts/Logger.d.ts.map +1 -1
  183. package/dist/dts/Mailbox.d.ts +2 -0
  184. package/dist/dts/Mailbox.d.ts.map +1 -1
  185. package/dist/dts/ManagedRuntime.d.ts +2 -0
  186. package/dist/dts/ManagedRuntime.d.ts.map +1 -1
  187. package/dist/dts/Metric.d.ts +18 -0
  188. package/dist/dts/Metric.d.ts.map +1 -1
  189. package/dist/dts/Micro.d.ts +880 -863
  190. package/dist/dts/Micro.d.ts.map +1 -1
  191. package/dist/dts/Number.d.ts +104 -0
  192. package/dist/dts/Number.d.ts.map +1 -1
  193. package/dist/dts/Option.d.ts +142 -0
  194. package/dist/dts/Option.d.ts.map +1 -1
  195. package/dist/dts/Order.d.ts +2 -0
  196. package/dist/dts/Order.d.ts.map +1 -1
  197. package/dist/dts/Ordering.d.ts +8 -0
  198. package/dist/dts/Ordering.d.ts.map +1 -1
  199. package/dist/dts/Predicate.d.ts +104 -0
  200. package/dist/dts/Predicate.d.ts.map +1 -1
  201. package/dist/dts/Random.d.ts +4 -0
  202. package/dist/dts/Random.d.ts.map +1 -1
  203. package/dist/dts/RateLimiter.d.ts +4 -0
  204. package/dist/dts/RateLimiter.d.ts.map +1 -1
  205. package/dist/dts/RcMap.d.ts +6 -0
  206. package/dist/dts/RcMap.d.ts.map +1 -1
  207. package/dist/dts/RcRef.d.ts +2 -0
  208. package/dist/dts/RcRef.d.ts.map +1 -1
  209. package/dist/dts/Record.d.ts +136 -0
  210. package/dist/dts/Record.d.ts.map +1 -1
  211. package/dist/dts/Redacted.d.ts +8 -0
  212. package/dist/dts/Redacted.d.ts.map +1 -1
  213. package/dist/dts/RegExp.d.ts +4 -0
  214. package/dist/dts/RegExp.d.ts.map +1 -1
  215. package/dist/dts/Request.d.ts +4 -0
  216. package/dist/dts/Request.d.ts.map +1 -1
  217. package/dist/dts/RequestResolver.d.ts +6 -0
  218. package/dist/dts/RequestResolver.d.ts.map +1 -1
  219. package/dist/dts/Runtime.d.ts +18 -0
  220. package/dist/dts/Runtime.d.ts.map +1 -1
  221. package/dist/dts/STM.d.ts +2 -0
  222. package/dist/dts/STM.d.ts.map +1 -1
  223. package/dist/dts/Schema.d.ts +90 -0
  224. package/dist/dts/Schema.d.ts.map +1 -1
  225. package/dist/dts/Sink.d.ts +8 -0
  226. package/dist/dts/Sink.d.ts.map +1 -1
  227. package/dist/dts/Stream.d.ts +394 -32
  228. package/dist/dts/Stream.d.ts.map +1 -1
  229. package/dist/dts/String.d.ts +94 -0
  230. package/dist/dts/String.d.ts.map +1 -1
  231. package/dist/dts/Struct.d.ts +24 -0
  232. package/dist/dts/Struct.d.ts.map +1 -1
  233. package/dist/dts/Symbol.d.ts +2 -0
  234. package/dist/dts/Symbol.d.ts.map +1 -1
  235. package/dist/dts/Trie.d.ts +132 -0
  236. package/dist/dts/Trie.d.ts.map +1 -1
  237. package/dist/dts/Tuple.d.ts +42 -0
  238. package/dist/dts/Tuple.d.ts.map +1 -1
  239. package/dist/dts/Types.d.ts +24 -0
  240. package/dist/dts/Types.d.ts.map +1 -1
  241. package/dist/dts/Utils.d.ts +4 -0
  242. package/dist/dts/Utils.d.ts.map +1 -1
  243. package/dist/dts/internal/context.d.ts +1 -1
  244. package/dist/dts/internal/context.d.ts.map +1 -1
  245. package/dist/dts/internal/dateTime.d.ts +2 -0
  246. package/dist/dts/internal/dateTime.d.ts.map +1 -0
  247. package/dist/dts/internal/fiberRuntime.d.ts.map +1 -1
  248. package/dist/dts/internal/stream.d.ts.map +1 -1
  249. package/dist/esm/Array.js +208 -0
  250. package/dist/esm/Array.js.map +1 -1
  251. package/dist/esm/BigDecimal.js +175 -20
  252. package/dist/esm/BigDecimal.js.map +1 -1
  253. package/dist/esm/BigInt.js +54 -0
  254. package/dist/esm/BigInt.js.map +1 -1
  255. package/dist/esm/Boolean.js +24 -0
  256. package/dist/esm/Boolean.js.map +1 -1
  257. package/dist/esm/Brand.js +4 -0
  258. package/dist/esm/Brand.js.map +1 -1
  259. package/dist/esm/Channel.js +42 -2
  260. package/dist/esm/Channel.js.map +1 -1
  261. package/dist/esm/Chunk.js +8 -0
  262. package/dist/esm/Chunk.js.map +1 -1
  263. package/dist/esm/Config.js +9 -0
  264. package/dist/esm/Config.js.map +1 -1
  265. package/dist/esm/Context.js +49 -0
  266. package/dist/esm/Context.js.map +1 -1
  267. package/dist/esm/Cron.js +81 -67
  268. package/dist/esm/Cron.js.map +1 -1
  269. package/dist/esm/Data.js +16 -0
  270. package/dist/esm/Data.js.map +1 -1
  271. package/dist/esm/DateTime.js +176 -627
  272. package/dist/esm/DateTime.js.map +1 -1
  273. package/dist/esm/Duration.js +2 -0
  274. package/dist/esm/Duration.js.map +1 -1
  275. package/dist/esm/Effect.js +297 -0
  276. package/dist/esm/Effect.js.map +1 -1
  277. package/dist/esm/Either.js +40 -2
  278. package/dist/esm/Either.js.map +1 -1
  279. package/dist/esm/FiberHandle.js +6 -0
  280. package/dist/esm/FiberHandle.js.map +1 -1
  281. package/dist/esm/FiberMap.js +6 -0
  282. package/dist/esm/FiberMap.js.map +1 -1
  283. package/dist/esm/FiberSet.js +6 -0
  284. package/dist/esm/FiberSet.js.map +1 -1
  285. package/dist/esm/Function.js +34 -0
  286. package/dist/esm/Function.js.map +1 -1
  287. package/dist/esm/GlobalValue.js +2 -0
  288. package/dist/esm/GlobalValue.js.map +1 -1
  289. package/dist/esm/HashMap.js.map +1 -1
  290. package/dist/esm/Inspectable.js +8 -4
  291. package/dist/esm/Inspectable.js.map +1 -1
  292. package/dist/esm/Iterable.js +18 -0
  293. package/dist/esm/Iterable.js.map +1 -1
  294. package/dist/esm/JSONSchema.js.map +1 -1
  295. package/dist/esm/List.js +4 -0
  296. package/dist/esm/List.js.map +1 -1
  297. package/dist/esm/Logger.js +26 -0
  298. package/dist/esm/Logger.js.map +1 -1
  299. package/dist/esm/Mailbox.js +2 -0
  300. package/dist/esm/Mailbox.js.map +1 -1
  301. package/dist/esm/ManagedRuntime.js +2 -0
  302. package/dist/esm/ManagedRuntime.js.map +1 -1
  303. package/dist/esm/Metric.js +10 -0
  304. package/dist/esm/Metric.js.map +1 -1
  305. package/dist/esm/Micro.js +1077 -1037
  306. package/dist/esm/Micro.js.map +1 -1
  307. package/dist/esm/Number.js +44 -0
  308. package/dist/esm/Number.js.map +1 -1
  309. package/dist/esm/Option.js +72 -0
  310. package/dist/esm/Option.js.map +1 -1
  311. package/dist/esm/Order.js +2 -0
  312. package/dist/esm/Order.js.map +1 -1
  313. package/dist/esm/Ordering.js +4 -0
  314. package/dist/esm/Ordering.js.map +1 -1
  315. package/dist/esm/Predicate.js +68 -0
  316. package/dist/esm/Predicate.js.map +1 -1
  317. package/dist/esm/Random.js +4 -0
  318. package/dist/esm/Random.js.map +1 -1
  319. package/dist/esm/RateLimiter.js +4 -0
  320. package/dist/esm/RateLimiter.js.map +1 -1
  321. package/dist/esm/RcMap.js +2 -0
  322. package/dist/esm/RcMap.js.map +1 -1
  323. package/dist/esm/RcRef.js +2 -0
  324. package/dist/esm/RcRef.js.map +1 -1
  325. package/dist/esm/Record.js +56 -0
  326. package/dist/esm/Record.js.map +1 -1
  327. package/dist/esm/Redacted.js +8 -0
  328. package/dist/esm/Redacted.js.map +1 -1
  329. package/dist/esm/RegExp.js +4 -0
  330. package/dist/esm/RegExp.js.map +1 -1
  331. package/dist/esm/Request.js +4 -0
  332. package/dist/esm/Request.js.map +1 -1
  333. package/dist/esm/RequestResolver.js +2 -0
  334. package/dist/esm/RequestResolver.js.map +1 -1
  335. package/dist/esm/Runtime.js +6 -0
  336. package/dist/esm/Runtime.js.map +1 -1
  337. package/dist/esm/STM.js.map +1 -1
  338. package/dist/esm/Schema.js +88 -0
  339. package/dist/esm/Schema.js.map +1 -1
  340. package/dist/esm/Sink.js +8 -0
  341. package/dist/esm/Sink.js.map +1 -1
  342. package/dist/esm/Stream.js +183 -5
  343. package/dist/esm/Stream.js.map +1 -1
  344. package/dist/esm/String.js +62 -0
  345. package/dist/esm/String.js.map +1 -1
  346. package/dist/esm/Struct.js +12 -0
  347. package/dist/esm/Struct.js.map +1 -1
  348. package/dist/esm/Symbol.js +2 -0
  349. package/dist/esm/Symbol.js.map +1 -1
  350. package/dist/esm/Trie.js +56 -0
  351. package/dist/esm/Trie.js.map +1 -1
  352. package/dist/esm/Tuple.js +22 -0
  353. package/dist/esm/Tuple.js.map +1 -1
  354. package/dist/esm/Utils.js +5 -0
  355. package/dist/esm/Utils.js.map +1 -1
  356. package/dist/esm/internal/channel/channelExecutor.js +5 -7
  357. package/dist/esm/internal/channel/channelExecutor.js.map +1 -1
  358. package/dist/esm/internal/channel.js +152 -129
  359. package/dist/esm/internal/channel.js.map +1 -1
  360. package/dist/esm/internal/config.js +11 -3
  361. package/dist/esm/internal/config.js.map +1 -1
  362. package/dist/esm/internal/context.js +42 -2
  363. package/dist/esm/internal/context.js.map +1 -1
  364. package/dist/esm/internal/dateTime.js +704 -0
  365. package/dist/esm/internal/dateTime.js.map +1 -0
  366. package/dist/esm/internal/fiberRuntime.js +31 -9
  367. package/dist/esm/internal/fiberRuntime.js.map +1 -1
  368. package/dist/esm/internal/groupBy.js +9 -3
  369. package/dist/esm/internal/groupBy.js.map +1 -1
  370. package/dist/esm/internal/layer.js +1 -1
  371. package/dist/esm/internal/layer.js.map +1 -1
  372. package/dist/esm/internal/mailbox.js +1 -1
  373. package/dist/esm/internal/mailbox.js.map +1 -1
  374. package/dist/esm/internal/sink.js +23 -20
  375. package/dist/esm/internal/sink.js.map +1 -1
  376. package/dist/esm/internal/stream.js +66 -69
  377. package/dist/esm/internal/stream.js.map +1 -1
  378. package/dist/esm/internal/version.js +1 -1
  379. package/dist/esm/internal/version.js.map +1 -1
  380. package/package.json +1 -1
  381. package/src/Array.ts +534 -0
  382. package/src/BigDecimal.ts +247 -21
  383. package/src/BigInt.ts +114 -0
  384. package/src/Boolean.ts +56 -0
  385. package/src/Brand.ts +6 -0
  386. package/src/Channel.ts +81 -5
  387. package/src/Chunk.ts +32 -0
  388. package/src/Config.ts +26 -1
  389. package/src/Context.ts +163 -0
  390. package/src/Cron.ts +91 -68
  391. package/src/Data.ts +26 -0
  392. package/src/DateTime.ts +307 -757
  393. package/src/Duration.ts +2 -0
  394. package/src/Effect.ts +910 -1
  395. package/src/Either.ts +84 -2
  396. package/src/FiberHandle.ts +6 -0
  397. package/src/FiberMap.ts +6 -0
  398. package/src/FiberSet.ts +6 -0
  399. package/src/Function.ts +50 -0
  400. package/src/GlobalValue.ts +2 -0
  401. package/src/HashMap.ts +6 -0
  402. package/src/Inspectable.ts +11 -7
  403. package/src/Iterable.ts +26 -0
  404. package/src/JSONSchema.ts +1 -0
  405. package/src/List.ts +24 -0
  406. package/src/Logger.ts +34 -0
  407. package/src/Mailbox.ts +2 -0
  408. package/src/ManagedRuntime.ts +2 -0
  409. package/src/Metric.ts +18 -0
  410. package/src/Micro.ts +2007 -1745
  411. package/src/Number.ts +104 -0
  412. package/src/Option.ts +142 -0
  413. package/src/Order.ts +2 -0
  414. package/src/Ordering.ts +8 -0
  415. package/src/Predicate.ts +104 -0
  416. package/src/Random.ts +4 -0
  417. package/src/RateLimiter.ts +4 -0
  418. package/src/RcMap.ts +6 -0
  419. package/src/RcRef.ts +2 -0
  420. package/src/Record.ts +136 -0
  421. package/src/Redacted.ts +8 -0
  422. package/src/RegExp.ts +4 -0
  423. package/src/Request.ts +4 -0
  424. package/src/RequestResolver.ts +6 -0
  425. package/src/Runtime.ts +18 -0
  426. package/src/STM.ts +2 -0
  427. package/src/Schema.ts +124 -0
  428. package/src/Sink.ts +11 -0
  429. package/src/Stream.ts +399 -44
  430. package/src/String.ts +94 -0
  431. package/src/Struct.ts +24 -0
  432. package/src/Symbol.ts +2 -0
  433. package/src/Trie.ts +132 -0
  434. package/src/Tuple.ts +42 -0
  435. package/src/Types.ts +24 -0
  436. package/src/Utils.ts +8 -0
  437. package/src/internal/channel/channelExecutor.ts +37 -33
  438. package/src/internal/channel.ts +504 -467
  439. package/src/internal/config.ts +18 -6
  440. package/src/internal/context.ts +56 -4
  441. package/src/internal/dateTime.ts +1126 -0
  442. package/src/internal/fiberRuntime.ts +35 -16
  443. package/src/internal/groupBy.ts +13 -22
  444. package/src/internal/layer.ts +5 -8
  445. package/src/internal/mailbox.ts +6 -4
  446. package/src/internal/sink.ts +55 -35
  447. package/src/internal/stream.ts +299 -299
  448. package/src/internal/version.ts +1 -1
package/dist/cjs/Micro.js CHANGED
@@ -3,23 +3,33 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.locally = exports.let = exports.isMicroCause = exports.isMicro = exports.isHandle = exports.interruptible = exports.interrupt = exports.ignoreLogged = exports.ignore = exports.getEnvRef = exports.gen = exports.fromOption = exports.fromExitSync = exports.fromExit = exports.fromEither = exports.forkScoped = exports.forkIn = exports.forkDaemon = exports.fork = exports.forever = exports.forEach = exports.flip = exports.flatten = exports.flatMap = exports.filterOrFailCause = exports.filterOrFail = exports.filterMap = exports.filter = exports.failSync = exports.failCauseSync = exports.failCause = exports.fail = exports.exitVoid = exports.exitSucceed = exports.exitIsSuccess = exports.exitIsInterrupt = exports.exitIsFailure = exports.exitIsFail = exports.exitIsDie = exports.exitInterrupt = exports.exitFailCause = exports.exitFail = exports.exitDie = exports.exit = exports.envUnsafeMakeEmpty = exports.envSet = exports.envRefMake = exports.envMutate = exports.envMake = exports.envGet = exports.ensuring = exports.either = exports.die = exports.delay = exports.currentScheduler = exports.currentMaxDepthBeforeYield = exports.currentContext = exports.currentConcurrency = exports.currentAbortSignal = exports.currentAbortController = exports.context = exports.causeWithTrace = exports.causeSquash = exports.causeIsInterrupt = exports.causeIsFail = exports.causeIsDie = exports.causeInterrupt = exports.causeFail = exports.causeDie = exports.catchTag = exports.catchIf = exports.catchCauseIf = exports.catchAllDefect = exports.catchAllCause = exports.catchAll = exports.bindTo = exports.bind = exports.async = exports.asVoid = exports.asSome = exports.as = exports.andThen = exports.all = exports.addFinalizer = exports.acquireUseRelease = exports.acquireRelease = exports.TypeId = exports.TimeoutException = exports.TaggedError = exports.NoSuchElementException = exports.MicroScopeTypeId = exports.MicroScope = exports.MicroSchedulerDefault = exports.MicroCauseTypeId = exports.HandleTypeId = exports.Error = exports.EnvTypeId = exports.EnvRefTypeId = exports.Do = exports.Class = void 0;
7
- exports.zipWith = exports.zip = exports.yieldWithPriority = exports.yieldNow = exports.yieldFlush = exports.withTrace = exports.withConcurrency = exports.when = exports.void = exports.uninterruptibleMask = exports.uninterruptible = exports.tryPromise = exports.try = exports.timeoutOrElse = exports.timeoutOption = exports.timeout = exports.tapErrorCauseIf = exports.tapErrorCause = exports.tapError = exports.tapDefect = exports.tap = exports.sync = exports.suspend = exports.succeedSome = exports.succeedNone = exports.succeed = exports.sleep = exports.serviceOption = exports.service = exports.scoped = exports.scopeUnsafeMake = exports.scopeMake = exports.scope = exports.scheduleWithMaxElapsed = exports.scheduleWithMaxDelay = exports.scheduleUnion = exports.scheduleSpaced = exports.scheduleRecurs = exports.scheduleIntersect = exports.scheduleExponential = exports.scheduleAddDelay = exports.sandbox = exports.runSyncExit = exports.runSync = exports.runSymbol = exports.runPromiseExit = exports.runPromise = exports.runFork = exports.retry = exports.repeatExit = exports.repeat = exports.raceFirst = exports.raceAllFirst = exports.raceAll = exports.race = exports.provideServiceEffect = exports.provideService = exports.provideScope = exports.provideContext = exports.promise = exports.orElseSucceed = exports.orDie = exports.option = exports.onInterrupt = exports.onExitIf = exports.onExit = exports.onError = exports.never = exports.matchEffect = exports.matchCauseEffect = exports.matchCause = exports.match = exports.mapErrorCause = exports.mapError = exports.map = exports.make = void 0;
6
+ exports.onExitIf = exports.onExit = exports.onError = exports.never = exports.matchEffect = exports.matchCauseEffect = exports.matchCause = exports.match = exports.mapErrorCause = exports.mapError = exports.map = exports.let = exports.isMicroExit = exports.isMicroCause = exports.isMicro = exports.interruptible = exports.interrupt = exports.ignoreLogged = exports.ignore = exports.gen = exports.fromOption = exports.fromEither = exports.forkScoped = exports.forkIn = exports.forkDaemon = exports.fork = exports.forever = exports.forEach = exports.flip = exports.flatten = exports.flatMap = exports.filterOrFailCause = exports.filterOrFail = exports.filterMap = exports.filter = exports.fiberInterruptAll = exports.fiberInterrupt = exports.fiberAwait = exports.failSync = exports.failCauseSync = exports.failCause = exports.fail = exports.exitVoidAll = exports.exitVoid = exports.exitSucceed = exports.exitIsSuccess = exports.exitIsInterrupt = exports.exitIsFailure = exports.exitIsFail = exports.exitIsDie = exports.exitInterrupt = exports.exitFailCause = exports.exitFail = exports.exitDie = exports.exit = exports.ensuring = exports.either = exports.die = exports.delay = exports.context = exports.causeWithTrace = exports.causeSquash = exports.causeIsInterrupt = exports.causeIsFail = exports.causeIsDie = exports.causeInterrupt = exports.causeFail = exports.causeDie = exports.catchTag = exports.catchIf = exports.catchCauseIf = exports.catchAllDefect = exports.catchAllCause = exports.catchAll = exports.bindTo = exports.bind = exports.async = exports.asVoid = exports.asSome = exports.as = exports.andThen = exports.all = exports.addFinalizer = exports.acquireUseRelease = exports.acquireRelease = exports.TypeId = exports.TimeoutException = exports.TaggedError = exports.NoSuchElementException = exports.MicroScopeTypeId = exports.MicroScope = exports.MicroSchedulerDefault = exports.MicroExitTypeId = exports.MicroCauseTypeId = exports.MaxOpsBeforeYield = exports.FiberTypeId = exports.Error = exports.Do = exports.CurrentScheduler = exports.CurrentConcurrency = void 0;
7
+ exports.zipWith = exports.zip = exports.yieldNowWith = exports.yieldNow = exports.yieldFlush = exports.withTrace = exports.withFiber = exports.withConcurrency = exports.whileLoop = exports.when = exports.void = exports.updateService = exports.updateContext = exports.uninterruptibleMask = exports.uninterruptible = exports.tryPromise = exports.try = exports.timeoutOrElse = exports.timeoutOption = exports.timeout = exports.tapErrorCauseIf = exports.tapErrorCause = exports.tapError = exports.tapDefect = exports.tap = exports.sync = exports.suspend = exports.succeedSome = exports.succeedNone = exports.succeed = exports.sleep = exports.serviceOption = exports.service = exports.scoped = exports.scopeUnsafeMake = exports.scopeMake = exports.scope = exports.scheduleWithMaxElapsed = exports.scheduleWithMaxDelay = exports.scheduleUnion = exports.scheduleSpaced = exports.scheduleRecurs = exports.scheduleIntersect = exports.scheduleExponential = exports.scheduleAddDelay = exports.sandbox = exports.runSyncExit = exports.runSync = exports.runPromiseExit = exports.runPromise = exports.runFork = exports.retry = exports.replicateEffect = exports.replicate = exports.repeatExit = exports.repeat = exports.raceFirst = exports.raceAllFirst = exports.raceAll = exports.race = exports.provideServiceEffect = exports.provideService = exports.provideScope = exports.provideContext = exports.promise = exports.orElseSucceed = exports.orDie = exports.option = exports.onInterrupt = void 0;
8
+ var Arr = _interopRequireWildcard(require("effect/Array"));
8
9
  var Context = _interopRequireWildcard(require("./Context.js"));
9
10
  var Effectable = _interopRequireWildcard(require("./Effectable.js"));
10
11
  var Either = _interopRequireWildcard(require("./Either.js"));
12
+ var Equal = _interopRequireWildcard(require("./Equal.js"));
11
13
  var _Function = require("./Function.js");
12
14
  var _GlobalValue = require("./GlobalValue.js");
15
+ var Hash = _interopRequireWildcard(require("./Hash.js"));
13
16
  var _Inspectable = require("./Inspectable.js");
17
+ var InternalContext = _interopRequireWildcard(require("./internal/context.js"));
14
18
  var doNotation = _interopRequireWildcard(require("./internal/doNotation.js"));
15
19
  var _effectable = require("./internal/effectable.js");
16
- var _singleShotGen = require("./internal/singleShotGen.js");
17
20
  var Option = _interopRequireWildcard(require("./Option.js"));
18
21
  var _Pipeable = require("./Pipeable.js");
19
22
  var _Predicate = require("./Predicate.js");
20
23
  var _Utils = require("./Utils.js");
21
24
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
22
25
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
26
+ /**
27
+ * A lightweight alternative to the `Effect` data type, with a subset of the functionality.
28
+ *
29
+ * @since 3.4.0
30
+ * @experimental
31
+ */
32
+
23
33
  /**
24
34
  * @since 3.4.0
25
35
  * @experimental
@@ -29,9 +39,9 @@ const TypeId = exports.TypeId = /*#__PURE__*/Symbol.for("effect/Micro");
29
39
  /**
30
40
  * @since 3.4.0
31
41
  * @experimental
32
- * @category symbols
42
+ * @category MicroExit
33
43
  */
34
- const runSymbol = exports.runSymbol = /*#__PURE__*/Symbol.for("effect/Micro/runSymbol");
44
+ const MicroExitTypeId = exports.MicroExitTypeId = /*#__PURE__*/Symbol.for("effect/Micro/MicroExit");
35
45
  /**
36
46
  * @since 3.4.0
37
47
  * @experimental
@@ -39,41 +49,6 @@ const runSymbol = exports.runSymbol = /*#__PURE__*/Symbol.for("effect/Micro/runS
39
49
  */
40
50
  const isMicro = u => typeof u === "object" && u !== null && TypeId in u;
41
51
  // ----------------------------------------------------------------------------
42
- // Microable
43
- // ----------------------------------------------------------------------------
44
- exports.isMicro = isMicro;
45
- const MicroProto = {
46
- ...Effectable.EffectPrototype,
47
- _op: "Micro",
48
- [TypeId]: {
49
- _A: _Function.identity,
50
- _E: _Function.identity,
51
- _R: _Function.identity
52
- },
53
- [Symbol.iterator]() {
54
- return new _singleShotGen.SingleShotGen(new _Utils.YieldWrap(this));
55
- }
56
- };
57
- const MicroBase = /*#__PURE__*/function () {
58
- function Base() {}
59
- Base.prototype = MicroProto;
60
- return Base;
61
- }();
62
- /**
63
- * @since 3.8.4
64
- * @experimental
65
- * @category constructors
66
- */
67
- class Class extends MicroBase {
68
- /**
69
- * @since 3.8.4
70
- * @experimental
71
- */
72
- [runSymbol](env, onExit) {
73
- this.asMicro()[runSymbol](env, onExit);
74
- }
75
- }
76
- // ----------------------------------------------------------------------------
77
52
  // MicroCause
78
53
  // ----------------------------------------------------------------------------
79
54
  /**
@@ -81,7 +56,7 @@ class Class extends MicroBase {
81
56
  * @experimental
82
57
  * @category MicroCause
83
58
  */
84
- exports.Class = Class;
59
+ exports.isMicro = isMicro;
85
60
  const MicroCauseTypeId = exports.MicroCauseTypeId = /*#__PURE__*/Symbol.for("effect/Micro/MicroCause");
86
61
  /**
87
62
  * @since 3.6.6
@@ -216,985 +191,1113 @@ const causeWithTrace = exports.causeWithTrace = /*#__PURE__*/(0, _Function.dual)
216
191
  return causeFail(self.error, traces);
217
192
  }
218
193
  });
194
+ // ----------------------------------------------------------------------------
195
+ // Fiber
196
+ // ----------------------------------------------------------------------------
219
197
  /**
220
- * @since 3.4.6
221
- * @experimental
222
- * @category MicroExit
223
- */
224
- const exitInterrupt = exports.exitInterrupt = /*#__PURE__*/Either.left( /*#__PURE__*/causeInterrupt());
225
- /**
226
- * @since 3.4.6
227
- * @experimental
228
- * @category MicroExit
229
- */
230
- const exitSucceed = exports.exitSucceed = Either.right;
231
- /**
232
- * @since 3.4.6
233
- * @experimental
234
- * @category MicroExit
235
- */
236
- const exitFail = e => Either.left(causeFail(e));
237
- /**
238
- * @since 3.4.6
239
- * @experimental
240
- * @category MicroExit
241
- */
242
- exports.exitFail = exitFail;
243
- const exitDie = defect => Either.left(causeDie(defect));
244
- /**
245
- * @since 3.4.6
246
- * @experimental
247
- * @category MicroExit
248
- */
249
- exports.exitDie = exitDie;
250
- const exitFailCause = exports.exitFailCause = Either.left;
251
- /**
252
- * @since 3.4.6
253
- * @experimental
254
- * @category MicroExit
255
- */
256
- const exitIsSuccess = exports.exitIsSuccess = Either.isRight;
257
- /**
258
- * @since 3.4.6
259
- * @experimental
260
- * @category MicroExit
261
- */
262
- const exitIsFailure = exports.exitIsFailure = Either.isLeft;
263
- /**
264
- * @since 3.4.6
265
- * @experimental
266
- * @category MicroExit
267
- */
268
- const exitIsInterrupt = self => exitIsFailure(self) && self.left._tag === "Interrupt";
269
- /**
270
- * @since 3.4.6
198
+ * @since 3.11.0
271
199
  * @experimental
272
- * @category MicroExit
200
+ * @category Fiber
273
201
  */
274
- exports.exitIsInterrupt = exitIsInterrupt;
275
- const exitIsFail = self => exitIsFailure(self) && self.left._tag === "Fail";
202
+ const FiberTypeId = exports.FiberTypeId = /*#__PURE__*/Symbol.for("effect/Micro/Fiber");
203
+ const fiberVariance = {
204
+ _A: _Function.identity,
205
+ _E: _Function.identity
206
+ };
207
+ class FiberImpl {
208
+ context;
209
+ interruptible;
210
+ [FiberTypeId];
211
+ _stack = [];
212
+ _observers = [];
213
+ _exit;
214
+ _children;
215
+ currentOpCount = 0;
216
+ constructor(context, interruptible = true) {
217
+ this.context = context;
218
+ this.interruptible = interruptible;
219
+ this[FiberTypeId] = fiberVariance;
220
+ }
221
+ getRef(ref) {
222
+ return InternalContext.unsafeGetReference(this.context, ref);
223
+ }
224
+ addObserver(cb) {
225
+ if (this._exit) {
226
+ cb(this._exit);
227
+ return _Function.constVoid;
228
+ }
229
+ this._observers.push(cb);
230
+ return () => {
231
+ const index = this._observers.indexOf(cb);
232
+ if (index >= 0) {
233
+ this._observers.splice(index, 1);
234
+ }
235
+ };
236
+ }
237
+ _interrupted = false;
238
+ unsafeInterrupt() {
239
+ if (this._exit) {
240
+ return;
241
+ }
242
+ this._interrupted = true;
243
+ if (this.interruptible) {
244
+ this.evaluate(exitInterrupt);
245
+ }
246
+ }
247
+ unsafePoll() {
248
+ return this._exit;
249
+ }
250
+ evaluate(effect) {
251
+ if (this._exit) {
252
+ return;
253
+ } else if (this._yielded !== undefined) {
254
+ const yielded = this._yielded;
255
+ this._yielded = undefined;
256
+ yielded();
257
+ }
258
+ const exit = this.runLoop(effect);
259
+ if (exit === Yield) {
260
+ return;
261
+ }
262
+ // the interruptChildren middlware is added in Micro.fork, so it can be
263
+ // tree-shaken if not used
264
+ const interruptChildren = fiberMiddleware.interruptChildren && fiberMiddleware.interruptChildren(this);
265
+ if (interruptChildren !== undefined) {
266
+ return this.evaluate(flatMap(interruptChildren, () => exit));
267
+ }
268
+ this._exit = exit;
269
+ for (let i = 0; i < this._observers.length; i++) {
270
+ this._observers[i](exit);
271
+ }
272
+ this._observers.length = 0;
273
+ }
274
+ runLoop(effect) {
275
+ let yielding = false;
276
+ let current = effect;
277
+ this.currentOpCount = 0;
278
+ try {
279
+ while (true) {
280
+ this.currentOpCount++;
281
+ if (!yielding && this.getRef(CurrentScheduler).shouldYield(this)) {
282
+ yielding = true;
283
+ const prev = current;
284
+ current = flatMap(yieldNow, () => prev);
285
+ }
286
+ current = current[evaluate](this);
287
+ if (current === Yield) {
288
+ const yielded = this._yielded;
289
+ if (MicroExitTypeId in yielded) {
290
+ this._yielded = undefined;
291
+ return yielded;
292
+ }
293
+ return Yield;
294
+ }
295
+ }
296
+ } catch (error) {
297
+ if (!(0, _Predicate.hasProperty)(current, evaluate)) {
298
+ return exitDie(`Micro/Fiber.runLoop: Not a valid effect: ${String(current)}`);
299
+ }
300
+ return exitDie(error);
301
+ }
302
+ }
303
+ getCont(symbol) {
304
+ while (true) {
305
+ const op = this._stack.pop();
306
+ if (!op) return undefined;
307
+ const cont = op[ensureCont] && op[ensureCont](this);
308
+ if (cont) return {
309
+ [symbol]: cont
310
+ };
311
+ if (op[symbol]) return op;
312
+ }
313
+ }
314
+ // cancel the yielded operation, or for the yielded exit value
315
+ _yielded = undefined;
316
+ yieldWith(value) {
317
+ this._yielded = value;
318
+ return Yield;
319
+ }
320
+ children() {
321
+ return this._children ??= new Set();
322
+ }
323
+ }
324
+ const fiberMiddleware = /*#__PURE__*/(0, _GlobalValue.globalValue)("effect/Micro/fiberMiddleware", () => ({
325
+ interruptChildren: undefined
326
+ }));
327
+ const fiberInterruptChildren = fiber => {
328
+ if (fiber._children === undefined || fiber._children.size === 0) {
329
+ return undefined;
330
+ }
331
+ return fiberInterruptAll(fiber._children);
332
+ };
276
333
  /**
277
- * @since 3.4.6
334
+ * @since 3.11.0
278
335
  * @experimental
279
- * @category MicroExit
336
+ * @category Fiber
280
337
  */
281
- exports.exitIsFail = exitIsFail;
282
- const exitIsDie = self => exitIsFailure(self) && self.left._tag === "Die";
338
+ const fiberAwait = self => async(resume => sync(self.addObserver(exit => resume(succeed(exit)))));
283
339
  /**
284
- * @since 3.4.6
340
+ * @since 3.11.0
285
341
  * @experimental
286
- * @category MicroExit
342
+ * @category Fiber
287
343
  */
288
- exports.exitIsDie = exitIsDie;
289
- const exitVoid = exports.exitVoid = /*#__PURE__*/exitSucceed(void 0);
290
- // ----------------------------------------------------------------------------
291
- // env
292
- // ----------------------------------------------------------------------------
344
+ exports.fiberAwait = fiberAwait;
345
+ const fiberInterrupt = self => suspend(() => {
346
+ self.unsafeInterrupt();
347
+ return asVoid(fiberAwait(self));
348
+ });
293
349
  /**
294
- * @since 3.4.0
350
+ * @since 3.11.0
295
351
  * @experimental
296
- * @category environment
352
+ * @category Fiber
297
353
  */
298
- const EnvTypeId = exports.EnvTypeId = /*#__PURE__*/Symbol.for("effect/Micro/Env");
299
- const EnvProto = {
300
- [EnvTypeId]: {
301
- _R: _Function.identity
302
- },
354
+ exports.fiberInterrupt = fiberInterrupt;
355
+ const fiberInterruptAll = fibers => suspend(() => {
356
+ for (const fiber of fibers) fiber.unsafeInterrupt();
357
+ const iter = fibers[Symbol.iterator]();
358
+ const wait = suspend(() => {
359
+ let result = iter.next();
360
+ while (!result.done) {
361
+ if (result.value.unsafePoll()) {
362
+ result = iter.next();
363
+ continue;
364
+ }
365
+ const fiber = result.value;
366
+ return async(resume => {
367
+ fiber.addObserver(_ => {
368
+ resume(wait);
369
+ });
370
+ });
371
+ }
372
+ return exitVoid;
373
+ });
374
+ return wait;
375
+ });
376
+ exports.fiberInterruptAll = fiberInterruptAll;
377
+ const identifier = /*#__PURE__*/Symbol.for("effect/Micro/identifier");
378
+ const args = /*#__PURE__*/Symbol.for("effect/Micro/args");
379
+ const evaluate = /*#__PURE__*/Symbol.for("effect/Micro/evaluate");
380
+ const successCont = /*#__PURE__*/Symbol.for("effect/Micro/successCont");
381
+ const failureCont = /*#__PURE__*/Symbol.for("effect/Micro/failureCont");
382
+ const ensureCont = /*#__PURE__*/Symbol.for("effect/Micro/ensureCont");
383
+ const Yield = /*#__PURE__*/Symbol.for("effect/Micro/Yield");
384
+ const microVariance = {
385
+ _A: _Function.identity,
386
+ _E: _Function.identity,
387
+ _R: _Function.identity
388
+ };
389
+ const MicroProto = {
390
+ ...Effectable.EffectPrototype,
391
+ _op: "Micro",
392
+ [TypeId]: microVariance,
303
393
  pipe() {
304
394
  return (0, _Pipeable.pipeArguments)(this, arguments);
395
+ },
396
+ [Symbol.iterator]() {
397
+ return new _Utils.SingleShotGen(new _Utils.YieldWrap(this));
398
+ },
399
+ toJSON() {
400
+ return {
401
+ _id: "effect/Micro",
402
+ op: this[identifier],
403
+ ...(args in this ? {
404
+ args: this[args]
405
+ } : undefined)
406
+ };
407
+ },
408
+ toString() {
409
+ return (0, _Inspectable.format)(this);
410
+ },
411
+ [_Inspectable.NodeInspectSymbol]() {
412
+ return (0, _Inspectable.format)(this);
305
413
  }
306
414
  };
307
- /**
308
- * @since 3.4.0
309
- * @experimental
310
- * @category environment
311
- */
312
- const envMake = refs => {
313
- const self = Object.create(EnvProto);
314
- self.refs = refs;
315
- return self;
415
+ function defaultEvaluate(_fiber) {
416
+ return exitDie(`Micro.evaluate: Not implemented`);
417
+ }
418
+ const makePrimitiveProto = options => ({
419
+ ...MicroProto,
420
+ [identifier]: options.op,
421
+ [evaluate]: options.eval ?? defaultEvaluate,
422
+ [successCont]: options.contA,
423
+ [failureCont]: options.contE,
424
+ [ensureCont]: options.ensure
425
+ });
426
+ const makePrimitive = options => {
427
+ const Proto = makePrimitiveProto(options);
428
+ return function () {
429
+ const self = Object.create(Proto);
430
+ self[args] = options.single === false ? arguments : arguments[0];
431
+ return self;
432
+ };
433
+ };
434
+ const makeExit = options => {
435
+ const Proto = {
436
+ ...makePrimitiveProto(options),
437
+ [MicroExitTypeId]: MicroExitTypeId,
438
+ _tag: options.op,
439
+ get [options.prop]() {
440
+ return this[args];
441
+ },
442
+ toJSON() {
443
+ return {
444
+ _id: "effect/Micro/Exit",
445
+ _tag: options.op,
446
+ [options.prop]: this[args]
447
+ };
448
+ },
449
+ [Equal.symbol](that) {
450
+ return isMicroExit(that) && that._tag === options.op && Equal.equals(this[args], that[args]);
451
+ },
452
+ [Hash.symbol]() {
453
+ return Hash.cached(this, Hash.combine(Hash.string(options.op))(Hash.hash(this[args])));
454
+ }
455
+ };
456
+ return function (value) {
457
+ const self = Object.create(Proto);
458
+ self[args] = value;
459
+ self[successCont] = undefined;
460
+ self[failureCont] = undefined;
461
+ self[ensureCont] = undefined;
462
+ return self;
463
+ };
316
464
  };
317
465
  /**
466
+ * Creates a `Micro` effect that will succeed with the specified constant value.
467
+ *
318
468
  * @since 3.4.0
319
469
  * @experimental
320
- * @category environment
470
+ * @category constructors
321
471
  */
322
- exports.envMake = envMake;
323
- const envUnsafeMakeEmpty = () => {
324
- const controller = new AbortController();
325
- const refs = Object.create(null);
326
- refs[currentAbortController.key] = controller;
327
- refs[currentAbortSignal.key] = controller.signal;
328
- refs[currentScheduler.key] = new MicroSchedulerDefault();
329
- return envMake(refs);
330
- };
472
+ const succeed = exports.succeed = /*#__PURE__*/makeExit({
473
+ op: "Success",
474
+ prop: "value",
475
+ eval(fiber) {
476
+ const cont = fiber.getCont(successCont);
477
+ return cont ? cont[successCont](this[args], fiber) : fiber.yieldWith(this);
478
+ }
479
+ });
331
480
  /**
332
- * @since 3.4.0
481
+ * Creates a `Micro` effect that will fail with the specified `MicroCause`.
482
+ *
483
+ * @since 3.4.6
333
484
  * @experimental
334
- * @category environment
485
+ * @category constructors
335
486
  */
336
- exports.envUnsafeMakeEmpty = envUnsafeMakeEmpty;
337
- const envGet = exports.envGet = /*#__PURE__*/(0, _Function.dual)(2, (self, ref) => ref.key in self.refs ? self.refs[ref.key] : ref.initial);
487
+ const failCause = exports.failCause = /*#__PURE__*/makeExit({
488
+ op: "Failure",
489
+ prop: "cause",
490
+ eval(fiber) {
491
+ let cont = fiber.getCont(failureCont);
492
+ while (causeIsInterrupt(this[args]) && cont && fiber.interruptible) {
493
+ cont = fiber.getCont(failureCont);
494
+ }
495
+ return cont ? cont[failureCont](this[args], fiber) : fiber.yieldWith(this);
496
+ }
497
+ });
338
498
  /**
499
+ * Creates a `Micro` effect that will fail with the specified error.
500
+ *
501
+ * This will result in a `CauseFail`, where the error is tracked at the
502
+ * type level.
503
+ *
339
504
  * @since 3.4.0
340
505
  * @experimental
341
- * @category environment
506
+ * @category constructors
342
507
  */
343
- const envSet = exports.envSet = /*#__PURE__*/(0, _Function.dual)(3, (self, ref, value) => {
344
- const refs = Object.assign(Object.create(null), self.refs);
345
- refs[ref.key] = value;
346
- return envMake(refs);
347
- });
508
+ const fail = error => failCause(causeFail(error));
348
509
  /**
510
+ * Creates a `Micro` effect that will succeed with the lazily evaluated value.
511
+ *
512
+ * If the evaluation of the value throws an error, the effect will fail with
513
+ * `CauseDie`.
514
+ *
349
515
  * @since 3.4.0
350
516
  * @experimental
351
- * @category environment
517
+ * @category constructors
352
518
  */
353
- const envMutate = exports.envMutate = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => envMake(f(Object.assign(Object.create(null), self.refs))));
519
+ exports.fail = fail;
520
+ const sync = exports.sync = /*#__PURE__*/makePrimitive({
521
+ op: "Sync",
522
+ eval(fiber) {
523
+ const value = this[args]();
524
+ const cont = fiber.getCont(successCont);
525
+ return cont ? cont[successCont](value, fiber) : fiber.yieldWith(exitSucceed(value));
526
+ }
527
+ });
354
528
  /**
355
- * Access the given `Context.Tag` from the environment.
529
+ * Lazily creates a `Micro` effect from the given side-effect.
356
530
  *
357
531
  * @since 3.4.0
358
532
  * @experimental
359
- * @category environment
533
+ * @category constructors
360
534
  */
361
- const service = tag => make(function (env, onExit) {
362
- onExit(exitSucceed(Context.get(envGet(env, currentContext), tag)));
535
+ const suspend = exports.suspend = /*#__PURE__*/makePrimitive({
536
+ op: "Suspend",
537
+ eval(_fiber) {
538
+ return this[args]();
539
+ }
363
540
  });
364
541
  /**
365
- * Access the given `Context.Tag` from the environment, without tracking the
366
- * dependency at the type level.
367
- *
368
- * It will return an `Option` of the service, depending on whether it is
369
- * available in the environment or not.
542
+ * Pause the execution of the current `Micro` effect, and resume it on the next
543
+ * scheduler tick.
370
544
  *
371
545
  * @since 3.4.0
372
546
  * @experimental
373
- * @category environment
547
+ * @category constructors
374
548
  */
375
- exports.service = service;
376
- const serviceOption = tag => make(function (env, onExit) {
377
- onExit(exitSucceed(Context.getOption(envGet(env, currentContext), tag)));
549
+ const yieldNowWith = exports.yieldNowWith = /*#__PURE__*/makePrimitive({
550
+ op: "Yield",
551
+ eval(fiber) {
552
+ let resumed = false;
553
+ fiber.getRef(CurrentScheduler).scheduleTask(() => {
554
+ if (resumed) return;
555
+ fiber.evaluate(exitVoid);
556
+ }, this[args] ?? 0);
557
+ return fiber.yieldWith(() => {
558
+ resumed = true;
559
+ });
560
+ }
378
561
  });
379
562
  /**
380
- * Retrieve the current value of the given `EnvRef`.
563
+ * Pause the execution of the current `Micro` effect, and resume it on the next
564
+ * scheduler tick.
381
565
  *
382
566
  * @since 3.4.0
383
567
  * @experimental
384
- * @category environment
568
+ * @category constructors
385
569
  */
386
- exports.serviceOption = serviceOption;
387
- const getEnvRef = envRef => make((env, onExit) => onExit(Either.right(envGet(env, envRef))));
570
+ const yieldNow = exports.yieldNow = /*#__PURE__*/yieldNowWith(0);
388
571
  /**
389
- * Set the value of the given `EnvRef` for the duration of the effect.
572
+ * Creates a `Micro` effect that will succeed with `Option.Some` of the value.
390
573
  *
391
574
  * @since 3.4.0
392
575
  * @experimental
393
- * @category environment
576
+ * @category constructors
394
577
  */
395
- exports.getEnvRef = getEnvRef;
396
- const locally = exports.locally = /*#__PURE__*/(0, _Function.dual)(3, (self, fiberRef, value) => make((env, onExit) => self[runSymbol](envSet(env, fiberRef, value), onExit)));
578
+ const succeedSome = a => succeed(Option.some(a));
397
579
  /**
398
- * Access the current `Context` from the environment.
580
+ * Creates a `Micro` effect that will succeed with `Option.None`.
399
581
  *
400
582
  * @since 3.4.0
401
583
  * @experimental
402
- * @category environment
584
+ * @category constructors
403
585
  */
404
- const context = () => getEnvRef(currentContext);
586
+ exports.succeedSome = succeedSome;
587
+ const succeedNone = exports.succeedNone = /*#__PURE__*/succeed( /*#__PURE__*/Option.none());
405
588
  /**
406
- * Merge the given `Context` with the current context.
589
+ * Creates a `Micro` effect that will fail with the lazily evaluated `MicroCause`.
407
590
  *
408
591
  * @since 3.4.0
409
592
  * @experimental
410
- * @category environment
593
+ * @category constructors
411
594
  */
412
- exports.context = context;
413
- const provideContext = exports.provideContext = /*#__PURE__*/(0, _Function.dual)(2, (self, provided) => make(function (env, onExit) {
414
- const context = envGet(env, currentContext);
415
- const nextEnv = envSet(env, currentContext, Context.merge(context, provided));
416
- self[runSymbol](nextEnv, onExit);
417
- }));
595
+ const failCauseSync = evaluate => suspend(() => failCause(evaluate()));
418
596
  /**
419
- * Add the provided service to the current context.
597
+ * Creates a `Micro` effect that will die with the specified error.
598
+ *
599
+ * This will result in a `CauseDie`, where the error is not tracked at
600
+ * the type level.
420
601
  *
421
602
  * @since 3.4.0
422
603
  * @experimental
423
- * @category environment
604
+ * @category constructors
424
605
  */
425
- const provideService = exports.provideService = /*#__PURE__*/(0, _Function.dual)(3, (self, tag, service) => make(function (env, onExit) {
426
- const context = envGet(env, currentContext);
427
- const nextEnv = envSet(env, currentContext, Context.add(context, tag, service));
428
- self[runSymbol](nextEnv, onExit);
429
- }));
606
+ exports.failCauseSync = failCauseSync;
607
+ const die = defect => exitDie(defect);
430
608
  /**
431
- * Create a service using the provided `Micro` effect, and add it to the
432
- * current context.
609
+ * Creates a `Micro` effect that will fail with the lazily evaluated error.
610
+ *
611
+ * This will result in a `CauseFail`, where the error is tracked at the
612
+ * type level.
433
613
  *
434
614
  * @since 3.4.6
435
615
  * @experimental
436
- * @category environment
616
+ * @category constructors
437
617
  */
438
- const provideServiceEffect = exports.provideServiceEffect = /*#__PURE__*/(0, _Function.dual)(3, (self, tag, acquire) => flatMap(acquire, service => provideService(self, tag, service)));
439
- const setImmediate = "setImmediate" in globalThis ? globalThis.setImmediate : f => setTimeout(f, 0);
440
- /**
441
- * @since 3.5.9
442
- * @experimental
443
- * @category scheduler
444
- */
445
- class MicroSchedulerDefault {
446
- tasks = [];
447
- running = false;
448
- /**
449
- * @since 3.5.9
450
- */
451
- scheduleTask(task, _priority) {
452
- this.tasks.push(task);
453
- if (!this.running) {
454
- this.running = true;
455
- setImmediate(this.afterScheduled);
456
- }
457
- }
458
- /**
459
- * @since 3.5.9
460
- */
461
- afterScheduled = () => {
462
- this.running = false;
463
- this.runTasks();
464
- };
465
- /**
466
- * @since 3.5.9
467
- */
468
- runTasks() {
469
- const tasks = this.tasks;
470
- this.tasks = [];
471
- for (let i = 0, len = tasks.length; i < len; i++) {
472
- tasks[i]();
473
- }
474
- }
475
- /**
476
- * @since 3.5.9
477
- */
478
- shouldYield(_env) {
479
- return false;
480
- }
481
- /**
482
- * @since 3.5.9
483
- */
484
- flush() {
485
- while (this.tasks.length > 0) {
486
- this.runTasks();
487
- }
488
- }
489
- }
490
- // ========================================================================
491
- // Env refs
492
- // ========================================================================
618
+ exports.die = die;
619
+ const failSync = error => suspend(() => fail(error()));
493
620
  /**
621
+ * Converts an `Option` into a `Micro` effect, that will fail with
622
+ * `NoSuchElementException` if the option is `None`. Otherwise, it will succeed with the
623
+ * value of the option.
624
+ *
494
625
  * @since 3.4.0
495
626
  * @experimental
496
- * @category environment
627
+ * @category constructors
497
628
  */
498
- exports.MicroSchedulerDefault = MicroSchedulerDefault;
499
- const EnvRefTypeId = exports.EnvRefTypeId = /*#__PURE__*/Symbol.for("effect/Micro/EnvRef");
500
- const EnvRefProto = {
501
- ...MicroProto,
502
- [EnvRefTypeId]: EnvRefTypeId,
503
- [runSymbol](env, onExit) {
504
- getEnvRef(this)[runSymbol](env, onExit);
505
- }
506
- };
629
+ exports.failSync = failSync;
630
+ const fromOption = option => option._tag === "Some" ? succeed(option.value) : fail(new NoSuchElementException({}));
507
631
  /**
632
+ * Converts an `Either` into a `Micro` effect, that will fail with the left side
633
+ * of the either if it is a `Left`. Otherwise, it will succeed with the right
634
+ * side of the either.
635
+ *
508
636
  * @since 3.4.0
509
637
  * @experimental
510
- * @category environment refs
638
+ * @category constructors
511
639
  */
512
- const envRefMake = (key, initial) => (0, _GlobalValue.globalValue)(key, () => {
513
- const self = Object.create(EnvRefProto);
514
- self.key = key;
515
- self.initial = initial();
516
- return self;
640
+ exports.fromOption = fromOption;
641
+ const fromEither = either => either._tag === "Right" ? succeed(either.right) : fail(either.left);
642
+ exports.fromEither = fromEither;
643
+ const void_ = exports.void = /*#__PURE__*/succeed(void 0);
644
+ const try_ = options => suspend(() => {
645
+ try {
646
+ return succeed(options.try());
647
+ } catch (err) {
648
+ return fail(options.catch(err));
649
+ }
517
650
  });
651
+ exports.try = try_;
518
652
  /**
653
+ * Wrap a `Promise` into a `Micro` effect. Any errors will result in a
654
+ * `CauseDie`.
655
+ *
519
656
  * @since 3.4.0
520
657
  * @experimental
521
- * @category environment refs
522
- */
523
- exports.envRefMake = envRefMake;
524
- const currentAbortController = exports.currentAbortController = /*#__PURE__*/envRefMake("effect/Micro/currentAbortController", () => undefined);
525
- /**
526
- * @since 3.4.0
527
- * @experimental
528
- * @category environment refs
529
- */
530
- const currentAbortSignal = exports.currentAbortSignal = /*#__PURE__*/envRefMake("effect/Micro/currentAbortSignal", () => undefined);
531
- /**
532
- * @since 3.4.0
533
- * @experimental
534
- * @category environment refs
535
- */
536
- const currentContext = exports.currentContext = /*#__PURE__*/envRefMake("effect/Micro/currentContext", () => Context.empty());
537
- /**
538
- * @since 3.4.0
539
- * @experimental
540
- * @category environment refs
658
+ * @category constructors
541
659
  */
542
- const currentConcurrency = exports.currentConcurrency = /*#__PURE__*/envRefMake("effect/Micro/currentConcurrency", () => "unbounded");
660
+ const promise = evaluate => asyncOptions(function (resume, signal) {
661
+ evaluate(signal).then(a => resume(succeed(a)), e => resume(die(e)));
662
+ }, evaluate.length !== 0);
543
663
  /**
664
+ * Wrap a `Promise` into a `Micro` effect. Any errors will be caught and
665
+ * converted into a specific error type.
666
+ *
544
667
  * @since 3.4.0
545
668
  * @experimental
546
- * @category environment refs
669
+ * @category constructors
670
+ * @example
671
+ * ```ts
672
+ * import { Micro } from "effect"
673
+ *
674
+ * Micro.tryPromise({
675
+ * try: () => Promise.resolve("success"),
676
+ * catch: (cause) => new Error("caught", { cause })
677
+ * })
678
+ * ```
547
679
  */
548
- const currentMaxDepthBeforeYield = exports.currentMaxDepthBeforeYield = /*#__PURE__*/envRefMake("effect/Micro/currentMaxDepthBeforeYield", () => 2048);
549
- const currentInterruptible = /*#__PURE__*/envRefMake("effect/Micro/currentInterruptible", () => true);
680
+ exports.promise = promise;
681
+ const tryPromise = options => asyncOptions(function (resume, signal) {
682
+ try {
683
+ options.try(signal).then(a => resume(succeed(a)), e => resume(fail(options.catch(e))));
684
+ } catch (err) {
685
+ resume(fail(options.catch(err)));
686
+ }
687
+ }, options.try.length !== 0);
550
688
  /**
689
+ * Create a `Micro` effect using the current `Fiber`.
690
+ *
551
691
  * @since 3.4.0
552
692
  * @experimental
553
- * @category environment refs
693
+ * @category constructors
554
694
  */
555
- const currentScheduler = exports.currentScheduler = /*#__PURE__*/envRefMake("effect/Micro/currentScheduler", () => new MicroSchedulerDefault());
695
+ exports.tryPromise = tryPromise;
696
+ const withFiber = exports.withFiber = /*#__PURE__*/makePrimitive({
697
+ op: "WithFiber",
698
+ eval(fiber) {
699
+ return this[args](fiber);
700
+ }
701
+ });
556
702
  /**
557
- * If you have a `Micro` that uses `concurrency: "inherit"`, you can use this
558
- * api to control the concurrency of that `Micro` when it is run.
703
+ * Flush any yielded effects that are waiting to be executed.
559
704
  *
560
705
  * @since 3.4.0
561
706
  * @experimental
562
- * @category environment refs
563
- * @example
564
- * import * as Micro from "effect/Micro"
565
- *
566
- * Micro.forEach([1, 2, 3], (n) => Micro.succeed(n), {
567
- * concurrency: "inherit"
568
- * }).pipe(
569
- * Micro.withConcurrency(2) // use a concurrency of 2
570
- * )
707
+ * @category constructors
571
708
  */
572
- const withConcurrency = exports.withConcurrency = /*#__PURE__*/(0, _Function.dual)(2, (self, concurrency) => locally(self, currentConcurrency, concurrency));
573
- // ----------------------------------------------------------------------------
574
- // constructors
575
- // ----------------------------------------------------------------------------
576
- const microDepthState = /*#__PURE__*/(0, _GlobalValue.globalValue)("effect/Micro/microDepthState", () => ({
577
- depth: 0,
578
- maxDepthBeforeYield: currentMaxDepthBeforeYield.initial
579
- }));
580
- const unsafeMake = run => {
581
- const self = Object.create(MicroProto);
582
- self[runSymbol] = run;
583
- return self;
584
- };
585
- const unsafeMakeOptions = (run, checkAbort) => unsafeMake(function execute(env, onExit) {
586
- if (checkAbort && env.refs[currentInterruptible.key] !== false && env.refs[currentAbortSignal.key].aborted) {
587
- return onExit(exitInterrupt);
588
- }
589
- microDepthState.depth++;
590
- if (microDepthState.depth === 1) {
591
- microDepthState.maxDepthBeforeYield = envGet(env, currentMaxDepthBeforeYield);
709
+ const yieldFlush = exports.yieldFlush = /*#__PURE__*/withFiber(fiber => {
710
+ fiber.getRef(CurrentScheduler).flush();
711
+ return exitVoid;
712
+ });
713
+ const asyncOptions = /*#__PURE__*/makePrimitive({
714
+ op: "Async",
715
+ single: false,
716
+ eval(fiber) {
717
+ const register = this[args][0];
718
+ let resumed = false;
719
+ let yielded = false;
720
+ const controller = this[args][1] ? new AbortController() : undefined;
721
+ const onCancel = register(effect => {
722
+ if (resumed) return;
723
+ resumed = true;
724
+ if (yielded) {
725
+ fiber.evaluate(effect);
726
+ } else {
727
+ yielded = effect;
728
+ }
729
+ }, controller?.signal);
730
+ if (yielded !== false) return yielded;
731
+ yielded = true;
732
+ fiber._yielded = () => {
733
+ resumed = true;
734
+ };
735
+ if (controller === undefined && onCancel === undefined) {
736
+ return Yield;
737
+ }
738
+ fiber._stack.push(asyncFinalizer(() => {
739
+ resumed = true;
740
+ controller?.abort();
741
+ return onCancel ?? exitVoid;
742
+ }));
743
+ return Yield;
592
744
  }
593
- const scheduler = env.refs[currentScheduler.key];
594
- if (microDepthState.depth >= microDepthState.maxDepthBeforeYield || scheduler.shouldYield(env)) {
595
- scheduler.scheduleTask(() => execute(env, onExit), 0);
596
- } else {
597
- try {
598
- run(env, onExit);
599
- } catch (err) {
600
- onExit(exitDie(err));
745
+ });
746
+ const asyncFinalizer = /*#__PURE__*/makePrimitive({
747
+ op: "AsyncFinalizer",
748
+ ensure(fiber) {
749
+ if (fiber.interruptible) {
750
+ fiber.interruptible = false;
751
+ fiber._stack.push(setInterruptible(true));
601
752
  }
753
+ },
754
+ contE(cause, _fiber) {
755
+ return causeIsInterrupt(cause) ? flatMap(this[args](), () => failCause(cause)) : failCause(cause);
602
756
  }
603
- microDepthState.depth--;
604
757
  });
605
758
  /**
606
- * A low-level constructor for creating a `Micro` effect. It takes a function
607
- * that receives an environment and a callback which should be called with the
608
- * result of the effect.
759
+ * Create a `Micro` effect from an asynchronous computation.
760
+ *
761
+ * You can return a cleanup effect that will be run when the effect is aborted.
762
+ * It is also passed an `AbortSignal` that is triggered when the effect is
763
+ * aborted.
609
764
  *
610
765
  * @since 3.4.0
611
766
  * @experimental
612
767
  * @category constructors
613
768
  */
614
- const make = run => unsafeMakeOptions(run, true);
769
+ const async = register => asyncOptions(register, register.length >= 2);
615
770
  /**
616
- * Converts a `MicroExit` into a `Micro` effect.
771
+ * A `Micro` that will never succeed or fail. It wraps `setInterval` to prevent
772
+ * the Javascript runtime from exiting.
617
773
  *
618
- * @since 3.4.6
774
+ * @since 3.4.0
619
775
  * @experimental
620
776
  * @category constructors
621
777
  */
622
- exports.make = make;
623
- const fromExit = self => make(function (_env, onExit) {
624
- onExit(self);
778
+ exports.async = async;
779
+ const never = exports.never = /*#__PURE__*/async(function () {
780
+ const interval = setInterval(_Function.constVoid, 2147483646);
781
+ return sync(() => clearInterval(interval));
625
782
  });
626
783
  /**
627
- * Converts a lazy `MicroExit` into a `Micro` effect.
628
- *
629
- * @since 3.4.6
784
+ * @since 3.4.0
630
785
  * @experimental
631
786
  * @category constructors
632
787
  */
633
- exports.fromExit = fromExit;
634
- const fromExitSync = self => make(function (_env, onExit) {
635
- onExit(self());
788
+ const gen = (...args) => suspend(() => fromIterator(args.length === 1 ? args[0]() : args[1].call(args[0])));
789
+ exports.gen = gen;
790
+ const fromIterator = /*#__PURE__*/makePrimitive({
791
+ op: "Iterator",
792
+ contA(value, fiber) {
793
+ const state = this[args].next(value);
794
+ if (state.done) return succeed(state.value);
795
+ fiber._stack.push(this);
796
+ return (0, _Utils.yieldWrapGet)(state.value);
797
+ },
798
+ eval(fiber) {
799
+ return this[successCont](undefined, fiber);
800
+ }
636
801
  });
802
+ // ----------------------------------------------------------------------------
803
+ // mapping & sequencing
804
+ // ----------------------------------------------------------------------------
637
805
  /**
638
- * Creates a `Micro` effect that will succeed with the specified constant value.
806
+ * Create a `Micro` effect that will replace the success value of the given
807
+ * effect.
639
808
  *
640
809
  * @since 3.4.0
641
810
  * @experimental
642
- * @category constructors
811
+ * @category mapping & sequencing
643
812
  */
644
- exports.fromExitSync = fromExitSync;
645
- const succeed = a => fromExit(exitSucceed(a));
813
+ const as = exports.as = /*#__PURE__*/(0, _Function.dual)(2, (self, value) => map(self, _ => value));
646
814
  /**
647
- * Creates a `Micro` effect that will succeed with `Option.Some` of the value.
815
+ * Wrap the success value of this `Micro` effect in an `Option.Some`.
648
816
  *
649
817
  * @since 3.4.0
650
818
  * @experimental
651
- * @category constructors
819
+ * @category mapping & sequencing
652
820
  */
653
- exports.succeed = succeed;
654
- const succeedSome = a => succeed(Option.some(a));
821
+ const asSome = self => map(self, Option.some);
655
822
  /**
656
- * Creates a `Micro` effect that will succeed with `Option.None`.
823
+ * Swap the error and success types of the `Micro` effect.
657
824
  *
658
825
  * @since 3.4.0
659
826
  * @experimental
660
- * @category constructors
827
+ * @category mapping & sequencing
661
828
  */
662
- exports.succeedSome = succeedSome;
663
- const succeedNone = exports.succeedNone = /*#__PURE__*/succeed( /*#__PURE__*/Option.none());
829
+ exports.asSome = asSome;
830
+ const flip = self => matchEffect(self, {
831
+ onFailure: succeed,
832
+ onSuccess: fail
833
+ });
664
834
  /**
665
- * Creates a `Micro` effect that will fail with the specified error.
835
+ * A more flexible version of `flatMap`, that combines `map` and `flatMap` into
836
+ * a single api.
666
837
  *
667
- * This will result in a `CauseFail`, where the error is tracked at the
668
- * type level.
838
+ * It also allows you to pass in a `Micro` effect directly, which will be
839
+ * executed after the current effect.
669
840
  *
670
841
  * @since 3.4.0
671
842
  * @experimental
672
- * @category constructors
843
+ * @category mapping & sequencing
673
844
  */
674
- const fail = e => fromExit(exitFail(e));
845
+ exports.flip = flip;
846
+ const andThen = exports.andThen = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => flatMap(self, a => {
847
+ const value = isMicro(f) ? f : typeof f === "function" ? f(a) : f;
848
+ return isMicro(value) ? value : succeed(value);
849
+ }));
675
850
  /**
676
- * Creates a `Micro` effect that will fail with the lazily evaluated error.
851
+ * Execute a side effect from the success value of the `Micro` effect.
677
852
  *
678
- * This will result in a `CauseFail`, where the error is tracked at the
679
- * type level.
853
+ * It is similar to the `andThen` api, but the success value is ignored.
680
854
  *
681
855
  * @since 3.4.0
682
856
  * @experimental
683
- * @category constructors
857
+ * @category mapping & sequencing
684
858
  */
685
- exports.fail = fail;
686
- const failSync = e => make(function (_env, onExit) {
687
- onExit(exitFail(e()));
688
- });
859
+ const tap = exports.tap = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => flatMap(self, a => {
860
+ const value = isMicro(f) ? f : typeof f === "function" ? f(a) : f;
861
+ return isMicro(value) ? as(value, a) : succeed(a);
862
+ }));
689
863
  /**
690
- * Creates a `Micro` effect that will die with the specified error.
691
- *
692
- * This will result in a `CauseDie`, where the error is not tracked at
693
- * the type level.
864
+ * Replace the success value of the `Micro` effect with `void`.
694
865
  *
695
866
  * @since 3.4.0
696
867
  * @experimental
697
- * @category constructors
698
- */
699
- exports.failSync = failSync;
700
- const die = defect => fromExit(exitDie(defect));
701
- /**
702
- * Creates a `Micro` effect that will fail with the specified `MicroCause`.
703
- *
704
- * @since 3.4.6
705
- * @experimental
706
- * @category constructors
868
+ * @category mapping & sequencing
707
869
  */
708
- exports.die = die;
709
- const failCause = cause => fromExit(exitFailCause(cause));
870
+ const asVoid = self => flatMap(self, _ => exitVoid);
710
871
  /**
711
- * Creates a `Micro` effect that will fail with the lazily evaluated `MicroCause`.
872
+ * Access the `MicroExit` of the given `Micro` effect.
712
873
  *
713
874
  * @since 3.4.6
714
875
  * @experimental
715
- * @category constructors
716
- */
717
- exports.failCause = failCause;
718
- const failCauseSync = cause => fromExitSync(() => exitFailCause(cause()));
719
- /**
720
- * Creates a `Micro` effect that will succeed with the lazily evaluated value.
721
- *
722
- * If the evaluation of the value throws an error, the effect will fail with
723
- * `CauseDie`.
724
- *
725
- * @since 3.4.0
726
- * @experimental
727
- * @category constructors
876
+ * @category mapping & sequencing
728
877
  */
729
- exports.failCauseSync = failCauseSync;
730
- const sync = evaluate => make(function (_env, onExit) {
731
- onExit(exitSucceed(evaluate()));
878
+ exports.asVoid = asVoid;
879
+ const exit = self => matchCause(self, {
880
+ onFailure: exitFailCause,
881
+ onSuccess: exitSucceed
732
882
  });
733
883
  /**
734
- * Converts an `Option` into a `Micro` effect, that will fail with
735
- * `NoSuchElementException` if the option is `None`. Otherwise, it will succeed with the
736
- * value of the option.
884
+ * Replace the error type of the given `Micro` with the full `MicroCause` object.
737
885
  *
738
886
  * @since 3.4.0
739
887
  * @experimental
740
- * @category constructors
888
+ * @category mapping & sequencing
741
889
  */
742
- exports.sync = sync;
743
- const fromOption = option => make(function (_env, onExit) {
744
- onExit(option._tag === "Some" ? exitSucceed(option.value) : exitFail(new NoSuchElementException({})));
745
- });
890
+ exports.exit = exit;
891
+ const sandbox = self => catchAllCause(self, fail);
746
892
  /**
747
- * Converts an `Either` into a `Micro` effect, that will fail with the left side
748
- * of the either if it is a `Left`. Otherwise, it will succeed with the right
749
- * side of the either.
893
+ * Returns an effect that races all the specified effects,
894
+ * yielding the value of the first effect to succeed with a value. Losers of
895
+ * the race will be interrupted immediately
750
896
  *
751
897
  * @since 3.4.0
752
898
  * @experimental
753
- * @category constructors
899
+ * @category sequencing
754
900
  */
755
- exports.fromOption = fromOption;
756
- const fromEither = either => make(function (_env, onExit) {
757
- onExit(either._tag === "Right" ? either : exitFail(either.left));
758
- });
901
+ exports.sandbox = sandbox;
902
+ const raceAll = all => withFiber(parent => async(resume => {
903
+ const effects = Arr.fromIterable(all);
904
+ const len = effects.length;
905
+ let doneCount = 0;
906
+ let done = false;
907
+ const fibers = new Set();
908
+ const causes = [];
909
+ const onExit = exit => {
910
+ doneCount++;
911
+ if (exit._tag === "Failure") {
912
+ causes.push(exit.cause);
913
+ if (doneCount >= len) {
914
+ resume(failCause(causes[0]));
915
+ }
916
+ return;
917
+ }
918
+ done = true;
919
+ resume(fibers.size === 0 ? exit : flatMap(uninterruptible(fiberInterruptAll(fibers)), () => exit));
920
+ };
921
+ for (let i = 0; i < len; i++) {
922
+ if (done) break;
923
+ const fiber = unsafeFork(parent, interruptible(effects[i]), true, true);
924
+ fibers.add(fiber);
925
+ fiber.addObserver(exit => {
926
+ fibers.delete(fiber);
927
+ onExit(exit);
928
+ });
929
+ }
930
+ return fiberInterruptAll(fibers);
931
+ }));
759
932
  /**
760
- * Lazily creates a `Micro` effect from the given side-effect.
933
+ * Returns an effect that races all the specified effects,
934
+ * yielding the value of the first effect to succeed or fail. Losers of
935
+ * the race will be interrupted immediately
761
936
  *
762
937
  * @since 3.4.0
763
938
  * @experimental
764
- * @category constructors
939
+ * @category sequencing
765
940
  */
766
- exports.fromEither = fromEither;
767
- const suspend = evaluate => make(function (env, onExit) {
768
- evaluate()[runSymbol](env, onExit);
769
- });
770
- exports.suspend = suspend;
771
- const void_ = exports.void = /*#__PURE__*/succeed(void 0);
941
+ exports.raceAll = raceAll;
942
+ const raceAllFirst = all => withFiber(parent => async(resume => {
943
+ let done = false;
944
+ const fibers = new Set();
945
+ const onExit = exit => {
946
+ done = true;
947
+ resume(fibers.size === 0 ? exit : flatMap(fiberInterruptAll(fibers), () => exit));
948
+ };
949
+ for (const effect of all) {
950
+ if (done) break;
951
+ const fiber = unsafeFork(parent, interruptible(effect), true, true);
952
+ fibers.add(fiber);
953
+ fiber.addObserver(exit => {
954
+ fibers.delete(fiber);
955
+ onExit(exit);
956
+ });
957
+ }
958
+ return fiberInterruptAll(fibers);
959
+ }));
772
960
  /**
773
- * Create a `Micro` effect from an asynchronous computation.
774
- *
775
- * You can return a cleanup effect that will be run when the effect is aborted.
776
- * It is also passed an `AbortSignal` that is triggered when the effect is
777
- * aborted.
961
+ * Returns an effect that races two effects, yielding the value of the first
962
+ * effect to succeed. Losers of the race will be interrupted immediately
778
963
  *
779
964
  * @since 3.4.0
780
965
  * @experimental
781
- * @category constructors
966
+ * @category sequencing
782
967
  */
783
- const async = register => make(function (env, onExit) {
784
- let resumed = false;
785
- const controller = register.length > 1 ? new AbortController() : undefined;
786
- const signal = envGet(env, currentAbortSignal);
787
- let cleanup = undefined;
788
- function onAbort() {
789
- if (cleanup) {
790
- resume(uninterruptible(andThen(cleanup, fromExit(exitInterrupt))));
791
- } else {
792
- resume(fromExit(exitInterrupt));
793
- }
794
- if (controller !== undefined) {
795
- controller.abort();
796
- }
797
- }
798
- function resume(effect) {
799
- if (resumed) {
800
- return;
801
- }
802
- resumed = true;
803
- signal.removeEventListener("abort", onAbort);
804
- effect[runSymbol](env, onExit);
805
- }
806
- cleanup = controller === undefined ? register(resume) : register(resume, controller.signal);
807
- if (resumed) return;
808
- signal.addEventListener("abort", onAbort);
809
- });
810
- exports.async = async;
811
- const try_ = options => make(function (_env, onExit) {
812
- try {
813
- onExit(exitSucceed(options.try()));
814
- } catch (err) {
815
- onExit(exitFail(options.catch(err)));
816
- }
817
- });
818
- exports.try = try_;
968
+ exports.raceAllFirst = raceAllFirst;
969
+ const race = exports.race = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => raceAll([self, that]));
819
970
  /**
820
- * Wrap a `Promise` into a `Micro` effect. Any errors will result in a
821
- * `CauseDie`.
971
+ * Returns an effect that races two effects, yielding the value of the first
972
+ * effect to succeed *or* fail. Losers of the race will be interrupted immediately
822
973
  *
823
974
  * @since 3.4.0
824
975
  * @experimental
825
- * @category constructors
976
+ * @category sequencing
826
977
  */
827
- const promise = evaluate => async(function (resume, signal) {
828
- evaluate(signal).then(a => resume(succeed(a)), e => resume(die(e)));
829
- });
978
+ const raceFirst = exports.raceFirst = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => raceAllFirst([self, that]));
830
979
  /**
831
- * Wrap a `Promise` into a `Micro` effect. Any errors will be caught and
832
- * converted into a specific error type.
980
+ * Map the success value of this `Micro` effect to another `Micro` effect, then
981
+ * flatten the result.
833
982
  *
834
983
  * @since 3.4.0
835
984
  * @experimental
836
- * @category constructors
837
- * @example
838
- * import { Micro } from "effect"
839
- *
840
- * Micro.tryPromise({
841
- * try: () => Promise.resolve("success"),
842
- * catch: (cause) => new Error("caught", { cause })
843
- * })
985
+ * @category mapping & sequencing
844
986
  */
845
- exports.promise = promise;
846
- const tryPromise = options => async(function (resume, signal) {
847
- try {
848
- options.try(signal).then(a => resume(succeed(a)), e => resume(fail(options.catch(e))));
849
- } catch (err) {
850
- resume(fail(options.catch(err)));
987
+ const flatMap = exports.flatMap = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => {
988
+ const onSuccess = Object.create(OnSuccessProto);
989
+ onSuccess[args] = self;
990
+ onSuccess[successCont] = f;
991
+ return onSuccess;
992
+ });
993
+ const OnSuccessProto = /*#__PURE__*/makePrimitiveProto({
994
+ op: "OnSuccess",
995
+ eval(fiber) {
996
+ fiber._stack.push(this);
997
+ return this[args];
851
998
  }
852
999
  });
1000
+ // ----------------------------------------------------------------------------
1001
+ // mapping & sequencing
1002
+ // ----------------------------------------------------------------------------
853
1003
  /**
854
- * Pause the execution of the current `Micro` effect, and resume it on the next
855
- * iteration of the event loop.
856
- *
857
- * You can specify a priority for the task, which will determine when it is
858
- * executed relative to other tasks.
1004
+ * Flattens any nested `Micro` effects, merging the error and requirement types.
859
1005
  *
860
1006
  * @since 3.4.0
861
1007
  * @experimental
862
- * @category constructors
1008
+ * @category mapping & sequencing
863
1009
  */
864
- exports.tryPromise = tryPromise;
865
- const yieldWithPriority = priority => make(function (env, onExit) {
866
- envGet(env, currentScheduler).scheduleTask(() => onExit(exitVoid), priority);
867
- });
1010
+ const flatten = self => flatMap(self, _Function.identity);
868
1011
  /**
869
- * Pause the execution of the current `Micro` effect, and resume it on the next
870
- * iteration of the event loop.
1012
+ * Transforms the success value of the `Micro` effect with the specified
1013
+ * function.
871
1014
  *
872
1015
  * @since 3.4.0
873
1016
  * @experimental
874
- * @category constructors
1017
+ * @category mapping & sequencing
875
1018
  */
876
- exports.yieldWithPriority = yieldWithPriority;
877
- const yieldNow = exports.yieldNow = /*#__PURE__*/yieldWithPriority(0);
1019
+ exports.flatten = flatten;
1020
+ const map = exports.map = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => flatMap(self, a => succeed(f(a))));
878
1021
  /**
879
- * Flush any yielded effects that are waiting to be executed.
880
- *
881
- * @since 3.4.0
1022
+ * @since 3.4.6
882
1023
  * @experimental
883
- * @category constructors
1024
+ * @category MicroExit
884
1025
  */
885
- const yieldFlush = exports.yieldFlush = /*#__PURE__*/make(function (env, onExit) {
886
- envGet(env, currentScheduler).flush();
887
- onExit(exitVoid);
888
- });
1026
+ const isMicroExit = u => (0, _Predicate.hasProperty)(u, MicroExitTypeId);
889
1027
  /**
890
- * A `Micro` that will never succeed or fail. It wraps `setInterval` to prevent
891
- * the Javascript runtime from exiting.
892
- *
893
- * @since 3.4.0
1028
+ * @since 3.4.6
894
1029
  * @experimental
895
- * @category constructors
1030
+ * @category MicroExit
896
1031
  */
897
- const never = exports.never = /*#__PURE__*/async(function () {
898
- const interval = setInterval(_Function.constVoid, 2147483646);
899
- return sync(() => clearInterval(interval));
900
- });
1032
+ exports.isMicroExit = isMicroExit;
1033
+ const exitSucceed = exports.exitSucceed = succeed;
901
1034
  /**
902
- * @since 3.4.0
1035
+ * @since 3.4.6
903
1036
  * @experimental
904
- * @category constructors
1037
+ * @category MicroExit
905
1038
  */
906
- const gen = (...args) => make(function (env, onExit) {
907
- const iterator = args.length === 1 ? args[0]() : args[1].call(args[0]);
908
- let running = false;
909
- let value = undefined;
910
- function run() {
911
- running = true;
912
- try {
913
- let shouldContinue = true;
914
- while (shouldContinue) {
915
- const result = iterator.next(value);
916
- if (result.done) {
917
- return onExit(exitSucceed(result.value));
918
- }
919
- shouldContinue = false;
920
- (0, _Utils.yieldWrapGet)(result.value)[runSymbol](env, function (exit) {
921
- if (exit._tag === "Left") {
922
- onExit(exit);
923
- } else {
924
- shouldContinue = true;
925
- value = exit.right;
926
- if (!running) run();
927
- }
928
- });
929
- }
930
- } catch (err) {
931
- onExit(exitDie(err));
932
- }
933
- running = false;
934
- }
935
- run();
936
- });
937
- // ----------------------------------------------------------------------------
938
- // mapping & sequencing
939
- // ----------------------------------------------------------------------------
1039
+ const exitFailCause = exports.exitFailCause = failCause;
940
1040
  /**
941
- * Flattens any nested `Micro` effects, merging the error and requirement types.
942
- *
943
- * @since 3.4.0
1041
+ * @since 3.4.6
944
1042
  * @experimental
945
- * @category mapping & sequencing
1043
+ * @category MicroExit
946
1044
  */
947
- exports.gen = gen;
948
- const flatten = self => make(function (env, onExit) {
949
- self[runSymbol](env, exit => exit._tag === "Left" ? onExit(exit) : exit.right[runSymbol](env, onExit));
950
- });
1045
+ const exitInterrupt = exports.exitInterrupt = /*#__PURE__*/exitFailCause( /*#__PURE__*/causeInterrupt());
951
1046
  /**
952
- * Transforms the success value of the `Micro` effect with the specified
953
- * function.
954
- *
955
- * @since 3.4.0
1047
+ * @since 3.4.6
956
1048
  * @experimental
957
- * @category mapping & sequencing
1049
+ * @category MicroExit
958
1050
  */
959
- exports.flatten = flatten;
960
- const map = exports.map = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => make(function (env, onExit) {
961
- self[runSymbol](env, function (exit) {
962
- onExit(exit._tag === "Left" ? exit : exitSucceed(f(exit.right)));
963
- });
964
- }));
1051
+ const exitFail = e => exitFailCause(causeFail(e));
965
1052
  /**
966
- * Create a `Micro` effect that will replace the success value of the given
967
- * effect.
968
- *
969
- * @since 3.4.0
1053
+ * @since 3.4.6
970
1054
  * @experimental
971
- * @category mapping & sequencing
1055
+ * @category MicroExit
972
1056
  */
973
- const as = exports.as = /*#__PURE__*/(0, _Function.dual)(2, (self, value) => map(self, _ => value));
1057
+ exports.exitFail = exitFail;
1058
+ const exitDie = defect => exitFailCause(causeDie(defect));
974
1059
  /**
975
- * Wrap the success value of this `Micro` effect in an `Option.Some`.
976
- *
977
- * @since 3.4.0
1060
+ * @since 3.4.6
978
1061
  * @experimental
979
- * @category mapping & sequencing
1062
+ * @category MicroExit
980
1063
  */
981
- const asSome = self => map(self, Option.some);
1064
+ exports.exitDie = exitDie;
1065
+ const exitIsSuccess = self => self._tag === "Success";
982
1066
  /**
983
- * Map the success value of this `Micro` effect to another `Micro` effect, then
984
- * flatten the result.
985
- *
986
- * @since 3.4.0
1067
+ * @since 3.4.6
987
1068
  * @experimental
988
- * @category mapping & sequencing
1069
+ * @category MicroExit
989
1070
  */
990
- exports.asSome = asSome;
991
- const flatMap = exports.flatMap = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => make(function (env, onExit) {
992
- self[runSymbol](env, function (exit) {
993
- if (exit._tag === "Left") {
994
- return onExit(exit);
1071
+ exports.exitIsSuccess = exitIsSuccess;
1072
+ const exitIsFailure = self => self._tag === "Failure";
1073
+ /**
1074
+ * @since 3.4.6
1075
+ * @experimental
1076
+ * @category MicroExit
1077
+ */
1078
+ exports.exitIsFailure = exitIsFailure;
1079
+ const exitIsInterrupt = self => exitIsFailure(self) && self.cause._tag === "Interrupt";
1080
+ /**
1081
+ * @since 3.4.6
1082
+ * @experimental
1083
+ * @category MicroExit
1084
+ */
1085
+ exports.exitIsInterrupt = exitIsInterrupt;
1086
+ const exitIsFail = self => exitIsFailure(self) && self.cause._tag === "Fail";
1087
+ /**
1088
+ * @since 3.4.6
1089
+ * @experimental
1090
+ * @category MicroExit
1091
+ */
1092
+ exports.exitIsFail = exitIsFail;
1093
+ const exitIsDie = self => exitIsFailure(self) && self.cause._tag === "Die";
1094
+ /**
1095
+ * @since 3.4.6
1096
+ * @experimental
1097
+ * @category MicroExit
1098
+ */
1099
+ exports.exitIsDie = exitIsDie;
1100
+ const exitVoid = exports.exitVoid = /*#__PURE__*/exitSucceed(void 0);
1101
+ /**
1102
+ * @since 3.11.0
1103
+ * @experimental
1104
+ * @category MicroExit
1105
+ */
1106
+ const exitVoidAll = exits => {
1107
+ for (const exit of exits) {
1108
+ if (exit._tag === "Failure") {
1109
+ return exit;
995
1110
  }
996
- f(exit.right)[runSymbol](env, onExit);
997
- });
998
- }));
1111
+ }
1112
+ return exitVoid;
1113
+ };
1114
+ exports.exitVoidAll = exitVoidAll;
1115
+ const setImmediate = "setImmediate" in globalThis ? globalThis.setImmediate : f => setTimeout(f, 0);
999
1116
  /**
1000
- * Swap the error and success types of the `Micro` effect.
1117
+ * @since 3.5.9
1118
+ * @experimental
1119
+ * @category scheduler
1120
+ */
1121
+ class MicroSchedulerDefault {
1122
+ tasks = [];
1123
+ running = false;
1124
+ /**
1125
+ * @since 3.5.9
1126
+ */
1127
+ scheduleTask(task, _priority) {
1128
+ this.tasks.push(task);
1129
+ if (!this.running) {
1130
+ this.running = true;
1131
+ setImmediate(this.afterScheduled);
1132
+ }
1133
+ }
1134
+ /**
1135
+ * @since 3.5.9
1136
+ */
1137
+ afterScheduled = () => {
1138
+ this.running = false;
1139
+ this.runTasks();
1140
+ };
1141
+ /**
1142
+ * @since 3.5.9
1143
+ */
1144
+ runTasks() {
1145
+ const tasks = this.tasks;
1146
+ this.tasks = [];
1147
+ for (let i = 0, len = tasks.length; i < len; i++) {
1148
+ tasks[i]();
1149
+ }
1150
+ }
1151
+ /**
1152
+ * @since 3.5.9
1153
+ */
1154
+ shouldYield(fiber) {
1155
+ return fiber.currentOpCount >= fiber.getRef(MaxOpsBeforeYield);
1156
+ }
1157
+ /**
1158
+ * @since 3.5.9
1159
+ */
1160
+ flush() {
1161
+ while (this.tasks.length > 0) {
1162
+ this.runTasks();
1163
+ }
1164
+ }
1165
+ }
1166
+ /**
1167
+ * Access the given `Context.Tag` from the environment.
1001
1168
  *
1002
1169
  * @since 3.4.0
1003
1170
  * @experimental
1004
- * @category mapping & sequencing
1171
+ * @category environment
1005
1172
  */
1006
- const flip = self => matchEffect(self, {
1007
- onFailure: succeed,
1008
- onSuccess: fail
1009
- });
1173
+ exports.MicroSchedulerDefault = MicroSchedulerDefault;
1174
+ const service = tag => withFiber(fiber => succeed(Context.unsafeGet(fiber.context, tag)));
1010
1175
  /**
1011
- * A more flexible version of `flatMap`, that combines `map` and `flatMap` into
1012
- * a single api.
1176
+ * Access the given `Context.Tag` from the environment, without tracking the
1177
+ * dependency at the type level.
1013
1178
  *
1014
- * It also allows you to pass in a `Micro` effect directly, which will be
1015
- * executed after the current effect.
1179
+ * It will return an `Option` of the service, depending on whether it is
1180
+ * available in the environment or not.
1016
1181
  *
1017
1182
  * @since 3.4.0
1018
1183
  * @experimental
1019
- * @category mapping & sequencing
1184
+ * @category environment
1020
1185
  */
1021
- exports.flip = flip;
1022
- const andThen = exports.andThen = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => make(function (env, onExit) {
1023
- self[runSymbol](env, function (exit) {
1024
- if (exit._tag === "Left") {
1025
- return onExit(exit);
1026
- } else if (envGet(env, currentAbortSignal).aborted) {
1027
- return onExit(exitInterrupt);
1028
- }
1029
- const value = isMicro(f) ? f : typeof f === "function" ? f(exit.right) : f;
1030
- if (isMicro(value)) {
1031
- value[runSymbol](env, onExit);
1032
- } else {
1033
- onExit(exitSucceed(value));
1034
- }
1186
+ exports.service = service;
1187
+ const serviceOption = tag => withFiber(fiber => succeed(Context.getOption(fiber.context, tag)));
1188
+ /**
1189
+ * Update the Context with the given mapping function.
1190
+ *
1191
+ * @since 3.11.0
1192
+ * @experimental
1193
+ * @category environment
1194
+ */
1195
+ exports.serviceOption = serviceOption;
1196
+ const updateContext = exports.updateContext = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => withFiber(fiber => {
1197
+ const prev = fiber.context;
1198
+ fiber.context = f(prev);
1199
+ return onExit(self, () => {
1200
+ fiber.context = prev;
1201
+ return void_;
1035
1202
  });
1036
1203
  }));
1037
1204
  /**
1038
- * Execute a side effect from the success value of the `Micro` effect.
1205
+ * Update the service for the given `Context.Tag` in the environment.
1039
1206
  *
1040
- * It is similar to the `andThen` api, but the success value is ignored.
1041
- *
1042
- * @since 3.4.0
1207
+ * @since 3.11.0
1043
1208
  * @experimental
1044
- * @category mapping & sequencing
1209
+ * @category environment
1045
1210
  */
1046
- const tap = exports.tap = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => make(function (env, onExit) {
1047
- self[runSymbol](env, function (selfExit) {
1048
- if (selfExit._tag === "Left") {
1049
- return onExit(selfExit);
1050
- } else if (envGet(env, currentAbortSignal).aborted) {
1051
- return onExit(exitInterrupt);
1052
- }
1053
- const value = isMicro(f) ? f : typeof f === "function" ? f(selfExit.right) : f;
1054
- if (isMicro(value)) {
1055
- value[runSymbol](env, function (tapExit) {
1056
- if (tapExit._tag === "Left") {
1057
- return onExit(tapExit);
1058
- }
1059
- onExit(selfExit);
1060
- });
1061
- } else {
1062
- onExit(selfExit);
1063
- }
1211
+ const updateService = exports.updateService = /*#__PURE__*/(0, _Function.dual)(3, (self, tag, f) => withFiber(fiber => {
1212
+ const prev = Context.unsafeGet(fiber.context, tag);
1213
+ fiber.context = Context.add(fiber.context, tag, f(prev));
1214
+ return onExit(self, () => {
1215
+ fiber.context = Context.add(fiber.context, tag, prev);
1216
+ return void_;
1064
1217
  });
1065
1218
  }));
1066
1219
  /**
1067
- * Replace the success value of the `Micro` effect with `void`.
1220
+ * Access the current `Context` from the environment.
1068
1221
  *
1069
1222
  * @since 3.4.0
1070
1223
  * @experimental
1071
- * @category mapping & sequencing
1224
+ * @category environment
1072
1225
  */
1073
- const asVoid = self => map(self, _ => void 0);
1226
+ const context = () => getContext;
1227
+ exports.context = context;
1228
+ const getContext = /*#__PURE__*/withFiber(fiber => succeed(fiber.context));
1074
1229
  /**
1075
- * Access the `MicroExit` of the given `Micro` effect.
1230
+ * Merge the given `Context` with the current context.
1076
1231
  *
1077
- * @since 3.4.6
1232
+ * @since 3.4.0
1078
1233
  * @experimental
1079
- * @category mapping & sequencing
1234
+ * @category environment
1080
1235
  */
1081
- exports.asVoid = asVoid;
1082
- const exit = self => make(function (env, onExit) {
1083
- self[runSymbol](env, function (exit) {
1084
- onExit(exitSucceed(exit));
1085
- });
1086
- });
1236
+ const provideContext = exports.provideContext = /*#__PURE__*/(0, _Function.dual)(2, (self, provided) => updateContext(self, Context.merge(provided)));
1087
1237
  /**
1088
- * Replace the error type of the given `Micro` with the full `MicroCause` object.
1238
+ * Add the provided service to the current context.
1089
1239
  *
1090
1240
  * @since 3.4.0
1091
1241
  * @experimental
1092
- * @category mapping & sequencing
1242
+ * @category environment
1093
1243
  */
1094
- exports.exit = exit;
1095
- const sandbox = self => catchAllCause(self, cause => fail(cause));
1096
- exports.sandbox = sandbox;
1097
- function forkSignal(env) {
1098
- const controller = new AbortController();
1099
- const parentSignal = envGet(env, currentAbortSignal);
1100
- function onAbort() {
1101
- controller.abort();
1102
- parentSignal.removeEventListener("abort", onAbort);
1103
- }
1104
- parentSignal.addEventListener("abort", onAbort);
1105
- const envWithSignal = envMutate(env, function (refs) {
1106
- refs[currentAbortController.key] = controller;
1107
- refs[currentAbortSignal.key] = controller.signal;
1108
- return refs;
1109
- });
1110
- return [envWithSignal, onAbort];
1111
- }
1244
+ const provideService = exports.provideService = /*#__PURE__*/(0, _Function.dual)(3, (self, tag, service) => updateContext(self, Context.add(tag, service)));
1112
1245
  /**
1113
- * Returns an effect that races all the specified effects,
1114
- * yielding the value of the first effect to succeed with a value. Losers of
1115
- * the race will be interrupted immediately
1246
+ * Create a service using the provided `Micro` effect, and add it to the
1247
+ * current context.
1116
1248
  *
1117
- * @since 3.4.0
1249
+ * @since 3.4.6
1118
1250
  * @experimental
1119
- * @category sequencing
1251
+ * @category environment
1120
1252
  */
1121
- const raceAll = all => make(function (env, onExit) {
1122
- const [envWithSignal, onAbort] = forkSignal(env);
1123
- const effects = Array.from(all);
1124
- let len = effects.length;
1125
- let index = 0;
1126
- let done = 0;
1127
- let exit = undefined;
1128
- const causes = [];
1129
- function onDone(exit_) {
1130
- done++;
1131
- if (exit_._tag === "Right" && exit === undefined) {
1132
- len = index;
1133
- exit = exit_;
1134
- onAbort();
1135
- } else if (exit_._tag === "Left") {
1136
- causes.push(exit_.left);
1137
- }
1138
- if (done >= len) {
1139
- onExit(exit ?? Either.left(causes[0]));
1140
- }
1141
- }
1142
- for (; index < len; index++) {
1143
- effects[index][runSymbol](envWithSignal, onDone);
1144
- }
1145
- });
1253
+ const provideServiceEffect = exports.provideServiceEffect = /*#__PURE__*/(0, _Function.dual)(3, (self, tag, acquire) => flatMap(acquire, service => provideService(self, tag, service)));
1254
+ // ========================================================================
1255
+ // References
1256
+ // ========================================================================
1146
1257
  /**
1147
- * Returns an effect that races all the specified effects,
1148
- * yielding the value of the first effect to succeed or fail. Losers of
1149
- * the race will be interrupted immediately
1150
- *
1151
- * @since 3.4.0
1258
+ * @since 3.11.0
1152
1259
  * @experimental
1153
- * @category sequencing
1260
+ * @category references
1154
1261
  */
1155
- exports.raceAll = raceAll;
1156
- const raceAllFirst = all => make(function (env, onExit) {
1157
- const [envWithSignal, onAbort] = forkSignal(env);
1158
- const effects = Array.from(all);
1159
- let len = effects.length;
1160
- let index = 0;
1161
- let done = 0;
1162
- let exit = undefined;
1163
- const causes = [];
1164
- function onDone(exit_) {
1165
- done++;
1166
- if (exit === undefined) {
1167
- len = index;
1168
- exit = exit_;
1169
- onAbort();
1170
- }
1171
- if (done >= len) {
1172
- onExit(exit ?? Either.left(causes[0]));
1173
- }
1174
- }
1175
- for (; index < len; index++) {
1176
- effects[index][runSymbol](envWithSignal, onDone);
1177
- }
1178
- });
1262
+ class MaxOpsBeforeYield extends /*#__PURE__*/Context.Reference()("effect/Micro/currentMaxOpsBeforeYield", {
1263
+ defaultValue: () => 2048
1264
+ }) {}
1179
1265
  /**
1180
- * Returns an effect that races two effects, yielding the value of the first
1181
- * effect to succeed. Losers of the race will be interrupted immediately
1182
- *
1183
- * @since 3.4.0
1266
+ * @since 3.11.0
1184
1267
  * @experimental
1185
- * @category sequencing
1268
+ * @category environment refs
1186
1269
  */
1187
- exports.raceAllFirst = raceAllFirst;
1188
- const race = exports.race = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => raceAll([self, that]));
1270
+ exports.MaxOpsBeforeYield = MaxOpsBeforeYield;
1271
+ class CurrentConcurrency extends /*#__PURE__*/Context.Reference()("effect/Micro/currentConcurrency", {
1272
+ defaultValue: () => "unbounded"
1273
+ }) {}
1189
1274
  /**
1190
- * Returns an effect that races two effects, yielding the value of the first
1191
- * effect to succeed *or* fail. Losers of the race will be interrupted immediately
1275
+ * @since 3.11.0
1276
+ * @experimental
1277
+ * @category environment refs
1278
+ */
1279
+ exports.CurrentConcurrency = CurrentConcurrency;
1280
+ class CurrentScheduler extends /*#__PURE__*/Context.Reference()("effect/Micro/currentScheduler", {
1281
+ defaultValue: () => new MicroSchedulerDefault()
1282
+ }) {}
1283
+ /**
1284
+ * If you have a `Micro` that uses `concurrency: "inherit"`, you can use this
1285
+ * api to control the concurrency of that `Micro` when it is run.
1192
1286
  *
1193
1287
  * @since 3.4.0
1194
1288
  * @experimental
1195
- * @category sequencing
1289
+ * @category environment refs
1290
+ * @example
1291
+ * import * as Micro from "effect/Micro"
1292
+ *
1293
+ * Micro.forEach([1, 2, 3], (n) => Micro.succeed(n), {
1294
+ * concurrency: "inherit"
1295
+ * }).pipe(
1296
+ * Micro.withConcurrency(2) // use a concurrency of 2
1297
+ * )
1196
1298
  */
1197
- const raceFirst = exports.raceFirst = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => raceAllFirst([self, that]));
1299
+ exports.CurrentScheduler = CurrentScheduler;
1300
+ const withConcurrency = exports.withConcurrency = /*#__PURE__*/(0, _Function.dual)(2, (self, concurrency) => provideService(self, CurrentConcurrency, concurrency));
1198
1301
  // ----------------------------------------------------------------------------
1199
1302
  // zipping
1200
1303
  // ----------------------------------------------------------------------------
@@ -1216,15 +1319,11 @@ const zip = exports.zip = /*#__PURE__*/(0, _Function.dual)(args => isMicro(args[
1216
1319
  * @experimental
1217
1320
  * @category zipping
1218
1321
  */
1219
- const zipWith = exports.zipWith = /*#__PURE__*/(0, _Function.dual)(args => isMicro(args[1]), (self, that, f, options) => {
1220
- if (options?.concurrent) {
1221
- // Use `all` exclusively for concurrent cases, as it introduces additional overhead due to the management of concurrency
1222
- return map(all([self, that], {
1223
- concurrency: "unbounded"
1224
- }), ([a, a2]) => f(a, a2));
1225
- }
1226
- return flatMap(self, a => map(that, a2 => f(a, a2)));
1227
- });
1322
+ const zipWith = exports.zipWith = /*#__PURE__*/(0, _Function.dual)(args => isMicro(args[1]), (self, that, f, options) => options?.concurrent
1323
+ // Use `all` exclusively for concurrent cases, as it introduces additional overhead due to the management of concurrency
1324
+ ? map(all([self, that], {
1325
+ concurrency: 2
1326
+ }), ([a, a2]) => f(a, a2)) : flatMap(self, a => map(that, a2 => f(a, a2))));
1228
1327
  // ----------------------------------------------------------------------------
1229
1328
  // filtering & conditionals
1230
1329
  // ----------------------------------------------------------------------------
@@ -1259,7 +1358,7 @@ const filterOrFail = exports.filterOrFail = /*#__PURE__*/(0, _Function.dual)(arg
1259
1358
  * @experimental
1260
1359
  * @category filtering & conditionals
1261
1360
  */
1262
- const when = exports.when = /*#__PURE__*/(0, _Function.dual)(2, (self, condition) => flatMap(isMicro(condition) ? condition : sync(condition), pass => pass ? asSome(self) : succeed(Option.none())));
1361
+ const when = exports.when = /*#__PURE__*/(0, _Function.dual)(2, (self, condition) => flatMap(isMicro(condition) ? condition : sync(condition), pass => pass ? asSome(self) : succeedNone));
1263
1362
  // ----------------------------------------------------------------------------
1264
1363
  // repetition
1265
1364
  // ----------------------------------------------------------------------------
@@ -1273,14 +1372,14 @@ const when = exports.when = /*#__PURE__*/(0, _Function.dual)(2, (self, condition
1273
1372
  * @experimental
1274
1373
  * @category repetition
1275
1374
  */
1276
- const repeatExit = exports.repeatExit = /*#__PURE__*/(0, _Function.dual)(2, (self, options) => make(function (env, onExit) {
1375
+ const repeatExit = exports.repeatExit = /*#__PURE__*/(0, _Function.dual)(2, (self, options) => suspend(() => {
1277
1376
  const startedAt = options.schedule ? Date.now() : 0;
1278
1377
  let attempt = 0;
1279
- self[runSymbol](env, function loop(exit) {
1378
+ const loop = flatMap(exit(self), exit => {
1280
1379
  if (options.while !== undefined && !options.while(exit)) {
1281
- return onExit(exit);
1380
+ return exit;
1282
1381
  } else if (options.times !== undefined && attempt >= options.times) {
1283
- return onExit(exit);
1382
+ return exit;
1284
1383
  }
1285
1384
  attempt++;
1286
1385
  let delayEffect = yieldNow;
@@ -1288,17 +1387,13 @@ const repeatExit = exports.repeatExit = /*#__PURE__*/(0, _Function.dual)(2, (sel
1288
1387
  const elapsed = Date.now() - startedAt;
1289
1388
  const duration = options.schedule(attempt, elapsed);
1290
1389
  if (Option.isNone(duration)) {
1291
- return onExit(exit);
1390
+ return exit;
1292
1391
  }
1293
1392
  delayEffect = sleep(duration.value);
1294
1393
  }
1295
- delayEffect[runSymbol](env, function (exit) {
1296
- if (exit._tag === "Left") {
1297
- return onExit(exit);
1298
- }
1299
- self[runSymbol](env, loop);
1300
- });
1394
+ return flatMap(delayEffect, () => loop);
1301
1395
  });
1396
+ return loop;
1302
1397
  }));
1303
1398
  /**
1304
1399
  * Repeat the given `Micro` effect using the provided options. Only successful
@@ -1310,8 +1405,26 @@ const repeatExit = exports.repeatExit = /*#__PURE__*/(0, _Function.dual)(2, (sel
1310
1405
  */
1311
1406
  const repeat = exports.repeat = /*#__PURE__*/(0, _Function.dual)(args => isMicro(args[0]), (self, options) => repeatExit(self, {
1312
1407
  ...options,
1313
- while: exit => exit._tag === "Right" && (options?.while === undefined || options.while(exit.right))
1408
+ while: exit => exit._tag === "Success" && (options?.while === undefined || options.while(exit.value))
1314
1409
  }));
1410
+ /**
1411
+ * Replicates the given effect `n` times.
1412
+ *
1413
+ * @since 3.11.0
1414
+ * @experimental
1415
+ * @category repetition
1416
+ */
1417
+ const replicate = exports.replicate = /*#__PURE__*/(0, _Function.dual)(2, (self, n) => Array.from({
1418
+ length: n
1419
+ }, () => self));
1420
+ /**
1421
+ * Performs this effect the specified number of times and collects the
1422
+ * results.
1423
+ *
1424
+ * @since 3.11.0
1425
+ * @category repetition
1426
+ */
1427
+ const replicateEffect = exports.replicateEffect = /*#__PURE__*/(0, _Function.dual)(args => isMicro(args[0]), (self, n, options) => all(replicate(self, n), options));
1315
1428
  /**
1316
1429
  * Repeat the given `Micro` effect forever, only stopping if the effect fails.
1317
1430
  *
@@ -1405,7 +1518,19 @@ const scheduleIntersect = exports.scheduleIntersect = /*#__PURE__*/(0, _Function
1405
1518
  * @experimental
1406
1519
  * @category error handling
1407
1520
  */
1408
- const catchAllCause = exports.catchAllCause = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => catchCauseIf(self, _Function.constTrue, f));
1521
+ const catchAllCause = exports.catchAllCause = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => {
1522
+ const onFailure = Object.create(OnFailureProto);
1523
+ onFailure[args] = self;
1524
+ onFailure[failureCont] = f;
1525
+ return onFailure;
1526
+ });
1527
+ const OnFailureProto = /*#__PURE__*/makePrimitiveProto({
1528
+ op: "OnFailure",
1529
+ eval(fiber) {
1530
+ fiber._stack.push(this);
1531
+ return this[args];
1532
+ }
1533
+ });
1409
1534
  /**
1410
1535
  * Selectively catch a `MicroCause` object of the given `Micro` effect,
1411
1536
  * using the provided predicate to determine if the failure should be caught.
@@ -1414,15 +1539,7 @@ const catchAllCause = exports.catchAllCause = /*#__PURE__*/(0, _Function.dual)(2
1414
1539
  * @experimental
1415
1540
  * @category error handling
1416
1541
  */
1417
- const catchCauseIf = exports.catchCauseIf = /*#__PURE__*/(0, _Function.dual)(3, (self, predicate, f) => make(function (env, onExit) {
1418
- self[runSymbol](env, function (exit) {
1419
- if (exit._tag === "Right" || !predicate(exit.left)) {
1420
- onExit(exit);
1421
- } else {
1422
- f(exit.left)[runSymbol](env, onExit);
1423
- }
1424
- });
1425
- }));
1542
+ const catchCauseIf = exports.catchCauseIf = /*#__PURE__*/(0, _Function.dual)(3, (self, predicate, f) => catchAllCause(self, cause => predicate(cause) ? f(cause) : failCause(cause)));
1426
1543
  /**
1427
1544
  * Catch the error of the given `Micro` effect, allowing you to recover from it.
1428
1545
  *
@@ -1432,7 +1549,7 @@ const catchCauseIf = exports.catchCauseIf = /*#__PURE__*/(0, _Function.dual)(3,
1432
1549
  * @experimental
1433
1550
  * @category error handling
1434
1551
  */
1435
- const catchAll = exports.catchAll = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => catchAllCause(self, cause => causeIsFail(cause) ? f(cause.error) : failCause(cause)));
1552
+ const catchAll = exports.catchAll = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => catchCauseIf(self, causeIsFail, cause => f(cause.error)));
1436
1553
  /**
1437
1554
  * Catch any unexpected errors of the given `Micro` effect, allowing you to recover from them.
1438
1555
  *
@@ -1559,7 +1676,7 @@ const ignoreLogged = self => matchEffect(self, {
1559
1676
  */
1560
1677
  exports.ignoreLogged = ignoreLogged;
1561
1678
  const option = self => match(self, {
1562
- onFailure: _ => Option.none(),
1679
+ onFailure: Option.none,
1563
1680
  onSuccess: Option.some
1564
1681
  });
1565
1682
  /**
@@ -1586,7 +1703,7 @@ const either = self => match(self, {
1586
1703
  exports.either = either;
1587
1704
  const retry = exports.retry = /*#__PURE__*/(0, _Function.dual)(args => isMicro(args[0]), (self, options) => repeatExit(self, {
1588
1705
  ...options,
1589
- while: exit => exit._tag === "Left" && exit.left._tag === "Fail" && (options?.while === undefined || options.while(exit.left.error))
1706
+ while: exit => exit._tag === "Failure" && exit.cause._tag === "Fail" && (options?.while === undefined || options.while(exit.cause.error))
1590
1707
  }));
1591
1708
  /**
1592
1709
  * Add a stack trace to any failures that occur in the effect. The trace will be
@@ -1613,11 +1730,7 @@ const withTrace = function () {
1613
1730
  const lineMatch = line.match(/\((.*)\)$/);
1614
1731
  return causeWithTrace(cause, `at ${name} (${lineMatch ? lineMatch[1] : line})`);
1615
1732
  }
1616
- const f = name => self => unsafeMakeOptions(function (env, onExit) {
1617
- self[runSymbol](env, function (exit) {
1618
- onExit(exit._tag === "Left" ? Either.left(generate(name, exit.left)) : exit);
1619
- });
1620
- }, false);
1733
+ const f = name => self => onError(self, cause => failCause(generate(name, cause)));
1621
1734
  if (arguments.length === 2) {
1622
1735
  return f(arguments[1])(arguments[0]);
1623
1736
  }
@@ -1632,16 +1745,20 @@ const withTrace = function () {
1632
1745
  * @category pattern matching
1633
1746
  */
1634
1747
  exports.withTrace = withTrace;
1635
- const matchCauseEffect = exports.matchCauseEffect = /*#__PURE__*/(0, _Function.dual)(2, (self, options) => make(function (env, onExit) {
1636
- self[runSymbol](env, function (exit) {
1637
- try {
1638
- const next = exit._tag === "Left" ? options.onFailure(exit.left) : options.onSuccess(exit.right);
1639
- next[runSymbol](env, onExit);
1640
- } catch (err) {
1641
- onExit(exitDie(err));
1642
- }
1643
- });
1644
- }));
1748
+ const matchCauseEffect = exports.matchCauseEffect = /*#__PURE__*/(0, _Function.dual)(2, (self, options) => {
1749
+ const primitive = Object.create(OnSuccessAndFailureProto);
1750
+ primitive[args] = self;
1751
+ primitive[successCont] = options.onSuccess;
1752
+ primitive[failureCont] = options.onFailure;
1753
+ return primitive;
1754
+ });
1755
+ const OnSuccessAndFailureProto = /*#__PURE__*/makePrimitiveProto({
1756
+ op: "OnSuccessAndFailure",
1757
+ eval(fiber) {
1758
+ fiber._stack.push(this);
1759
+ return this[args];
1760
+ }
1761
+ });
1645
1762
  /**
1646
1763
  * @since 3.4.6
1647
1764
  * @experimental
@@ -1679,12 +1796,12 @@ const match = exports.match = /*#__PURE__*/(0, _Function.dual)(2, (self, options
1679
1796
  * @experimental
1680
1797
  * @category delays & timeouts
1681
1798
  */
1682
- const sleep = millis => async(function (resume) {
1683
- const timeout = setTimeout(function () {
1799
+ const sleep = millis => async(resume => {
1800
+ const timeout = setTimeout(() => {
1684
1801
  resume(void_);
1685
1802
  }, millis);
1686
1803
  return sync(() => {
1687
- return clearTimeout(timeout);
1804
+ clearTimeout(timeout);
1688
1805
  });
1689
1806
  });
1690
1807
  /**
@@ -1786,7 +1903,7 @@ class MicroScopeImpl {
1786
1903
  _tag: "Closed",
1787
1904
  exit: microExit
1788
1905
  };
1789
- return flatMap(forEach(finalizers, finalizer => exit(finalizer(microExit))), exits => asVoid(fromExit(Either.all(exits))));
1906
+ return flatMap(forEach(finalizers, finalizer => exit(finalizer(microExit))), exitVoidAll);
1790
1907
  }
1791
1908
  return void_;
1792
1909
  });
@@ -1844,7 +1961,7 @@ const provideScope = exports.provideScope = /*#__PURE__*/(0, _Function.dual)(2,
1844
1961
  * @experimental
1845
1962
  * @category resources & finalization
1846
1963
  */
1847
- const scoped = self => suspend(function () {
1964
+ const scoped = self => suspend(() => {
1848
1965
  const scope = new MicroScopeImpl();
1849
1966
  return onExit(provideService(self, MicroScope, scope), exit => scope.close(exit));
1850
1967
  });
@@ -1876,36 +1993,27 @@ const addFinalizer = finalizer => flatMap(scope, scope => scope.addFinalizer(fin
1876
1993
  * @category resources & finalization
1877
1994
  */
1878
1995
  exports.addFinalizer = addFinalizer;
1879
- const onExit = exports.onExit = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => onExitIf(self, _Function.constTrue, f));
1996
+ const onExit = exports.onExit = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => uninterruptibleMask(restore => matchCauseEffect(restore(self), {
1997
+ onFailure: cause => flatMap(f(exitFailCause(cause)), () => failCause(cause)),
1998
+ onSuccess: a => flatMap(f(exitSucceed(a)), () => succeed(a))
1999
+ })));
1880
2000
  /**
1881
- * When the `Micro` effect is completed, run the given finalizer effect if it
1882
- * matches the specified predicate.
2001
+ * Regardless of the result of the this `Micro` effect, run the finalizer effect.
1883
2002
  *
1884
- * @since 3.4.6
2003
+ * @since 3.4.0
1885
2004
  * @experimental
1886
2005
  * @category resources & finalization
1887
2006
  */
1888
- const onExitIf = exports.onExitIf = /*#__PURE__*/(0, _Function.dual)(3, (self, refinement, f) => uninterruptibleMask(restore => make(function (env, onExit) {
1889
- restore(self)[runSymbol](env, function (exit) {
1890
- if (!refinement(exit)) {
1891
- return onExit(exit);
1892
- }
1893
- f(exit)[runSymbol](env, function (finalizerExit) {
1894
- if (finalizerExit._tag === "Left") {
1895
- return onExit(finalizerExit);
1896
- }
1897
- onExit(exit);
1898
- });
1899
- });
1900
- })));
2007
+ const ensuring = exports.ensuring = /*#__PURE__*/(0, _Function.dual)(2, (self, finalizer) => onExit(self, _ => finalizer));
1901
2008
  /**
1902
- * Regardless of the result of the this `Micro` effect, run the finalizer effect.
2009
+ * When the `Micro` effect is completed, run the given finalizer effect if it
2010
+ * matches the specified predicate.
1903
2011
  *
1904
- * @since 3.4.0
2012
+ * @since 3.4.6
1905
2013
  * @experimental
1906
2014
  * @category resources & finalization
1907
2015
  */
1908
- const ensuring = exports.ensuring = /*#__PURE__*/(0, _Function.dual)(2, (self, finalizer) => onExit(self, _ => finalizer));
2016
+ const onExitIf = exports.onExitIf = /*#__PURE__*/(0, _Function.dual)(3, (self, refinement, f) => onExit(self, exit => refinement(exit) ? f(exit) : exitVoid));
1909
2017
  /**
1910
2018
  * When the `Micro` effect fails, run the given finalizer effect with the
1911
2019
  * `MicroCause` of the executed effect.
@@ -1914,7 +2022,7 @@ const ensuring = exports.ensuring = /*#__PURE__*/(0, _Function.dual)(2, (self, f
1914
2022
  * @experimental
1915
2023
  * @category resources & finalization
1916
2024
  */
1917
- const onError = exports.onError = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => onExitIf(self, exitIsFailure, exit => f(exit.left)));
2025
+ const onError = exports.onError = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => onExitIf(self, exitIsFailure, exit => f(exit.cause)));
1918
2026
  /**
1919
2027
  * If this `Micro` effect is aborted, run the finalizer effect.
1920
2028
  *
@@ -1931,7 +2039,7 @@ const onInterrupt = exports.onInterrupt = /*#__PURE__*/(0, _Function.dual)(2, (s
1931
2039
  * @experimental
1932
2040
  * @category resources & finalization
1933
2041
  */
1934
- const acquireUseRelease = (acquire, use, release) => uninterruptibleMask(restore => flatMap(acquire, a => flatMap(exit(restore(use(a))), exit => andThen(release(a, exit), fromExit(exit)))));
2042
+ const acquireUseRelease = (acquire, use, release) => uninterruptibleMask(restore => flatMap(acquire, a => flatMap(exit(restore(use(a))), exit => andThen(release(a, exit), exit))));
1935
2043
  // ----------------------------------------------------------------------------
1936
2044
  // interruption
1937
2045
  // ----------------------------------------------------------------------------
@@ -1943,27 +2051,46 @@ const acquireUseRelease = (acquire, use, release) => uninterruptibleMask(restore
1943
2051
  * @category interruption
1944
2052
  */
1945
2053
  exports.acquireUseRelease = acquireUseRelease;
1946
- const interrupt = exports.interrupt = /*#__PURE__*/make(function (env, onExit) {
1947
- const controller = envGet(env, currentAbortController);
1948
- controller.abort();
1949
- onExit(exitInterrupt);
2054
+ const interrupt = exports.interrupt = /*#__PURE__*/failCause( /*#__PURE__*/causeInterrupt());
2055
+ /**
2056
+ * Flag the effect as uninterruptible, which means that when the effect is
2057
+ * interrupted, it will be allowed to continue running until completion.
2058
+ *
2059
+ * @since 3.4.0
2060
+ * @experimental
2061
+ * @category flags
2062
+ */
2063
+ const uninterruptible = self => withFiber(fiber => {
2064
+ if (!fiber.interruptible) return self;
2065
+ fiber.interruptible = false;
2066
+ fiber._stack.push(setInterruptible(true));
2067
+ return self;
2068
+ });
2069
+ exports.uninterruptible = uninterruptible;
2070
+ const setInterruptible = /*#__PURE__*/makePrimitive({
2071
+ op: "SetInterruptible",
2072
+ ensure(fiber) {
2073
+ fiber.interruptible = this[args];
2074
+ if (fiber._interrupted && fiber.interruptible) {
2075
+ return () => exitInterrupt;
2076
+ }
2077
+ }
1950
2078
  });
1951
2079
  /**
1952
- * Wrap the given `Micro` effect in an uninterruptible region, preventing the
1953
- * effect from being aborted.
2080
+ * Flag the effect as interruptible, which means that when the effect is
2081
+ * interrupted, it will be interrupted immediately.
1954
2082
  *
1955
2083
  * @since 3.4.0
1956
2084
  * @experimental
1957
- * @category interruption
2085
+ * @category flags
1958
2086
  */
1959
- const uninterruptible = self => unsafeMakeOptions(function (env, onExit) {
1960
- const nextEnv = envMutate(env, function (env) {
1961
- env[currentInterruptible.key] = false;
1962
- env[currentAbortSignal.key] = new AbortController().signal;
1963
- return env;
1964
- });
1965
- self[runSymbol](nextEnv, onExit);
1966
- }, false);
2087
+ const interruptible = self => withFiber(fiber => {
2088
+ if (fiber.interruptible) return self;
2089
+ fiber.interruptible = true;
2090
+ fiber._stack.push(setInterruptible(false));
2091
+ if (fiber._interrupted) return exitInterrupt;
2092
+ return self;
2093
+ });
1967
2094
  /**
1968
2095
  * Wrap the given `Micro` effect in an uninterruptible region, preventing the
1969
2096
  * effect from being aborted.
@@ -1975,6 +2102,7 @@ const uninterruptible = self => unsafeMakeOptions(function (env, onExit) {
1975
2102
  * @experimental
1976
2103
  * @category interruption
1977
2104
  * @example
2105
+ * ```ts
1978
2106
  * import * as Micro from "effect/Micro"
1979
2107
  *
1980
2108
  * Micro.uninterruptibleMask((restore) =>
@@ -1982,39 +2110,14 @@ const uninterruptible = self => unsafeMakeOptions(function (env, onExit) {
1982
2110
  * Micro.andThen(restore(Micro.sleep(1000))) // interruptible
1983
2111
  * )
1984
2112
  * )
2113
+ * ```
1985
2114
  */
1986
- exports.uninterruptible = uninterruptible;
1987
- const uninterruptibleMask = f => unsafeMakeOptions((env, onExit) => {
1988
- const isInterruptible = envGet(env, currentInterruptible);
1989
- const effect = isInterruptible ? f(interruptible) : f(_Function.identity);
1990
- const nextEnv = isInterruptible ? envMutate(env, function (env) {
1991
- env[currentInterruptible.key] = false;
1992
- env[currentAbortSignal.key] = new AbortController().signal;
1993
- return env;
1994
- }) : env;
1995
- effect[runSymbol](nextEnv, onExit);
1996
- }, false);
1997
- /**
1998
- * Wrap the given `Micro` effect in an interruptible region, allowing the effect
1999
- * to be aborted.
2000
- *
2001
- * @since 3.4.0
2002
- * @experimental
2003
- * @category interruption
2004
- */
2005
- exports.uninterruptibleMask = uninterruptibleMask;
2006
- const interruptible = self => make((env, onExit) => {
2007
- const isInterruptible = envGet(env, currentInterruptible);
2008
- let newEnv = env;
2009
- if (!isInterruptible) {
2010
- const controller = envGet(env, currentAbortController);
2011
- newEnv = envMutate(env, function (env) {
2012
- env[currentInterruptible.key] = true;
2013
- env[currentAbortSignal.key] = controller.signal;
2014
- return env;
2015
- });
2016
- }
2017
- self[runSymbol](newEnv, onExit);
2115
+ exports.interruptible = interruptible;
2116
+ const uninterruptibleMask = f => withFiber(fiber => {
2117
+ if (!fiber.interruptible) return f(_Function.identity);
2118
+ fiber.interruptible = false;
2119
+ fiber._stack.push(setInterruptible(true));
2120
+ return f(interruptible);
2018
2121
  });
2019
2122
  /**
2020
2123
  * Runs all the provided effects in sequence respecting the structure provided in input.
@@ -2025,7 +2128,7 @@ const interruptible = self => make((env, onExit) => {
2025
2128
  * @experimental
2026
2129
  * @category collecting & elements
2027
2130
  */
2028
- exports.interruptible = interruptible;
2131
+ exports.uninterruptibleMask = uninterruptibleMask;
2029
2132
  const all = (arg, options) => {
2030
2133
  if (Array.isArray(arg) || (0, _Predicate.isIterable)(arg)) {
2031
2134
  return forEach(arg, _Function.identity, options);
@@ -2042,6 +2145,30 @@ const all = (arg, options) => {
2042
2145
  }), out);
2043
2146
  });
2044
2147
  };
2148
+ /**
2149
+ * @since 3.11.0
2150
+ * @experimental
2151
+ * @category collecting & elements
2152
+ */
2153
+ exports.all = all;
2154
+ const whileLoop = exports.whileLoop = /*#__PURE__*/makePrimitive({
2155
+ op: "While",
2156
+ contA(value, fiber) {
2157
+ this[args].step(value);
2158
+ if (this[args].while()) {
2159
+ fiber._stack.push(this);
2160
+ return this[args].body();
2161
+ }
2162
+ return exitVoid;
2163
+ },
2164
+ eval(fiber) {
2165
+ if (this[args].while()) {
2166
+ fiber._stack.push(this);
2167
+ return this[args].body();
2168
+ }
2169
+ return exitVoid;
2170
+ }
2171
+ });
2045
2172
  /**
2046
2173
  * For each element of the provided iterable, run the effect and collect the results.
2047
2174
  *
@@ -2055,59 +2182,76 @@ const all = (arg, options) => {
2055
2182
  * @experimental
2056
2183
  * @category collecting & elements
2057
2184
  */
2058
- exports.all = all;
2059
- const forEach = (iterable, f, options) => make(function (env, onExit) {
2060
- const concurrencyOption = options?.concurrency === "inherit" ? envGet(env, currentConcurrency) : options?.concurrency ?? 1;
2185
+ const forEach = (iterable, f, options) => withFiber(parent => {
2186
+ const concurrencyOption = options?.concurrency === "inherit" ? parent.getRef(CurrentConcurrency) : options?.concurrency ?? 1;
2061
2187
  const concurrency = concurrencyOption === "unbounded" ? Number.POSITIVE_INFINITY : Math.max(1, concurrencyOption);
2062
- // abort
2063
- const [envWithSignal, onAbort] = forkSignal(env);
2064
- // iterate
2065
- let result = undefined;
2066
- const items = Array.from(iterable);
2188
+ const items = Arr.fromIterable(iterable);
2067
2189
  let length = items.length;
2068
2190
  if (length === 0) {
2069
- return onExit(Either.right(options?.discard ? undefined : []));
2191
+ return options?.discard ? void_ : succeed([]);
2070
2192
  }
2071
2193
  const out = options?.discard ? undefined : new Array(length);
2072
2194
  let index = 0;
2073
- let inProgress = 0;
2074
- let doneCount = 0;
2075
- let pumping = false;
2076
- function pump() {
2077
- pumping = true;
2078
- while (inProgress < concurrency && index < length) {
2079
- const currentIndex = index;
2080
- const item = items[currentIndex];
2081
- index++;
2082
- inProgress++;
2083
- try {
2084
- f(item, currentIndex)[runSymbol](envWithSignal, function (exit) {
2085
- if (exit._tag === "Left") {
2086
- if (result === undefined) {
2087
- result = exit;
2088
- length = index;
2089
- onAbort();
2195
+ if (concurrency === 1) {
2196
+ return as(whileLoop({
2197
+ while: () => index < items.length,
2198
+ body: () => f(items[index], index),
2199
+ step: out ? b => out[index++] = b : _ => index++
2200
+ }), out);
2201
+ }
2202
+ return async(resume => {
2203
+ const fibers = new Set();
2204
+ let result = undefined;
2205
+ let inProgress = 0;
2206
+ let doneCount = 0;
2207
+ let pumping = false;
2208
+ let interrupted = false;
2209
+ function pump() {
2210
+ pumping = true;
2211
+ while (inProgress < concurrency && index < length) {
2212
+ const currentIndex = index;
2213
+ const item = items[currentIndex];
2214
+ index++;
2215
+ inProgress++;
2216
+ try {
2217
+ const child = unsafeFork(parent, f(item, currentIndex), true, true);
2218
+ fibers.add(child);
2219
+ child.addObserver(exit => {
2220
+ fibers.delete(child);
2221
+ if (interrupted) {
2222
+ return;
2223
+ } else if (exit._tag === "Failure") {
2224
+ if (result === undefined) {
2225
+ result = exit;
2226
+ length = index;
2227
+ fibers.forEach(fiber => fiber.unsafeInterrupt());
2228
+ }
2229
+ } else if (out !== undefined) {
2230
+ out[currentIndex] = exit.value;
2090
2231
  }
2091
- } else if (out !== undefined) {
2092
- out[currentIndex] = exit.right;
2093
- }
2094
- doneCount++;
2095
- inProgress--;
2096
- if (doneCount === length) {
2097
- onExit(result ?? Either.right(out));
2098
- } else if (!pumping && inProgress < concurrency) {
2099
- pump();
2100
- }
2101
- });
2102
- } catch (err) {
2103
- result = exitDie(err);
2104
- length = index;
2105
- onAbort();
2232
+ doneCount++;
2233
+ inProgress--;
2234
+ if (doneCount === length) {
2235
+ resume(result ?? succeed(out));
2236
+ } else if (!pumping && inProgress < concurrency) {
2237
+ pump();
2238
+ }
2239
+ });
2240
+ } catch (err) {
2241
+ result = exitDie(err);
2242
+ length = index;
2243
+ fibers.forEach(fiber => fiber.unsafeInterrupt());
2244
+ }
2106
2245
  }
2246
+ pumping = false;
2107
2247
  }
2108
- pumping = false;
2109
- }
2110
- pump();
2248
+ pump();
2249
+ return suspend(() => {
2250
+ interrupted = true;
2251
+ index = length;
2252
+ return fiberInterruptAll(fibers);
2253
+ });
2254
+ });
2111
2255
  });
2112
2256
  /**
2113
2257
  * Effectfully filter the elements of the provided iterable.
@@ -2174,93 +2318,8 @@ const bindTo = exports.bindTo = /*#__PURE__*/doNotation.bindTo(map);
2174
2318
  const bind = exports.bind = /*#__PURE__*/doNotation.bind(map, flatMap);
2175
2319
  const let_ = exports.let = /*#__PURE__*/doNotation.let_(map);
2176
2320
  // ----------------------------------------------------------------------------
2177
- // handle & forking
2321
+ // fibers & forking
2178
2322
  // ----------------------------------------------------------------------------
2179
- /**
2180
- * @since 3.4.0
2181
- * @experimental
2182
- * @category handle & forking
2183
- */
2184
- const HandleTypeId = exports.HandleTypeId = /*#__PURE__*/Symbol.for("effect/Micro/Handle");
2185
- /**
2186
- * @since 3.4.0
2187
- * @experimental
2188
- * @category handle & forking
2189
- */
2190
- const isHandle = u => typeof u === "object" && u !== null && HandleTypeId in u;
2191
- exports.isHandle = isHandle;
2192
- class HandleImpl extends Class {
2193
- parentSignal;
2194
- [HandleTypeId];
2195
- observers = /*#__PURE__*/new Set();
2196
- _exit = undefined;
2197
- _controller;
2198
- isRoot;
2199
- constructor(parentSignal, controller) {
2200
- super();
2201
- this.parentSignal = parentSignal;
2202
- this[HandleTypeId] = HandleTypeId;
2203
- this.isRoot = controller !== undefined;
2204
- this._controller = controller ?? new AbortController();
2205
- if (!this.isRoot) {
2206
- parentSignal.addEventListener("abort", this.unsafeInterrupt);
2207
- }
2208
- }
2209
- unsafePoll() {
2210
- return this._exit ?? null;
2211
- }
2212
- unsafeInterrupt = () => {
2213
- this._controller.abort();
2214
- };
2215
- emit(exit) {
2216
- if (this._exit) {
2217
- return;
2218
- }
2219
- this._exit = exit;
2220
- if (!this.isRoot) {
2221
- this.parentSignal.removeEventListener("abort", this.unsafeInterrupt);
2222
- }
2223
- this.observers.forEach(observer => observer(exit));
2224
- this.observers.clear();
2225
- }
2226
- addObserver(observer) {
2227
- if (this._exit) {
2228
- return observer(this._exit);
2229
- }
2230
- this.observers.add(observer);
2231
- }
2232
- removeObserver(observer) {
2233
- this.observers.delete(observer);
2234
- }
2235
- get await() {
2236
- return suspend(() => {
2237
- if (this._exit) {
2238
- return succeed(this._exit);
2239
- }
2240
- return async(resume => {
2241
- function observer(exit) {
2242
- resume(succeed(exit));
2243
- }
2244
- this.addObserver(observer);
2245
- return sync(() => {
2246
- this.removeObserver(observer);
2247
- });
2248
- });
2249
- });
2250
- }
2251
- get join() {
2252
- return flatMap(this.await, fromExit);
2253
- }
2254
- get interrupt() {
2255
- return suspend(() => {
2256
- this.unsafeInterrupt();
2257
- return this.await;
2258
- });
2259
- }
2260
- asMicro() {
2261
- return this.join;
2262
- }
2263
- }
2264
2323
  /**
2265
2324
  * Run the `Micro` effect in a new `Handle` that can be awaited, joined, or
2266
2325
  * aborted.
@@ -2271,21 +2330,24 @@ class HandleImpl extends Class {
2271
2330
  * @experimental
2272
2331
  * @category handle & forking
2273
2332
  */
2274
- const fork = self => make(function (env, onExit) {
2275
- const signal = envGet(env, currentAbortSignal);
2276
- const handle = new HandleImpl(signal);
2277
- const nextEnv = envMutate(env, map => {
2278
- map[currentAbortController.key] = handle._controller;
2279
- map[currentAbortSignal.key] = handle._controller.signal;
2280
- return map;
2281
- });
2282
- envGet(env, currentScheduler).scheduleTask(() => {
2283
- self[runSymbol](nextEnv, exit => {
2284
- handle.emit(exit);
2285
- });
2286
- }, 0);
2287
- onExit(Either.right(handle));
2333
+ const fork = self => withFiber(fiber => {
2334
+ fiberMiddleware.interruptChildren ??= fiberInterruptChildren;
2335
+ return succeed(unsafeFork(fiber, self));
2288
2336
  });
2337
+ exports.fork = fork;
2338
+ const unsafeFork = (parent, effect, immediate = false, daemon = false) => {
2339
+ const child = new FiberImpl(parent.context, parent.interruptible);
2340
+ if (!daemon) {
2341
+ parent.children().add(child);
2342
+ child.addObserver(() => parent.children().delete(child));
2343
+ }
2344
+ if (immediate) {
2345
+ child.evaluate(effect);
2346
+ } else {
2347
+ parent.getRef(CurrentScheduler).scheduleTask(() => child.evaluate(effect), 0);
2348
+ }
2349
+ return child;
2350
+ };
2289
2351
  /**
2290
2352
  * Run the `Micro` effect in a new `Handle` that can be awaited, joined, or
2291
2353
  * aborted.
@@ -2296,22 +2358,7 @@ const fork = self => make(function (env, onExit) {
2296
2358
  * @experimental
2297
2359
  * @category handle & forking
2298
2360
  */
2299
- exports.fork = fork;
2300
- const forkDaemon = self => make(function (env, onExit) {
2301
- const controller = new AbortController();
2302
- const handle = new HandleImpl(controller.signal, controller);
2303
- const nextEnv = envMutate(env, map => {
2304
- map[currentAbortController.key] = controller;
2305
- map[currentAbortSignal.key] = controller.signal;
2306
- return map;
2307
- });
2308
- envGet(env, currentScheduler).scheduleTask(() => {
2309
- self[runSymbol](nextEnv, exit => {
2310
- handle.emit(exit);
2311
- });
2312
- }, 0);
2313
- onExit(Either.right(handle));
2314
- });
2361
+ const forkDaemon = self => withFiber(fiber => succeed(unsafeFork(fiber, self, false, true)));
2315
2362
  /**
2316
2363
  * Run the `Micro` effect in a new `Handle` that can be awaited, joined, or
2317
2364
  * aborted.
@@ -2323,7 +2370,7 @@ const forkDaemon = self => make(function (env, onExit) {
2323
2370
  * @category handle & forking
2324
2371
  */
2325
2372
  exports.forkDaemon = forkDaemon;
2326
- const forkIn = exports.forkIn = /*#__PURE__*/(0, _Function.dual)(2, (self, scope) => uninterruptibleMask(restore => flatMap(scope.fork, scope => tap(restore(forkDaemon(onExit(self, exit => scope.close(exit)))), fiber => scope.addFinalizer(_ => asVoid(fiber.interrupt))))));
2373
+ const forkIn = exports.forkIn = /*#__PURE__*/(0, _Function.dual)(2, (self, scope) => uninterruptibleMask(restore => flatMap(scope.fork, scope => tap(restore(forkDaemon(onExit(self, exit => scope.close(exit)))), fiber => scope.addFinalizer(_ => fiberInterrupt(fiber))))));
2327
2374
  /**
2328
2375
  * Run the `Micro` effect in a new `Handle` that can be awaited, joined, or
2329
2376
  * aborted.
@@ -2349,6 +2396,7 @@ const forkScoped = self => flatMap(scope, scope => forkIn(self, scope));
2349
2396
  * @experimental
2350
2397
  * @category execution
2351
2398
  * @example
2399
+ * ```ts
2352
2400
  * import * as Micro from "effect/Micro"
2353
2401
  *
2354
2402
  * const handle = Micro.succeed(42).pipe(
@@ -2359,32 +2407,24 @@ const forkScoped = self => flatMap(scope, scope => forkIn(self, scope));
2359
2407
  * handle.addObserver((exit) => {
2360
2408
  * console.log(exit)
2361
2409
  * })
2410
+ * ```
2362
2411
  */
2363
2412
  exports.forkScoped = forkScoped;
2364
2413
  const runFork = (effect, options) => {
2365
- const controller = new AbortController();
2366
- const refs = Object.create(null);
2367
- refs[currentAbortController.key] = controller;
2368
- refs[currentAbortSignal.key] = controller.signal;
2369
- refs[currentScheduler.key] = options?.scheduler ?? new MicroSchedulerDefault();
2370
- const env = envMake(refs);
2371
- const handle = new HandleImpl(controller.signal, controller);
2372
- effect[runSymbol](envSet(env, currentAbortSignal, handle._controller.signal), exit => {
2373
- handle.emit(exit);
2374
- if (options?.signal) {
2375
- options.signal.removeEventListener("abort", handle.unsafeInterrupt);
2376
- }
2377
- });
2414
+ const fiber = new FiberImpl(CurrentScheduler.context(options?.scheduler ?? new MicroSchedulerDefault()));
2415
+ fiber.evaluate(effect);
2378
2416
  if (options?.signal) {
2379
2417
  if (options.signal.aborted) {
2380
- handle.unsafeInterrupt();
2418
+ fiber.unsafeInterrupt();
2381
2419
  } else {
2382
- options.signal.addEventListener("abort", handle.unsafeInterrupt, {
2420
+ const abort = () => fiber.unsafeInterrupt();
2421
+ options.signal.addEventListener("abort", abort, {
2383
2422
  once: true
2384
2423
  });
2424
+ fiber.addObserver(() => options.signal.removeEventListener("abort", abort));
2385
2425
  }
2386
2426
  }
2387
- return handle;
2427
+ return fiber;
2388
2428
  };
2389
2429
  /**
2390
2430
  * Execute the `Micro` effect and return a `Promise` that resolves with the
@@ -2409,10 +2449,10 @@ const runPromiseExit = (effect, options) => new Promise((resolve, _reject) => {
2409
2449
  */
2410
2450
  exports.runPromiseExit = runPromiseExit;
2411
2451
  const runPromise = (effect, options) => runPromiseExit(effect, options).then(exit => {
2412
- if (exit._tag === "Left") {
2413
- throw exit.left;
2452
+ if (exit._tag === "Failure") {
2453
+ throw exit.cause;
2414
2454
  }
2415
- return exit.right;
2455
+ return exit.value;
2416
2456
  });
2417
2457
  /**
2418
2458
  * Attempt to execute the `Micro` effect synchronously and return the `MicroExit`.
@@ -2427,15 +2467,11 @@ const runPromise = (effect, options) => runPromiseExit(effect, options).then(exi
2427
2467
  exports.runPromise = runPromise;
2428
2468
  const runSyncExit = effect => {
2429
2469
  const scheduler = new MicroSchedulerDefault();
2430
- const handle = runFork(effect, {
2470
+ const fiber = runFork(effect, {
2431
2471
  scheduler
2432
2472
  });
2433
2473
  scheduler.flush();
2434
- const exit = handle.unsafePoll();
2435
- if (exit === null) {
2436
- return exitDie(handle);
2437
- }
2438
- return exit;
2474
+ return fiber._exit ?? exitDie(fiber);
2439
2475
  };
2440
2476
  /**
2441
2477
  * Attempt to execute the `Micro` effect synchronously and return the success
@@ -2448,25 +2484,25 @@ const runSyncExit = effect => {
2448
2484
  exports.runSyncExit = runSyncExit;
2449
2485
  const runSync = effect => {
2450
2486
  const exit = runSyncExit(effect);
2451
- if (exit._tag === "Left") {
2452
- throw exit.left;
2453
- }
2454
- return exit.right;
2487
+ if (exit._tag === "Failure") throw exit.cause;
2488
+ return exit.value;
2455
2489
  };
2456
2490
  exports.runSync = runSync;
2457
2491
  const YieldableError = /*#__PURE__*/function () {
2458
- class YieldableError extends globalThis.Error {
2459
- [runSymbol](_env, onExit) {
2460
- onExit(exitFail(this));
2461
- }
2492
+ class YieldableError extends globalThis.Error {}
2493
+ Object.assign(YieldableError.prototype, MicroProto, _effectable.StructuralPrototype, {
2494
+ [identifier]: "Failure",
2495
+ [evaluate]() {
2496
+ return fail(this);
2497
+ },
2462
2498
  toString() {
2463
2499
  return this.message ? `${this.name}: ${this.message}` : this.name;
2464
- }
2500
+ },
2465
2501
  toJSON() {
2466
2502
  return {
2467
2503
  ...this
2468
2504
  };
2469
- }
2505
+ },
2470
2506
  [_Inspectable.NodeInspectSymbol]() {
2471
2507
  const stack = this.stack;
2472
2508
  if (stack) {
@@ -2474,8 +2510,7 @@ const YieldableError = /*#__PURE__*/function () {
2474
2510
  }
2475
2511
  return this.toString();
2476
2512
  }
2477
- }
2478
- Object.assign(YieldableError.prototype, MicroProto, _effectable.StructuralPrototype);
2513
+ });
2479
2514
  return YieldableError;
2480
2515
  }();
2481
2516
  /**