@clayroach/effect 3.19.14-source-capture.8 → 3.19.14-source-trace.2

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 (452) hide show
  1. package/SourceLocation/package.json +6 -0
  2. package/dist/cjs/Effect.js +2 -28
  3. package/dist/cjs/Effect.js.map +1 -1
  4. package/dist/cjs/FiberRef.js +12 -1
  5. package/dist/cjs/FiberRef.js.map +1 -1
  6. package/dist/cjs/Layer.js +2 -24
  7. package/dist/cjs/Layer.js.map +1 -1
  8. package/dist/cjs/RuntimeFlags.js +1 -29
  9. package/dist/cjs/RuntimeFlags.js.map +1 -1
  10. package/dist/cjs/SourceLocation.js +60 -0
  11. package/dist/cjs/SourceLocation.js.map +1 -0
  12. package/dist/cjs/Tracer.js +1 -15
  13. package/dist/cjs/Tracer.js.map +1 -1
  14. package/dist/cjs/Utils.js +1 -1
  15. package/dist/cjs/Utils.js.map +1 -1
  16. package/dist/cjs/index.js +3 -1
  17. package/dist/cjs/index.js.map +1 -1
  18. package/dist/cjs/internal/clock.js +1 -1
  19. package/dist/cjs/internal/clock.js.map +1 -1
  20. package/dist/cjs/internal/core.js +17 -50
  21. package/dist/cjs/internal/core.js.map +1 -1
  22. package/dist/cjs/internal/effect/circular.js +18 -30
  23. package/dist/cjs/internal/effect/circular.js.map +1 -1
  24. package/dist/cjs/internal/fiberRuntime.js +16 -65
  25. package/dist/cjs/internal/fiberRuntime.js.map +1 -1
  26. package/dist/cjs/internal/layer/circular.js +1 -5
  27. package/dist/cjs/internal/layer/circular.js.map +1 -1
  28. package/dist/cjs/internal/layer.js +1 -3
  29. package/dist/cjs/internal/layer.js.map +1 -1
  30. package/dist/cjs/internal/logger.js +25 -2
  31. package/dist/cjs/internal/logger.js.map +1 -1
  32. package/dist/cjs/internal/runtimeFlags.js +2 -11
  33. package/dist/cjs/internal/runtimeFlags.js.map +1 -1
  34. package/dist/cjs/internal/tracer.js +1 -114
  35. package/dist/cjs/internal/tracer.js.map +1 -1
  36. package/dist/dts/Config.d.ts +2 -2
  37. package/dist/dts/Config.d.ts.map +1 -1
  38. package/dist/dts/Effect.d.ts +8 -29
  39. package/dist/dts/Effect.d.ts.map +1 -1
  40. package/dist/dts/FiberRef.d.ts +12 -0
  41. package/dist/dts/FiberRef.d.ts.map +1 -1
  42. package/dist/dts/Layer.d.ts +0 -22
  43. package/dist/dts/Layer.d.ts.map +1 -1
  44. package/dist/dts/RuntimeFlags.d.ts +0 -28
  45. package/dist/dts/RuntimeFlags.d.ts.map +1 -1
  46. package/dist/dts/SourceLocation.d.ts +88 -0
  47. package/dist/dts/SourceLocation.d.ts.map +1 -0
  48. package/dist/dts/Tracer.d.ts +0 -15
  49. package/dist/dts/Tracer.d.ts.map +1 -1
  50. package/dist/dts/index.d.ts +6 -0
  51. package/dist/dts/index.d.ts.map +1 -1
  52. package/dist/dts/internal/core.d.ts.map +1 -1
  53. package/dist/dts/internal/layer.d.ts.map +1 -1
  54. package/dist/dts/internal/runtimeFlags.d.ts.map +1 -1
  55. package/dist/esm/Effect.js +0 -26
  56. package/dist/esm/Effect.js.map +1 -1
  57. package/dist/esm/FiberRef.js +11 -0
  58. package/dist/esm/FiberRef.js.map +1 -1
  59. package/dist/esm/Layer.js +0 -22
  60. package/dist/esm/Layer.js.map +1 -1
  61. package/dist/esm/RuntimeFlags.js +0 -28
  62. package/dist/esm/RuntimeFlags.js.map +1 -1
  63. package/dist/esm/SourceLocation.js +51 -0
  64. package/dist/esm/SourceLocation.js.map +1 -0
  65. package/dist/esm/Tracer.js +0 -14
  66. package/dist/esm/Tracer.js.map +1 -1
  67. package/dist/esm/Utils.js +1 -1
  68. package/dist/esm/Utils.js.map +1 -1
  69. package/dist/esm/index.js +6 -0
  70. package/dist/esm/index.js.map +1 -1
  71. package/dist/esm/internal/clock.js +1 -1
  72. package/dist/esm/internal/clock.js.map +1 -1
  73. package/dist/esm/internal/core.js +12 -45
  74. package/dist/esm/internal/core.js.map +1 -1
  75. package/dist/esm/internal/effect/circular.js +18 -30
  76. package/dist/esm/internal/effect/circular.js.map +1 -1
  77. package/dist/esm/internal/fiberRuntime.js +13 -60
  78. package/dist/esm/internal/fiberRuntime.js.map +1 -1
  79. package/dist/esm/internal/layer/circular.js +0 -4
  80. package/dist/esm/internal/layer/circular.js.map +1 -1
  81. package/dist/esm/internal/layer.js +0 -2
  82. package/dist/esm/internal/layer.js.map +1 -1
  83. package/dist/esm/internal/logger.js +25 -2
  84. package/dist/esm/internal/logger.js.map +1 -1
  85. package/dist/esm/internal/runtimeFlags.js +1 -9
  86. package/dist/esm/internal/runtimeFlags.js.map +1 -1
  87. package/dist/esm/internal/tracer.js +0 -111
  88. package/dist/esm/internal/tracer.js.map +1 -1
  89. package/package.json +12 -1
  90. package/src/Arbitrary.ts +1101 -0
  91. package/src/Array.ts +3589 -0
  92. package/src/BigDecimal.ts +1349 -0
  93. package/src/BigInt.ts +643 -0
  94. package/src/Boolean.ts +287 -0
  95. package/src/Brand.ts +360 -0
  96. package/src/Cache.ts +281 -0
  97. package/src/Cause.ts +1555 -0
  98. package/src/Channel.ts +2355 -0
  99. package/src/ChildExecutorDecision.ts +146 -0
  100. package/src/Chunk.ts +1495 -0
  101. package/src/Clock.ts +111 -0
  102. package/src/Config.ts +542 -0
  103. package/src/ConfigError.ts +270 -0
  104. package/src/ConfigProvider.ts +333 -0
  105. package/src/ConfigProviderPathPatch.ts +100 -0
  106. package/src/Console.ts +226 -0
  107. package/src/Context.ts +585 -0
  108. package/src/Cron.ts +706 -0
  109. package/src/Data.ts +596 -0
  110. package/src/DateTime.ts +1686 -0
  111. package/src/DefaultServices.ts +34 -0
  112. package/src/Deferred.ts +301 -0
  113. package/src/Differ.ts +450 -0
  114. package/src/Duration.ts +1000 -0
  115. package/src/Effect.ts +14817 -0
  116. package/src/Effectable.ts +107 -0
  117. package/src/Either.ts +1040 -0
  118. package/src/Encoding.ts +195 -0
  119. package/src/Equal.ts +98 -0
  120. package/src/Equivalence.ts +235 -0
  121. package/src/ExecutionPlan.ts +308 -0
  122. package/src/ExecutionStrategy.ts +119 -0
  123. package/src/Exit.ts +467 -0
  124. package/src/FastCheck.ts +9 -0
  125. package/src/Fiber.ts +744 -0
  126. package/src/FiberHandle.ts +540 -0
  127. package/src/FiberId.ts +195 -0
  128. package/src/FiberMap.ts +656 -0
  129. package/src/FiberRef.ts +444 -0
  130. package/src/FiberRefs.ts +204 -0
  131. package/src/FiberRefsPatch.ts +105 -0
  132. package/src/FiberSet.ts +491 -0
  133. package/src/FiberStatus.ts +108 -0
  134. package/src/Function.ts +1222 -0
  135. package/src/GlobalValue.ts +53 -0
  136. package/src/Graph.ts +3732 -0
  137. package/src/GroupBy.ts +103 -0
  138. package/src/HKT.ts +45 -0
  139. package/src/Hash.ts +195 -0
  140. package/src/HashMap.ts +519 -0
  141. package/src/HashRing.ts +317 -0
  142. package/src/HashSet.ts +2346 -0
  143. package/src/Inspectable.ts +287 -0
  144. package/src/Iterable.ts +1119 -0
  145. package/src/JSONSchema.ts +1044 -0
  146. package/src/KeyedPool.ts +167 -0
  147. package/src/Layer.ts +1228 -0
  148. package/src/LayerMap.ts +436 -0
  149. package/src/List.ts +977 -0
  150. package/src/LogLevel.ts +285 -0
  151. package/src/LogSpan.ts +25 -0
  152. package/src/Logger.ts +702 -0
  153. package/src/Mailbox.ts +268 -0
  154. package/src/ManagedRuntime.ts +180 -0
  155. package/src/Match.ts +1477 -0
  156. package/src/MergeDecision.ts +95 -0
  157. package/src/MergeState.ts +172 -0
  158. package/src/MergeStrategy.ts +107 -0
  159. package/src/Metric.ts +780 -0
  160. package/src/MetricBoundaries.ts +69 -0
  161. package/src/MetricHook.ts +151 -0
  162. package/src/MetricKey.ts +224 -0
  163. package/src/MetricKeyType.ts +262 -0
  164. package/src/MetricLabel.ts +47 -0
  165. package/src/MetricPair.ts +71 -0
  166. package/src/MetricPolling.ts +148 -0
  167. package/src/MetricRegistry.ts +48 -0
  168. package/src/MetricState.ts +257 -0
  169. package/src/Micro.ts +4405 -0
  170. package/src/ModuleVersion.ts +18 -0
  171. package/src/MutableHashMap.ts +411 -0
  172. package/src/MutableHashSet.ts +706 -0
  173. package/src/MutableList.ts +297 -0
  174. package/src/MutableQueue.ts +227 -0
  175. package/src/MutableRef.ts +202 -0
  176. package/src/NonEmptyIterable.ts +32 -0
  177. package/src/Number.ts +1071 -0
  178. package/src/Option.ts +2170 -0
  179. package/src/Order.ts +373 -0
  180. package/src/Ordering.ts +111 -0
  181. package/src/ParseResult.ts +2031 -0
  182. package/src/PartitionedSemaphore.ts +200 -0
  183. package/src/Pipeable.ts +566 -0
  184. package/src/Pool.ts +204 -0
  185. package/src/Predicate.ts +1405 -0
  186. package/src/Pretty.ts +205 -0
  187. package/src/PrimaryKey.ts +23 -0
  188. package/src/PubSub.ts +182 -0
  189. package/src/Queue.ts +644 -0
  190. package/src/Random.ts +204 -0
  191. package/src/RateLimiter.ts +138 -0
  192. package/src/RcMap.ts +141 -0
  193. package/src/RcRef.ts +122 -0
  194. package/src/Readable.ts +93 -0
  195. package/src/Record.ts +1274 -0
  196. package/src/RedBlackTree.ts +421 -0
  197. package/src/Redacted.ts +144 -0
  198. package/src/Ref.ts +180 -0
  199. package/src/RegExp.ts +38 -0
  200. package/src/Reloadable.ts +127 -0
  201. package/src/Request.ts +347 -0
  202. package/src/RequestBlock.ts +118 -0
  203. package/src/RequestResolver.ts +366 -0
  204. package/src/Resource.ts +119 -0
  205. package/src/Runtime.ts +383 -0
  206. package/src/RuntimeFlags.ts +336 -0
  207. package/src/RuntimeFlagsPatch.ts +183 -0
  208. package/src/STM.ts +2045 -0
  209. package/src/Schedule.ts +2219 -0
  210. package/src/ScheduleDecision.ts +62 -0
  211. package/src/ScheduleInterval.ts +151 -0
  212. package/src/ScheduleIntervals.ts +122 -0
  213. package/src/Scheduler.ts +353 -0
  214. package/src/Schema.ts +10914 -0
  215. package/src/SchemaAST.ts +3043 -0
  216. package/src/Scope.ts +204 -0
  217. package/src/ScopedCache.ts +151 -0
  218. package/src/ScopedRef.ts +117 -0
  219. package/src/Secret.ts +88 -0
  220. package/src/SingleProducerAsyncInput.ts +67 -0
  221. package/src/Sink.ts +1461 -0
  222. package/src/SortedMap.ts +287 -0
  223. package/src/SortedSet.ts +390 -0
  224. package/src/SourceLocation.ts +108 -0
  225. package/src/Stream.ts +6468 -0
  226. package/src/StreamEmit.ts +136 -0
  227. package/src/StreamHaltStrategy.ts +123 -0
  228. package/src/Streamable.ts +45 -0
  229. package/src/String.ts +778 -0
  230. package/src/Struct.ts +243 -0
  231. package/src/Subscribable.ts +100 -0
  232. package/src/SubscriptionRef.ts +298 -0
  233. package/src/Supervisor.ts +240 -0
  234. package/src/Symbol.ts +29 -0
  235. package/src/SynchronizedRef.ts +270 -0
  236. package/src/TArray.ts +495 -0
  237. package/src/TDeferred.ts +100 -0
  238. package/src/TMap.ts +515 -0
  239. package/src/TPriorityQueue.ts +223 -0
  240. package/src/TPubSub.ts +200 -0
  241. package/src/TQueue.ts +432 -0
  242. package/src/TRandom.ts +129 -0
  243. package/src/TReentrantLock.ts +224 -0
  244. package/src/TRef.ts +178 -0
  245. package/src/TSemaphore.ts +129 -0
  246. package/src/TSet.ts +365 -0
  247. package/src/TSubscriptionRef.ts +192 -0
  248. package/src/Take.ts +258 -0
  249. package/src/TestAnnotation.ts +158 -0
  250. package/src/TestAnnotationMap.ts +119 -0
  251. package/src/TestAnnotations.ts +117 -0
  252. package/src/TestClock.ts +556 -0
  253. package/src/TestConfig.ts +47 -0
  254. package/src/TestContext.ts +36 -0
  255. package/src/TestLive.ts +53 -0
  256. package/src/TestServices.ts +390 -0
  257. package/src/TestSized.ts +55 -0
  258. package/src/Tracer.ts +182 -0
  259. package/src/Trie.ts +840 -0
  260. package/src/Tuple.ts +305 -0
  261. package/src/Types.ts +353 -0
  262. package/src/Unify.ts +113 -0
  263. package/src/UpstreamPullRequest.ts +117 -0
  264. package/src/UpstreamPullStrategy.ts +121 -0
  265. package/src/Utils.ts +809 -0
  266. package/src/index.ts +1568 -0
  267. package/src/internal/array.ts +8 -0
  268. package/src/internal/blockedRequests.ts +520 -0
  269. package/src/internal/cache.ts +733 -0
  270. package/src/internal/cause.ts +1050 -0
  271. package/src/internal/channel/channelExecutor.ts +1200 -0
  272. package/src/internal/channel/channelState.ts +134 -0
  273. package/src/internal/channel/childExecutorDecision.ts +96 -0
  274. package/src/internal/channel/continuation.ts +200 -0
  275. package/src/internal/channel/mergeDecision.ts +113 -0
  276. package/src/internal/channel/mergeState.ts +120 -0
  277. package/src/internal/channel/mergeStrategy.ts +72 -0
  278. package/src/internal/channel/singleProducerAsyncInput.ts +259 -0
  279. package/src/internal/channel/subexecutor.ts +229 -0
  280. package/src/internal/channel/upstreamPullRequest.ts +84 -0
  281. package/src/internal/channel/upstreamPullStrategy.ts +87 -0
  282. package/src/internal/channel.ts +2603 -0
  283. package/src/internal/clock.ts +95 -0
  284. package/src/internal/completedRequestMap.ts +9 -0
  285. package/src/internal/concurrency.ts +54 -0
  286. package/src/internal/config.ts +716 -0
  287. package/src/internal/configError.ts +304 -0
  288. package/src/internal/configProvider/pathPatch.ts +97 -0
  289. package/src/internal/configProvider.ts +799 -0
  290. package/src/internal/console.ts +153 -0
  291. package/src/internal/context.ts +337 -0
  292. package/src/internal/core-effect.ts +2293 -0
  293. package/src/internal/core-stream.ts +998 -0
  294. package/src/internal/core.ts +3189 -0
  295. package/src/internal/data.ts +36 -0
  296. package/src/internal/dataSource.ts +327 -0
  297. package/src/internal/dateTime.ts +1277 -0
  298. package/src/internal/defaultServices/console.ts +100 -0
  299. package/src/internal/defaultServices.ts +163 -0
  300. package/src/internal/deferred.ts +46 -0
  301. package/src/internal/differ/chunkPatch.ts +211 -0
  302. package/src/internal/differ/contextPatch.ts +232 -0
  303. package/src/internal/differ/hashMapPatch.ts +220 -0
  304. package/src/internal/differ/hashSetPatch.ts +176 -0
  305. package/src/internal/differ/orPatch.ts +311 -0
  306. package/src/internal/differ/readonlyArrayPatch.ts +210 -0
  307. package/src/internal/differ.ts +200 -0
  308. package/src/internal/doNotation.ts +80 -0
  309. package/src/internal/effect/circular.ts +895 -0
  310. package/src/internal/effectable.ts +131 -0
  311. package/src/internal/either.ts +110 -0
  312. package/src/internal/encoding/base64.ts +286 -0
  313. package/src/internal/encoding/base64Url.ts +29 -0
  314. package/src/internal/encoding/common.ts +51 -0
  315. package/src/internal/encoding/hex.ts +315 -0
  316. package/src/internal/errors.ts +7 -0
  317. package/src/internal/executionPlan.ts +114 -0
  318. package/src/internal/executionStrategy.ts +74 -0
  319. package/src/internal/fiber.ts +388 -0
  320. package/src/internal/fiberId.ts +267 -0
  321. package/src/internal/fiberMessage.ts +82 -0
  322. package/src/internal/fiberRefs/patch.ts +144 -0
  323. package/src/internal/fiberRefs.ts +297 -0
  324. package/src/internal/fiberRuntime.ts +3842 -0
  325. package/src/internal/fiberScope.ts +71 -0
  326. package/src/internal/fiberStatus.ts +119 -0
  327. package/src/internal/groupBy.ts +530 -0
  328. package/src/internal/hashMap/array.ts +49 -0
  329. package/src/internal/hashMap/bitwise.ts +32 -0
  330. package/src/internal/hashMap/config.ts +14 -0
  331. package/src/internal/hashMap/keySet.ts +8 -0
  332. package/src/internal/hashMap/node.ts +391 -0
  333. package/src/internal/hashMap.ts +586 -0
  334. package/src/internal/hashSet.ts +323 -0
  335. package/src/internal/keyedPool.ts +244 -0
  336. package/src/internal/layer/circular.ts +214 -0
  337. package/src/internal/layer.ts +1483 -0
  338. package/src/internal/logSpan.ts +20 -0
  339. package/src/internal/logger-circular.ts +24 -0
  340. package/src/internal/logger.ts +522 -0
  341. package/src/internal/mailbox.ts +561 -0
  342. package/src/internal/managedRuntime/circular.ts +6 -0
  343. package/src/internal/managedRuntime.ts +134 -0
  344. package/src/internal/matcher.ts +652 -0
  345. package/src/internal/metric/boundaries.ts +75 -0
  346. package/src/internal/metric/hook.ts +483 -0
  347. package/src/internal/metric/key.ts +167 -0
  348. package/src/internal/metric/keyType.ts +238 -0
  349. package/src/internal/metric/label.ts +41 -0
  350. package/src/internal/metric/pair.ts +48 -0
  351. package/src/internal/metric/polling.ts +149 -0
  352. package/src/internal/metric/registry.ts +187 -0
  353. package/src/internal/metric/state.ts +290 -0
  354. package/src/internal/metric.ts +577 -0
  355. package/src/internal/opCodes/cause.ts +35 -0
  356. package/src/internal/opCodes/channel.ts +83 -0
  357. package/src/internal/opCodes/channelChildExecutorDecision.ts +17 -0
  358. package/src/internal/opCodes/channelMergeDecision.ts +11 -0
  359. package/src/internal/opCodes/channelMergeState.ts +17 -0
  360. package/src/internal/opCodes/channelMergeStrategy.ts +11 -0
  361. package/src/internal/opCodes/channelState.ts +23 -0
  362. package/src/internal/opCodes/channelUpstreamPullRequest.ts +11 -0
  363. package/src/internal/opCodes/channelUpstreamPullStrategy.ts +11 -0
  364. package/src/internal/opCodes/config.ts +65 -0
  365. package/src/internal/opCodes/configError.ts +35 -0
  366. package/src/internal/opCodes/continuation.ts +11 -0
  367. package/src/internal/opCodes/deferred.ts +11 -0
  368. package/src/internal/opCodes/effect.ts +89 -0
  369. package/src/internal/opCodes/layer.ts +59 -0
  370. package/src/internal/opCodes/streamHaltStrategy.ts +23 -0
  371. package/src/internal/option.ts +80 -0
  372. package/src/internal/pool.ts +432 -0
  373. package/src/internal/pubsub.ts +1762 -0
  374. package/src/internal/query.ts +204 -0
  375. package/src/internal/queue.ts +766 -0
  376. package/src/internal/random.ts +161 -0
  377. package/src/internal/rateLimiter.ts +93 -0
  378. package/src/internal/rcMap.ts +285 -0
  379. package/src/internal/rcRef.ts +192 -0
  380. package/src/internal/redBlackTree/iterator.ts +200 -0
  381. package/src/internal/redBlackTree/node.ts +68 -0
  382. package/src/internal/redBlackTree.ts +1245 -0
  383. package/src/internal/redacted.ts +73 -0
  384. package/src/internal/ref.ts +171 -0
  385. package/src/internal/reloadable.ts +140 -0
  386. package/src/internal/request.ts +177 -0
  387. package/src/internal/resource.ts +76 -0
  388. package/src/internal/ringBuffer.ts +68 -0
  389. package/src/internal/runtime.ts +558 -0
  390. package/src/internal/runtimeFlags.ts +178 -0
  391. package/src/internal/runtimeFlagsPatch.ts +103 -0
  392. package/src/internal/schedule/decision.ts +47 -0
  393. package/src/internal/schedule/interval.ts +101 -0
  394. package/src/internal/schedule/intervals.ts +180 -0
  395. package/src/internal/schedule.ts +2199 -0
  396. package/src/internal/schema/errors.ts +191 -0
  397. package/src/internal/schema/schemaId.ts +106 -0
  398. package/src/internal/schema/util.ts +50 -0
  399. package/src/internal/scopedCache.ts +644 -0
  400. package/src/internal/scopedRef.ts +118 -0
  401. package/src/internal/secret.ts +89 -0
  402. package/src/internal/singleShotGen.ts +35 -0
  403. package/src/internal/sink.ts +2120 -0
  404. package/src/internal/stack.ts +10 -0
  405. package/src/internal/stm/core.ts +817 -0
  406. package/src/internal/stm/entry.ts +59 -0
  407. package/src/internal/stm/journal.ts +123 -0
  408. package/src/internal/stm/opCodes/stm.ts +71 -0
  409. package/src/internal/stm/opCodes/stmState.ts +17 -0
  410. package/src/internal/stm/opCodes/strategy.ts +17 -0
  411. package/src/internal/stm/opCodes/tExit.ts +29 -0
  412. package/src/internal/stm/opCodes/tryCommit.ts +11 -0
  413. package/src/internal/stm/stm.ts +1453 -0
  414. package/src/internal/stm/stmState.ts +136 -0
  415. package/src/internal/stm/tArray.ts +550 -0
  416. package/src/internal/stm/tDeferred.ts +81 -0
  417. package/src/internal/stm/tExit.ts +190 -0
  418. package/src/internal/stm/tMap.ts +824 -0
  419. package/src/internal/stm/tPriorityQueue.ts +267 -0
  420. package/src/internal/stm/tPubSub.ts +551 -0
  421. package/src/internal/stm/tQueue.ts +393 -0
  422. package/src/internal/stm/tRandom.ts +140 -0
  423. package/src/internal/stm/tReentrantLock.ts +352 -0
  424. package/src/internal/stm/tRef.ts +195 -0
  425. package/src/internal/stm/tSemaphore.ts +113 -0
  426. package/src/internal/stm/tSet.ts +259 -0
  427. package/src/internal/stm/tSubscriptionRef.ts +286 -0
  428. package/src/internal/stm/tryCommit.ts +34 -0
  429. package/src/internal/stm/txnId.ts +14 -0
  430. package/src/internal/stm/versioned.ts +4 -0
  431. package/src/internal/stream/debounceState.ts +57 -0
  432. package/src/internal/stream/emit.ts +123 -0
  433. package/src/internal/stream/haltStrategy.ts +94 -0
  434. package/src/internal/stream/handoff.ts +187 -0
  435. package/src/internal/stream/handoffSignal.ts +59 -0
  436. package/src/internal/stream/pull.ts +34 -0
  437. package/src/internal/stream/sinkEndReason.ts +30 -0
  438. package/src/internal/stream/zipAllState.ts +88 -0
  439. package/src/internal/stream/zipChunksState.ts +56 -0
  440. package/src/internal/stream.ts +8801 -0
  441. package/src/internal/string-utils.ts +107 -0
  442. package/src/internal/subscriptionRef.ts +138 -0
  443. package/src/internal/supervisor/patch.ts +190 -0
  444. package/src/internal/supervisor.ts +303 -0
  445. package/src/internal/synchronizedRef.ts +114 -0
  446. package/src/internal/take.ts +199 -0
  447. package/src/internal/testing/sleep.ts +27 -0
  448. package/src/internal/testing/suspendedWarningData.ts +85 -0
  449. package/src/internal/testing/warningData.ts +94 -0
  450. package/src/internal/tracer.ts +150 -0
  451. package/src/internal/trie.ts +722 -0
  452. package/src/internal/version.ts +7 -0
