@clayroach/effect 3.19.14-source-capture.6 → 3.19.14-source-capture.8

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 (371) hide show
  1. package/dist/cjs/Utils.js +1 -1
  2. package/dist/cjs/Utils.js.map +1 -1
  3. package/dist/cjs/internal/clock.js +1 -1
  4. package/dist/cjs/internal/clock.js.map +1 -1
  5. package/dist/esm/Utils.js +1 -1
  6. package/dist/esm/Utils.js.map +1 -1
  7. package/dist/esm/internal/clock.js +1 -1
  8. package/dist/esm/internal/clock.js.map +1 -1
  9. package/package.json +1 -1
  10. package/src/Arbitrary.ts +0 -1101
  11. package/src/Array.ts +0 -3589
  12. package/src/BigDecimal.ts +0 -1349
  13. package/src/BigInt.ts +0 -643
  14. package/src/Boolean.ts +0 -287
  15. package/src/Brand.ts +0 -360
  16. package/src/Cache.ts +0 -281
  17. package/src/Cause.ts +0 -1555
  18. package/src/Channel.ts +0 -2355
  19. package/src/ChildExecutorDecision.ts +0 -146
  20. package/src/Chunk.ts +0 -1495
  21. package/src/Clock.ts +0 -111
  22. package/src/Config.ts +0 -542
  23. package/src/ConfigError.ts +0 -270
  24. package/src/ConfigProvider.ts +0 -333
  25. package/src/ConfigProviderPathPatch.ts +0 -100
  26. package/src/Console.ts +0 -226
  27. package/src/Context.ts +0 -585
  28. package/src/Cron.ts +0 -706
  29. package/src/Data.ts +0 -596
  30. package/src/DateTime.ts +0 -1686
  31. package/src/DefaultServices.ts +0 -34
  32. package/src/Deferred.ts +0 -301
  33. package/src/Differ.ts +0 -450
  34. package/src/Duration.ts +0 -1000
  35. package/src/Effect.ts +0 -14839
  36. package/src/Effectable.ts +0 -107
  37. package/src/Either.ts +0 -1040
  38. package/src/Encoding.ts +0 -195
  39. package/src/Equal.ts +0 -98
  40. package/src/Equivalence.ts +0 -235
  41. package/src/ExecutionPlan.ts +0 -308
  42. package/src/ExecutionStrategy.ts +0 -119
  43. package/src/Exit.ts +0 -467
  44. package/src/FastCheck.ts +0 -9
  45. package/src/Fiber.ts +0 -744
  46. package/src/FiberHandle.ts +0 -540
  47. package/src/FiberId.ts +0 -195
  48. package/src/FiberMap.ts +0 -656
  49. package/src/FiberRef.ts +0 -431
  50. package/src/FiberRefs.ts +0 -204
  51. package/src/FiberRefsPatch.ts +0 -105
  52. package/src/FiberSet.ts +0 -491
  53. package/src/FiberStatus.ts +0 -108
  54. package/src/Function.ts +0 -1222
  55. package/src/GlobalValue.ts +0 -53
  56. package/src/Graph.ts +0 -3732
  57. package/src/GroupBy.ts +0 -103
  58. package/src/HKT.ts +0 -45
  59. package/src/Hash.ts +0 -195
  60. package/src/HashMap.ts +0 -519
  61. package/src/HashRing.ts +0 -317
  62. package/src/HashSet.ts +0 -2346
  63. package/src/Inspectable.ts +0 -287
  64. package/src/Iterable.ts +0 -1119
  65. package/src/JSONSchema.ts +0 -1044
  66. package/src/KeyedPool.ts +0 -167
  67. package/src/Layer.ts +0 -1251
  68. package/src/LayerMap.ts +0 -436
  69. package/src/List.ts +0 -977
  70. package/src/LogLevel.ts +0 -285
  71. package/src/LogSpan.ts +0 -25
  72. package/src/Logger.ts +0 -702
  73. package/src/Mailbox.ts +0 -268
  74. package/src/ManagedRuntime.ts +0 -180
  75. package/src/Match.ts +0 -1477
  76. package/src/MergeDecision.ts +0 -95
  77. package/src/MergeState.ts +0 -172
  78. package/src/MergeStrategy.ts +0 -107
  79. package/src/Metric.ts +0 -780
  80. package/src/MetricBoundaries.ts +0 -69
  81. package/src/MetricHook.ts +0 -151
  82. package/src/MetricKey.ts +0 -224
  83. package/src/MetricKeyType.ts +0 -262
  84. package/src/MetricLabel.ts +0 -47
  85. package/src/MetricPair.ts +0 -71
  86. package/src/MetricPolling.ts +0 -148
  87. package/src/MetricRegistry.ts +0 -48
  88. package/src/MetricState.ts +0 -257
  89. package/src/Micro.ts +0 -4405
  90. package/src/ModuleVersion.ts +0 -18
  91. package/src/MutableHashMap.ts +0 -411
  92. package/src/MutableHashSet.ts +0 -706
  93. package/src/MutableList.ts +0 -297
  94. package/src/MutableQueue.ts +0 -227
  95. package/src/MutableRef.ts +0 -202
  96. package/src/NonEmptyIterable.ts +0 -32
  97. package/src/Number.ts +0 -1071
  98. package/src/Option.ts +0 -2170
  99. package/src/Order.ts +0 -373
  100. package/src/Ordering.ts +0 -111
  101. package/src/ParseResult.ts +0 -2031
  102. package/src/PartitionedSemaphore.ts +0 -200
  103. package/src/Pipeable.ts +0 -566
  104. package/src/Pool.ts +0 -204
  105. package/src/Predicate.ts +0 -1405
  106. package/src/Pretty.ts +0 -205
  107. package/src/PrimaryKey.ts +0 -23
  108. package/src/PubSub.ts +0 -182
  109. package/src/Queue.ts +0 -644
  110. package/src/Random.ts +0 -204
  111. package/src/RateLimiter.ts +0 -138
  112. package/src/RcMap.ts +0 -141
  113. package/src/RcRef.ts +0 -122
  114. package/src/Readable.ts +0 -93
  115. package/src/Record.ts +0 -1274
  116. package/src/RedBlackTree.ts +0 -421
  117. package/src/Redacted.ts +0 -144
  118. package/src/Ref.ts +0 -180
  119. package/src/RegExp.ts +0 -38
  120. package/src/Reloadable.ts +0 -127
  121. package/src/Request.ts +0 -347
  122. package/src/RequestBlock.ts +0 -118
  123. package/src/RequestResolver.ts +0 -366
  124. package/src/Resource.ts +0 -119
  125. package/src/Runtime.ts +0 -383
  126. package/src/RuntimeFlags.ts +0 -368
  127. package/src/RuntimeFlagsPatch.ts +0 -183
  128. package/src/STM.ts +0 -2045
  129. package/src/Schedule.ts +0 -2219
  130. package/src/ScheduleDecision.ts +0 -62
  131. package/src/ScheduleInterval.ts +0 -151
  132. package/src/ScheduleIntervals.ts +0 -122
  133. package/src/Scheduler.ts +0 -353
  134. package/src/Schema.ts +0 -10914
  135. package/src/SchemaAST.ts +0 -3043
  136. package/src/Scope.ts +0 -204
  137. package/src/ScopedCache.ts +0 -151
  138. package/src/ScopedRef.ts +0 -117
  139. package/src/Secret.ts +0 -88
  140. package/src/SingleProducerAsyncInput.ts +0 -67
  141. package/src/Sink.ts +0 -1461
  142. package/src/SortedMap.ts +0 -287
  143. package/src/SortedSet.ts +0 -390
  144. package/src/Stream.ts +0 -6468
  145. package/src/StreamEmit.ts +0 -136
  146. package/src/StreamHaltStrategy.ts +0 -123
  147. package/src/Streamable.ts +0 -45
  148. package/src/String.ts +0 -778
  149. package/src/Struct.ts +0 -243
  150. package/src/Subscribable.ts +0 -100
  151. package/src/SubscriptionRef.ts +0 -298
  152. package/src/Supervisor.ts +0 -240
  153. package/src/Symbol.ts +0 -29
  154. package/src/SynchronizedRef.ts +0 -270
  155. package/src/TArray.ts +0 -495
  156. package/src/TDeferred.ts +0 -100
  157. package/src/TMap.ts +0 -515
  158. package/src/TPriorityQueue.ts +0 -223
  159. package/src/TPubSub.ts +0 -200
  160. package/src/TQueue.ts +0 -432
  161. package/src/TRandom.ts +0 -129
  162. package/src/TReentrantLock.ts +0 -224
  163. package/src/TRef.ts +0 -178
  164. package/src/TSemaphore.ts +0 -129
  165. package/src/TSet.ts +0 -365
  166. package/src/TSubscriptionRef.ts +0 -192
  167. package/src/Take.ts +0 -258
  168. package/src/TestAnnotation.ts +0 -158
  169. package/src/TestAnnotationMap.ts +0 -119
  170. package/src/TestAnnotations.ts +0 -117
  171. package/src/TestClock.ts +0 -556
  172. package/src/TestConfig.ts +0 -47
  173. package/src/TestContext.ts +0 -36
  174. package/src/TestLive.ts +0 -53
  175. package/src/TestServices.ts +0 -390
  176. package/src/TestSized.ts +0 -55
  177. package/src/Tracer.ts +0 -198
  178. package/src/Trie.ts +0 -840
  179. package/src/Tuple.ts +0 -305
  180. package/src/Types.ts +0 -353
  181. package/src/Unify.ts +0 -113
  182. package/src/UpstreamPullRequest.ts +0 -117
  183. package/src/UpstreamPullStrategy.ts +0 -121
  184. package/src/Utils.ts +0 -809
  185. package/src/index.ts +0 -1561
  186. package/src/internal/array.ts +0 -8
  187. package/src/internal/blockedRequests.ts +0 -520
  188. package/src/internal/cache.ts +0 -733
  189. package/src/internal/cause.ts +0 -1050
  190. package/src/internal/channel/channelExecutor.ts +0 -1200
  191. package/src/internal/channel/channelState.ts +0 -134
  192. package/src/internal/channel/childExecutorDecision.ts +0 -96
  193. package/src/internal/channel/continuation.ts +0 -200
  194. package/src/internal/channel/mergeDecision.ts +0 -113
  195. package/src/internal/channel/mergeState.ts +0 -120
  196. package/src/internal/channel/mergeStrategy.ts +0 -72
  197. package/src/internal/channel/singleProducerAsyncInput.ts +0 -259
  198. package/src/internal/channel/subexecutor.ts +0 -229
  199. package/src/internal/channel/upstreamPullRequest.ts +0 -84
  200. package/src/internal/channel/upstreamPullStrategy.ts +0 -87
  201. package/src/internal/channel.ts +0 -2603
  202. package/src/internal/clock.ts +0 -95
  203. package/src/internal/completedRequestMap.ts +0 -9
  204. package/src/internal/concurrency.ts +0 -54
  205. package/src/internal/config.ts +0 -716
  206. package/src/internal/configError.ts +0 -304
  207. package/src/internal/configProvider/pathPatch.ts +0 -97
  208. package/src/internal/configProvider.ts +0 -799
  209. package/src/internal/console.ts +0 -153
  210. package/src/internal/context.ts +0 -337
  211. package/src/internal/core-effect.ts +0 -2293
  212. package/src/internal/core-stream.ts +0 -998
  213. package/src/internal/core.ts +0 -3273
  214. package/src/internal/data.ts +0 -36
  215. package/src/internal/dataSource.ts +0 -327
  216. package/src/internal/dateTime.ts +0 -1277
  217. package/src/internal/defaultServices/console.ts +0 -100
  218. package/src/internal/defaultServices.ts +0 -163
  219. package/src/internal/deferred.ts +0 -46
  220. package/src/internal/differ/chunkPatch.ts +0 -211
  221. package/src/internal/differ/contextPatch.ts +0 -232
  222. package/src/internal/differ/hashMapPatch.ts +0 -220
  223. package/src/internal/differ/hashSetPatch.ts +0 -176
  224. package/src/internal/differ/orPatch.ts +0 -311
  225. package/src/internal/differ/readonlyArrayPatch.ts +0 -210
  226. package/src/internal/differ.ts +0 -200
  227. package/src/internal/doNotation.ts +0 -80
  228. package/src/internal/effect/circular.ts +0 -905
  229. package/src/internal/effectable.ts +0 -131
  230. package/src/internal/either.ts +0 -110
  231. package/src/internal/encoding/base64.ts +0 -286
  232. package/src/internal/encoding/base64Url.ts +0 -29
  233. package/src/internal/encoding/common.ts +0 -51
  234. package/src/internal/encoding/hex.ts +0 -315
  235. package/src/internal/errors.ts +0 -7
  236. package/src/internal/executionPlan.ts +0 -114
  237. package/src/internal/executionStrategy.ts +0 -74
  238. package/src/internal/fiber.ts +0 -388
  239. package/src/internal/fiberId.ts +0 -267
  240. package/src/internal/fiberMessage.ts +0 -82
  241. package/src/internal/fiberRefs/patch.ts +0 -144
  242. package/src/internal/fiberRefs.ts +0 -297
  243. package/src/internal/fiberRuntime.ts +0 -3915
  244. package/src/internal/fiberScope.ts +0 -71
  245. package/src/internal/fiberStatus.ts +0 -119
  246. package/src/internal/groupBy.ts +0 -530
  247. package/src/internal/hashMap/array.ts +0 -49
  248. package/src/internal/hashMap/bitwise.ts +0 -32
  249. package/src/internal/hashMap/config.ts +0 -14
  250. package/src/internal/hashMap/keySet.ts +0 -8
  251. package/src/internal/hashMap/node.ts +0 -391
  252. package/src/internal/hashMap.ts +0 -586
  253. package/src/internal/hashSet.ts +0 -323
  254. package/src/internal/keyedPool.ts +0 -244
  255. package/src/internal/layer/circular.ts +0 -228
  256. package/src/internal/layer.ts +0 -1487
  257. package/src/internal/logSpan.ts +0 -20
  258. package/src/internal/logger-circular.ts +0 -24
  259. package/src/internal/logger.ts +0 -485
  260. package/src/internal/mailbox.ts +0 -561
  261. package/src/internal/managedRuntime/circular.ts +0 -6
  262. package/src/internal/managedRuntime.ts +0 -134
  263. package/src/internal/matcher.ts +0 -652
  264. package/src/internal/metric/boundaries.ts +0 -75
  265. package/src/internal/metric/hook.ts +0 -483
  266. package/src/internal/metric/key.ts +0 -167
  267. package/src/internal/metric/keyType.ts +0 -238
  268. package/src/internal/metric/label.ts +0 -41
  269. package/src/internal/metric/pair.ts +0 -48
  270. package/src/internal/metric/polling.ts +0 -149
  271. package/src/internal/metric/registry.ts +0 -187
  272. package/src/internal/metric/state.ts +0 -290
  273. package/src/internal/metric.ts +0 -577
  274. package/src/internal/opCodes/cause.ts +0 -35
  275. package/src/internal/opCodes/channel.ts +0 -83
  276. package/src/internal/opCodes/channelChildExecutorDecision.ts +0 -17
  277. package/src/internal/opCodes/channelMergeDecision.ts +0 -11
  278. package/src/internal/opCodes/channelMergeState.ts +0 -17
  279. package/src/internal/opCodes/channelMergeStrategy.ts +0 -11
  280. package/src/internal/opCodes/channelState.ts +0 -23
  281. package/src/internal/opCodes/channelUpstreamPullRequest.ts +0 -11
  282. package/src/internal/opCodes/channelUpstreamPullStrategy.ts +0 -11
  283. package/src/internal/opCodes/config.ts +0 -65
  284. package/src/internal/opCodes/configError.ts +0 -35
  285. package/src/internal/opCodes/continuation.ts +0 -11
  286. package/src/internal/opCodes/deferred.ts +0 -11
  287. package/src/internal/opCodes/effect.ts +0 -89
  288. package/src/internal/opCodes/layer.ts +0 -59
  289. package/src/internal/opCodes/streamHaltStrategy.ts +0 -23
  290. package/src/internal/option.ts +0 -80
  291. package/src/internal/pool.ts +0 -432
  292. package/src/internal/pubsub.ts +0 -1762
  293. package/src/internal/query.ts +0 -204
  294. package/src/internal/queue.ts +0 -766
  295. package/src/internal/random.ts +0 -161
  296. package/src/internal/rateLimiter.ts +0 -93
  297. package/src/internal/rcMap.ts +0 -285
  298. package/src/internal/rcRef.ts +0 -192
  299. package/src/internal/redBlackTree/iterator.ts +0 -200
  300. package/src/internal/redBlackTree/node.ts +0 -68
  301. package/src/internal/redBlackTree.ts +0 -1245
  302. package/src/internal/redacted.ts +0 -73
  303. package/src/internal/ref.ts +0 -171
  304. package/src/internal/reloadable.ts +0 -140
  305. package/src/internal/request.ts +0 -177
  306. package/src/internal/resource.ts +0 -76
  307. package/src/internal/ringBuffer.ts +0 -68
  308. package/src/internal/runtime.ts +0 -558
  309. package/src/internal/runtimeFlags.ts +0 -188
  310. package/src/internal/runtimeFlagsPatch.ts +0 -103
  311. package/src/internal/schedule/decision.ts +0 -47
  312. package/src/internal/schedule/interval.ts +0 -101
  313. package/src/internal/schedule/intervals.ts +0 -180
  314. package/src/internal/schedule.ts +0 -2199
  315. package/src/internal/schema/errors.ts +0 -191
  316. package/src/internal/schema/schemaId.ts +0 -106
  317. package/src/internal/schema/util.ts +0 -50
  318. package/src/internal/scopedCache.ts +0 -644
  319. package/src/internal/scopedRef.ts +0 -118
  320. package/src/internal/secret.ts +0 -89
  321. package/src/internal/singleShotGen.ts +0 -35
  322. package/src/internal/sink.ts +0 -2120
  323. package/src/internal/stack.ts +0 -10
  324. package/src/internal/stm/core.ts +0 -817
  325. package/src/internal/stm/entry.ts +0 -59
  326. package/src/internal/stm/journal.ts +0 -123
  327. package/src/internal/stm/opCodes/stm.ts +0 -71
  328. package/src/internal/stm/opCodes/stmState.ts +0 -17
  329. package/src/internal/stm/opCodes/strategy.ts +0 -17
  330. package/src/internal/stm/opCodes/tExit.ts +0 -29
  331. package/src/internal/stm/opCodes/tryCommit.ts +0 -11
  332. package/src/internal/stm/stm.ts +0 -1453
  333. package/src/internal/stm/stmState.ts +0 -136
  334. package/src/internal/stm/tArray.ts +0 -550
  335. package/src/internal/stm/tDeferred.ts +0 -81
  336. package/src/internal/stm/tExit.ts +0 -190
  337. package/src/internal/stm/tMap.ts +0 -824
  338. package/src/internal/stm/tPriorityQueue.ts +0 -267
  339. package/src/internal/stm/tPubSub.ts +0 -551
  340. package/src/internal/stm/tQueue.ts +0 -393
  341. package/src/internal/stm/tRandom.ts +0 -140
  342. package/src/internal/stm/tReentrantLock.ts +0 -352
  343. package/src/internal/stm/tRef.ts +0 -195
  344. package/src/internal/stm/tSemaphore.ts +0 -113
  345. package/src/internal/stm/tSet.ts +0 -259
  346. package/src/internal/stm/tSubscriptionRef.ts +0 -286
  347. package/src/internal/stm/tryCommit.ts +0 -34
  348. package/src/internal/stm/txnId.ts +0 -14
  349. package/src/internal/stm/versioned.ts +0 -4
  350. package/src/internal/stream/debounceState.ts +0 -57
  351. package/src/internal/stream/emit.ts +0 -123
  352. package/src/internal/stream/haltStrategy.ts +0 -94
  353. package/src/internal/stream/handoff.ts +0 -187
  354. package/src/internal/stream/handoffSignal.ts +0 -59
  355. package/src/internal/stream/pull.ts +0 -34
  356. package/src/internal/stream/sinkEndReason.ts +0 -30
  357. package/src/internal/stream/zipAllState.ts +0 -88
  358. package/src/internal/stream/zipChunksState.ts +0 -56
  359. package/src/internal/stream.ts +0 -8801
  360. package/src/internal/string-utils.ts +0 -107
  361. package/src/internal/subscriptionRef.ts +0 -138
  362. package/src/internal/supervisor/patch.ts +0 -190
  363. package/src/internal/supervisor.ts +0 -303
  364. package/src/internal/synchronizedRef.ts +0 -114
  365. package/src/internal/take.ts +0 -199
  366. package/src/internal/testing/sleep.ts +0 -27
  367. package/src/internal/testing/suspendedWarningData.ts +0 -85
  368. package/src/internal/testing/warningData.ts +0 -94
  369. package/src/internal/tracer.ts +0 -293
  370. package/src/internal/trie.ts +0 -722
  371. package/src/internal/version.ts +0 -7