@@ -0,0 +1,2603 @@
1
+ import * as Cause from "../Cause.js"
2
+ import type * as Channel from "../Channel.js"
3
+ import * as Chunk from "../Chunk.js"
4
+ import * as Context from "../Context.js"
5
+ import * as Deferred from "../Deferred.js"
6
+ import * as Effect from "../Effect.js"
7
+ import * as Either from "../Either.js"
8
+ import * as Equal from "../Equal.js"
9
+ import * as Exit from "../Exit.js"
10
+ import * as Fiber from "../Fiber.js"
11
+ import * as FiberRef from "../FiberRef.js"
12
+ import { constVoid, dual, identity, pipe } from "../Function.js"
13
+ import type { LazyArg } from "../Function.js"
14
+ import * as Layer from "../Layer.js"
15
+ import type * as MergeDecision from "../MergeDecision.js"
16
+ import type * as MergeState from "../MergeState.js"
17
+ import type * as MergeStrategy from "../MergeStrategy.js"
18
+ import * as Option from "../Option.js"
19
+ import { hasProperty, type Predicate } from "../Predicate.js"
20
+ import * as PubSub from "../PubSub.js"
21
+ import * as Queue from "../Queue.js"
22
+ import * as Ref from "../Ref.js"
23
+ import * as Scope from "../Scope.js"
24
+ import type * as SingleProducerAsyncInput from "../SingleProducerAsyncInput.js"
25
+ import type * as Tracer from "../Tracer.js"
26
+ import type * as Types from "../Types.js"
27
+ import * as executor from "./channel/channelExecutor.js"
28
+ import type * as ChannelState from "./channel/channelState.js"
29
+ import * as mergeDecision from "./channel/mergeDecision.js"
30
+ import * as mergeState from "./channel/mergeState.js"
31
+ import * as mergeStrategy_ from "./channel/mergeStrategy.js"
32
+ import * as singleProducerAsyncInput from "./channel/singleProducerAsyncInput.js"
33
+ import * as coreEffect from "./core-effect.js"
34
+ import * as core from "./core-stream.js"
35
+ import * as MergeDecisionOpCodes from "./opCodes/channelMergeDecision.js"
36
+ import * as MergeStateOpCodes from "./opCodes/channelMergeState.js"
37
+ import * as ChannelStateOpCodes from "./opCodes/channelState.js"
38
+ import * as tracer from "./tracer.js"
39
+
40
+ /** @internal */
41
+ export const acquireUseRelease = <Acquired, OutErr, Env, OutElem1, InElem, InErr, OutDone, InDone>(
42
+ acquire: Effect.Effect<Acquired, OutErr, Env>,
43
+ use: (a: Acquired) => Channel.Channel<OutElem1, InElem, OutErr, InErr, OutDone, InDone, Env>,
44
+ release: (a: Acquired, exit: Exit.Exit<OutDone, OutErr>) => Effect.Effect<any, never, Env>
45
+ ): Channel.Channel<OutElem1, InElem, OutErr, InErr, OutDone, InDone, Env> =>
46
+ core.flatMap(
47
+ core.fromEffect(
48
+ Ref.make<
49
+ (exit: Exit.Exit<OutDone, OutErr>) => Effect.Effect<any, never, Env>
50
+ >(() => Effect.void)
51
+ ),
52
+ (ref) =>
53
+ pipe(
54
+ core.fromEffect(
55
+ Effect.uninterruptible(
56
+ Effect.tap(
57
+ acquire,
58
+ (a) => Ref.set(ref, (exit) => release(a, exit))
59
+ )
60
+ )
61
+ ),
62
+ core.flatMap(use),
63
+ core.ensuringWith((exit) => Effect.flatMap(Ref.get(ref), (f) => f(exit)))
64
+ )
65
+ )
66
+
67
+ /** @internal */
68
+ export const as = dual<
69
+ <OutDone2>(
70
+ value: OutDone2
71
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
72
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
73
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone2, InDone, Env>,
74
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutDone2>(
75
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
76
+ value: OutDone2
77
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone2, InDone, Env>
78
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutDone2>(
79
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
80
+ value: OutDone2
81
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone2, InDone, Env> => map(self, () => value))
82
+
83
+ /** @internal */
84
+ export const asVoid = <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
85
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
86
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, void, InDone, Env> => map(self, constVoid)
87
+
88
+ /** @internal */
89
+ export const buffer = <InElem, InErr, InDone>(
90
+ options: {
91
+ readonly empty: InElem
92
+ readonly isEmpty: Predicate<InElem>
93
+ readonly ref: Ref.Ref<InElem>
94
+ }
95
+ ): Channel.Channel<InElem, InElem, InErr, InErr, InDone, InDone> =>
96
+ core.suspend(() => {
97
+ const doBuffer = <InErr, InElem, InDone>(
98
+ empty: InElem,
99
+ isEmpty: Predicate<InElem>,
100
+ ref: Ref.Ref<InElem>
101
+ ): Channel.Channel<InElem, InElem, InErr, InErr, InDone, InDone> =>
102
+ unwrap(
103
+ Ref.modify(ref, (inElem) =>
104
+ isEmpty(inElem) ?
105
+ [
106
+ core.readWith({
107
+ onInput: (input: InElem) =>
108
+ core.flatMap(
109
+ core.write(input),
110
+ () => doBuffer<InErr, InElem, InDone>(empty, isEmpty, ref)
111
+ ),
112
+ onFailure: (error: InErr) => core.fail(error),
113
+ onDone: (done: InDone) => core.succeedNow(done)
114
+ }),
115
+ inElem
116
+ ] as const :
117
+ [
118
+ core.flatMap(
119
+ core.write(inElem),
120
+ () => doBuffer<InErr, InElem, InDone>(empty, isEmpty, ref)
121
+ ),
122
+ empty
123
+ ] as const)
124
+ )
125
+ return doBuffer(options.empty, options.isEmpty, options.ref)
126
+ })
127
+
128
+ /** @internal */
129
+ export const bufferChunk = <InElem, InErr, InDone>(
130
+ ref: Ref.Ref<Chunk.Chunk<InElem>>
131
+ ): Channel.Channel<Chunk.Chunk<InElem>, Chunk.Chunk<InElem>, InErr, InErr, InDone, InDone> =>
132
+ buffer({
133
+ empty: Chunk.empty(),
134
+ isEmpty: Chunk.isEmpty,
135
+ ref
136
+ })
137
+
138
+ /** @internal */
139
+ export const catchAll = dual<
140
+ <OutErr, OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
141
+ f: (error: OutErr) => Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>
142
+ ) => <OutElem, InElem, InErr, OutDone, InDone, Env>(
143
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
144
+ ) => Channel.Channel<
145
+ OutElem1 | OutElem,
146
+ InElem & InElem1,
147
+ OutErr1,
148
+ InErr & InErr1,
149
+ OutDone1 | OutDone,
150
+ InDone & InDone1,
151
+ Env1 | Env
152
+ >,
153
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
154
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
155
+ f: (error: OutErr) => Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>
156
+ ) => Channel.Channel<
157
+ OutElem1 | OutElem,
158
+ InElem & InElem1,
159
+ OutErr1,
160
+ InErr & InErr1,
161
+ OutDone1 | OutDone,
162
+ InDone & InDone1,
163
+ Env1 | Env
164
+ >
165
+ >(
166
+ 2,
167
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
168
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
169
+ f: (error: OutErr) => Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>
170
+ ): Channel.Channel<
171
+ OutElem | OutElem1,
172
+ InElem & InElem1,
173
+ OutErr1,
174
+ InErr & InErr1,
175
+ OutDone | OutDone1,
176
+ InDone & InDone1,
177
+ Env | Env1
178
+ > =>
179
+ core.catchAllCause(self, (cause) =>
180
+ Either.match(Cause.failureOrCause(cause), {
181
+ onLeft: f,
182
+ onRight: core.failCause
183
+ }))
184
+ )
185
+
186
+ /** @internal */
187
+ export const concatMap = dual<
188
+ <OutElem, OutElem2, InElem2, OutErr2, InErr2, X, InDone2, Env2>(
189
+ f: (o: OutElem) => Channel.Channel<OutElem2, InElem2, OutErr2, InErr2, X, InDone2, Env2>
190
+ ) => <Env, InErr, InElem, InDone, OutErr, OutDone>(
191
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
192
+ ) => Channel.Channel<
193
+ OutElem2,
194
+ InElem & InElem2,
195
+ OutErr2 | OutErr,
196
+ InErr & InErr2,
197
+ unknown,
198
+ InDone & InDone2,
199
+ Env2 | Env
200
+ >,
201
+ <Env, InErr, InElem, InDone, OutErr, OutDone, OutElem, OutElem2, Env2, InErr2, InElem2, InDone2, OutErr2, X>(
202
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
203
+ f: (o: OutElem) => Channel.Channel<OutElem2, InElem2, OutErr2, InErr2, X, InDone2, Env2>
204
+ ) => Channel.Channel<
205
+ OutElem2,
206
+ InElem & InElem2,
207
+ OutErr2 | OutErr,
208
+ InErr & InErr2,
209
+ unknown,
210
+ InDone & InDone2,
211
+ Env2 | Env
212
+ >
213
+ >(2, <Env, InErr, InElem, InDone, OutErr, OutDone, OutElem, OutElem2, Env2, InErr2, InElem2, InDone2, OutErr2, X>(
214
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
215
+ f: (o: OutElem) => Channel.Channel<OutElem2, InElem2, OutErr2, InErr2, X, InDone2, Env2>
216
+ ): Channel.Channel<
217
+ OutElem2,
218
+ InElem & InElem2,
219
+ OutErr | OutErr2,
220
+ InErr & InErr2,
221
+ unknown,
222
+ InDone & InDone2,
223
+ Env | Env2
224
+ > => core.concatMapWith(self, f, () => void 0, () => void 0))
225
+
226
+ /** @internal */
227
+ export const collect = dual<
228
+ <OutElem, OutElem2>(
229
+ pf: (o: OutElem) => Option.Option<OutElem2>
230
+ ) => <InElem, OutErr, InErr, OutDone, InDone, Env>(
231
+ self: Channel.Channel<OutElem>
232
+ ) => Channel.Channel<OutElem2, InElem, OutErr, InErr, OutDone, InDone, Env>,
233
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem2>(
234
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
235
+ pf: (o: OutElem) => Option.Option<OutElem2>
236
+ ) => Channel.Channel<OutElem2, InElem, OutErr, InErr, OutDone, InDone, Env>
237
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem2>(
238
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
239
+ pf: (o: OutElem) => Option.Option<OutElem2>
240
+ ): Channel.Channel<OutElem2, InElem, OutErr, InErr, OutDone, InDone, Env> => {
241
+ const collector: Channel.Channel<OutElem2, OutElem, OutErr, OutErr, OutDone, OutDone, Env> = core
242
+ .readWith({
243
+ onInput: (out) =>
244
+ Option.match(pf(out), {
245
+ onNone: () => collector,
246
+ onSome: (out2) => core.flatMap(core.write(out2), () => collector)
247
+ }),
248
+ onFailure: core.fail,
249
+ onDone: core.succeedNow
250
+ })
251
+ return core.pipeTo(self, collector)
252
+ })
253
+
254
+ /** @internal */
255
+ export const concatOut = <OutElem, InElem, OutErr, InErr, InDone, Env, OutDone>(
256
+ self: Channel.Channel<
257
+ Channel.Channel<OutElem, InElem, OutErr, InErr, unknown, InDone, Env>,
258
+ InElem,
259
+ OutErr,
260
+ InErr,
261
+ OutDone,
262
+ InDone,
263
+ Env
264
+ >
265
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, unknown, InDone, Env> => core.concatAll(self)
266
+
267
+ /** @internal */
268
+ export const mapInput = dual<
269
+ <InDone0, InDone>(
270
+ f: (a: InDone0) => InDone
271
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
272
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
273
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone0, Env>,
274
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, InDone0>(
275
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
276
+ f: (a: InDone0) => InDone
277
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone0, Env>
278
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, InDone0>(
279
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
280
+ f: (a: InDone0) => InDone
281
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone0, Env> => {
282
+ const reader: Channel.Channel<InElem, InElem, InErr, InErr, InDone, InDone0> = core.readWith({
283
+ onInput: (inElem: InElem) => core.flatMap(core.write(inElem), () => reader),
284
+ onFailure: core.fail,
285
+ onDone: (done: InDone0) => core.succeedNow(f(done))
286
+ })
287
+ return core.pipeTo(reader, self)
288
+ })
289
+
290
+ /** @internal */
291
+ export const mapInputEffect = dual<
292
+ <InDone0, InDone, InErr, Env1>(
293
+ f: (i: InDone0) => Effect.Effect<InDone, InErr, Env1>
294
+ ) => <OutElem, InElem, OutErr, OutDone, Env>(
295
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
296
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone0, Env1 | Env>,
297
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, InDone0, Env1>(
298
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
299
+ f: (i: InDone0) => Effect.Effect<InDone, InErr, Env1>
300
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone0, Env1 | Env>
301
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, InDone0, Env1>(
302
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
303
+ f: (i: InDone0) => Effect.Effect<InDone, InErr, Env1>
304
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone0, Env | Env1> => {
305
+ const reader: Channel.Channel<InElem, InElem, InErr, InErr, InDone, InDone0, Env1> = core.readWith({
306
+ onInput: (inElem) => core.flatMap(core.write(inElem), () => reader),
307
+ onFailure: core.fail,
308
+ onDone: (done) => core.fromEffect(f(done))
309
+ })
310
+ return core.pipeTo(reader, self)
311
+ })
312
+
313
+ /** @internal */
314
+ export const mapInputError = dual<
315
+ <InErr0, InErr>(
316
+ f: (a: InErr0) => InErr
317
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
318
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
319
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr0, OutDone, InDone, Env>,
320
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, InErr0>(
321
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
322
+ f: (a: InErr0) => InErr
323
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr0, OutDone, InDone, Env>
324
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, InErr0>(
325
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
326
+ f: (a: InErr0) => InErr
327
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr0, OutDone, InDone, Env> => {
328
+ const reader: Channel.Channel<InElem, InElem, InErr, InErr0, InDone, InDone> = core.readWith({
329
+ onInput: (inElem: InElem) => core.flatMap(core.write(inElem), () => reader),
330
+ onFailure: (error) => core.fail(f(error)),
331
+ onDone: core.succeedNow
332
+ })
333
+ return core.pipeTo(reader, self)
334
+ })
335
+
336
+ /** @internal */
337
+ export const mapInputErrorEffect = dual<
338
+ <InErr0, InDone, InErr, Env1>(
339
+ f: (error: InErr0) => Effect.Effect<InDone, InErr, Env1>
340
+ ) => <OutElem, InElem, OutErr, OutDone, Env>(
341
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
342
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr0, OutDone, InDone, Env1 | Env>,
343
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, InErr0, Env1>(
344
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
345
+ f: (error: InErr0) => Effect.Effect<InDone, InErr, Env1>
346
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr0, OutDone, InDone, Env1 | Env>
347
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, InErr0, Env1>(
348
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
349
+ f: (error: InErr0) => Effect.Effect<InDone, InErr, Env1>
350
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr0, OutDone, InDone, Env | Env1> => {
351
+ const reader: Channel.Channel<InElem, InElem, InErr, InErr0, InDone, InDone, Env1> = core.readWith({
352
+ onInput: (inElem) => core.flatMap(core.write(inElem), () => reader),
353
+ onFailure: (error) => core.fromEffect(f(error)),
354
+ onDone: core.succeedNow
355
+ })
356
+ return core.pipeTo(reader, self)
357
+ })
358
+
359
+ /** @internal */
360
+ export const mapInputIn = dual<
361
+ <InElem0, InElem>(
362
+ f: (a: InElem0) => InElem
363
+ ) => <OutElem, OutErr, InErr, OutDone, InDone, Env>(
364
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
365
+ ) => Channel.Channel<OutElem, InElem0, OutErr, InErr, OutDone, InDone, Env>,
366
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, InElem0>(
367
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
368
+ f: (a: InElem0) => InElem
369
+ ) => Channel.Channel<OutElem, InElem0, OutErr, InErr, OutDone, InDone, Env>
370
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, InElem0>(
371
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
372
+ f: (a: InElem0) => InElem
373
+ ): Channel.Channel<OutElem, InElem0, OutErr, InErr, OutDone, InDone, Env> => {
374
+ const reader: Channel.Channel<InElem, InElem0, InErr, InErr, InDone, InDone> = core.readWith({
375
+ onInput: (inElem) => core.flatMap(core.write(f(inElem)), () => reader),
376
+ onFailure: core.fail,
377
+ onDone: core.succeedNow
378
+ })
379
+ return core.pipeTo(reader, self)
380
+ })
381
+
382
+ /** @internal */
383
+ export const mapInputInEffect = dual<
384
+ <InElem0, InElem, InErr, Env1>(
385
+ f: (a: InElem0) => Effect.Effect<InElem, InErr, Env1>
386
+ ) => <OutElem, OutErr, OutDone, InDone, Env>(
387
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
388
+ ) => Channel.Channel<OutElem, InElem0, OutErr, InErr, OutDone, InDone, Env1 | Env>,
389
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, InElem0, Env1>(
390
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
391
+ f: (a: InElem0) => Effect.Effect<InElem, InErr, Env1>
392
+ ) => Channel.Channel<OutElem, InElem0, OutErr, InErr, OutDone, InDone, Env1 | Env>
393
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, InElem0, Env1>(
394
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
395
+ f: (a: InElem0) => Effect.Effect<InElem, InErr, Env1>
396
+ ): Channel.Channel<OutElem, InElem0, OutErr, InErr, OutDone, InDone, Env | Env1> => {
397
+ const reader: Channel.Channel<InElem, InElem0, InErr, InErr, InDone, InDone, Env1> = core.readWith({
398
+ onInput: (inElem) => core.flatMap(core.flatMap(core.fromEffect(f(inElem)), core.write), () => reader),
399
+ onFailure: core.fail,
400
+ onDone: core.succeedNow
401
+ })
402
+ return core.pipeTo(reader, self)
403
+ })
404
+
405
+ /** @internal */
406
+ export const doneCollect = <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
407
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
408
+ ): Channel.Channel<never, InElem, OutErr, InErr, [Chunk.Chunk<OutElem>, OutDone], InDone, Env> =>
409
+ core.suspend(() => {
410
+ const builder: Array<OutElem> = []
411
+ return pipe(
412
+ core.pipeTo(self, doneCollectReader<Env, OutErr, OutElem, OutDone>(builder)),
413
+ core.flatMap((outDone) => core.succeed([Chunk.unsafeFromArray(builder), outDone]))
414
+ )
415
+ })
416
+
417
+ /** @internal */
418
+ const doneCollectReader = <Env, OutErr, OutElem, OutDone>(
419
+ builder: Array<OutElem>
420
+ ): Channel.Channel<never, OutElem, OutErr, OutErr, OutDone, OutDone, Env> => {
421
+ return core.readWith({
422
+ onInput: (outElem) =>
423
+ core.flatMap(
424
+ core.sync(() => {
425
+ builder.push(outElem)
426
+ }),
427
+ () => doneCollectReader<Env, OutErr, OutElem, OutDone>(builder)
428
+ ),
429
+ onFailure: core.fail,
430
+ onDone: core.succeed
431
+ })
432
+ }
433
+
434
+ /** @internal */
435
+ export const drain = <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
436
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
437
+ ): Channel.Channel<never, InElem, OutErr, InErr, OutDone, InDone, Env> => {
438
+ const drainer: Channel.Channel<never, OutElem, OutErr, OutErr, OutDone, OutDone, Env> = core
439
+ .readWithCause({
440
+ onInput: () => drainer,
441
+ onFailure: core.failCause,
442
+ onDone: core.succeed
443
+ })
444
+ return core.pipeTo(self, drainer)
445
+ }
446
+
447
+ /** @internal */
448
+ export const emitCollect = <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
449
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
450
+ ): Channel.Channel<[Chunk.Chunk<OutElem>, OutDone], InElem, OutErr, InErr, void, InDone, Env> =>
451
+ core.flatMap(doneCollect(self), core.write)
452
+
453
+ /** @internal */
454
+ export const ensuring = dual<
455
+ <Z, Env1>(
456
+ finalizer: Effect.Effect<Z, never, Env1>
457
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
458
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
459
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env1 | Env>,
460
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, Z, Env1>(
461
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
462
+ finalizer: Effect.Effect<Z, never, Env1>
463
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env1 | Env>
464
+ >(2, <Env, InErr, InElem, InDone, OutErr, OutElem, OutDone, Env1, Z>(
465
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
466
+ finalizer: Effect.Effect<Z, never, Env1>
467
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env | Env1> =>
468
+ core.ensuringWith(self, () => finalizer))
469
+
470
+ /** @internal */
471
+ export const context = <Env>(): Channel.Channel<never, unknown, never, unknown, Context.Context<Env>, unknown, Env> =>
472
+ core.fromEffect(Effect.context<Env>())
473
+
474
+ /** @internal */
475
+ export const contextWith = <Env, OutDone>(
476
+ f: (env: Context.Context<Env>) => OutDone
477
+ ): Channel.Channel<never, unknown, never, unknown, OutDone, unknown, Env> => map(context<Env>(), f)
478
+
479
+ /** @internal */
480
+ export const contextWithChannel = <
481
+ Env,
482
+ OutElem,
483
+ InElem,
484
+ OutErr,
485
+ InErr,
486
+ OutDone,
487
+ InDone,
488
+ Env1
489
+ >(
490
+ f: (env: Context.Context<Env>) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env1>
491
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env | Env1> => core.flatMap(context<Env>(), f)
492
+
493
+ /** @internal */
494
+ export const contextWithEffect = <Env, OutDone, OutErr, Env1>(
495
+ f: (env: Context.Context<Env>) => Effect.Effect<OutDone, OutErr, Env1>
496
+ ): Channel.Channel<never, unknown, OutErr, unknown, OutDone, unknown, Env | Env1> => mapEffect(context<Env>(), f)
497
+
498
+ /** @internal */
499
+ export const flatten = <
500
+ OutElem,
501
+ InElem,
502
+ OutErr,
503
+ InErr,
504
+ OutElem1,
505
+ InElem1,
506
+ OutErr1,
507
+ InErr1,
508
+ OutDone2,
509
+ InDone1,
510
+ Env1,
511
+ InDone,
512
+ Env
513
+ >(
514
+ self: Channel.Channel<
515
+ OutElem,
516
+ InElem,
517
+ OutErr,
518
+ InErr,
519
+ Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone2, InDone1, Env1>,
520
+ InDone,
521
+ Env
522
+ >
523
+ ): Channel.Channel<
524
+ OutElem | OutElem1,
525
+ InElem & InElem1,
526
+ OutErr | OutErr1,
527
+ InErr & InErr1,
528
+ OutDone2,
529
+ InDone & InDone1,
530
+ Env | Env1
531
+ > => core.flatMap(self, identity)
532
+
533
+ /** @internal */
534
+ export const foldChannel = dual<
535
+ <
536
+ OutErr,
537
+ OutElem1,
538
+ InElem1,
539
+ OutErr1,
540
+ InErr1,
541
+ OutDone1,
542
+ InDone1,
543
+ Env1,
544
+ OutDone,
545
+ OutElem2,
546
+ InElem2,
547
+ OutErr2,
548
+ InErr2,
549
+ OutDone2,
550
+ InDone2,
551
+ Env2
552
+ >(
553
+ options: {
554
+ readonly onFailure: (
555
+ error: OutErr
556
+ ) => Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>
557
+ readonly onSuccess: (
558
+ done: OutDone
559
+ ) => Channel.Channel<OutElem2, InElem2, OutErr2, InErr2, OutDone2, InDone2, Env2>
560
+ }
561
+ ) => <Env, InErr, InElem, InDone, OutElem>(
562
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
563
+ ) => Channel.Channel<
564
+ OutElem1 | OutElem2 | OutElem,
565
+ InElem & InElem1 & InElem2,
566
+ OutErr1 | OutErr2,
567
+ InErr & InErr1 & InErr2,
568
+ OutDone1 | OutDone2,
569
+ InDone & InDone1 & InDone2,
570
+ Env1 | Env2 | Env
571
+ >,
572
+ <
573
+ OutElem,
574
+ InElem,
575
+ OutErr,
576
+ InErr,
577
+ OutDone,
578
+ InDone,
579
+ Env,
580
+ OutElem1,
581
+ InElem1,
582
+ OutErr1,
583
+ InErr1,
584
+ OutDone1,
585
+ InDone1,
586
+ Env1,
587
+ OutElem2,
588
+ InElem2,
589
+ OutErr2,
590
+ InErr2,
591
+ OutDone2,
592
+ InDone2,
593
+ Env2
594
+ >(
595
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
596
+ options: {
597
+ readonly onFailure: (
598
+ error: OutErr
599
+ ) => Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>
600
+ readonly onSuccess: (
601
+ done: OutDone
602
+ ) => Channel.Channel<OutElem2, InElem2, OutErr2, InErr2, OutDone2, InDone2, Env2>
603
+ }
604
+ ) => Channel.Channel<
605
+ OutElem1 | OutElem2 | OutElem,
606
+ InElem & InElem1 & InElem2,
607
+ OutErr1 | OutErr2,
608
+ InErr & InErr1 & InErr2,
609
+ OutDone1 | OutDone2,
610
+ InDone & InDone1 & InDone2,
611
+ Env1 | Env2 | Env
612
+ >
613
+ >(2, <
614
+ OutElem,
615
+ InElem,
616
+ OutErr,
617
+ InErr,
618
+ OutDone,
619
+ InDone,
620
+ Env,
621
+ OutElem1,
622
+ InElem1,
623
+ OutErr1,
624
+ InErr1,
625
+ OutDone1,
626
+ InDone1,
627
+ Env1,
628
+ OutElem2,
629
+ InElem2,
630
+ OutErr2,
631
+ InErr2,
632
+ OutDone2,
633
+ InDone2,
634
+ Env2
635
+ >(
636
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
637
+ options: {
638
+ readonly onFailure: (error: OutErr) => Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>
639
+ readonly onSuccess: (done: OutDone) => Channel.Channel<OutElem2, InElem2, OutErr2, InErr2, OutDone2, InDone2, Env2>
640
+ }
641
+ ): Channel.Channel<
642
+ OutElem | OutElem2 | OutElem1,
643
+ InElem & InElem1 & InElem2,
644
+ OutErr2 | OutErr1,
645
+ InErr & InErr1 & InErr2,
646
+ OutDone2 | OutDone1,
647
+ InDone & InDone1 & InDone2,
648
+ Env | Env1 | Env2
649
+ > =>
650
+ core.foldCauseChannel(self, {
651
+ onFailure: (cause) => {
652
+ const either = Cause.failureOrCause(cause)
653
+ switch (either._tag) {
654
+ case "Left": {
655
+ return options.onFailure(either.left)
656
+ }
657
+ case "Right": {
658
+ return core.failCause(either.right)
659
+ }
660
+ }
661
+ },
662
+ onSuccess: options.onSuccess
663
+ }))
664
+
665
+ /** @internal */
666
+ export const fromEither = <R, L>(
667
+ either: Either.Either<R, L>
668
+ ): Channel.Channel<never, unknown, L, unknown, R, unknown> =>
669
+ core.suspend(() => Either.match(either, { onLeft: core.fail, onRight: core.succeed }))
670
+
671
+ /** @internal */
672
+ export const fromInput = <Err, Elem, Done>(
673
+ input: SingleProducerAsyncInput.AsyncInputConsumer<Err, Elem, Done>
674
+ ): Channel.Channel<Elem, unknown, Err, unknown, Done, unknown> =>
675
+ unwrap(
676
+ input.takeWith(
677
+ core.failCause,
678
+ (elem) => core.flatMap(core.write(elem), () => fromInput(input)),
679
+ core.succeed
680
+ )
681
+ )
682
+
683
+ /** @internal */
684
+ export const fromPubSub = <Done, Err, Elem>(
685
+ pubsub: PubSub.PubSub<Either.Either<Elem, Exit.Exit<Done, Err>>>
686
+ ): Channel.Channel<Elem, unknown, Err, unknown, Done, unknown> =>
687
+ unwrapScoped(Effect.map(PubSub.subscribe(pubsub), fromQueue))
688
+
689
+ /** @internal */
690
+ export const fromPubSubScoped = <Done, Err, Elem>(
691
+ pubsub: PubSub.PubSub<Either.Either<Elem, Exit.Exit<Done, Err>>>
692
+ ): Effect.Effect<Channel.Channel<Elem, unknown, Err, unknown, Done, unknown>, never, Scope.Scope> =>
693
+ Effect.map(PubSub.subscribe(pubsub), fromQueue)
694
+
695
+ /** @internal */
696
+ export const fromOption = <A>(
697
+ option: Option.Option<A>
698
+ ): Channel.Channel<never, unknown, Option.Option<never>, unknown, A, unknown> =>
699
+ core.suspend(() =>
700
+ Option.match(option, {
701
+ onNone: () => core.fail(Option.none()),
702
+ onSome: core.succeed
703
+ })
704
+ )
705
+
706
+ /** @internal */
707
+ export const fromQueue = <Done, Err, Elem>(
708
+ queue: Queue.Dequeue<Either.Either<Elem, Exit.Exit<Done, Err>>>
709
+ ): Channel.Channel<Elem, unknown, Err, unknown, Done, unknown> => core.suspend(() => fromQueueInternal(queue))
710
+
711
+ /** @internal */
712
+ const fromQueueInternal = <Done, Err, Elem>(
713
+ queue: Queue.Dequeue<Either.Either<Elem, Exit.Exit<Done, Err>>>
714
+ ): Channel.Channel<Elem, unknown, Err, unknown, Done, unknown> =>
715
+ pipe(
716
+ core.fromEffect(Queue.take(queue)),
717
+ core.flatMap(Either.match({
718
+ onLeft: Exit.match({
719
+ onFailure: core.failCause,
720
+ onSuccess: core.succeedNow
721
+ }),
722
+ onRight: (elem) =>
723
+ core.flatMap(
724
+ core.write(elem),
725
+ () => fromQueueInternal(queue)
726
+ )
727
+ }))
728
+ )
729
+
730
+ /** @internal */
731
+ export const identityChannel = <Elem, Err, Done>(): Channel.Channel<Elem, Elem, Err, Err, Done, Done> =>
732
+ core.readWith({
733
+ onInput: (input: Elem) => core.flatMap(core.write(input), () => identityChannel()),
734
+ onFailure: core.fail,
735
+ onDone: core.succeedNow
736
+ })
737
+
738
+ /** @internal */
739
+ export const interruptWhen = dual<
740
+ <OutDone1, OutErr1, Env1>(
741
+ effect: Effect.Effect<OutDone1, OutErr1, Env1>
742
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
743
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
744
+ ) => Channel.Channel<OutElem, InElem, OutErr1 | OutErr, InErr, OutDone1 | OutDone, InDone, Env1 | Env>,
745
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutDone1, OutErr1, Env1>(
746
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
747
+ effect: Effect.Effect<OutDone1, OutErr1, Env1>
748
+ ) => Channel.Channel<OutElem, InElem, OutErr1 | OutErr, InErr, OutDone1 | OutDone, InDone, Env1 | Env>
749
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutDone1, OutErr1, Env1>(
750
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
751
+ effect: Effect.Effect<OutDone1, OutErr1, Env1>
752
+ ): Channel.Channel<OutElem, InElem, OutErr | OutErr1, InErr, OutDone | OutDone1, InDone, Env1 | Env> =>
753
+ mergeWith(self, {
754
+ other: core.fromEffect(effect),
755
+ onSelfDone: (selfDone) => mergeDecision.Done(Effect.suspend(() => selfDone)),
756
+ onOtherDone: (effectDone) => mergeDecision.Done(Effect.suspend(() => effectDone))
757
+ }))
758
+
759
+ /** @internal */
760
+ export const interruptWhenDeferred = dual<
761
+ <OutDone1, OutErr1>(
762
+ deferred: Deferred.Deferred<OutDone1, OutErr1>
763
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
764
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
765
+ ) => Channel.Channel<OutElem, InElem, OutErr1 | OutErr, InErr, OutDone1 | OutDone, InDone, Env>,
766
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutDone1, OutErr1>(
767
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
768
+ deferred: Deferred.Deferred<OutDone1, OutErr1>
769
+ ) => Channel.Channel<OutElem, InElem, OutErr1 | OutErr, InErr, OutDone1 | OutDone, InDone, Env>
770
+ >(2, <Env, InErr, InElem, InDone, OutErr, OutElem, OutDone, OutErr1, OutDone1>(
771
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
772
+ deferred: Deferred.Deferred<OutDone1, OutErr1>
773
+ ): Channel.Channel<OutElem, InElem, OutErr | OutErr1, InErr, OutDone | OutDone1, InDone, Env> =>
774
+ interruptWhen(self, Deferred.await(deferred)))
775
+
776
+ /** @internal */
777
+ export const map = dual<
778
+ <OutDone, OutDone2>(
779
+ f: (out: OutDone) => OutDone2
780
+ ) => <OutElem, InElem, OutErr, InErr, InDone, Env>(
781
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
782
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone2, InDone, Env>,
783
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutDone2>(
784
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
785
+ f: (out: OutDone) => OutDone2
786
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone2, InDone, Env>
787
+ >(2, <Env, InErr, InElem, InDone, OutErr, OutElem, OutDone, OutDone2>(
788
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
789
+ f: (out: OutDone) => OutDone2
790
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone2, InDone, Env> =>
791
+ core.flatMap(self, (a) => core.sync(() => f(a))))
792
+
793
+ /** @internal */
794
+ export const mapEffect = dual<
795
+ <OutDone, OutDone1, OutErr1, Env1>(
796
+ f: (o: OutDone) => Effect.Effect<OutDone1, OutErr1, Env1>
797
+ ) => <OutElem, InElem, OutErr, InErr, InDone, Env>(
798
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
799
+ ) => Channel.Channel<OutElem, InElem, OutErr1 | OutErr, InErr, OutDone1, InDone, Env1 | Env>,
800
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutDone1, OutErr1, Env1>(
801
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
802
+ f: (o: OutDone) => Effect.Effect<OutDone1, OutErr1, Env1>
803
+ ) => Channel.Channel<OutElem, InElem, OutErr1 | OutErr, InErr, OutDone1, InDone, Env1 | Env>
804
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutDone1, OutErr1, Env1>(
805
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
806
+ f: (o: OutDone) => Effect.Effect<OutDone1, OutErr1, Env1>
807
+ ): Channel.Channel<OutElem, InElem, OutErr | OutErr1, InErr, OutDone1, InDone, Env | Env1> =>
808
+ core.flatMap(self, (z) => core.fromEffect(f(z))))
809
+
810
+ /** @internal */
811
+ export const mapError = dual<
812
+ <OutErr, OutErr2>(
813
+ f: (err: OutErr) => OutErr2
814
+ ) => <OutElem, InElem, InErr, OutDone, InDone, Env>(
815
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
816
+ ) => Channel.Channel<OutElem, InElem, OutErr2, InErr, OutDone, InDone, Env>,
817
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutErr2>(
818
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
819
+ f: (err: OutErr) => OutErr2
820
+ ) => Channel.Channel<OutElem, InElem, OutErr2, InErr, OutDone, InDone, Env>
821
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutErr2>(
822
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
823
+ f: (err: OutErr) => OutErr2
824
+ ): Channel.Channel<OutElem, InElem, OutErr2, InErr, OutDone, InDone, Env> => mapErrorCause(self, Cause.map(f)))
825
+
826
+ /** @internal */
827
+ export const mapErrorCause = dual<
828
+ <OutErr, OutErr2>(
829
+ f: (cause: Cause.Cause<OutErr>) => Cause.Cause<OutErr2>
830
+ ) => <OutElem, InElem, InErr, OutDone, InDone, Env>(
831
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
832
+ ) => Channel.Channel<OutElem, InElem, OutErr2, InErr, OutDone, InDone, Env>,
833
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutErr2>(
834
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
835
+ f: (cause: Cause.Cause<OutErr>) => Cause.Cause<OutErr2>
836
+ ) => Channel.Channel<OutElem, InElem, OutErr2, InErr, OutDone, InDone, Env>
837
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutErr2>(
838
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
839
+ f: (cause: Cause.Cause<OutErr>) => Cause.Cause<OutErr2>
840
+ ): Channel.Channel<OutElem, InElem, OutErr2, InErr, OutDone, InDone, Env> =>
841
+ core.catchAllCause(self, (cause) => core.failCause(f(cause))))
842
+
843
+ /** @internal */
844
+ export const mapOut = dual<
845
+ <OutElem, OutElem2>(
846
+ f: (o: OutElem) => OutElem2
847
+ ) => <InElem, OutErr, InErr, OutDone, InDone, Env>(
848
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
849
+ ) => Channel.Channel<OutElem2, InElem, OutErr, InErr, OutDone, InDone, Env>,
850
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem2>(
851
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
852
+ f: (o: OutElem) => OutElem2
853
+ ) => Channel.Channel<OutElem2, InElem, OutErr, InErr, OutDone, InDone, Env>
854
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem2>(
855
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
856
+ f: (o: OutElem) => OutElem2
857
+ ): Channel.Channel<OutElem2, InElem, OutErr, InErr, OutDone, InDone, Env> => {
858
+ const reader: Channel.Channel<OutElem2, OutElem, OutErr, OutErr, OutDone, OutDone, Env> = core
859
+ .readWith({
860
+ onInput: (outElem) => core.flatMap(core.write(f(outElem)), () => reader),
861
+ onFailure: core.fail,
862
+ onDone: core.succeedNow
863
+ })
864
+ return core.pipeTo(self, reader)
865
+ })
866
+
867
+ /** @internal */
868
+ export const mapOutEffect = dual<
869
+ <OutElem, OutElem1, OutErr1, Env1>(
870
+ f: (o: OutElem) => Effect.Effect<OutElem1, OutErr1, Env1>
871
+ ) => <InElem, OutErr, InErr, OutDone, InDone, Env>(
872
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
873
+ ) => Channel.Channel<OutElem1, InElem, OutErr1 | OutErr, InErr, OutDone, InDone, Env1 | Env>,
874
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, OutErr1, Env1>(
875
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
876
+ f: (o: OutElem) => Effect.Effect<OutElem1, OutErr1, Env1>
877
+ ) => Channel.Channel<OutElem1, InElem, OutErr1 | OutErr, InErr, OutDone, InDone, Env1 | Env>
878
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, OutErr1, Env1>(
879
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
880
+ f: (o: OutElem) => Effect.Effect<OutElem1, OutErr1, Env1>
881
+ ): Channel.Channel<OutElem1, InElem, OutErr | OutErr1, InErr, OutDone, InDone, Env | Env1> => {
882
+ const reader: Channel.Channel<OutElem1, OutElem, OutErr | OutErr1, OutErr, OutDone, OutDone, Env | Env1> = core
883
+ .readWithCause({
884
+ onInput: (outElem) =>
885
+ pipe(
886
+ core.fromEffect(f(outElem)),
887
+ core.flatMap(core.write),
888
+ core.flatMap(() => reader)
889
+ ),
890
+ onFailure: core.failCause,
891
+ onDone: core.succeedNow
892
+ })
893
+ return core.pipeTo(self, reader)
894
+ })
895
+
896
+ /** @internal */
897
+ export const mapOutEffectPar = dual<
898
+ <OutElem, OutElem1, OutErr1, Env1>(
899
+ f: (o: OutElem) => Effect.Effect<OutElem1, OutErr1, Env1>,
900
+ n: number
901
+ ) => <InElem, OutErr, InErr, OutDone, InDone, Env>(
902
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
903
+ ) => Channel.Channel<OutElem1, InElem, OutErr1 | OutErr, InErr, OutDone, InDone, Env1 | Env>,
904
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, OutErr1, Env1>(
905
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
906
+ f: (o: OutElem) => Effect.Effect<OutElem1, OutErr1, Env1>,
907
+ n: number
908
+ ) => Channel.Channel<OutElem1, InElem, OutErr1 | OutErr, InErr, OutDone, InDone, Env1 | Env>
909
+ >(3, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, OutErr1, Env1>(
910
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
911
+ f: (o: OutElem) => Effect.Effect<OutElem1, OutErr1, Env1>,
912
+ n: number
913
+ ): Channel.Channel<OutElem1, InElem, OutErr | OutErr1, InErr, OutDone, InDone, Env | Env1> =>
914
+ unwrapScopedWith(
915
+ (scope) =>
916
+ Effect.gen(function*() {
917
+ const input = yield* singleProducerAsyncInput.make<InErr, InElem, InDone>()
918
+ const queueReader = fromInput(input)
919
+ const queue = yield* Queue.bounded<Effect.Effect<Either.Either<OutElem1, OutDone>, OutErr | OutErr1, Env1>>(n)
920
+ yield* Scope.addFinalizer(scope, Queue.shutdown(queue))
921
+ const errorSignal = yield* Deferred.make<never, OutErr1>()
922
+ const withPermits = n === Number.POSITIVE_INFINITY ?
923
+ ((_: number) => identity) :
924
+ (yield* Effect.makeSemaphore(n)).withPermits
925
+ const pull = yield* queueReader.pipe(core.pipeTo(self), toPullIn(scope))
926
+ yield* pull.pipe(
927
+ Effect.matchCauseEffect({
928
+ onFailure: (cause) => Queue.offer(queue, Effect.failCause(cause)),
929
+ onSuccess: Either.match({
930
+ onLeft: (outDone) =>
931
+ Effect.zipRight(
932
+ Effect.interruptible(withPermits(n)(Effect.void)),
933
+ Effect.asVoid(Queue.offer(queue, Effect.succeed(Either.left(outDone))))
934
+ ),
935
+ onRight: (outElem) =>
936
+ Effect.gen(function*() {
937
+ const deferred = yield* Deferred.make<OutElem1, OutErr1>()
938
+ const latch = yield* Deferred.make<void>()
939
+ yield* Queue.offer(queue, Effect.map(Deferred.await(deferred), Either.right))
940
+ yield* Deferred.succeed(latch, void 0).pipe(
941
+ Effect.zipRight(
942
+ Effect.uninterruptibleMask((restore) =>
943
+ Effect.exit(restore(Deferred.await(errorSignal))).pipe(
944
+ Effect.raceFirst(Effect.exit(restore(f(outElem)))),
945
+ Effect.flatMap(identity)
946
+ )
947
+ ).pipe(
948
+ Effect.tapErrorCause((cause) => Deferred.failCause(errorSignal, cause)),
949
+ Effect.intoDeferred(deferred)
950
+ )
951
+ ),
952
+ withPermits(1),
953
+ Effect.forkIn(scope)
954
+ )
955
+ yield* Deferred.await(latch)
956
+ })
957
+ })
958
+ }),
959
+ Effect.forever,
960
+ Effect.interruptible,
961
+ Effect.forkIn(scope)
962
+ )
963
+ const consumer: Channel.Channel<OutElem1, unknown, OutErr | OutErr1, unknown, OutDone, unknown, Env1> = unwrap(
964
+ Effect.matchCause(Effect.flatten(Queue.take(queue)), {
965
+ onFailure: core.failCause,
966
+ onSuccess: Either.match({
967
+ onLeft: core.succeedNow,
968
+ onRight: (outElem) => core.flatMap(core.write(outElem), () => consumer)
969
+ })
970
+ })
971
+ )
972
+ return core.embedInput(consumer, input)
973
+ })
974
+ ))
975
+
976
+ /** @internal */
977
+ export const mergeAll = (
978
+ options: {
979
+ readonly concurrency: number | "unbounded"
980
+ readonly bufferSize?: number | undefined
981
+ readonly mergeStrategy?: MergeStrategy.MergeStrategy | undefined
982
+ }
983
+ ) => {
984
+ return <
985
+ OutElem,
986
+ InElem1,
987
+ OutErr1,
988
+ InErr1,
989
+ InDone1,
990
+ Env1,
991
+ InElem,
992
+ OutErr,
993
+ InErr,
994
+ InDone,
995
+ Env
996
+ >(
997
+ channels: Channel.Channel<
998
+ Channel.Channel<OutElem, InElem1, OutErr1, InErr1, unknown, InDone1, Env1>,
999
+ InElem,
1000
+ OutErr,
1001
+ InErr,
1002
+ unknown,
1003
+ InDone,
1004
+ Env
1005
+ >
1006
+ ): Channel.Channel<
1007
+ OutElem,
1008
+ InElem & InElem1,
1009
+ OutErr | OutErr1,
1010
+ InErr & InErr1,
1011
+ unknown,
1012
+ InDone & InDone1,
1013
+ Env | Env1
1014
+ > => mergeAllWith(options)(channels, constVoid)
1015
+ }
1016
+
1017
+ /** @internal */
1018
+ export const mergeAllUnbounded = <
1019
+ OutElem,
1020
+ InElem1,
1021
+ OutErr1,
1022
+ InErr1,
1023
+ InDone1,
1024
+ Env1,
1025
+ InElem,
1026
+ OutErr,
1027
+ InErr,
1028
+ InDone,
1029
+ Env
1030
+ >(
1031
+ channels: Channel.Channel<
1032
+ Channel.Channel<OutElem, InElem1, OutErr1, InErr1, unknown, InDone1, Env1>,
1033
+ InElem,
1034
+ OutErr,
1035
+ InErr,
1036
+ unknown,
1037
+ InDone,
1038
+ Env
1039
+ >
1040
+ ): Channel.Channel<
1041
+ OutElem,
1042
+ InElem & InElem1,
1043
+ OutErr | OutErr1,
1044
+ InErr & InErr1,
1045
+ unknown,
1046
+ InDone & InDone1,
1047
+ Env | Env1
1048
+ > => mergeAllWith({ concurrency: "unbounded" })(channels, constVoid)
1049
+
1050
+ /** @internal */
1051
+ export const mergeAllUnboundedWith = <
1052
+ OutElem,
1053
+ InElem1,
1054
+ OutErr1,
1055
+ InErr1,
1056
+ OutDone,
1057
+ InDone1,
1058
+ Env1,
1059
+ InElem,
1060
+ OutErr,
1061
+ InErr,
1062
+ InDone,
1063
+ Env
1064
+ >(
1065
+ channels: Channel.Channel<
1066
+ Channel.Channel<OutElem, InElem1, OutErr1, InErr1, OutDone, InDone1, Env1>,
1067
+ InElem,
1068
+ OutErr,
1069
+ InErr,
1070
+ OutDone,
1071
+ InDone,
1072
+ Env
1073
+ >,
1074
+ f: (o1: OutDone, o2: OutDone) => OutDone
1075
+ ): Channel.Channel<
1076
+ OutElem,
1077
+ InElem & InElem1,
1078
+ OutErr | OutErr1,
1079
+ InErr & InErr1,
1080
+ OutDone,
1081
+ InDone & InDone1,
1082
+ Env | Env1
1083
+ > => mergeAllWith({ concurrency: "unbounded" })(channels, f)
1084
+
1085
+ /** @internal */
1086
+ export const mergeAllWith = (
1087
+ {
1088
+ bufferSize = 16,
1089
+ concurrency,
1090
+ mergeStrategy = mergeStrategy_.BackPressure()
1091
+ }: {
1092
+ readonly concurrency: number | "unbounded"
1093
+ readonly bufferSize?: number | undefined
1094
+ readonly mergeStrategy?: MergeStrategy.MergeStrategy | undefined
1095
+ }
1096
+ ) =>
1097
+ <OutElem, InElem1, OutErr1, InErr1, OutDone, InDone1, Env1, InElem, OutErr, InErr, InDone, Env>(
1098
+ channels: Channel.Channel<
1099
+ Channel.Channel<OutElem, InElem1, OutErr1, InErr1, OutDone, InDone1, Env1>,
1100
+ InElem,
1101
+ OutErr,
1102
+ InErr,
1103
+ OutDone,
1104
+ InDone,
1105
+ Env
1106
+ >,
1107
+ f: (o1: OutDone, o2: OutDone) => OutDone
1108
+ ): Channel.Channel<
1109
+ OutElem,
1110
+ InElem & InElem1,
1111
+ OutErr | OutErr1,
1112
+ InErr & InErr1,
1113
+ OutDone,
1114
+ InDone & InDone1,
1115
+ Env | Env1
1116
+ > =>
1117
+ unwrapScopedWith(
1118
+ (scope) =>
1119
+ Effect.gen(function*() {
1120
+ const concurrencyN = concurrency === "unbounded" ? Number.MAX_SAFE_INTEGER : concurrency
1121
+ const input = yield* singleProducerAsyncInput.make<
1122
+ InErr & InErr1,
1123
+ InElem & InElem1,
1124
+ InDone & InDone1
1125
+ >()
1126
+ const queueReader = fromInput(input)
1127
+ const queue = yield* Queue.bounded<Effect.Effect<Either.Either<OutElem, OutDone>, OutErr | OutErr1, Env>>(
1128
+ bufferSize
1129
+ )
1130
+ yield* Scope.addFinalizer(scope, Queue.shutdown(queue))
1131
+ const cancelers = yield* Queue.unbounded<Deferred.Deferred<void>>()
1132
+ yield* Scope.addFinalizer(scope, Queue.shutdown(cancelers))
1133
+ const lastDone = yield* Ref.make<Option.Option<OutDone>>(Option.none())
1134
+ const errorSignal = yield* Deferred.make<void>()
1135
+ const withPermits = (yield* Effect.makeSemaphore(concurrencyN)).withPermits
1136
+ const pull = yield* toPullIn(core.pipeTo(queueReader, channels), scope)
1137
+
1138
+ function evaluatePull(
1139
+ pull: Effect.Effect<
1140
+ Either.Either<OutElem, OutDone>,
1141
+ OutErr | OutErr1,
1142
+ Env | Env1
1143
+ >
1144
+ ) {
1145
+ return pull.pipe(
1146
+ Effect.flatMap(Either.match({
1147
+ onLeft: (done) => Effect.succeed(Option.some(done)),
1148
+ onRight: (outElem) =>
1149
+ Effect.as(
1150
+ Queue.offer(queue, Effect.succeed(Either.right(outElem))),
1151
+ Option.none()
1152
+ )
1153
+ })),
1154
+ Effect.repeat({ until: (_): _ is Option.Some<OutDone> => Option.isSome(_) }),
1155
+ Effect.flatMap((outDone) =>
1156
+ Ref.update(
1157
+ lastDone,
1158
+ Option.match({
1159
+ onNone: () => Option.some(outDone.value),
1160
+ onSome: (lastDone) => Option.some(f(lastDone, outDone.value))
1161
+ })
1162
+ )
1163
+ ),
1164
+ Effect.catchAllCause((cause) =>
1165
+ Cause.isInterrupted(cause)
1166
+ ? Effect.failCause(cause)
1167
+ : Queue.offer(queue, Effect.failCause(cause)).pipe(
1168
+ Effect.zipRight(Deferred.succeed(errorSignal, void 0)),
1169
+ Effect.asVoid
1170
+ )
1171
+ )
1172
+ )
1173
+ }
1174
+
1175
+ yield* pull.pipe(
1176
+ Effect.matchCauseEffect({
1177
+ onFailure: (cause) =>
1178
+ Queue.offer(queue, Effect.failCause(cause)).pipe(
1179
+ Effect.zipRight(Effect.succeed(false))
1180
+ ),
1181
+ onSuccess: Either.match({
1182
+ onLeft: (outDone) =>
1183
+ Effect.raceWith(
1184
+ Effect.interruptible(Deferred.await(errorSignal)),
1185
+ Effect.interruptible(withPermits(concurrencyN)(Effect.void)),
1186
+ {
1187
+ onSelfDone: (_, permitAcquisition) => Effect.as(Fiber.interrupt(permitAcquisition), false),
1188
+ onOtherDone: (_, failureAwait) =>
1189
+ Effect.zipRight(
1190
+ Fiber.interrupt(failureAwait),
1191
+ Ref.get(lastDone).pipe(
1192
+ Effect.flatMap(Option.match({
1193
+ onNone: () => Queue.offer(queue, Effect.succeed(Either.left(outDone))),
1194
+ onSome: (lastDone) => Queue.offer(queue, Effect.succeed(Either.left(f(lastDone, outDone))))
1195
+ })),
1196
+ Effect.as(false)
1197
+ )
1198
+ )
1199
+ }
1200
+ ),
1201
+ onRight: (channel) =>
1202
+ mergeStrategy_.match(mergeStrategy, {
1203
+ onBackPressure: () =>
1204
+ Effect.gen(function*() {
1205
+ const latch = yield* Deferred.make<void>()
1206
+ const raceEffects = Effect.scopedWith((scope) =>
1207
+ toPullIn(core.pipeTo(queueReader, channel), scope).pipe(
1208
+ Effect.flatMap((pull) =>
1209
+ Effect.race(
1210
+ Effect.exit(evaluatePull(pull)),
1211
+ Effect.exit(Effect.interruptible(Deferred.await(errorSignal)))
1212
+ )
1213
+ ),
1214
+ Effect.flatMap(identity)
1215
+ )
1216
+ )
1217
+ yield* Deferred.succeed(latch, void 0).pipe(
1218
+ Effect.zipRight(raceEffects),
1219
+ withPermits(1),
1220
+ Effect.forkIn(scope)
1221
+ )
1222
+ yield* Deferred.await(latch)
1223
+ const errored = yield* Deferred.isDone(errorSignal)
1224
+ return !errored
1225
+ }),
1226
+ onBufferSliding: () =>
1227
+ Effect.gen(function*() {
1228
+ const canceler = yield* Deferred.make<void>()
1229
+ const latch = yield* Deferred.make<void>()
1230
+ const size = yield* Queue.size(cancelers)
1231
+ yield* Queue.take(cancelers).pipe(
1232
+ Effect.flatMap((canceler) => Deferred.succeed(canceler, void 0)),
1233
+ Effect.when(() => size >= concurrencyN)
1234
+ )
1235
+ yield* Queue.offer(cancelers, canceler)
1236
+ const raceEffects = Effect.scopedWith((scope) =>
1237
+ toPullIn(core.pipeTo(queueReader, channel), scope).pipe(
1238
+ Effect.flatMap((pull) =>
1239
+ Effect.exit(evaluatePull(pull)).pipe(
1240
+ Effect.race(Effect.exit(Effect.interruptible(Deferred.await(errorSignal)))),
1241
+ Effect.race(Effect.exit(Effect.interruptible(Deferred.await(canceler))))
1242
+ )
1243
+ ),
1244
+ Effect.flatMap(identity)
1245
+ )
1246
+ )
1247
+ yield* Deferred.succeed(latch, void 0).pipe(
1248
+ Effect.zipRight(raceEffects),
1249
+ withPermits(1),
1250
+ Effect.forkIn(scope)
1251
+ )
1252
+ yield* Deferred.await(latch)
1253
+ const errored = yield* Deferred.isDone(errorSignal)
1254
+ return !errored
1255
+ })
1256
+ })
1257
+ })
1258
+ }),
1259
+ Effect.repeat({ while: (_) => _ }),
1260
+ Effect.forkIn(scope)
1261
+ )
1262
+
1263
+ const consumer: Channel.Channel<OutElem, unknown, OutErr | OutErr1, unknown, OutDone, unknown, Env | Env1> =
1264
+ pipe(
1265
+ Queue.take(queue),
1266
+ Effect.flatten,
1267
+ Effect.matchCause({
1268
+ onFailure: core.failCause,
1269
+ onSuccess: Either.match({
1270
+ onLeft: core.succeedNow,
1271
+ onRight: (outElem) => core.flatMap(core.write(outElem), () => consumer)
1272
+ })
1273
+ }),
1274
+ unwrap
1275
+ )
1276
+
1277
+ return core.embedInput(consumer, input)
1278
+ })
1279
+ )
1280
+
1281
+ /** @internal */
1282
+ export const mergeMap = dual<
1283
+ <OutElem, OutElem1, InElem1, OutErr1, InErr1, Z, InDone1, Env1>(
1284
+ f: (outElem: OutElem) => Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, Z, InDone1, Env1>,
1285
+ options: {
1286
+ readonly concurrency: number | "unbounded"
1287
+ readonly bufferSize?: number | undefined
1288
+ readonly mergeStrategy?: MergeStrategy.MergeStrategy | undefined
1289
+ }
1290
+ ) => <InElem, OutErr, InErr, OutDone, InDone, Env>(
1291
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
1292
+ ) => Channel.Channel<
1293
+ OutElem1,
1294
+ InElem & InElem1,
1295
+ OutErr1 | OutErr,
1296
+ InErr & InErr1,
1297
+ unknown,
1298
+ InDone & InDone1,
1299
+ Env1 | Env
1300
+ >,
1301
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, InElem1, OutErr1, InErr1, Z, InDone1, Env1>(
1302
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1303
+ f: (outElem: OutElem) => Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, Z, InDone1, Env1>,
1304
+ options: {
1305
+ readonly concurrency: number | "unbounded"
1306
+ readonly bufferSize?: number | undefined
1307
+ readonly mergeStrategy?: MergeStrategy.MergeStrategy | undefined
1308
+ }
1309
+ ) => Channel.Channel<
1310
+ OutElem1,
1311
+ InElem & InElem1,
1312
+ OutErr1 | OutErr,
1313
+ InErr & InErr1,
1314
+ unknown,
1315
+ InDone & InDone1,
1316
+ Env1 | Env
1317
+ >
1318
+ >(3, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, InElem1, OutErr1, InErr1, Z, InDone1, Env1>(
1319
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1320
+ f: (outElem: OutElem) => Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, Z, InDone1, Env1>,
1321
+ options: {
1322
+ readonly concurrency: number | "unbounded"
1323
+ readonly bufferSize?: number | undefined
1324
+ readonly mergeStrategy?: MergeStrategy.MergeStrategy | undefined
1325
+ }
1326
+ ): Channel.Channel<
1327
+ OutElem1,
1328
+ InElem & InElem1,
1329
+ OutErr | OutErr1,
1330
+ InErr & InErr1,
1331
+ unknown,
1332
+ InDone & InDone1,
1333
+ Env | Env1
1334
+ > => mergeAll(options)(mapOut(self, f)))
1335
+
1336
+ /** @internal */
1337
+ export const mergeOut = dual<
1338
+ (
1339
+ n: number
1340
+ ) => <OutElem1, InElem1, OutErr1, InErr1, Z, InDone1, Env1, InElem, OutErr, InErr, OutDone, InDone, Env>(
1341
+ self: Channel.Channel<
1342
+ Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, Z, InDone1, Env1>,
1343
+ InElem,
1344
+ OutErr,
1345
+ InErr,
1346
+ OutDone,
1347
+ InDone,
1348
+ Env
1349
+ >
1350
+ ) => Channel.Channel<
1351
+ OutElem1,
1352
+ InElem & InElem1,
1353
+ OutErr | OutErr1,
1354
+ InErr & InErr1,
1355
+ unknown,
1356
+ InDone & InDone1,
1357
+ Env | Env1
1358
+ >,
1359
+ <OutElem1, InElem1, OutErr1, InErr1, Z, InDone1, Env1, InElem, OutErr, InErr, OutDone, InDone, Env>(
1360
+ self: Channel.Channel<
1361
+ Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, Z, InDone1, Env1>,
1362
+ InElem,
1363
+ OutErr,
1364
+ InErr,
1365
+ OutDone,
1366
+ InDone,
1367
+ Env
1368
+ >,
1369
+ n: number
1370
+ ) => Channel.Channel<
1371
+ OutElem1,
1372
+ InElem & InElem1,
1373
+ OutErr | OutErr1,
1374
+ InErr & InErr1,
1375
+ unknown,
1376
+ InDone & InDone1,
1377
+ Env | Env1
1378
+ >
1379
+ >(2, <OutElem1, InElem1, OutErr1, InErr1, Z, InDone1, Env1, InElem, OutErr, InErr, OutDone, InDone, Env>(
1380
+ self: Channel.Channel<
1381
+ Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, Z, InDone1, Env1>,
1382
+ InElem,
1383
+ OutErr,
1384
+ InErr,
1385
+ OutDone,
1386
+ InDone,
1387
+ Env
1388
+ >,
1389
+ n: number
1390
+ ): Channel.Channel<
1391
+ OutElem1,
1392
+ InElem & InElem1,
1393
+ OutErr | OutErr1,
1394
+ InErr & InErr1,
1395
+ unknown,
1396
+ InDone & InDone1,
1397
+ Env | Env1
1398
+ > => mergeAll({ concurrency: n })(mapOut(self, identity)))
1399
+
1400
+ /** @internal */
1401
+ export const mergeOutWith = dual<
1402
+ <OutDone1>(
1403
+ n: number,
1404
+ f: (o1: OutDone1, o2: OutDone1) => OutDone1
1405
+ ) => <OutElem1, InElem1, OutErr1, InErr1, InDone1, Env1, InElem, OutErr, InErr, InDone, Env>(
1406
+ self: Channel.Channel<
1407
+ Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>,
1408
+ InElem,
1409
+ OutErr,
1410
+ InErr,
1411
+ OutDone1,
1412
+ InDone,
1413
+ Env
1414
+ >
1415
+ ) => Channel.Channel<
1416
+ OutElem1,
1417
+ InElem & InElem1,
1418
+ OutErr | OutErr1,
1419
+ InErr & InErr1,
1420
+ OutDone1,
1421
+ InDone & InDone1,
1422
+ Env | Env1
1423
+ >,
1424
+ <OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1, InElem, OutErr, InErr, InDone, Env>(
1425
+ self: Channel.Channel<
1426
+ Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>,
1427
+ InElem,
1428
+ OutErr,
1429
+ InErr,
1430
+ OutDone1,
1431
+ InDone,
1432
+ Env
1433
+ >,
1434
+ n: number,
1435
+ f: (o1: OutDone1, o2: OutDone1) => OutDone1
1436
+ ) => Channel.Channel<
1437
+ OutElem1,
1438
+ InElem & InElem1,
1439
+ OutErr | OutErr1,
1440
+ InErr & InErr1,
1441
+ OutDone1,
1442
+ InDone & InDone1,
1443
+ Env | Env1
1444
+ >
1445
+ >(3, <OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1, InElem, OutErr, InErr, InDone, Env>(
1446
+ self: Channel.Channel<
1447
+ Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>,
1448
+ InElem,
1449
+ OutErr,
1450
+ InErr,
1451
+ OutDone1,
1452
+ InDone,
1453
+ Env
1454
+ >,
1455
+ n: number,
1456
+ f: (o1: OutDone1, o2: OutDone1) => OutDone1
1457
+ ): Channel.Channel<
1458
+ OutElem1,
1459
+ InElem & InElem1,
1460
+ OutErr | OutErr1,
1461
+ InErr & InErr1,
1462
+ OutDone1,
1463
+ InDone & InDone1,
1464
+ Env | Env1
1465
+ > => mergeAllWith({ concurrency: n })(mapOut(self, identity), f))
1466
+
1467
+ /** @internal */
1468
+ export const mergeWith = dual<
1469
+ <OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1, OutDone, OutErr, OutErr2, OutDone2, OutErr3, OutDone3>(
1470
+ options: {
1471
+ readonly other: Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>
1472
+ readonly onSelfDone: (
1473
+ exit: Exit.Exit<OutDone, OutErr>
1474
+ ) => MergeDecision.MergeDecision<Env1, OutErr1, OutDone1, OutErr2, OutDone2>
1475
+ readonly onOtherDone: (
1476
+ ex: Exit.Exit<OutDone1, OutErr1>
1477
+ ) => MergeDecision.MergeDecision<Env1, OutErr, OutDone, OutErr3, OutDone3>
1478
+ }
1479
+ ) => <Env, InErr, InElem, InDone, OutElem>(
1480
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
1481
+ ) => Channel.Channel<
1482
+ OutElem1 | OutElem,
1483
+ InElem & InElem1,
1484
+ OutErr2 | OutErr3,
1485
+ InErr & InErr1,
1486
+ OutDone2 | OutDone3,
1487
+ InDone & InDone1,
1488
+ Env1 | Env
1489
+ >,
1490
+ <
1491
+ OutElem,
1492
+ InElem,
1493
+ OutErr,
1494
+ InErr,
1495
+ OutDone,
1496
+ InDone,
1497
+ Env,
1498
+ OutElem1,
1499
+ InElem1,
1500
+ OutErr1,
1501
+ InErr1,
1502
+ OutDone1,
1503
+ InDone1,
1504
+ Env1,
1505
+ OutErr2,
1506
+ OutDone2,
1507
+ OutErr3,
1508
+ OutDone3
1509
+ >(
1510
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1511
+ options: {
1512
+ readonly other: Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>
1513
+ readonly onSelfDone: (
1514
+ exit: Exit.Exit<OutDone, OutErr>
1515
+ ) => MergeDecision.MergeDecision<Env1, OutErr1, OutDone1, OutErr2, OutDone2>
1516
+ readonly onOtherDone: (
1517
+ ex: Exit.Exit<OutDone1, OutErr1>
1518
+ ) => MergeDecision.MergeDecision<Env1, OutErr, OutDone, OutErr3, OutDone3>
1519
+ }
1520
+ ) => Channel.Channel<
1521
+ OutElem1 | OutElem,
1522
+ InElem & InElem1,
1523
+ OutErr2 | OutErr3,
1524
+ InErr & InErr1,
1525
+ OutDone2 | OutDone3,
1526
+ InDone & InDone1,
1527
+ Env1 | Env
1528
+ >
1529
+ >(2, <
1530
+ OutElem,
1531
+ InElem,
1532
+ OutErr,
1533
+ InErr,
1534
+ OutDone,
1535
+ InDone,
1536
+ Env,
1537
+ OutElem1,
1538
+ InElem1,
1539
+ OutErr1,
1540
+ InErr1,
1541
+ OutDone1,
1542
+ InDone1,
1543
+ Env1,
1544
+ OutErr2,
1545
+ OutDone2,
1546
+ OutErr3,
1547
+ OutDone3
1548
+ >(
1549
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1550
+ options: {
1551
+ readonly other: Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>
1552
+ readonly onSelfDone: (
1553
+ exit: Exit.Exit<OutDone, OutErr>
1554
+ ) => MergeDecision.MergeDecision<Env1, OutErr1, OutDone1, OutErr2, OutDone2>
1555
+ readonly onOtherDone: (
1556
+ ex: Exit.Exit<OutDone1, OutErr1>
1557
+ ) => MergeDecision.MergeDecision<Env1, OutErr, OutDone, OutErr3, OutDone3>
1558
+ }
1559
+ ): Channel.Channel<
1560
+ OutElem | OutElem1,
1561
+ InElem & InElem1,
1562
+ OutErr2 | OutErr3,
1563
+ InErr & InErr1,
1564
+ OutDone2 | OutDone3,
1565
+ InDone & InDone1,
1566
+ Env1 | Env
1567
+ > => {
1568
+ function merge(scope: Scope.Scope) {
1569
+ return Effect.gen(function*() {
1570
+ type State = MergeState.MergeState<
1571
+ Env | Env1,
1572
+ OutErr,
1573
+ OutErr1,
1574
+ OutErr2 | OutErr3,
1575
+ OutElem | OutElem1,
1576
+ OutDone,
1577
+ OutDone1,
1578
+ OutDone2 | OutDone3
1579
+ >
1580
+
1581
+ const input = yield* singleProducerAsyncInput.make<
1582
+ InErr & InErr1,
1583
+ InElem & InElem1,
1584
+ InDone & InDone1
1585
+ >()
1586
+ const queueReader = fromInput(input)
1587
+ const pullL = yield* toPullIn(core.pipeTo(queueReader, self), scope)
1588
+ const pullR = yield* toPullIn(core.pipeTo(queueReader, options.other), scope)
1589
+
1590
+ function handleSide<Err, Done, Err2, Done2>(
1591
+ exit: Exit.Exit<Either.Either<OutElem | OutElem1, Done>, Err>,
1592
+ fiber: Fiber.Fiber<Either.Either<OutElem | OutElem1, Done2>, Err2>,
1593
+ pull: Effect.Effect<Either.Either<OutElem | OutElem1, Done>, Err, Env | Env1>
1594
+ ) {
1595
+ return (
1596
+ done: (
1597
+ ex: Exit.Exit<Done, Err>
1598
+ ) => MergeDecision.MergeDecision<
1599
+ Env | Env1,
1600
+ Err2,
1601
+ Done2,
1602
+ OutErr2 | OutErr3,
1603
+ OutDone2 | OutDone3
1604
+ >,
1605
+ both: (
1606
+ f1: Fiber.Fiber<Either.Either<OutElem | OutElem1, Done>, Err>,
1607
+ f2: Fiber.Fiber<Either.Either<OutElem | OutElem1, Done2>, Err2>
1608
+ ) => State,
1609
+ single: (
1610
+ f: (
1611
+ ex: Exit.Exit<Done2, Err2>
1612
+ ) => Effect.Effect<OutDone2 | OutDone3, OutErr2 | OutErr3, Env | Env1>
1613
+ ) => State
1614
+ ): Effect.Effect<
1615
+ Channel.Channel<
1616
+ OutElem | OutElem1,
1617
+ unknown,
1618
+ OutErr2 | OutErr3,
1619
+ unknown,
1620
+ OutDone2 | OutDone3,
1621
+ unknown,
1622
+ Env | Env1
1623
+ >,
1624
+ never,
1625
+ Env | Env1
1626
+ > => {
1627
+ function onDecision(
1628
+ decision: MergeDecision.MergeDecision<
1629
+ Env | Env1,
1630
+ Err2,
1631
+ Done2,
1632
+ OutErr2 | OutErr3,
1633
+ OutDone2 | OutDone3
1634
+ >
1635
+ ): Effect.Effect<
1636
+ Channel.Channel<
1637
+ OutElem | OutElem1,
1638
+ unknown,
1639
+ OutErr2 | OutErr3,
1640
+ unknown,
1641
+ OutDone2 | OutDone3,
1642
+ unknown,
1643
+ Env | Env1
1644
+ >
1645
+ > {
1646
+ const op = decision as mergeDecision.Primitive
1647
+ if (op._tag === MergeDecisionOpCodes.OP_DONE) {
1648
+ return Effect.succeed(
1649
+ core.fromEffect(
1650
+ Effect.zipRight(
1651
+ Fiber.interrupt(fiber),
1652
+ op.effect
1653
+ )
1654
+ )
1655
+ )
1656
+ }
1657
+ return Effect.map(
1658
+ Fiber.await(fiber),
1659
+ Exit.match({
1660
+ onFailure: (cause) => core.fromEffect(op.f(Exit.failCause(cause))),
1661
+ onSuccess: Either.match({
1662
+ onLeft: (done) => core.fromEffect(op.f(Exit.succeed(done))),
1663
+ onRight: (elem) => zipRight(core.write(elem), go(single(op.f)))
1664
+ })
1665
+ })
1666
+ )
1667
+ }
1668
+
1669
+ return Exit.match(exit, {
1670
+ onFailure: (cause) => onDecision(done(Exit.failCause(cause))),
1671
+ onSuccess: Either.match({
1672
+ onLeft: (z) => onDecision(done(Exit.succeed(z))),
1673
+ onRight: (elem) =>
1674
+ Effect.succeed(
1675
+ core.flatMap(core.write(elem), () =>
1676
+ core.flatMap(
1677
+ core.fromEffect(Effect.forkIn(Effect.interruptible(pull), scope)),
1678
+ (leftFiber) => go(both(leftFiber, fiber))
1679
+ ))
1680
+ )
1681
+ })
1682
+ })
1683
+ }
1684
+ }
1685
+
1686
+ function go(
1687
+ state: State
1688
+ ): Channel.Channel<
1689
+ OutElem | OutElem1,
1690
+ unknown,
1691
+ OutErr2 | OutErr3,
1692
+ unknown,
1693
+ OutDone2 | OutDone3,
1694
+ unknown,
1695
+ Env | Env1
1696
+ > {
1697
+ switch (state._tag) {
1698
+ case MergeStateOpCodes.OP_BOTH_RUNNING: {
1699
+ const leftJoin = Effect.interruptible(Fiber.join(state.left))
1700
+ const rightJoin = Effect.interruptible(Fiber.join(state.right))
1701
+ return unwrap(
1702
+ Effect.raceWith(leftJoin, rightJoin, {
1703
+ onSelfDone: (leftExit, rf) =>
1704
+ Effect.zipRight(
1705
+ Fiber.interrupt(rf),
1706
+ handleSide(leftExit, state.right, pullL)(
1707
+ options.onSelfDone,
1708
+ mergeState.BothRunning,
1709
+ (f) => mergeState.LeftDone(f)
1710
+ )
1711
+ ),
1712
+ onOtherDone: (rightExit, lf) =>
1713
+ Effect.zipRight(
1714
+ Fiber.interrupt(lf),
1715
+ handleSide(rightExit, state.left, pullR)(
1716
+ options.onOtherDone as (
1717
+ ex: Exit.Exit<OutDone1, InErr1 | OutErr1>
1718
+ ) => MergeDecision.MergeDecision<
1719
+ Env1 | Env,
1720
+ OutErr,
1721
+ OutDone,
1722
+ OutErr2 | OutErr3,
1723
+ OutDone2 | OutDone3
1724
+ >,
1725
+ (left, right) => mergeState.BothRunning(right, left),
1726
+ (f) => mergeState.RightDone(f)
1727
+ )
1728
+ )
1729
+ })
1730
+ )
1731
+ }
1732
+ case MergeStateOpCodes.OP_LEFT_DONE: {
1733
+ return unwrap(
1734
+ Effect.map(
1735
+ Effect.exit(pullR),
1736
+ Exit.match({
1737
+ onFailure: (cause) => core.fromEffect(state.f(Exit.failCause(cause))),
1738
+ onSuccess: Either.match({
1739
+ onLeft: (done) => core.fromEffect(state.f(Exit.succeed(done))),
1740
+ onRight: (elem) =>
1741
+ core.flatMap(
1742
+ core.write(elem),
1743
+ () => go(mergeState.LeftDone(state.f))
1744
+ )
1745
+ })
1746
+ })
1747
+ )
1748
+ )
1749
+ }
1750
+ case MergeStateOpCodes.OP_RIGHT_DONE: {
1751
+ return unwrap(
1752
+ Effect.map(
1753
+ Effect.exit(pullL),
1754
+ Exit.match({
1755
+ onFailure: (cause) => core.fromEffect(state.f(Exit.failCause(cause))),
1756
+ onSuccess: Either.match({
1757
+ onLeft: (done) => core.fromEffect(state.f(Exit.succeed(done))),
1758
+ onRight: (elem) =>
1759
+ core.flatMap(
1760
+ core.write(elem),
1761
+ () => go(mergeState.RightDone(state.f))
1762
+ )
1763
+ })
1764
+ })
1765
+ )
1766
+ )
1767
+ }
1768
+ }
1769
+ }
1770
+
1771
+ return core.fromEffect(
1772
+ Effect.withFiberRuntime<
1773
+ MergeState.MergeState<
1774
+ Env | Env1,
1775
+ OutErr,
1776
+ OutErr1,
1777
+ OutErr2 | OutErr3,
1778
+ OutElem | OutElem1,
1779
+ OutDone,
1780
+ OutDone1,
1781
+ OutDone2 | OutDone3
1782
+ >,
1783
+ never,
1784
+ Env | Env1
1785
+ >((parent) => {
1786
+ const inherit = Effect.withFiberRuntime<void, never, never>((state) => {
1787
+ ;(state as any).transferChildren((parent as any).scope())
1788
+ return Effect.void
1789
+ })
1790
+ const leftFiber = Effect.interruptible(pullL).pipe(
1791
+ Effect.ensuring(inherit),
1792
+ Effect.forkIn(scope)
1793
+ )
1794
+ const rightFiber = Effect.interruptible(pullR).pipe(
1795
+ Effect.ensuring(inherit),
1796
+ Effect.forkIn(scope)
1797
+ )
1798
+ return Effect.zipWith(
1799
+ leftFiber,
1800
+ rightFiber,
1801
+ (left, right): State =>
1802
+ mergeState.BothRunning<
1803
+ Env | Env1,
1804
+ OutErr,
1805
+ OutErr1,
1806
+ OutErr2 | OutErr3,
1807
+ OutElem | OutElem1,
1808
+ OutDone,
1809
+ OutDone1,
1810
+ OutDone2 | OutDone3
1811
+ >(left, right)
1812
+ )
1813
+ })
1814
+ ).pipe(
1815
+ core.flatMap(go),
1816
+ core.embedInput(input)
1817
+ )
1818
+ })
1819
+ }
1820
+ return unwrapScopedWith(merge)
1821
+ })
1822
+
1823
+ /** @internal */
1824
+ export const never: Channel.Channel<never, unknown, never, unknown, never, unknown> = core.fromEffect(
1825
+ Effect.never
1826
+ )
1827
+
1828
+ /** @internal */
1829
+ export const orDie = dual<
1830
+ <E>(
1831
+ error: LazyArg<E>
1832
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
1833
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
1834
+ ) => Channel.Channel<OutElem, InElem, never, InErr, OutDone, InDone, Env>,
1835
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, E>(
1836
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1837
+ error: LazyArg<E>
1838
+ ) => Channel.Channel<OutElem, InElem, never, InErr, OutDone, InDone, Env>
1839
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, E>(
1840
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1841
+ error: LazyArg<E>
1842
+ ): Channel.Channel<OutElem, InElem, never, InErr, OutDone, InDone, Env> => orDieWith(self, error))
1843
+
1844
+ /** @internal */
1845
+ export const orDieWith = dual<
1846
+ <OutErr>(
1847
+ f: (e: OutErr) => unknown
1848
+ ) => <OutElem, InElem, InErr, OutDone, InDone, Env>(
1849
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
1850
+ ) => Channel.Channel<OutElem, InElem, never, InErr, OutDone, InDone, Env>,
1851
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
1852
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1853
+ f: (e: OutErr) => unknown
1854
+ ) => Channel.Channel<OutElem, InElem, never, InErr, OutDone, InDone, Env>
1855
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
1856
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1857
+ f: (e: OutErr) => unknown
1858
+ ): Channel.Channel<OutElem, InElem, never, InErr, OutDone, InDone, Env> =>
1859
+ catchAll(self, (e) => core.failCauseSync(() => Cause.die(f(e)))) as Channel.Channel<
1860
+ OutElem,
1861
+ InElem,
1862
+ never,
1863
+ InErr,
1864
+ OutDone,
1865
+ InDone,
1866
+ Env
1867
+ >)
1868
+
1869
+ /** @internal */
1870
+ export const orElse = dual<
1871
+ <OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
1872
+ that: LazyArg<Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>>
1873
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
1874
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
1875
+ ) => Channel.Channel<
1876
+ OutElem1 | OutElem,
1877
+ InElem & InElem1,
1878
+ OutErr1,
1879
+ InErr & InErr1,
1880
+ OutDone1 | OutDone,
1881
+ InDone & InDone1,
1882
+ Env1 | Env
1883
+ >,
1884
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
1885
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1886
+ that: LazyArg<Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>>
1887
+ ) => Channel.Channel<
1888
+ OutElem1 | OutElem,
1889
+ InElem & InElem1,
1890
+ OutErr1,
1891
+ InErr & InErr1,
1892
+ OutDone1 | OutDone,
1893
+ InDone & InDone1,
1894
+ Env1 | Env
1895
+ >
1896
+ >(
1897
+ 2,
1898
+ <Env, InErr, InElem, InDone, OutErr, OutElem, OutDone, Env1, InErr1, InElem1, InDone1, OutErr1, OutElem1, OutDone1>(
1899
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1900
+ that: LazyArg<Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>>
1901
+ ): Channel.Channel<
1902
+ OutElem | OutElem1,
1903
+ InElem & InElem1,
1904
+ OutErr1,
1905
+ InErr & InErr1,
1906
+ OutDone | OutDone1,
1907
+ InDone & InDone1,
1908
+ Env | Env1
1909
+ > => catchAll(self, that)
1910
+ )
1911
+
1912
+ /** @internal */
1913
+ export const pipeToOrFail = dual<
1914
+ <OutElem2, OutElem, OutErr2, OutDone2, OutDone, Env2>(
1915
+ that: Channel.Channel<OutElem2, OutElem, OutErr2, never, OutDone2, OutDone, Env2>
1916
+ ) => <InElem, OutErr, InErr, InDone, Env>(
1917
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
1918
+ ) => Channel.Channel<OutElem2, InElem, OutErr2 | OutErr, InErr, OutDone2, InDone, Env2 | Env>,
1919
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem2, OutErr2, OutDone2, Env2>(
1920
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1921
+ that: Channel.Channel<OutElem2, OutElem, OutErr2, never, OutDone2, OutDone, Env2>
1922
+ ) => Channel.Channel<OutElem2, InElem, OutErr2 | OutErr, InErr, OutDone2, InDone, Env2 | Env>
1923
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem2, OutErr2, OutDone2, Env2>(
1924
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1925
+ that: Channel.Channel<OutElem2, OutElem, OutErr2, never, OutDone2, OutDone, Env2>
1926
+ ): Channel.Channel<OutElem2, InElem, OutErr | OutErr2, InErr, OutDone2, InDone, Env | Env2> =>
1927
+ core.suspend(() => {
1928
+ let channelException: Channel.ChannelException<OutErr | OutErr2> | undefined = undefined
1929
+
1930
+ const reader: Channel.Channel<OutElem, OutElem, never, OutErr, OutDone, OutDone, Env> = core
1931
+ .readWith({
1932
+ onInput: (outElem) => core.flatMap(core.write(outElem), () => reader),
1933
+ onFailure: (outErr) => {
1934
+ channelException = ChannelException(outErr)
1935
+ return core.failCause(Cause.die(channelException))
1936
+ },
1937
+ onDone: core.succeedNow
1938
+ })
1939
+
1940
+ const writer: Channel.Channel<
1941
+ OutElem2,
1942
+ OutElem2,
1943
+ OutErr2,
1944
+ OutErr2,
1945
+ OutDone2,
1946
+ OutDone2,
1947
+ Env2
1948
+ > = core.readWithCause({
1949
+ onInput: (outElem) => pipe(core.write(outElem), core.flatMap(() => writer)),
1950
+ onFailure: (cause) =>
1951
+ Cause.isDieType(cause) &&
1952
+ isChannelException(cause.defect) &&
1953
+ Equal.equals(cause.defect, channelException)
1954
+ ? core.fail(cause.defect.error as OutErr2)
1955
+ : core.failCause(cause),
1956
+ onDone: core.succeedNow
1957
+ })
1958
+
1959
+ return core.pipeTo(core.pipeTo(core.pipeTo(self, reader), that), writer)
1960
+ }))
1961
+
1962
+ /** @internal */
1963
+ export const provideService = dual<
1964
+ <I, S>(
1965
+ tag: Context.Tag<I, S>,
1966
+ service: Types.NoInfer<S>
1967
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
1968
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
1969
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Exclude<Env, I>>,
1970
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, I, S>(
1971
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1972
+ tag: Context.Tag<I, S>,
1973
+ service: Types.NoInfer<S>
1974
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Exclude<Env, I>>
1975
+ >(3, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, I, S>(
1976
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1977
+ tag: Context.Tag<I, S>,
1978
+ service: Types.NoInfer<S>
1979
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Exclude<Env, I>> => {
1980
+ return core.flatMap(
1981
+ context<any>(),
1982
+ (context) => core.provideContext(self, Context.add(context, tag, service))
1983
+ )
1984
+ })
1985
+
1986
+ /** @internal */
1987
+ export const provideLayer = dual<
1988
+ <Env, OutErr2, Env0>(
1989
+ layer: Layer.Layer<Env, OutErr2, Env0>
1990
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone>(
1991
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
1992
+ ) => Channel.Channel<OutElem, InElem, OutErr2 | OutErr, InErr, OutDone, InDone, Env0>,
1993
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutErr2, Env0>(
1994
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1995
+ layer: Layer.Layer<Env, OutErr2, Env0>
1996
+ ) => Channel.Channel<OutElem, InElem, OutErr2 | OutErr, InErr, OutDone, InDone, Env0>
1997
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutErr2, Env0>(
1998
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
1999
+ layer: Layer.Layer<Env, OutErr2, Env0>
2000
+ ): Channel.Channel<OutElem, InElem, OutErr | OutErr2, InErr, OutDone, InDone, Env0> =>
2001
+ unwrapScopedWith((scope) =>
2002
+ Effect.map(Layer.buildWithScope(layer, scope), (context) => core.provideContext(self, context))
2003
+ ))
2004
+
2005
+ /** @internal */
2006
+ export const mapInputContext = dual<
2007
+ <Env0, Env>(
2008
+ f: (env: Context.Context<Env0>) => Context.Context<Env>
2009
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone>(
2010
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
2011
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env0>,
2012
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, Env0>(
2013
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
2014
+ f: (env: Context.Context<Env0>) => Context.Context<Env>
2015
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env0>
2016
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, Env0>(
2017
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
2018
+ f: (env: Context.Context<Env0>) => Context.Context<Env>
2019
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env0> =>
2020
+ contextWithChannel((context: Context.Context<Env0>) => core.provideContext(self, f(context))))
2021
+
2022
+ /** @internal */
2023
+ export const provideSomeLayer = dual<
2024
+ <R2, OutErr2, Env0>(
2025
+ layer: Layer.Layer<R2, OutErr2, Env0>
2026
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, R>(
2027
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, R>
2028
+ ) => Channel.Channel<OutElem, InElem, OutErr2 | OutErr, InErr, OutDone, InDone, Env0 | Exclude<R, R2>>,
2029
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, R, R2, OutErr2, Env0>(
2030
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, R>,
2031
+ layer: Layer.Layer<R2, OutErr2, Env0>
2032
+ ) => Channel.Channel<OutElem, InElem, OutErr2 | OutErr, InErr, OutDone, InDone, Env0 | Exclude<R, R2>>
2033
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, R, R2, OutErr2, Env0>(
2034
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, R>,
2035
+ layer: Layer.Layer<R2, OutErr2, Env0>
2036
+ ): Channel.Channel<OutElem, InElem, OutErr | OutErr2, InErr, OutDone, InDone, Env0 | Exclude<R, R2>> =>
2037
+ // @ts-expect-error
2038
+ provideLayer(self, Layer.merge(Layer.context<Exclude<R, R2>>(), layer)))
2039
+
2040
+ /** @internal */
2041
+ export const read = <In>(): Channel.Channel<never, In, Option.Option<never>, unknown, In, unknown> =>
2042
+ core.readOrFail<Option.Option<never>, In>(Option.none())
2043
+
2044
+ /** @internal */
2045
+ export const repeated = <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
2046
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
2047
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env> => core.flatMap(self, () => repeated(self))
2048
+
2049
+ /** @internal */
2050
+ export const run = <OutErr, InErr, OutDone, InDone, Env>(
2051
+ self: Channel.Channel<never, unknown, OutErr, InErr, OutDone, InDone, Env>
2052
+ ): Effect.Effect<OutDone, OutErr, Env> => Effect.scopedWith((scope) => executor.runIn(self, scope))
2053
+
2054
+ /** @internal */
2055
+ export const runCollect = <OutElem, OutErr, InErr, OutDone, InDone, Env>(
2056
+ self: Channel.Channel<OutElem, unknown, OutErr, InErr, OutDone, InDone, Env>
2057
+ ): Effect.Effect<[Chunk.Chunk<OutElem>, OutDone], OutErr, Env> => run(core.collectElements(self))
2058
+
2059
+ /** @internal */
2060
+ export const runDrain = <OutElem, OutErr, InErr, OutDone, InDone, Env>(
2061
+ self: Channel.Channel<OutElem, unknown, OutErr, InErr, OutDone, InDone, Env>
2062
+ ): Effect.Effect<OutDone, OutErr, Env> => run(drain(self))
2063
+
2064
+ /** @internal */
2065
+ export const runScoped = <OutErr, InErr, OutDone, InDone, Env>(
2066
+ self: Channel.Channel<never, unknown, OutErr, InErr, OutDone, InDone, Env>
2067
+ ): Effect.Effect<OutDone, OutErr, Env | Scope.Scope> => Effect.scopeWith((scope) => executor.runIn(self, scope))
2068
+
2069
+ /** @internal */
2070
+ export const scoped = <A, E, R>(
2071
+ effect: Effect.Effect<A, E, R>
2072
+ ): Channel.Channel<A, unknown, E, unknown, unknown, unknown, Exclude<R, Scope.Scope>> =>
2073
+ unwrap(
2074
+ Effect.uninterruptibleMask((restore) =>
2075
+ Effect.map(Scope.make(), (scope) =>
2076
+ core.acquireReleaseOut(
2077
+ Effect.tapErrorCause(
2078
+ restore(Scope.extend(effect, scope)),
2079
+ (cause) => Scope.close(scope, Exit.failCause(cause))
2080
+ ),
2081
+ (_, exit) => Scope.close(scope, exit)
2082
+ ))
2083
+ )
2084
+ )
2085
+
2086
+ /** @internal */
2087
+ export const scopedWith = <A, E, R>(
2088
+ f: (scope: Scope.Scope) => Effect.Effect<A, E, R>
2089
+ ): Channel.Channel<A, unknown, E, unknown, unknown, unknown, R> =>
2090
+ unwrapScoped(Effect.map(Effect.scope, (scope) => core.flatMap(core.fromEffect(f(scope)), core.write)))
2091
+
2092
+ /** @internal */
2093
+ export const service = <I, S>(
2094
+ tag: Context.Tag<I, S>
2095
+ ): Channel.Channel<never, unknown, never, unknown, S, unknown, I> => core.fromEffect(tag)
2096
+
2097
+ /** @internal */
2098
+ export const serviceWith = <I, S>(tag: Context.Tag<I, S>) =>
2099
+ <OutDone>(
2100
+ f: (resource: Types.NoInfer<S>) => OutDone
2101
+ ): Channel.Channel<never, unknown, never, unknown, OutDone, unknown, I> => map(service(tag), f)
2102
+
2103
+ /** @internal */
2104
+ export const serviceWithChannel =
2105
+ <I, S>(tag: Context.Tag<I, S>) =>
2106
+ <Env, InErr, InElem, InDone, OutErr, OutElem, OutDone>(
2107
+ f: (resource: Types.NoInfer<S>) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
2108
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env | I> => core.flatMap(service(tag), f)
2109
+
2110
+ /** @internal */
2111
+ export const serviceWithEffect = <I, S>(tag: Context.Tag<I, S>) =>
2112
+ <Env, OutErr, OutDone>(
2113
+ f: (resource: Types.NoInfer<S>) => Effect.Effect<OutDone, OutErr, Env>
2114
+ ): Channel.Channel<never, unknown, OutErr, unknown, OutDone, unknown, Env | I> => mapEffect(service(tag), f)
2115
+
2116
+ /** @internal */
2117
+ export const splitLines = <Err, Done>(): Channel.Channel<
2118
+ Chunk.Chunk<string>,
2119
+ Chunk.Chunk<string>,
2120
+ Err,
2121
+ Err,
2122
+ Done,
2123
+ Done,
2124
+ never
2125
+ > =>
2126
+ core.suspend(() => {
2127
+ let stringBuilder = ""
2128
+ let midCRLF = false
2129
+ const splitLinesChunk = (chunk: Chunk.Chunk<string>): Chunk.Chunk<string> => {
2130
+ const chunkBuilder: Array<string> = []
2131
+ Chunk.map(chunk, (str) => {
2132
+ if (str.length !== 0) {
2133
+ let from = 0
2134
+ let indexOfCR = str.indexOf("\r")
2135
+ let indexOfLF = str.indexOf("\n")
2136
+ if (midCRLF) {
2137
+ if (indexOfLF === 0) {
2138
+ chunkBuilder.push(stringBuilder)
2139
+ stringBuilder = ""
2140
+ from = 1
2141
+ indexOfLF = str.indexOf("\n", from)
2142
+ } else {
2143
+ stringBuilder = stringBuilder + "\r"
2144
+ }
2145
+ midCRLF = false
2146
+ }
2147
+ while (indexOfCR !== -1 || indexOfLF !== -1) {
2148
+ if (indexOfCR === -1 || (indexOfLF !== -1 && indexOfLF < indexOfCR)) {
2149
+ if (stringBuilder.length === 0) {
2150
+ chunkBuilder.push(str.substring(from, indexOfLF))
2151
+ } else {
2152
+ chunkBuilder.push(stringBuilder + str.substring(from, indexOfLF))
2153
+ stringBuilder = ""
2154
+ }
2155
+ from = indexOfLF + 1
2156
+ indexOfLF = str.indexOf("\n", from)
2157
+ } else {
2158
+ if (str.length === indexOfCR + 1) {
2159
+ midCRLF = true
2160
+ indexOfCR = -1
2161
+ } else {
2162
+ if (indexOfLF === indexOfCR + 1) {
2163
+ if (stringBuilder.length === 0) {
2164
+ chunkBuilder.push(str.substring(from, indexOfCR))
2165
+ } else {
2166
+ stringBuilder = stringBuilder + str.substring(from, indexOfCR)
2167
+ chunkBuilder.push(stringBuilder)
2168
+ stringBuilder = ""
2169
+ }
2170
+ from = indexOfCR + 2
2171
+ indexOfCR = str.indexOf("\r", from)
2172
+ indexOfLF = str.indexOf("\n", from)
2173
+ } else {
2174
+ indexOfCR = str.indexOf("\r", indexOfCR + 1)
2175
+ }
2176
+ }
2177
+ }
2178
+ }
2179
+ if (midCRLF) {
2180
+ stringBuilder = stringBuilder + str.substring(from, str.length - 1)
2181
+ } else {
2182
+ stringBuilder = stringBuilder + str.substring(from, str.length)
2183
+ }
2184
+ }
2185
+ })
2186
+ return Chunk.unsafeFromArray(chunkBuilder)
2187
+ }
2188
+ const loop: Channel.Channel<Chunk.Chunk<string>, Chunk.Chunk<string>, Err, Err, Done, Done, never> = core
2189
+ .readWithCause({
2190
+ onInput: (input: Chunk.Chunk<string>) => {
2191
+ const out = splitLinesChunk(input)
2192
+ return Chunk.isEmpty(out)
2193
+ ? loop
2194
+ : core.flatMap(core.write(out), () => loop)
2195
+ },
2196
+ onFailure: (cause) =>
2197
+ stringBuilder.length === 0
2198
+ ? core.failCause(cause)
2199
+ : core.flatMap(core.write(Chunk.of(stringBuilder)), () => core.failCause(cause)),
2200
+ onDone: (done) =>
2201
+ stringBuilder.length === 0
2202
+ ? core.succeed(done)
2203
+ : core.flatMap(core.write(Chunk.of(stringBuilder)), () => core.succeed(done))
2204
+ })
2205
+ return loop
2206
+ })
2207
+
2208
+ /** @internal */
2209
+ export const toPubSub = <Done, Err, Elem>(
2210
+ pubsub: PubSub.PubSub<Either.Either<Elem, Exit.Exit<Done, Err>>>
2211
+ ): Channel.Channel<never, Elem, never, Err, unknown, Done> => toQueue(pubsub)
2212
+
2213
+ /** @internal */
2214
+ export const toPull = <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
2215
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
2216
+ ): Effect.Effect<Effect.Effect<Either.Either<OutElem, OutDone>, OutErr, Env>, never, Env | Scope.Scope> =>
2217
+ Effect.flatMap(Effect.scope, (scope) => toPullIn(self, scope))
2218
+
2219
+ /** @internal */
2220
+ export const toPullIn = dual<
2221
+ (scope: Scope.Scope) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
2222
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
2223
+ ) => Effect.Effect<Effect.Effect<Either.Either<OutElem, OutDone>, OutErr, Env>, never, Env>,
2224
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
2225
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
2226
+ scope: Scope.Scope
2227
+ ) => Effect.Effect<Effect.Effect<Either.Either<OutElem, OutDone>, OutErr, Env>, never, Env>
2228
+ >(2, <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
2229
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
2230
+ scope: Scope.Scope
2231
+ ) =>
2232
+ Effect.zip(
2233
+ Effect.sync(() => new executor.ChannelExecutor(self, void 0, identity)),
2234
+ Effect.runtime<Env>()
2235
+ ).pipe(
2236
+ Effect.tap(([executor, runtime]) =>
2237
+ Scope.addFinalizerExit(scope, (exit) => {
2238
+ const finalizer = executor.close(exit)
2239
+ return finalizer !== undefined
2240
+ ? Effect.provide(finalizer, runtime)
2241
+ : Effect.void
2242
+ })
2243
+ ),
2244
+ Effect.uninterruptible,
2245
+ Effect.map(([executor]) =>
2246
+ Effect.suspend(() =>
2247
+ interpretToPull(
2248
+ executor.run() as ChannelState.ChannelState<OutErr, Env>,
2249
+ executor
2250
+ )
2251
+ )
2252
+ )
2253
+ ))
2254
+
2255
+ /** @internal */
2256
+ const interpretToPull = <Env, InErr, InElem, InDone, OutErr, OutElem, OutDone>(
2257
+ channelState: ChannelState.ChannelState<OutErr, Env>,
2258
+ exec: executor.ChannelExecutor<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
2259
+ ): Effect.Effect<Either.Either<OutElem, OutDone>, OutErr, Env> => {
2260
+ const state = channelState as ChannelState.Primitive
2261
+ switch (state._tag) {
2262
+ case ChannelStateOpCodes.OP_DONE: {
2263
+ return Exit.match(exec.getDone(), {
2264
+ onFailure: Effect.failCause,
2265
+ onSuccess: (done): Effect.Effect<Either.Either<OutElem, OutDone>, OutErr, Env> =>
2266
+ Effect.succeed(Either.left(done))
2267
+ })
2268
+ }
2269
+ case ChannelStateOpCodes.OP_EMIT: {
2270
+ return Effect.succeed(Either.right(exec.getEmit()))
2271
+ }
2272
+ case ChannelStateOpCodes.OP_FROM_EFFECT: {
2273
+ return pipe(
2274
+ state.effect as Effect.Effect<Either.Either<OutElem, OutDone>, OutErr, Env>,
2275
+ Effect.flatMap(() => interpretToPull(exec.run() as ChannelState.ChannelState<OutErr, Env>, exec))
2276
+ )
2277
+ }
2278
+ case ChannelStateOpCodes.OP_READ: {
2279
+ return executor.readUpstream(
2280
+ state,
2281
+ () => interpretToPull(exec.run() as ChannelState.ChannelState<OutErr, Env>, exec),
2282
+ (cause) => Effect.failCause(cause) as Effect.Effect<Either.Either<OutElem, OutDone>, OutErr, Env>
2283
+ )
2284
+ }
2285
+ }
2286
+ }
2287
+
2288
+ /** @internal */
2289
+ export const toQueue = <Done, Err, Elem>(
2290
+ queue: Queue.Enqueue<Either.Either<Elem, Exit.Exit<Done, Err>>>
2291
+ ): Channel.Channel<never, Elem, never, Err, unknown, Done> => core.suspend(() => toQueueInternal(queue))
2292
+
2293
+ /** @internal */
2294
+ const toQueueInternal = <Err, Done, Elem>(
2295
+ queue: Queue.Enqueue<Either.Either<Elem, Exit.Exit<Done, Err>>>
2296
+ ): Channel.Channel<never, Elem, never, Err, unknown, Done> => {
2297
+ return core.readWithCause({
2298
+ onInput: (elem) =>
2299
+ core.flatMap(
2300
+ core.fromEffect(Queue.offer(queue, Either.right(elem))),
2301
+ () => toQueueInternal(queue)
2302
+ ),
2303
+ onFailure: (cause) => core.fromEffect(Queue.offer(queue, Either.left(Exit.failCause(cause)))),
2304
+ onDone: (done) => core.fromEffect(Queue.offer(queue, Either.left(Exit.succeed(done))))
2305
+ })
2306
+ }
2307
+
2308
+ /** @internal */
2309
+ export const unwrap = <OutElem, InElem, OutErr, InErr, OutDone, InDone, R2, E, R>(
2310
+ channel: Effect.Effect<Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, R2>, E, R>
2311
+ ): Channel.Channel<OutElem, InElem, E | OutErr, InErr, OutDone, InDone, R | R2> => flatten(core.fromEffect(channel))
2312
+
2313
+ /** @internal */
2314
+ export const unwrapScoped = <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, E, R>(
2315
+ self: Effect.Effect<Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>, E, R>
2316
+ ): Channel.Channel<OutElem, InElem, E | OutErr, InErr, OutDone, InDone, Env | Exclude<R, Scope.Scope>> =>
2317
+ core.concatAllWith(
2318
+ scoped(self),
2319
+ (d, _) => d,
2320
+ (d, _) => d
2321
+ )
2322
+
2323
+ /** @internal */
2324
+ export const unwrapScopedWith = <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, E, R>(
2325
+ f: (scope: Scope.Scope) => Effect.Effect<Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>, E, R>
2326
+ ): Channel.Channel<OutElem, InElem, E | OutErr, InErr, OutDone, InDone, R | Env> =>
2327
+ core.concatAllWith(
2328
+ scopedWith(f),
2329
+ (d, _) => d,
2330
+ (d, _) => d
2331
+ )
2332
+
2333
+ /** @internal */
2334
+ export const updateService = dual<
2335
+ <I, S>(
2336
+ tag: Context.Tag<I, S>,
2337
+ f: (resource: Types.NoInfer<S>) => Types.NoInfer<S>
2338
+ ) => <OutElem, OutErr, InErr, OutDone, InDone, R>(
2339
+ self: Channel.Channel<OutElem, unknown, OutErr, InErr, OutDone, InDone, R>
2340
+ ) => Channel.Channel<OutElem, unknown, OutErr, InErr, OutDone, InDone, I | R>,
2341
+ <OutElem, OutErr, InErr, OutDone, InDone, R, I, S>(
2342
+ self: Channel.Channel<OutElem, unknown, OutErr, InErr, OutDone, InDone, R>,
2343
+ tag: Context.Tag<I, S>,
2344
+ f: (resource: Types.NoInfer<S>) => Types.NoInfer<S>
2345
+ ) => Channel.Channel<OutElem, unknown, OutErr, InErr, OutDone, InDone, I | R>
2346
+ >(3, <OutElem, OutErr, InErr, OutDone, InDone, R, I, S>(
2347
+ self: Channel.Channel<OutElem, unknown, OutErr, InErr, OutDone, InDone, R>,
2348
+ tag: Context.Tag<I, S>,
2349
+ f: (resource: Types.NoInfer<S>) => Types.NoInfer<S>
2350
+ ): Channel.Channel<OutElem, unknown, OutErr, InErr, OutDone, InDone, R | I> =>
2351
+ mapInputContext(self, (context: Context.Context<R>) =>
2352
+ Context.merge(
2353
+ context,
2354
+ Context.make(tag, f(Context.unsafeGet(context, tag)))
2355
+ )))
2356
+
2357
+ /** @internal */
2358
+ export const withSpan: {
2359
+ (
2360
+ name: string,
2361
+ options?: Tracer.SpanOptions
2362
+ ): <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
2363
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
2364
+ ) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Exclude<Env, Tracer.ParentSpan>>
2365
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
2366
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
2367
+ name: string,
2368
+ options?: Tracer.SpanOptions
2369
+ ): Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Exclude<Env, Tracer.ParentSpan>>
2370
+ } = function() {
2371
+ const dataFirst = typeof arguments[0] !== "string"
2372
+ const name = dataFirst ? arguments[1] : arguments[0]
2373
+ const options = tracer.addSpanStackTrace(dataFirst ? arguments[2] : arguments[1])
2374
+ const acquire = Effect.all([
2375
+ Effect.makeSpan(name, options),
2376
+ Effect.context(),
2377
+ Effect.clock,
2378
+ FiberRef.get(FiberRef.currentTracerTimingEnabled)
2379
+ ])
2380
+ if (dataFirst) {
2381
+ const self = arguments[0]
2382
+ return acquireUseRelease(
2383
+ acquire,
2384
+ ([span, context]) => core.provideContext(self, Context.add(context, tracer.spanTag, span)),
2385
+ ([span, , clock, timingEnabled], exit) => coreEffect.endSpan(span, exit, clock, timingEnabled)
2386
+ )
2387
+ }
2388
+ return (self: Channel.Channel<any>) =>
2389
+ acquireUseRelease(
2390
+ acquire,
2391
+ ([span, context]) => core.provideContext(self, Context.add(context, tracer.spanTag, span)),
2392
+ ([span, , clock, timingEnabled], exit) => coreEffect.endSpan(span, exit, clock, timingEnabled)
2393
+ )
2394
+ } as any
2395
+
2396
+ /** @internal */
2397
+ export const writeAll = <OutElem>(
2398
+ ...outs: Array<OutElem>
2399
+ ): Channel.Channel<OutElem> => writeChunk(Chunk.fromIterable(outs))
2400
+
2401
+ /** @internal */
2402
+ export const writeChunk = <OutElem>(
2403
+ outs: Chunk.Chunk<OutElem>
2404
+ ): Channel.Channel<OutElem> => writeChunkWriter(0, outs.length, outs)
2405
+
2406
+ /** @internal */
2407
+ const writeChunkWriter = <OutElem>(
2408
+ idx: number,
2409
+ len: number,
2410
+ chunk: Chunk.Chunk<OutElem>
2411
+ ): Channel.Channel<OutElem> => {
2412
+ return idx === len
2413
+ ? core.void
2414
+ : pipe(
2415
+ core.write(pipe(chunk, Chunk.unsafeGet(idx))),
2416
+ core.flatMap(() => writeChunkWriter(idx + 1, len, chunk))
2417
+ )
2418
+ }
2419
+
2420
+ /** @internal */
2421
+ export const zip = dual<
2422
+ <OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
2423
+ that: Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>,
2424
+ options?: {
2425
+ readonly concurrent?: boolean | undefined
2426
+ }
2427
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
2428
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
2429
+ ) => Channel.Channel<
2430
+ OutElem1 | OutElem,
2431
+ InElem & InElem1,
2432
+ OutErr1 | OutErr,
2433
+ InErr & InErr1,
2434
+ readonly [OutDone, OutDone1],
2435
+ InDone & InDone1,
2436
+ Env1 | Env
2437
+ >,
2438
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
2439
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
2440
+ that: Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>,
2441
+ options?: {
2442
+ readonly concurrent?: boolean | undefined
2443
+ }
2444
+ ) => Channel.Channel<
2445
+ OutElem1 | OutElem,
2446
+ InElem & InElem1,
2447
+ OutErr1 | OutErr,
2448
+ InErr & InErr1,
2449
+ readonly [OutDone, OutDone1],
2450
+ InDone & InDone1,
2451
+ Env1 | Env
2452
+ >
2453
+ >(
2454
+ (args) => core.isChannel(args[1]),
2455
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
2456
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
2457
+ that: Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>,
2458
+ options?: {
2459
+ readonly concurrent?: boolean | undefined
2460
+ }
2461
+ ): Channel.Channel<
2462
+ OutElem | OutElem1,
2463
+ InElem & InElem1,
2464
+ OutErr | OutErr1,
2465
+ InErr & InErr1,
2466
+ readonly [OutDone, OutDone1],
2467
+ InDone & InDone1,
2468
+ Env | Env1
2469
+ > =>
2470
+ options?.concurrent ?
2471
+ mergeWith(self, {
2472
+ other: that,
2473
+ onSelfDone: (exit1) => mergeDecision.Await((exit2) => Effect.suspend(() => Exit.zip(exit1, exit2))),
2474
+ onOtherDone: (exit2) => mergeDecision.Await((exit1) => Effect.suspend(() => Exit.zip(exit1, exit2)))
2475
+ }) :
2476
+ core.flatMap(self, (a) => map(that, (b) => [a, b] as const))
2477
+ )
2478
+
2479
+ /** @internal */
2480
+ export const zipLeft = dual<
2481
+ <OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
2482
+ that: Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>,
2483
+ options?: {
2484
+ readonly concurrent?: boolean | undefined
2485
+ }
2486
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
2487
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
2488
+ ) => Channel.Channel<
2489
+ OutElem1 | OutElem,
2490
+ InElem & InElem1,
2491
+ OutErr1 | OutErr,
2492
+ InErr & InErr1,
2493
+ OutDone,
2494
+ InDone & InDone1,
2495
+ Env1 | Env
2496
+ >,
2497
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
2498
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
2499
+ that: Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>,
2500
+ options?: {
2501
+ readonly concurrent?: boolean | undefined
2502
+ }
2503
+ ) => Channel.Channel<
2504
+ OutElem1 | OutElem,
2505
+ InElem & InElem1,
2506
+ OutErr1 | OutErr,
2507
+ InErr & InErr1,
2508
+ OutDone,
2509
+ InDone & InDone1,
2510
+ Env1 | Env
2511
+ >
2512
+ >(
2513
+ (args) => core.isChannel(args[1]),
2514
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
2515
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
2516
+ that: Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>,
2517
+ options?: {
2518
+ readonly concurrent?: boolean | undefined
2519
+ }
2520
+ ): Channel.Channel<
2521
+ OutElem | OutElem1,
2522
+ InElem & InElem1,
2523
+ OutErr | OutErr1,
2524
+ InErr & InErr1,
2525
+ OutDone,
2526
+ InDone & InDone1,
2527
+ Env | Env1
2528
+ > =>
2529
+ options?.concurrent ?
2530
+ map(zip(self, that, { concurrent: true }), (tuple) => tuple[0]) :
2531
+ core.flatMap(self, (z) => as(that, z))
2532
+ )
2533
+
2534
+ /** @internal */
2535
+ export const zipRight = dual<
2536
+ <OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
2537
+ that: Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>,
2538
+ options?: {
2539
+ readonly concurrent?: boolean | undefined
2540
+ }
2541
+ ) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
2542
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
2543
+ ) => Channel.Channel<
2544
+ OutElem1 | OutElem,
2545
+ InElem & InElem1,
2546
+ OutErr1 | OutErr,
2547
+ InErr & InErr1,
2548
+ OutDone1,
2549
+ InDone & InDone1,
2550
+ Env1 | Env
2551
+ >,
2552
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
2553
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
2554
+ that: Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>,
2555
+ options?: {
2556
+ readonly concurrent?: boolean | undefined
2557
+ }
2558
+ ) => Channel.Channel<
2559
+ OutElem1 | OutElem,
2560
+ InElem & InElem1,
2561
+ OutErr1 | OutErr,
2562
+ InErr & InErr1,
2563
+ OutDone1,
2564
+ InDone & InDone1,
2565
+ Env1 | Env
2566
+ >
2567
+ >(
2568
+ (args) => core.isChannel(args[1]),
2569
+ <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env, OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>(
2570
+ self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
2571
+ that: Channel.Channel<OutElem1, InElem1, OutErr1, InErr1, OutDone1, InDone1, Env1>,
2572
+ options?: {
2573
+ readonly concurrent?: boolean | undefined
2574
+ }
2575
+ ): Channel.Channel<
2576
+ OutElem | OutElem1,
2577
+ InElem & InElem1,
2578
+ OutErr | OutErr1,
2579
+ InErr & InErr1,
2580
+ OutDone1,
2581
+ InDone & InDone1,
2582
+ Env | Env1
2583
+ > =>
2584
+ options?.concurrent ?
2585
+ map(zip(self, that, { concurrent: true }), (tuple) => tuple[1]) :
2586
+ core.flatMap(self, () => that)
2587
+ )
2588
+
2589
+ /** @internal */
2590
+ export const ChannelExceptionTypeId: Channel.ChannelExceptionTypeId = Symbol.for(
2591
+ "effect/Channel/ChannelException"
2592
+ ) as Channel.ChannelExceptionTypeId
2593
+
2594
+ /** @internal */
2595
+ export const ChannelException = <E>(error: E): Channel.ChannelException<E> => ({
2596
+ _tag: "ChannelException",
2597
+ [ChannelExceptionTypeId]: ChannelExceptionTypeId,
2598
+ error
2599
+ })
2600
+
2601
+ /** @internal */
2602
+ export const isChannelException = (u: unknown): u is Channel.ChannelException<unknown> =>
2603
+ hasProperty(u, ChannelExceptionTypeId)