@@ -1,1200 +0,0 @@
1
- import * as Cause from "../../Cause.js"
2
- import type * as Channel from "../../Channel.js"
3
- import type * as ChildExecutorDecision from "../../ChildExecutorDecision.js"
4
- import type * as Context from "../../Context.js"
5
- import * as Deferred from "../../Deferred.js"
6
- import * as Effect from "../../Effect.js"
7
- import * as ExecutionStrategy from "../../ExecutionStrategy.js"
8
- import * as Exit from "../../Exit.js"
9
- import * as Fiber from "../../Fiber.js"
10
- import * as FiberId from "../../FiberId.js"
11
- import { dual, identity, pipe } from "../../Function.js"
12
- import * as HashSet from "../../HashSet.js"
13
- import * as Option from "../../Option.js"
14
- import * as Scope from "../../Scope.js"
15
- import type * as UpstreamPullStrategy from "../../UpstreamPullStrategy.js"
16
- import * as core from "../core-stream.js"
17
- import * as ChannelOpCodes from "../opCodes/channel.js"
18
- import * as ChildExecutorDecisionOpCodes from "../opCodes/channelChildExecutorDecision.js"
19
- import * as ChannelStateOpCodes from "../opCodes/channelState.js"
20
- import * as UpstreamPullStrategyOpCodes from "../opCodes/channelUpstreamPullStrategy.js"
21
- import * as ContinuationOpCodes from "../opCodes/continuation.js"
22
- import * as ChannelState from "./channelState.js"
23
- import * as Continuation from "./continuation.js"
24
- import * as Subexecutor from "./subexecutor.js"
25
- import * as upstreamPullRequest from "./upstreamPullRequest.js"
26
-
27
- export type ErasedChannel<R> = Channel.Channel<unknown, unknown, unknown, unknown, unknown, unknown, R>
28
-
29
- /** @internal */
30
- export type ErasedExecutor<R> = ChannelExecutor<unknown, unknown, unknown, unknown, unknown, unknown, R>
31
-
32
- /** @internal */
33
- export type ErasedContinuation<R> = Continuation.Continuation<
34
- R,
35
- unknown,
36
- unknown,
37
- unknown,
38
- unknown,
39
- unknown,
40
- unknown,
41
- unknown,
42
- unknown
43
- >
44
-
45
- /** @internal */
46
- export type ErasedFinalizer<R> = (exit: Exit.Exit<unknown, unknown>) => Effect.Effect<unknown, never, R>
47
-
48
- /** @internal */
49
- export class ChannelExecutor<
50
- out OutElem,
51
- in InElem = unknown,
52
- out OutErr = never,
53
- in InErr = unknown,
54
- out OutDone = void,
55
- in InDone = unknown,
56
- in out Env = never
57
- > {
58
- private _activeSubexecutor: Subexecutor.Subexecutor<Env> | undefined = undefined
59
-
60
- private _cancelled: Exit.Exit<OutErr, OutDone> | undefined = undefined
61
-
62
- private _closeLastSubstream: Effect.Effect<unknown, never, Env> | undefined = undefined
63
-
64
- private _currentChannel: core.Primitive | undefined
65
-
66
- private _done: Exit.Exit<unknown, unknown> | undefined = undefined
67
-
68
- private _doneStack: Array<ErasedContinuation<Env>> = []
69
-
70
- private _emitted: unknown | undefined = undefined
71
-
72
- private _executeCloseLastSubstream: (
73
- effect: Effect.Effect<unknown, never, Env>
74
- ) => Effect.Effect<unknown, never, Env>
75
-
76
- private _input: ErasedExecutor<Env> | undefined = undefined
77
-
78
- private _inProgressFinalizer: Effect.Effect<unknown, never, Env> | undefined = undefined
79
-
80
- private _providedEnv: Context.Context<unknown> | undefined
81
-
82
- constructor(
83
- initialChannel: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
84
- providedEnv: Context.Context<unknown> | undefined,
85
- executeCloseLastSubstream: (effect: Effect.Effect<unknown, never, Env>) => Effect.Effect<unknown, never, Env>
86
- ) {
87
- this._currentChannel = initialChannel as core.Primitive
88
- this._executeCloseLastSubstream = executeCloseLastSubstream
89
- this._providedEnv = providedEnv
90
- }
91
-
92
- run(): ChannelState.ChannelState<unknown, Env> {
93
- let result: ChannelState.ChannelState<unknown, Env> | undefined = undefined
94
- while (result === undefined) {
95
- if (this._cancelled !== undefined) {
96
- result = this.processCancellation()
97
- } else if (this._activeSubexecutor !== undefined) {
98
- result = this.runSubexecutor()
99
- } else {
100
- try {
101
- if (this._currentChannel === undefined) {
102
- result = ChannelState.Done()
103
- } else {
104
- if (Effect.isEffect(this._currentChannel)) {
105
- this._currentChannel = core.fromEffect(this._currentChannel) as core.Primitive
106
- }
107
- switch (this._currentChannel._tag) {
108
- case ChannelOpCodes.OP_BRACKET_OUT: {
109
- result = this.runBracketOut(this._currentChannel)
110
- break
111
- }
112
-
113
- case ChannelOpCodes.OP_BRIDGE: {
114
- const bridgeInput = this._currentChannel.input
115
-
116
- // PipeTo(left, Bridge(queue, channel))
117
- // In a fiber: repeatedly run left and push its outputs to the queue
118
- // Add a finalizer to interrupt the fiber and close the executor
119
- this._currentChannel = this._currentChannel.channel as core.Primitive
120
-
121
- if (this._input !== undefined) {
122
- const inputExecutor = this._input
123
- this._input = undefined
124
-
125
- const drainer = (): Effect.Effect<unknown, never, Env> =>
126
- Effect.flatMap(bridgeInput.awaitRead(), () =>
127
- Effect.suspend(() => {
128
- const state = inputExecutor.run() as ChannelState.Primitive
129
- switch (state._tag) {
130
- case ChannelStateOpCodes.OP_DONE: {
131
- return Exit.match(inputExecutor.getDone(), {
132
- onFailure: (cause) => bridgeInput.error(cause),
133
- onSuccess: (value) => bridgeInput.done(value)
134
- })
135
- }
136
- case ChannelStateOpCodes.OP_EMIT: {
137
- return Effect.flatMap(
138
- bridgeInput.emit(inputExecutor.getEmit()),
139
- () => drainer()
140
- )
141
- }
142
- case ChannelStateOpCodes.OP_FROM_EFFECT: {
143
- return Effect.matchCauseEffect(state.effect, {
144
- onFailure: (cause) => bridgeInput.error(cause),
145
- onSuccess: () => drainer()
146
- })
147
- }
148
- case ChannelStateOpCodes.OP_READ: {
149
- return readUpstream(
150
- state,
151
- () => drainer(),
152
- (cause) => bridgeInput.error(cause)
153
- )
154
- }
155
- }
156
- })) as Effect.Effect<unknown, never, Env>
157
-
158
- result = ChannelState.fromEffect(
159
- Effect.flatMap(
160
- Effect.forkDaemon(Effect.interruptible(drainer())),
161
- (fiber) =>
162
- Effect.sync(() =>
163
- this.addFinalizer((exit) =>
164
- Effect.flatMap(Fiber.interrupt(fiber), () =>
165
- Effect.suspend(() => {
166
- const effect = this.restorePipe(exit, inputExecutor)
167
- return effect !== undefined ? effect : Effect.void
168
- }))
169
- )
170
- )
171
- )
172
- )
173
- }
174
-
175
- break
176
- }
177
-
178
- case ChannelOpCodes.OP_CONCAT_ALL: {
179
- const executor: ErasedExecutor<Env> = new ChannelExecutor(
180
- this._currentChannel.value() as Channel.Channel<
181
- never,
182
- unknown,
183
- never,
184
- unknown,
185
- never,
186
- unknown,
187
- Env
188
- >,
189
- this._providedEnv,
190
- (effect) =>
191
- Effect.sync(() => {
192
- const prevLastClose = this._closeLastSubstream === undefined
193
- ? Effect.void
194
- : this._closeLastSubstream
195
- this._closeLastSubstream = pipe(prevLastClose, Effect.zipRight(effect))
196
- })
197
- )
198
- executor._input = this._input
199
-
200
- const channel = this._currentChannel
201
- this._activeSubexecutor = new Subexecutor.PullFromUpstream(
202
- executor,
203
- (value) => channel.k(value),
204
- undefined,
205
- [],
206
- (x, y) => channel.combineInners(x, y),
207
- (x, y) => channel.combineAll(x, y),
208
- (request) => channel.onPull(request),
209
- (value) => channel.onEmit(value)
210
- )
211
-
212
- this._closeLastSubstream = undefined
213
- this._currentChannel = undefined
214
-
215
- break
216
- }
217
-
218
- case ChannelOpCodes.OP_EMIT: {
219
- this._emitted = this._currentChannel.out
220
- this._currentChannel = (this._activeSubexecutor !== undefined ?
221
- undefined :
222
- core.void) as core.Primitive | undefined
223
- result = ChannelState.Emit()
224
- break
225
- }
226
-
227
- case ChannelOpCodes.OP_ENSURING: {
228
- this.runEnsuring(this._currentChannel)
229
- break
230
- }
231
-
232
- case ChannelOpCodes.OP_FAIL: {
233
- result = this.doneHalt(this._currentChannel.error())
234
- break
235
- }
236
-
237
- case ChannelOpCodes.OP_FOLD: {
238
- this._doneStack.push(this._currentChannel.k as ErasedContinuation<Env>)
239
- this._currentChannel = this._currentChannel.channel as core.Primitive
240
- break
241
- }
242
-
243
- case ChannelOpCodes.OP_FROM_EFFECT: {
244
- const effect = this._providedEnv === undefined ?
245
- this._currentChannel.effect() :
246
- pipe(
247
- this._currentChannel.effect(),
248
- Effect.provide(this._providedEnv)
249
- )
250
-
251
- result = ChannelState.fromEffect(
252
- Effect.matchCauseEffect(effect, {
253
- onFailure: (cause) => {
254
- const state = this.doneHalt(cause)
255
- return state !== undefined && ChannelState.isFromEffect(state) ?
256
- state.effect :
257
- Effect.void
258
- },
259
- onSuccess: (value) => {
260
- const state = this.doneSucceed(value)
261
- return state !== undefined && ChannelState.isFromEffect(state) ?
262
- state.effect :
263
- Effect.void
264
- }
265
- })
266
- ) as ChannelState.ChannelState<unknown, Env> | undefined
267
-
268
- break
269
- }
270
-
271
- case ChannelOpCodes.OP_PIPE_TO: {
272
- const previousInput = this._input
273
-
274
- const leftExec: ErasedExecutor<Env> = new ChannelExecutor(
275
- this._currentChannel.left() as Channel.Channel<never, unknown, never, unknown, never, unknown, Env>,
276
- this._providedEnv,
277
- (effect) => this._executeCloseLastSubstream(effect)
278
- )
279
- leftExec._input = previousInput
280
- this._input = leftExec
281
-
282
- this.addFinalizer((exit) => {
283
- const effect = this.restorePipe(exit, previousInput)
284
- return effect !== undefined ? effect : Effect.void
285
- })
286
-
287
- this._currentChannel = this._currentChannel.right() as core.Primitive
288
-
289
- break
290
- }
291
-
292
- case ChannelOpCodes.OP_PROVIDE: {
293
- const previousEnv = this._providedEnv
294
- this._providedEnv = this._currentChannel.context()
295
- this._currentChannel = this._currentChannel.inner as core.Primitive
296
- this.addFinalizer(() =>
297
- Effect.sync(() => {
298
- this._providedEnv = previousEnv
299
- })
300
- )
301
- break
302
- }
303
-
304
- case ChannelOpCodes.OP_READ: {
305
- const read = this._currentChannel
306
- result = ChannelState.Read(
307
- this._input!,
308
- identity,
309
- (emitted) => {
310
- try {
311
- this._currentChannel = read.more(emitted) as core.Primitive
312
- } catch (error) {
313
- this._currentChannel = read.done.onExit(Exit.die(error)) as core.Primitive
314
- }
315
- return undefined
316
- },
317
- (exit) => {
318
- const onExit = (exit: Exit.Exit<unknown, unknown>): core.Primitive => {
319
- return read.done.onExit(exit) as core.Primitive
320
- }
321
- this._currentChannel = onExit(exit)
322
- return undefined
323
- }
324
- )
325
- break
326
- }
327
-
328
- case ChannelOpCodes.OP_SUCCEED: {
329
- result = this.doneSucceed(this._currentChannel.evaluate())
330
- break
331
- }
332
-
333
- case ChannelOpCodes.OP_SUCCEED_NOW: {
334
- result = this.doneSucceed(this._currentChannel.terminal)
335
- break
336
- }
337
-
338
- case ChannelOpCodes.OP_SUSPEND: {
339
- this._currentChannel = this._currentChannel.channel() as core.Primitive
340
- break
341
- }
342
- }
343
- }
344
- } catch (error) {
345
- this._currentChannel = core.failCause(Cause.die(error)) as core.Primitive
346
- }
347
- }
348
- }
349
- return result
350
- }
351
-
352
- getDone(): Exit.Exit<OutDone, OutErr> {
353
- return this._done as Exit.Exit<OutDone, OutErr>
354
- }
355
-
356
- getEmit(): OutElem {
357
- return this._emitted as OutElem
358
- }
359
-
360
- cancelWith(exit: Exit.Exit<OutErr, OutDone>): void {
361
- this._cancelled = exit
362
- }
363
-
364
- clearInProgressFinalizer(): void {
365
- this._inProgressFinalizer = undefined
366
- }
367
-
368
- storeInProgressFinalizer(finalizer: Effect.Effect<unknown, never, Env> | undefined): void {
369
- this._inProgressFinalizer = finalizer
370
- }
371
-
372
- popAllFinalizers(exit: Exit.Exit<unknown, unknown>): Effect.Effect<unknown, never, Env> {
373
- const finalizers: Array<ErasedFinalizer<Env>> = []
374
- let next = this._doneStack.pop() as Continuation.Primitive | undefined
375
- while (next) {
376
- if (next._tag === "ContinuationFinalizer") {
377
- finalizers.push(next.finalizer as ErasedFinalizer<Env>)
378
- }
379
- next = this._doneStack.pop() as Continuation.Primitive | undefined
380
- }
381
- const effect = (finalizers.length === 0 ? Effect.void : runFinalizers(finalizers, exit)) as Effect.Effect<
382
- unknown,
383
- never,
384
- Env
385
- >
386
- this.storeInProgressFinalizer(effect)
387
- return effect
388
- }
389
-
390
- popNextFinalizers(): Array<Continuation.ContinuationFinalizer<Env, unknown, unknown>> {
391
- const builder: Array<Continuation.ContinuationFinalizer<Env, unknown, unknown>> = []
392
- while (this._doneStack.length !== 0) {
393
- const cont = this._doneStack[this._doneStack.length - 1] as Continuation.Primitive
394
- if (cont._tag === ContinuationOpCodes.OP_CONTINUATION_K) {
395
- return builder
396
- }
397
- builder.push(cont as Continuation.ContinuationFinalizer<Env, unknown, unknown>)
398
- this._doneStack.pop()
399
- }
400
- return builder
401
- }
402
-
403
- restorePipe(
404
- exit: Exit.Exit<unknown, unknown>,
405
- prev: ErasedExecutor<Env> | undefined
406
- ): Effect.Effect<unknown, never, Env> | undefined {
407
- const currInput = this._input
408
- this._input = prev
409
- if (currInput !== undefined) {
410
- const effect = currInput.close(exit)
411
- return effect
412
- }
413
- return Effect.void
414
- }
415
-
416
- close(exit: Exit.Exit<unknown, unknown>): Effect.Effect<unknown, never, Env> | undefined {
417
- let runInProgressFinalizers: Effect.Effect<unknown, never, Env> | undefined = undefined
418
- const finalizer = this._inProgressFinalizer
419
- if (finalizer !== undefined) {
420
- runInProgressFinalizers = pipe(
421
- finalizer,
422
- Effect.ensuring(Effect.sync(() => this.clearInProgressFinalizer()))
423
- )
424
- }
425
-
426
- let closeSelf: Effect.Effect<unknown, never, Env> | undefined = undefined
427
- const selfFinalizers = this.popAllFinalizers(exit)
428
- if (selfFinalizers !== undefined) {
429
- closeSelf = pipe(
430
- selfFinalizers,
431
- Effect.ensuring(Effect.sync(() => this.clearInProgressFinalizer()))
432
- )
433
- }
434
-
435
- const closeSubexecutors = this._activeSubexecutor === undefined ?
436
- undefined :
437
- this._activeSubexecutor.close(exit)
438
-
439
- if (
440
- closeSubexecutors === undefined &&
441
- runInProgressFinalizers === undefined &&
442
- closeSelf === undefined
443
- ) {
444
- return undefined
445
- }
446
-
447
- return pipe(
448
- Effect.exit(ifNotNull(closeSubexecutors)),
449
- Effect.zip(Effect.exit(ifNotNull(runInProgressFinalizers))),
450
- Effect.zip(Effect.exit(ifNotNull(closeSelf))),
451
- Effect.map(([[exit1, exit2], exit3]) => pipe(exit1, Exit.zipRight(exit2), Exit.zipRight(exit3))),
452
- Effect.uninterruptible,
453
- // TODO: remove
454
- Effect.flatMap((exit) => Effect.suspend(() => exit))
455
- )
456
- }
457
-
458
- doneSucceed(value: unknown): ChannelState.ChannelState<unknown, Env> | undefined {
459
- if (this._doneStack.length === 0) {
460
- this._done = Exit.succeed(value)
461
- this._currentChannel = undefined
462
- return ChannelState.Done()
463
- }
464
-
465
- const head = this._doneStack[this._doneStack.length - 1] as Continuation.Primitive
466
- if (head._tag === ContinuationOpCodes.OP_CONTINUATION_K) {
467
- this._doneStack.pop()
468
- this._currentChannel = head.onSuccess(value) as core.Primitive
469
- return undefined
470
- }
471
-
472
- const finalizers = this.popNextFinalizers()
473
- if (this._doneStack.length === 0) {
474
- this._doneStack = finalizers.reverse()
475
- this._done = Exit.succeed(value)
476
- this._currentChannel = undefined
477
- return ChannelState.Done()
478
- }
479
-
480
- const finalizerEffect = runFinalizers(finalizers.map((f) => f.finalizer), Exit.succeed(value))!
481
- this.storeInProgressFinalizer(finalizerEffect)
482
-
483
- const effect = pipe(
484
- finalizerEffect,
485
- Effect.ensuring(Effect.sync(() => this.clearInProgressFinalizer())),
486
- Effect.uninterruptible,
487
- Effect.flatMap(() => Effect.sync(() => this.doneSucceed(value)))
488
- )
489
-
490
- return ChannelState.fromEffect(effect)
491
- }
492
-
493
- doneHalt(cause: Cause.Cause<unknown>): ChannelState.ChannelState<unknown, Env> | undefined {
494
- if (this._doneStack.length === 0) {
495
- this._done = Exit.failCause(cause)
496
- this._currentChannel = undefined
497
- return ChannelState.Done()
498
- }
499
-
500
- const head = this._doneStack[this._doneStack.length - 1] as Continuation.Primitive
501
- if (head._tag === ContinuationOpCodes.OP_CONTINUATION_K) {
502
- this._doneStack.pop()
503
- try {
504
- this._currentChannel = head.onHalt(cause) as core.Primitive
505
- } catch (error) {
506
- this._currentChannel = core.failCause(Cause.die(error)) as core.Primitive
507
- }
508
- return undefined
509
- }
510
-
511
- const finalizers = this.popNextFinalizers()
512
- if (this._doneStack.length === 0) {
513
- this._doneStack = finalizers.reverse()
514
- this._done = Exit.failCause(cause)
515
- this._currentChannel = undefined
516
- return ChannelState.Done()
517
- }
518
-
519
- const finalizerEffect = runFinalizers(finalizers.map((f) => f.finalizer), Exit.failCause(cause))!
520
- this.storeInProgressFinalizer(finalizerEffect)
521
-
522
- const effect = pipe(
523
- finalizerEffect,
524
- Effect.ensuring(Effect.sync(() => this.clearInProgressFinalizer())),
525
- Effect.uninterruptible,
526
- Effect.flatMap(() => Effect.sync(() => this.doneHalt(cause)))
527
- )
528
-
529
- return ChannelState.fromEffect(effect)
530
- }
531
-
532
- processCancellation(): ChannelState.ChannelState<unknown, Env> {
533
- this._currentChannel = undefined
534
- this._done = this._cancelled
535
- this._cancelled = undefined
536
- return ChannelState.Done()
537
- }
538
-
539
- runBracketOut(bracketOut: core.BracketOut): ChannelState.ChannelState<unknown, Env> {
540
- const effect = Effect.uninterruptible(
541
- Effect.matchCauseEffect(this.provide(bracketOut.acquire() as Effect.Effect<OutDone, OutErr, Env>), {
542
- onFailure: (cause) =>
543
- Effect.sync(() => {
544
- this._currentChannel = core.failCause(cause) as core.Primitive
545
- }),
546
- onSuccess: (out) =>
547
- Effect.sync(() => {
548
- this.addFinalizer((exit) =>
549
- this.provide(bracketOut.finalizer(out, exit)) as Effect.Effect<unknown, never, Env>
550
- )
551
- this._currentChannel = core.write(out) as core.Primitive
552
- })
553
- })
554
- )
555
- return ChannelState.fromEffect(effect) as ChannelState.ChannelState<unknown, Env>
556
- }
557
-
558
- provide(effect: Effect.Effect<unknown, unknown, unknown>): Effect.Effect<unknown, unknown, unknown> {
559
- if (this._providedEnv === undefined) {
560
- return effect
561
- }
562
- return pipe(effect, Effect.provide(this._providedEnv))
563
- }
564
-
565
- runEnsuring(ensuring: core.Ensuring): void {
566
- this.addFinalizer(ensuring.finalizer as ErasedFinalizer<Env>)
567
- this._currentChannel = ensuring.channel as core.Primitive
568
- }
569
-
570
- addFinalizer(f: ErasedFinalizer<Env>): void {
571
- this._doneStack.push(new Continuation.ContinuationFinalizerImpl(f))
572
- }
573
-
574
- runSubexecutor(): ChannelState.ChannelState<unknown, Env> | undefined {
575
- const subexecutor = this._activeSubexecutor as Subexecutor.Primitive<Env>
576
- switch (subexecutor._tag) {
577
- case Subexecutor.OP_PULL_FROM_CHILD: {
578
- return this.pullFromChild(
579
- subexecutor.childExecutor,
580
- subexecutor.parentSubexecutor,
581
- subexecutor.onEmit,
582
- subexecutor
583
- )
584
- }
585
- case Subexecutor.OP_PULL_FROM_UPSTREAM: {
586
- return this.pullFromUpstream(subexecutor)
587
- }
588
- case Subexecutor.OP_DRAIN_CHILD_EXECUTORS: {
589
- return this.drainChildExecutors(subexecutor)
590
- }
591
- case Subexecutor.OP_EMIT: {
592
- this._emitted = subexecutor.value
593
- this._activeSubexecutor = subexecutor.next
594
- return ChannelState.Emit()
595
- }
596
- }
597
- }
598
-
599
- replaceSubexecutor(nextSubExec: Subexecutor.Subexecutor<Env>): void {
600
- this._currentChannel = undefined
601
- this._activeSubexecutor = nextSubExec
602
- }
603
-
604
- finishWithExit(exit: Exit.Exit<unknown, unknown>): Effect.Effect<unknown, unknown, Env> {
605
- const state = Exit.match(exit, {
606
- onFailure: (cause) => this.doneHalt(cause),
607
- onSuccess: (value) => this.doneSucceed(value)
608
- })
609
- this._activeSubexecutor = undefined
610
- return state === undefined ?
611
- Effect.void :
612
- ChannelState.effect(state)
613
- }
614
-
615
- finishSubexecutorWithCloseEffect(
616
- subexecutorDone: Exit.Exit<unknown, unknown>,
617
- ...closeFuncs: Array<(exit: Exit.Exit<unknown, unknown>) => Effect.Effect<unknown, never, Env> | undefined>
618
- ): ChannelState.ChannelState<unknown, Env> | undefined {
619
- this.addFinalizer(() =>
620
- pipe(
621
- closeFuncs,
622
- Effect.forEach((closeFunc) =>
623
- pipe(
624
- Effect.sync(() => closeFunc(subexecutorDone)),
625
- Effect.flatMap((closeEffect) => closeEffect !== undefined ? closeEffect : Effect.void)
626
- ), { discard: true })
627
- )
628
- )
629
- const state = pipe(
630
- subexecutorDone,
631
- Exit.match({
632
- onFailure: (cause) => this.doneHalt(cause),
633
- onSuccess: (value) => this.doneSucceed(value)
634
- })
635
- )
636
- this._activeSubexecutor = undefined
637
- return state
638
- }
639
-
640
- applyUpstreamPullStrategy(
641
- upstreamFinished: boolean,
642
- queue: ReadonlyArray<Subexecutor.PullFromChild<Env> | undefined>,
643
- strategy: UpstreamPullStrategy.UpstreamPullStrategy<unknown>
644
- ): [Option.Option<unknown>, ReadonlyArray<Subexecutor.PullFromChild<Env> | undefined>] {
645
- switch (strategy._tag) {
646
- case UpstreamPullStrategyOpCodes.OP_PULL_AFTER_NEXT: {
647
- const shouldPrepend = !upstreamFinished || queue.some((subexecutor) => subexecutor !== undefined)
648
- return [strategy.emitSeparator, shouldPrepend ? [undefined, ...queue] : queue]
649
- }
650
- case UpstreamPullStrategyOpCodes.OP_PULL_AFTER_ALL_ENQUEUED: {
651
- const shouldEnqueue = !upstreamFinished || queue.some((subexecutor) => subexecutor !== undefined)
652
- return [strategy.emitSeparator, shouldEnqueue ? [...queue, undefined] : queue]
653
- }
654
- }
655
- }
656
-
657
- pullFromChild(
658
- childExecutor: ErasedExecutor<Env>,
659
- parentSubexecutor: Subexecutor.Subexecutor<Env>,
660
- onEmitted: (emitted: unknown) => ChildExecutorDecision.ChildExecutorDecision,
661
- subexecutor: Subexecutor.PullFromChild<Env>
662
- ): ChannelState.ChannelState<unknown, Env> | undefined {
663
- return ChannelState.Read(
664
- childExecutor,
665
- identity,
666
- (emitted) => {
667
- const childExecutorDecision = onEmitted(emitted)
668
- switch (childExecutorDecision._tag) {
669
- case ChildExecutorDecisionOpCodes.OP_CONTINUE: {
670
- break
671
- }
672
- case ChildExecutorDecisionOpCodes.OP_CLOSE: {
673
- this.finishWithDoneValue(childExecutor, parentSubexecutor, childExecutorDecision.value)
674
- break
675
- }
676
- case ChildExecutorDecisionOpCodes.OP_YIELD: {
677
- const modifiedParent = parentSubexecutor.enqueuePullFromChild(subexecutor)
678
- this.replaceSubexecutor(modifiedParent)
679
- break
680
- }
681
- }
682
- this._activeSubexecutor = new Subexecutor.Emit(emitted, this._activeSubexecutor!)
683
- return undefined
684
- },
685
- Exit.match({
686
- onFailure: (cause) => {
687
- const state = this.handleSubexecutorFailure(childExecutor, parentSubexecutor, cause)
688
- return state === undefined ?
689
- undefined :
690
- ChannelState.effectOrUndefinedIgnored(state) as Effect.Effect<void, never, Env>
691
- },
692
- onSuccess: (doneValue) => {
693
- this.finishWithDoneValue(childExecutor, parentSubexecutor, doneValue)
694
- return undefined
695
- }
696
- })
697
- )
698
- }
699
-
700
- finishWithDoneValue(
701
- childExecutor: ErasedExecutor<Env>,
702
- parentSubexecutor: Subexecutor.Subexecutor<Env>,
703
- doneValue: unknown
704
- ): void {
705
- const subexecutor = parentSubexecutor as Subexecutor.Primitive<Env>
706
- switch (subexecutor._tag) {
707
- case Subexecutor.OP_PULL_FROM_UPSTREAM: {
708
- const modifiedParent = new Subexecutor.PullFromUpstream(
709
- subexecutor.upstreamExecutor,
710
- subexecutor.createChild,
711
- subexecutor.lastDone !== undefined
712
- ? subexecutor.combineChildResults(
713
- subexecutor.lastDone,
714
- doneValue
715
- )
716
- : doneValue,
717
- subexecutor.activeChildExecutors,
718
- subexecutor.combineChildResults,
719
- subexecutor.combineWithChildResult,
720
- subexecutor.onPull,
721
- subexecutor.onEmit
722
- )
723
- this._closeLastSubstream = childExecutor.close(Exit.succeed(doneValue))
724
- this.replaceSubexecutor(modifiedParent)
725
- break
726
- }
727
- case Subexecutor.OP_DRAIN_CHILD_EXECUTORS: {
728
- const modifiedParent = new Subexecutor.DrainChildExecutors(
729
- subexecutor.upstreamExecutor,
730
- subexecutor.lastDone !== undefined
731
- ? subexecutor.combineChildResults(
732
- subexecutor.lastDone,
733
- doneValue
734
- )
735
- : doneValue,
736
- subexecutor.activeChildExecutors,
737
- subexecutor.upstreamDone,
738
- subexecutor.combineChildResults,
739
- subexecutor.combineWithChildResult,
740
- subexecutor.onPull
741
- )
742
- this._closeLastSubstream = childExecutor.close(Exit.succeed(doneValue))
743
- this.replaceSubexecutor(modifiedParent)
744
- break
745
- }
746
- default: {
747
- break
748
- }
749
- }
750
- }
751
-
752
- handleSubexecutorFailure(
753
- childExecutor: ErasedExecutor<Env>,
754
- parentSubexecutor: Subexecutor.Subexecutor<Env>,
755
- cause: Cause.Cause<unknown>
756
- ): ChannelState.ChannelState<unknown, Env> | undefined {
757
- return this.finishSubexecutorWithCloseEffect(
758
- Exit.failCause(cause),
759
- (exit) => parentSubexecutor.close(exit),
760
- (exit) => childExecutor.close(exit)
761
- )
762
- }
763
-
764
- pullFromUpstream(
765
- subexecutor: Subexecutor.PullFromUpstream<Env>
766
- ): ChannelState.ChannelState<unknown, Env> | undefined {
767
- if (subexecutor.activeChildExecutors.length === 0) {
768
- return this.performPullFromUpstream(subexecutor)
769
- }
770
-
771
- const activeChild = subexecutor.activeChildExecutors[0]
772
-
773
- const parentSubexecutor = new Subexecutor.PullFromUpstream(
774
- subexecutor.upstreamExecutor,
775
- subexecutor.createChild,
776
- subexecutor.lastDone,
777
- subexecutor.activeChildExecutors.slice(1),
778
- subexecutor.combineChildResults,
779
- subexecutor.combineWithChildResult,
780
- subexecutor.onPull,
781
- subexecutor.onEmit
782
- )
783
-
784
- if (activeChild === undefined) {
785
- return this.performPullFromUpstream(parentSubexecutor)
786
- }
787
-
788
- this.replaceSubexecutor(
789
- new Subexecutor.PullFromChild(
790
- activeChild.childExecutor,
791
- parentSubexecutor,
792
- activeChild.onEmit
793
- )
794
- )
795
-
796
- return undefined
797
- }
798
-
799
- performPullFromUpstream(
800
- subexecutor: Subexecutor.PullFromUpstream<Env>
801
- ): ChannelState.ChannelState<unknown, Env> | undefined {
802
- return ChannelState.Read(
803
- subexecutor.upstreamExecutor,
804
- (effect) => {
805
- const closeLastSubstream = this._closeLastSubstream === undefined ? Effect.void : this._closeLastSubstream
806
- this._closeLastSubstream = undefined
807
- return pipe(
808
- this._executeCloseLastSubstream(closeLastSubstream),
809
- Effect.zipRight(effect)
810
- )
811
- },
812
- (emitted) => {
813
- if (this._closeLastSubstream !== undefined) {
814
- const closeLastSubstream = this._closeLastSubstream
815
- this._closeLastSubstream = undefined
816
- return pipe(
817
- this._executeCloseLastSubstream(closeLastSubstream),
818
- Effect.map(() => {
819
- const childExecutor: ErasedExecutor<Env> = new ChannelExecutor(
820
- subexecutor.createChild(emitted),
821
- this._providedEnv,
822
- this._executeCloseLastSubstream
823
- )
824
-
825
- childExecutor._input = this._input
826
-
827
- const [emitSeparator, updatedChildExecutors] = this.applyUpstreamPullStrategy(
828
- false,
829
- subexecutor.activeChildExecutors,
830
- subexecutor.onPull(upstreamPullRequest.Pulled(emitted))
831
- )
832
-
833
- this._activeSubexecutor = new Subexecutor.PullFromChild(
834
- childExecutor,
835
- new Subexecutor.PullFromUpstream(
836
- subexecutor.upstreamExecutor,
837
- subexecutor.createChild,
838
- subexecutor.lastDone,
839
- updatedChildExecutors,
840
- subexecutor.combineChildResults,
841
- subexecutor.combineWithChildResult,
842
- subexecutor.onPull,
843
- subexecutor.onEmit
844
- ),
845
- subexecutor.onEmit
846
- )
847
-
848
- if (Option.isSome(emitSeparator)) {
849
- this._activeSubexecutor = new Subexecutor.Emit(emitSeparator.value, this._activeSubexecutor)
850
- }
851
-
852
- return undefined
853
- })
854
- )
855
- }
856
-
857
- const childExecutor: ErasedExecutor<Env> = new ChannelExecutor(
858
- subexecutor.createChild(emitted),
859
- this._providedEnv,
860
- this._executeCloseLastSubstream
861
- )
862
-
863
- childExecutor._input = this._input
864
-
865
- const [emitSeparator, updatedChildExecutors] = this.applyUpstreamPullStrategy(
866
- false,
867
- subexecutor.activeChildExecutors,
868
- subexecutor.onPull(upstreamPullRequest.Pulled(emitted))
869
- )
870
-
871
- this._activeSubexecutor = new Subexecutor.PullFromChild(
872
- childExecutor,
873
- new Subexecutor.PullFromUpstream(
874
- subexecutor.upstreamExecutor,
875
- subexecutor.createChild,
876
- subexecutor.lastDone,
877
- updatedChildExecutors,
878
- subexecutor.combineChildResults,
879
- subexecutor.combineWithChildResult,
880
- subexecutor.onPull,
881
- subexecutor.onEmit
882
- ),
883
- subexecutor.onEmit
884
- )
885
-
886
- if (Option.isSome(emitSeparator)) {
887
- this._activeSubexecutor = new Subexecutor.Emit(emitSeparator.value, this._activeSubexecutor)
888
- }
889
-
890
- return undefined
891
- },
892
- (exit) => {
893
- if (subexecutor.activeChildExecutors.some((subexecutor) => subexecutor !== undefined)) {
894
- const drain = new Subexecutor.DrainChildExecutors(
895
- subexecutor.upstreamExecutor,
896
- subexecutor.lastDone,
897
- [undefined, ...subexecutor.activeChildExecutors],
898
- subexecutor.upstreamExecutor.getDone(),
899
- subexecutor.combineChildResults,
900
- subexecutor.combineWithChildResult,
901
- subexecutor.onPull
902
- )
903
-
904
- if (this._closeLastSubstream !== undefined) {
905
- const closeLastSubstream = this._closeLastSubstream
906
- this._closeLastSubstream = undefined
907
- return pipe(
908
- this._executeCloseLastSubstream(closeLastSubstream),
909
- Effect.map(() => this.replaceSubexecutor(drain))
910
- )
911
- }
912
-
913
- this.replaceSubexecutor(drain)
914
-
915
- return undefined
916
- }
917
-
918
- const closeLastSubstream = this._closeLastSubstream
919
- const state = this.finishSubexecutorWithCloseEffect(
920
- pipe(exit, Exit.map((a) => subexecutor.combineWithChildResult(subexecutor.lastDone, a))),
921
- () => closeLastSubstream,
922
- (exit) => subexecutor.upstreamExecutor.close(exit)
923
- )
924
- return state === undefined ?
925
- undefined :
926
- // NOTE: assuming finalizers cannot fail
927
- ChannelState.effectOrUndefinedIgnored(state as ChannelState.ChannelState<never, Env>)
928
- }
929
- )
930
- }
931
-
932
- drainChildExecutors(
933
- subexecutor: Subexecutor.DrainChildExecutors<Env>
934
- ): ChannelState.ChannelState<unknown, Env> | undefined {
935
- if (subexecutor.activeChildExecutors.length === 0) {
936
- const lastClose = this._closeLastSubstream
937
- if (lastClose !== undefined) {
938
- this.addFinalizer(() => Effect.succeed(lastClose))
939
- }
940
- return this.finishSubexecutorWithCloseEffect(
941
- subexecutor.upstreamDone,
942
- () => lastClose,
943
- (exit) => subexecutor.upstreamExecutor.close(exit)
944
- )
945
- }
946
-
947
- const activeChild = subexecutor.activeChildExecutors[0]
948
- const rest = subexecutor.activeChildExecutors.slice(1)
949
-
950
- if (activeChild === undefined) {
951
- const [emitSeparator, remainingExecutors] = this.applyUpstreamPullStrategy(
952
- true,
953
- rest,
954
- subexecutor.onPull(
955
- upstreamPullRequest.NoUpstream(rest.reduce((n, curr) => curr !== undefined ? n + 1 : n, 0))
956
- )
957
- )
958
-
959
- this.replaceSubexecutor(
960
- new Subexecutor.DrainChildExecutors(
961
- subexecutor.upstreamExecutor,
962
- subexecutor.lastDone,
963
- remainingExecutors,
964
- subexecutor.upstreamDone,
965
- subexecutor.combineChildResults,
966
- subexecutor.combineWithChildResult,
967
- subexecutor.onPull
968
- )
969
- )
970
-
971
- if (Option.isSome(emitSeparator)) {
972
- this._emitted = emitSeparator.value
973
- return ChannelState.Emit()
974
- }
975
-
976
- return undefined
977
- }
978
-
979
- const parentSubexecutor = new Subexecutor.DrainChildExecutors(
980
- subexecutor.upstreamExecutor,
981
- subexecutor.lastDone,
982
- rest,
983
- subexecutor.upstreamDone,
984
- subexecutor.combineChildResults,
985
- subexecutor.combineWithChildResult,
986
- subexecutor.onPull
987
- )
988
-
989
- this.replaceSubexecutor(
990
- new Subexecutor.PullFromChild(
991
- activeChild.childExecutor,
992
- parentSubexecutor,
993
- activeChild.onEmit
994
- )
995
- )
996
-
997
- return undefined
998
- }
999
- }
1000
-
1001
- const ifNotNull = <Env>(effect: Effect.Effect<unknown, never, Env> | undefined): Effect.Effect<unknown, never, Env> =>
1002
- effect !== undefined ? effect : Effect.void
1003
-
1004
- const runFinalizers = <Env>(
1005
- finalizers: Array<ErasedFinalizer<Env>>,
1006
- exit: Exit.Exit<unknown, unknown>
1007
- ): Effect.Effect<unknown, never, Env> => {
1008
- return pipe(
1009
- Effect.forEach(finalizers, (fin) => Effect.exit(fin(exit))),
1010
- Effect.map((exits) => pipe(Exit.all(exits), Option.getOrElse(() => Exit.void))),
1011
- Effect.flatMap((exit) => Effect.suspend(() => exit as Exit.Exit<unknown>))
1012
- )
1013
- }
1014
-
1015
- /**
1016
- * @internal
1017
- */
1018
- export const readUpstream = <A, E2, R, E>(
1019
- r: ChannelState.Read,
1020
- onSuccess: () => Effect.Effect<A, E2, R>,
1021
- onFailure: (cause: Cause.Cause<E>) => Effect.Effect<A, E2, R>
1022
- ): Effect.Effect<A, E2, R> => {
1023
- const readStack = [r as ChannelState.Read]
1024
- const read = (): Effect.Effect<A, E2, R> => {
1025
- const current = readStack.pop()
1026
- if (current === undefined || current.upstream === undefined) {
1027
- return Effect.dieMessage("Unexpected end of input for channel execution")
1028
- }
1029
- const state = current.upstream.run() as ChannelState.Primitive
1030
- switch (state._tag) {
1031
- case ChannelStateOpCodes.OP_EMIT: {
1032
- const emitEffect = current.onEmit(current.upstream.getEmit())
1033
- if (readStack.length === 0) {
1034
- if (emitEffect === undefined) {
1035
- return Effect.suspend(onSuccess)
1036
- }
1037
- return pipe(
1038
- emitEffect as Effect.Effect<void>,
1039
- Effect.matchCauseEffect({ onFailure, onSuccess })
1040
- )
1041
- }
1042
- if (emitEffect === undefined) {
1043
- return Effect.suspend(() => read())
1044
- }
1045
- return pipe(
1046
- emitEffect as Effect.Effect<void>,
1047
- Effect.matchCauseEffect({ onFailure, onSuccess: () => read() })
1048
- )
1049
- }
1050
-
1051
- case ChannelStateOpCodes.OP_DONE: {
1052
- const doneEffect = current.onDone(current.upstream.getDone())
1053
- if (readStack.length === 0) {
1054
- if (doneEffect === undefined) {
1055
- return Effect.suspend(onSuccess)
1056
- }
1057
- return pipe(
1058
- doneEffect as Effect.Effect<void>,
1059
- Effect.matchCauseEffect({ onFailure, onSuccess })
1060
- )
1061
- }
1062
- if (doneEffect === undefined) {
1063
- return Effect.suspend(() => read())
1064
- }
1065
- return pipe(
1066
- doneEffect as Effect.Effect<void>,
1067
- Effect.matchCauseEffect({ onFailure, onSuccess: () => read() })
1068
- )
1069
- }
1070
-
1071
- case ChannelStateOpCodes.OP_FROM_EFFECT: {
1072
- readStack.push(current)
1073
- return pipe(
1074
- current.onEffect(state.effect as Effect.Effect<void>) as Effect.Effect<void>,
1075
- Effect.catchAllCause((cause) =>
1076
- Effect.suspend(() => {
1077
- const doneEffect = current.onDone(Exit.failCause(cause)) as Effect.Effect<void>
1078
- return doneEffect === undefined ? Effect.void : doneEffect
1079
- })
1080
- ),
1081
- Effect.matchCauseEffect({ onFailure, onSuccess: () => read() })
1082
- )
1083
- }
1084
-
1085
- case ChannelStateOpCodes.OP_READ: {
1086
- readStack.push(current)
1087
- readStack.push(state)
1088
- return Effect.suspend(() => read())
1089
- }
1090
- }
1091
- }
1092
- return read()
1093
- }
1094
-
1095
- /** @internal */
1096
- export const runIn = dual<
1097
- (scope: Scope.Scope) => <Env, InErr, InDone, OutErr, OutDone>(
1098
- self: Channel.Channel<never, unknown, OutErr, InErr, OutDone, InDone, Env>
1099
- ) => Effect.Effect<OutDone, OutErr, Env>,
1100
- <Env, InErr, InDone, OutErr, OutDone>(
1101
- self: Channel.Channel<never, unknown, OutErr, InErr, OutDone, InDone, Env>,
1102
- scope: Scope.Scope
1103
- ) => Effect.Effect<OutDone, OutErr, Env>
1104
- >(2, <Env, InErr, InDone, OutErr, OutDone>(
1105
- self: Channel.Channel<never, unknown, OutErr, InErr, OutDone, InDone, Env>,
1106
- scope: Scope.Scope
1107
- ) => {
1108
- const run = (
1109
- channelDeferred: Deferred.Deferred<OutDone, OutErr>,
1110
- scopeDeferred: Deferred.Deferred<void>,
1111
- scope: Scope.Scope
1112
- ) =>
1113
- Effect.acquireUseRelease(
1114
- Effect.sync(() => new ChannelExecutor(self, void 0, identity)),
1115
- (exec) =>
1116
- Effect.suspend(() =>
1117
- runScopedInterpret(exec.run() as ChannelState.ChannelState<OutErr, Env>, exec).pipe(
1118
- Effect.intoDeferred(channelDeferred),
1119
- Effect.zipRight(Deferred.await(channelDeferred)),
1120
- Effect.zipLeft(Deferred.await(scopeDeferred))
1121
- )
1122
- ),
1123
- (exec, exit) => {
1124
- const finalize = exec.close(exit)
1125
- if (finalize === undefined) {
1126
- return Effect.void
1127
- }
1128
- return Effect.tapErrorCause(
1129
- finalize,
1130
- (cause) => Scope.addFinalizer(scope, Effect.failCause(cause))
1131
- )
1132
- }
1133
- )
1134
- return Effect.uninterruptibleMask((restore) =>
1135
- Effect.all([
1136
- Scope.fork(scope, ExecutionStrategy.sequential),
1137
- Deferred.make<OutDone, OutErr>(),
1138
- Deferred.make<void>()
1139
- ]).pipe(Effect.flatMap(([child, channelDeferred, scopeDeferred]) =>
1140
- restore(run(channelDeferred, scopeDeferred, child)).pipe(
1141
- Effect.forkIn(scope),
1142
- Effect.flatMap((fiber) =>
1143
- scope.addFinalizer((exit) => {
1144
- const interruptors = Exit.isFailure(exit) ? Cause.interruptors(exit.cause) : undefined
1145
- return Deferred.isDone(channelDeferred).pipe(
1146
- Effect.flatMap((isDone) =>
1147
- isDone
1148
- ? Deferred.succeed(scopeDeferred, void 0).pipe(
1149
- Effect.zipRight(Fiber.await(fiber)),
1150
- Effect.zipRight(Fiber.inheritAll(fiber))
1151
- )
1152
- : Deferred.succeed(scopeDeferred, void 0).pipe(
1153
- Effect.zipRight(
1154
- interruptors && HashSet.size(interruptors) > 0
1155
- ? Fiber.interruptAs(fiber, FiberId.combineAll(interruptors))
1156
- : Fiber.interrupt(fiber)
1157
- ),
1158
- Effect.zipRight(Fiber.inheritAll(fiber))
1159
- )
1160
- )
1161
- )
1162
- }).pipe(Effect.zipRight(restore(Deferred.await(channelDeferred))))
1163
- )
1164
- )
1165
- ))
1166
- )
1167
- })
1168
-
1169
- /** @internal */
1170
- const runScopedInterpret = <Env, InErr, InDone, OutErr, OutDone>(
1171
- channelState: ChannelState.ChannelState<OutErr, Env>,
1172
- exec: ChannelExecutor<never, unknown, OutErr, InErr, OutDone, InDone, Env>
1173
- ): Effect.Effect<OutDone, OutErr, Env> => {
1174
- const op = channelState as ChannelState.Primitive
1175
- switch (op._tag) {
1176
- case ChannelStateOpCodes.OP_FROM_EFFECT: {
1177
- return pipe(
1178
- op.effect as Effect.Effect<OutDone, OutErr, Env>,
1179
- Effect.flatMap(() => runScopedInterpret(exec.run() as ChannelState.ChannelState<OutErr, Env>, exec))
1180
- )
1181
- }
1182
- case ChannelStateOpCodes.OP_EMIT: {
1183
- // Can't really happen because Out <:< Nothing. So just skip ahead.
1184
- return runScopedInterpret<Env, InErr, InDone, OutErr, OutDone>(
1185
- exec.run() as ChannelState.ChannelState<OutErr, Env>,
1186
- exec
1187
- )
1188
- }
1189
- case ChannelStateOpCodes.OP_DONE: {
1190
- return Effect.suspend(() => exec.getDone())
1191
- }
1192
- case ChannelStateOpCodes.OP_READ: {
1193
- return readUpstream(
1194
- op,
1195
- () => runScopedInterpret(exec.run() as ChannelState.ChannelState<OutErr, Env>, exec),
1196
- Effect.failCause
1197
- ) as Effect.Effect<OutDone, OutErr, Env>
1198
- }
1199
- }
1200
- }