@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,2120 +0,0 @@
1
- import * as Arr from "../Array.js"
2
- import * as Cause from "../Cause.js"
3
- import type * as Channel from "../Channel.js"
4
- import * as Chunk from "../Chunk.js"
5
- import * as Clock from "../Clock.js"
6
- import type * as Context from "../Context.js"
7
- import * as Duration from "../Duration.js"
8
- import * as Effect from "../Effect.js"
9
- import * as Either from "../Either.js"
10
- import * as Exit from "../Exit.js"
11
- import { constTrue, dual, identity, pipe } from "../Function.js"
12
- import type { LazyArg } from "../Function.js"
13
- import * as HashMap from "../HashMap.js"
14
- import * as HashSet from "../HashSet.js"
15
- import type * as MergeDecision from "../MergeDecision.js"
16
- import * as Option from "../Option.js"
17
- import { pipeArguments } from "../Pipeable.js"
18
- import { hasProperty, type Predicate, type Refinement } from "../Predicate.js"
19
- import * as PubSub from "../PubSub.js"
20
- import * as Queue from "../Queue.js"
21
- import * as Ref from "../Ref.js"
22
- import * as Scope from "../Scope.js"
23
- import type * as Sink from "../Sink.js"
24
- import type * as Types from "../Types.js"
25
- import * as channel from "./channel.js"
26
- import * as mergeDecision from "./channel/mergeDecision.js"
27
- import * as core from "./core-stream.js"
28
-
29
- /** @internal */
30
- export const SinkTypeId: Sink.SinkTypeId = Symbol.for("effect/Sink") as Sink.SinkTypeId
31
-
32
- const sinkVariance = {
33
- /* c8 ignore next */
34
- _A: (_: never) => _,
35
- /* c8 ignore next */
36
- _In: (_: unknown) => _,
37
- /* c8 ignore next */
38
- _L: (_: never) => _,
39
- /* c8 ignore next */
40
- _E: (_: never) => _,
41
- /* c8 ignore next */
42
- _R: (_: never) => _
43
- }
44
-
45
- /** @internal */
46
- export class SinkImpl<out A, in In = unknown, out L = never, out E = never, out R = never>
47
- implements Sink.Sink<A, In, L, E, R>
48
- {
49
- readonly [SinkTypeId] = sinkVariance
50
- constructor(
51
- readonly channel: Channel.Channel<Chunk.Chunk<L>, Chunk.Chunk<In>, E, never, A, unknown, R>
52
- ) {
53
- }
54
- pipe() {
55
- return pipeArguments(this, arguments)
56
- }
57
- }
58
-
59
- /** @internal */
60
- export const isSink = (u: unknown): u is Sink.Sink<unknown, unknown, unknown, unknown, unknown> =>
61
- hasProperty(u, SinkTypeId)
62
-
63
- /** @internal */
64
- export const suspend = <A, In, L, E, R>(evaluate: LazyArg<Sink.Sink<A, In, L, E, R>>): Sink.Sink<A, In, L, E, R> =>
65
- new SinkImpl(core.suspend(() => toChannel(evaluate())))
66
-
67
- /** @internal */
68
- export const as = dual<
69
- <A2>(a: A2) => <A, In, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A2, In, L, E, R>,
70
- <A, In, L, E, R, A2>(self: Sink.Sink<A, In, L, E, R>, a: A2) => Sink.Sink<A2, In, L, E, R>
71
- >(
72
- 2,
73
- (self, a) => pipe(self, map(() => a))
74
- )
75
-
76
- /** @internal */
77
- export const collectAll = <In>(): Sink.Sink<Chunk.Chunk<In>, In> => new SinkImpl(collectAllLoop(Chunk.empty()))
78
-
79
- /** @internal */
80
- const collectAllLoop = <In>(
81
- acc: Chunk.Chunk<In>
82
- ): Channel.Channel<never, Chunk.Chunk<In>, never, never, Chunk.Chunk<In>, unknown> =>
83
- core.readWithCause({
84
- onInput: (chunk: Chunk.Chunk<In>) => collectAllLoop(pipe(acc, Chunk.appendAll(chunk))),
85
- onFailure: core.failCause,
86
- onDone: () => core.succeed(acc)
87
- })
88
-
89
- /** @internal */
90
- export const collectAllN = <In>(n: number): Sink.Sink<Chunk.Chunk<In>, In, In> =>
91
- suspend(() => fromChannel(collectAllNLoop(n, Chunk.empty())))
92
-
93
- /** @internal */
94
- const collectAllNLoop = <In>(
95
- n: number,
96
- acc: Chunk.Chunk<In>
97
- ): Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, never, never, Chunk.Chunk<In>, unknown> =>
98
- core.readWithCause({
99
- onInput: (chunk: Chunk.Chunk<In>) => {
100
- const [collected, leftovers] = Chunk.splitAt(chunk, n)
101
- if (collected.length < n) {
102
- return collectAllNLoop(n - collected.length, Chunk.appendAll(acc, collected))
103
- }
104
- if (Chunk.isEmpty(leftovers)) {
105
- return core.succeed(Chunk.appendAll(acc, collected))
106
- }
107
- return core.flatMap(core.write(leftovers), () => core.succeed(Chunk.appendAll(acc, collected)))
108
- },
109
- onFailure: core.failCause,
110
- onDone: () => core.succeed(acc)
111
- })
112
-
113
- /** @internal */
114
- export const collectAllFrom = <A, In, L extends In, E, R>(
115
- self: Sink.Sink<A, In, L, E, R>
116
- ): Sink.Sink<Chunk.Chunk<A>, In, L, E, R> =>
117
- collectAllWhileWith(self, {
118
- initial: Chunk.empty<A>(),
119
- while: constTrue,
120
- body: (chunk, a) => pipe(chunk, Chunk.append(a))
121
- })
122
-
123
- /** @internal */
124
- export const collectAllToMap = <In, K>(
125
- key: (input: In) => K,
126
- merge: (x: In, y: In) => In
127
- ): Sink.Sink<HashMap.HashMap<K, In>, In> => {
128
- return foldLeftChunks(HashMap.empty<K, In>(), (map, chunk) =>
129
- pipe(
130
- chunk,
131
- Chunk.reduce(map, (map, input) => {
132
- const k: K = key(input)
133
- const v: In = pipe(map, HashMap.has(k)) ?
134
- merge(pipe(map, HashMap.unsafeGet(k)), input) :
135
- input
136
- return pipe(map, HashMap.set(k, v))
137
- })
138
- ))
139
- }
140
-
141
- /** @internal */
142
- export const collectAllToMapN = <In, K>(
143
- n: number,
144
- key: (input: In) => K,
145
- merge: (x: In, y: In) => In
146
- ): Sink.Sink<HashMap.HashMap<K, In>, In, In> => {
147
- return foldWeighted<HashMap.HashMap<K, In>, In>({
148
- initial: HashMap.empty(),
149
- maxCost: n,
150
- cost: (acc, input) => pipe(acc, HashMap.has(key(input))) ? 0 : 1,
151
- body: (acc, input) => {
152
- const k: K = key(input)
153
- const v: In = pipe(acc, HashMap.has(k)) ?
154
- merge(pipe(acc, HashMap.unsafeGet(k)), input) :
155
- input
156
- return pipe(acc, HashMap.set(k, v))
157
- }
158
- })
159
- }
160
-
161
- /** @internal */
162
- export const collectAllToSet = <In>(): Sink.Sink<HashSet.HashSet<In>, In> =>
163
- foldLeftChunks<HashSet.HashSet<In>, In>(
164
- HashSet.empty(),
165
- (acc, chunk) => pipe(chunk, Chunk.reduce(acc, (acc, input) => pipe(acc, HashSet.add(input))))
166
- )
167
-
168
- /** @internal */
169
- export const collectAllToSetN = <In>(n: number): Sink.Sink<HashSet.HashSet<In>, In, In> =>
170
- foldWeighted<HashSet.HashSet<In>, In>({
171
- initial: HashSet.empty(),
172
- maxCost: n,
173
- cost: (acc, input) => HashSet.has(acc, input) ? 0 : 1,
174
- body: (acc, input) => HashSet.add(acc, input)
175
- })
176
-
177
- /** @internal */
178
- export const collectAllUntil = <In>(p: Predicate<In>): Sink.Sink<Chunk.Chunk<In>, In, In> => {
179
- return pipe(
180
- fold<[Chunk.Chunk<In>, boolean], In>(
181
- [Chunk.empty(), true],
182
- (tuple) => tuple[1],
183
- ([chunk, _], input) => [pipe(chunk, Chunk.append(input)), !p(input)]
184
- ),
185
- map((tuple) => tuple[0])
186
- )
187
- }
188
-
189
- /** @internal */
190
- export const collectAllUntilEffect = <In, E, R>(p: (input: In) => Effect.Effect<boolean, E, R>) => {
191
- return pipe(
192
- foldEffect<[Chunk.Chunk<In>, boolean], In, E, R>(
193
- [Chunk.empty(), true],
194
- (tuple) => tuple[1],
195
- ([chunk, _], input) => pipe(p(input), Effect.map((bool) => [pipe(chunk, Chunk.append(input)), !bool]))
196
- ),
197
- map((tuple) => tuple[0])
198
- )
199
- }
200
-
201
- /** @internal */
202
- export const collectAllWhile: {
203
- <In, Out extends In>(refinement: Refinement<In, Out>): Sink.Sink<Chunk.Chunk<Out>, In, In>
204
- <In>(predicate: Predicate<In>): Sink.Sink<Chunk.Chunk<In>, In, In>
205
- } = <In>(predicate: Predicate<In>): Sink.Sink<Chunk.Chunk<In>, In, In> =>
206
- fromChannel(collectAllWhileReader(predicate, Chunk.empty()))
207
-
208
- /** @internal */
209
- const collectAllWhileReader = <In>(
210
- predicate: Predicate<In>,
211
- done: Chunk.Chunk<In>
212
- ): Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, never, never, Chunk.Chunk<In>, unknown> =>
213
- core.readWith({
214
- onInput: (input: Chunk.Chunk<In>) => {
215
- const [collected, leftovers] = pipe(Chunk.toReadonlyArray(input), Arr.span(predicate))
216
- if (leftovers.length === 0) {
217
- return collectAllWhileReader(
218
- predicate,
219
- pipe(done, Chunk.appendAll(Chunk.unsafeFromArray(collected)))
220
- )
221
- }
222
- return pipe(
223
- core.write(Chunk.unsafeFromArray(leftovers)),
224
- channel.zipRight(core.succeed(pipe(done, Chunk.appendAll(Chunk.unsafeFromArray(collected)))))
225
- )
226
- },
227
- onFailure: core.fail,
228
- onDone: () => core.succeed(done)
229
- })
230
-
231
- /** @internal */
232
- export const collectAllWhileEffect = <In, E, R>(
233
- predicate: (input: In) => Effect.Effect<boolean, E, R>
234
- ): Sink.Sink<Chunk.Chunk<In>, In, In, E, R> => fromChannel(collectAllWhileEffectReader(predicate, Chunk.empty()))
235
-
236
- /** @internal */
237
- const collectAllWhileEffectReader = <In, R, E>(
238
- predicate: (input: In) => Effect.Effect<boolean, E, R>,
239
- done: Chunk.Chunk<In>
240
- ): Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, E, never, Chunk.Chunk<In>, unknown, R> =>
241
- core.readWith({
242
- onInput: (input: Chunk.Chunk<In>) =>
243
- pipe(
244
- core.fromEffect(pipe(input, Effect.takeWhile(predicate), Effect.map(Chunk.unsafeFromArray))),
245
- core.flatMap((collected) => {
246
- const leftovers = pipe(input, Chunk.drop(collected.length))
247
- if (Chunk.isEmpty(leftovers)) {
248
- return collectAllWhileEffectReader(predicate, pipe(done, Chunk.appendAll(collected)))
249
- }
250
- return pipe(core.write(leftovers), channel.zipRight(core.succeed(pipe(done, Chunk.appendAll(collected)))))
251
- })
252
- ),
253
- onFailure: core.fail,
254
- onDone: () => core.succeed(done)
255
- })
256
-
257
- /** @internal */
258
- export const collectAllWhileWith: {
259
- <A, S>(
260
- options: {
261
- readonly initial: S
262
- readonly while: Predicate<A>
263
- readonly body: (s: S, a: A) => S
264
- }
265
- ): <In, L extends In, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<S, In, L, E, R>
266
- <A, In, L extends In, E, R, S>(
267
- self: Sink.Sink<A, In, L, E, R>,
268
- options: {
269
- readonly initial: S
270
- readonly while: Predicate<A>
271
- readonly body: (s: S, a: A) => S
272
- }
273
- ): Sink.Sink<S, In, L, E, R>
274
- } = dual(
275
- 2,
276
- <A, In, L extends In, E, R, S>(
277
- self: Sink.Sink<A, In, L, E, R>,
278
- options: {
279
- readonly initial: S
280
- readonly while: Predicate<A>
281
- readonly body: (s: S, a: A) => S
282
- }
283
- ): Sink.Sink<S, In, L, E, R> => {
284
- const refs = pipe(
285
- Ref.make(Chunk.empty<In>()),
286
- Effect.zip(Ref.make(false))
287
- )
288
- const newChannel = pipe(
289
- core.fromEffect(refs),
290
- core.flatMap(([leftoversRef, upstreamDoneRef]) => {
291
- const upstreamMarker: Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, never, never, unknown, unknown> = core
292
- .readWith({
293
- onInput: (input) => pipe(core.write(input), core.flatMap(() => upstreamMarker)),
294
- onFailure: core.fail,
295
- onDone: (done) => pipe(core.fromEffect(Ref.set(upstreamDoneRef, true)), channel.as(done))
296
- })
297
- return pipe(
298
- upstreamMarker,
299
- core.pipeTo(channel.bufferChunk(leftoversRef)),
300
- core.pipeTo(
301
- collectAllWhileWithLoop(self, leftoversRef, upstreamDoneRef, options.initial, options.while, options.body)
302
- )
303
- )
304
- })
305
- )
306
- return new SinkImpl(newChannel)
307
- }
308
- )
309
-
310
- const collectAllWhileWithLoop = <Z, In, L extends In, E, R, S>(
311
- self: Sink.Sink<Z, In, L, E, R>,
312
- leftoversRef: Ref.Ref<Chunk.Chunk<In>>,
313
- upstreamDoneRef: Ref.Ref<boolean>,
314
- currentResult: S,
315
- p: Predicate<Z>,
316
- f: (s: S, z: Z) => S
317
- ): Channel.Channel<Chunk.Chunk<L>, Chunk.Chunk<In>, E, never, S, unknown, R> => {
318
- return pipe(
319
- toChannel(self),
320
- channel.doneCollect,
321
- channel.foldChannel({
322
- onFailure: core.fail,
323
- onSuccess: ([leftovers, doneValue]) =>
324
- p(doneValue)
325
- ? pipe(
326
- core.fromEffect(
327
- Ref.set(leftoversRef, Chunk.flatten(leftovers as Chunk.Chunk<Chunk.Chunk<In>>))
328
- ),
329
- core.flatMap(() =>
330
- pipe(
331
- core.fromEffect(Ref.get(upstreamDoneRef)),
332
- core.flatMap((upstreamDone) => {
333
- const accumulatedResult = f(currentResult, doneValue)
334
- return upstreamDone
335
- ? pipe(core.write(Chunk.flatten(leftovers)), channel.as(accumulatedResult))
336
- : collectAllWhileWithLoop(self, leftoversRef, upstreamDoneRef, accumulatedResult, p, f)
337
- })
338
- )
339
- )
340
- )
341
- : pipe(core.write(Chunk.flatten(leftovers)), channel.as(currentResult))
342
- })
343
- )
344
- }
345
-
346
- /** @internal */
347
- export const collectLeftover = <A, In, L, E, R>(
348
- self: Sink.Sink<A, In, L, E, R>
349
- ): Sink.Sink<[A, Chunk.Chunk<L>], In, never, E, R> =>
350
- new SinkImpl(pipe(core.collectElements(toChannel(self)), channel.map(([chunks, z]) => [z, Chunk.flatten(chunks)])))
351
-
352
- /** @internal */
353
- export const mapInput = dual<
354
- <In0, In>(f: (input: In0) => In) => <A, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In0, L, E, R>,
355
- <A, In, L, E, R, In0>(self: Sink.Sink<A, In, L, E, R>, f: (input: In0) => In) => Sink.Sink<A, In0, L, E, R>
356
- >(
357
- 2,
358
- <A, In, L, E, R, In0>(self: Sink.Sink<A, In, L, E, R>, f: (input: In0) => In): Sink.Sink<A, In0, L, E, R> =>
359
- pipe(self, mapInputChunks(Chunk.map(f)))
360
- )
361
-
362
- /** @internal */
363
- export const mapInputEffect = dual<
364
- <In0, In, E2, R2>(
365
- f: (input: In0) => Effect.Effect<In, E2, R2>
366
- ) => <A, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In0, L, E2 | E, R2 | R>,
367
- <A, In, L, E, R, In0, E2, R2>(
368
- self: Sink.Sink<A, In, L, E, R>,
369
- f: (input: In0) => Effect.Effect<In, E2, R2>
370
- ) => Sink.Sink<A, In0, L, E2 | E, R2 | R>
371
- >(
372
- 2,
373
- <A, In, L, E, R, In0, E2, R2>(
374
- self: Sink.Sink<A, In, L, E, R>,
375
- f: (input: In0) => Effect.Effect<In, E2, R2>
376
- ): Sink.Sink<A, In0, L, E | E2, R | R2> =>
377
- mapInputChunksEffect(
378
- self,
379
- (chunk) =>
380
- Effect.map(
381
- Effect.forEach(chunk, (v) => f(v)),
382
- Chunk.unsafeFromArray
383
- )
384
- )
385
- )
386
-
387
- /** @internal */
388
- export const mapInputChunks = dual<
389
- <In0, In>(
390
- f: (chunk: Chunk.Chunk<In0>) => Chunk.Chunk<In>
391
- ) => <A, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In0, L, E, R>,
392
- <A, In, L, E, R, In0>(
393
- self: Sink.Sink<A, In, L, E, R>,
394
- f: (chunk: Chunk.Chunk<In0>) => Chunk.Chunk<In>
395
- ) => Sink.Sink<A, In0, L, E, R>
396
- >(
397
- 2,
398
- <A, In, L, E, R, In0>(
399
- self: Sink.Sink<A, In, L, E, R>,
400
- f: (chunk: Chunk.Chunk<In0>) => Chunk.Chunk<In>
401
- ): Sink.Sink<A, In0, L, E, R> => {
402
- const loop: Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In0>, never, never, unknown, unknown, R> = core.readWith({
403
- onInput: (chunk) => pipe(core.write(f(chunk)), core.flatMap(() => loop)),
404
- onFailure: core.fail,
405
- onDone: core.succeed
406
- })
407
- return new SinkImpl(pipe(loop, core.pipeTo(toChannel(self))))
408
- }
409
- )
410
-
411
- /** @internal */
412
- export const mapInputChunksEffect = dual<
413
- <In0, In, E2, R2>(
414
- f: (chunk: Chunk.Chunk<In0>) => Effect.Effect<Chunk.Chunk<In>, E2, R2>
415
- ) => <A, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In0, L, E2 | E, R2 | R>,
416
- <A, In, L, E, R, In0, E2, R2>(
417
- self: Sink.Sink<A, In, L, E, R>,
418
- f: (chunk: Chunk.Chunk<In0>) => Effect.Effect<Chunk.Chunk<In>, E2, R2>
419
- ) => Sink.Sink<A, In0, L, E2 | E, R2 | R>
420
- >(
421
- 2,
422
- <A, In, L, E, R, In0, E2, R2>(
423
- self: Sink.Sink<A, In, L, E, R>,
424
- f: (chunk: Chunk.Chunk<In0>) => Effect.Effect<Chunk.Chunk<In>, E2, R2>
425
- ): Sink.Sink<A, In0, L, E | E2, R | R2> => {
426
- const loop: Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In0>, E2, never, unknown, unknown, R | R2> = core
427
- .readWith({
428
- onInput: (chunk) => pipe(core.fromEffect(f(chunk)), core.flatMap(core.write), core.flatMap(() => loop)),
429
- onFailure: core.fail,
430
- onDone: core.succeed
431
- })
432
- return new SinkImpl(pipe(loop, channel.pipeToOrFail(toChannel(self))))
433
- }
434
- )
435
-
436
- /** @internal */
437
- export const die = (defect: unknown): Sink.Sink<never, unknown> => failCause(Cause.die(defect))
438
-
439
- /** @internal */
440
- export const dieMessage = (message: string): Sink.Sink<never, unknown> =>
441
- failCause(Cause.die(new Cause.RuntimeException(message)))
442
-
443
- /** @internal */
444
- export const dieSync = (evaluate: LazyArg<unknown>): Sink.Sink<never, unknown> =>
445
- failCauseSync(() => Cause.die(evaluate()))
446
-
447
- /** @internal */
448
- export const dimap = dual<
449
- <In0, In, A, A2>(
450
- options: {
451
- readonly onInput: (input: In0) => In
452
- readonly onDone: (a: A) => A2
453
- }
454
- ) => <L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A2, In0, L, E, R>,
455
- <A, In, L, E, R, In0, A2>(
456
- self: Sink.Sink<A, In, L, E, R>,
457
- options: {
458
- readonly onInput: (input: In0) => In
459
- readonly onDone: (a: A) => A2
460
- }
461
- ) => Sink.Sink<A2, In0, L, E, R>
462
- >(
463
- 2,
464
- <A, In, L, E, R, In0, A2>(
465
- self: Sink.Sink<A, In, L, E, R>,
466
- options: {
467
- readonly onInput: (input: In0) => In
468
- readonly onDone: (a: A) => A2
469
- }
470
- ): Sink.Sink<A2, In0, L, E, R> => map(mapInput(self, options.onInput), options.onDone)
471
- )
472
-
473
- /** @internal */
474
- export const dimapEffect = dual<
475
- <In0, In, E2, R2, A, A2, E3, R3>(
476
- options: {
477
- readonly onInput: (input: In0) => Effect.Effect<In, E2, R2>
478
- readonly onDone: (a: A) => Effect.Effect<A2, E3, R3>
479
- }
480
- ) => <L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A2, In0, L, E2 | E3 | E, R2 | R3 | R>,
481
- <A, In, L, E, R, In0, E2, R2, A2, E3, R3>(
482
- self: Sink.Sink<A, In, L, E, R>,
483
- options: {
484
- readonly onInput: (input: In0) => Effect.Effect<In, E2, R2>
485
- readonly onDone: (a: A) => Effect.Effect<A2, E3, R3>
486
- }
487
- ) => Sink.Sink<A2, In0, L, E2 | E3 | E, R2 | R3 | R>
488
- >(
489
- 2,
490
- (self, options) =>
491
- mapEffect(
492
- mapInputEffect(self, options.onInput),
493
- options.onDone
494
- )
495
- )
496
-
497
- /** @internal */
498
- export const dimapChunks = dual<
499
- <In0, In, A, A2>(
500
- options: {
501
- readonly onInput: (chunk: Chunk.Chunk<In0>) => Chunk.Chunk<In>
502
- readonly onDone: (a: A) => A2
503
- }
504
- ) => <L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A2, In0, L, E, R>,
505
- <A, In, L, E, R, In0, A2>(
506
- self: Sink.Sink<A, In, L, E, R>,
507
- options: {
508
- readonly onInput: (chunk: Chunk.Chunk<In0>) => Chunk.Chunk<In>
509
- readonly onDone: (a: A) => A2
510
- }
511
- ) => Sink.Sink<A2, In0, L, E, R>
512
- >(
513
- 2,
514
- (self, options) =>
515
- map(
516
- mapInputChunks(self, options.onInput),
517
- options.onDone
518
- )
519
- )
520
-
521
- /** @internal */
522
- export const dimapChunksEffect = dual<
523
- <In0, In, E2, R2, A, A2, E3, R3>(
524
- options: {
525
- readonly onInput: (chunk: Chunk.Chunk<In0>) => Effect.Effect<Chunk.Chunk<In>, E2, R2>
526
- readonly onDone: (a: A) => Effect.Effect<A2, E3, R3>
527
- }
528
- ) => <L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A2, In0, L, E2 | E3 | E, R2 | R3 | R>,
529
- <A, In, L, E, R, In0, E2, R2, A2, E3, R3>(
530
- self: Sink.Sink<A, In, L, E, R>,
531
- options: {
532
- readonly onInput: (chunk: Chunk.Chunk<In0>) => Effect.Effect<Chunk.Chunk<In>, E2, R2>
533
- readonly onDone: (a: A) => Effect.Effect<A2, E3, R3>
534
- }
535
- ) => Sink.Sink<A2, In0, L, E2 | E3 | E, R2 | R3 | R>
536
- >(
537
- 2,
538
- (self, options) => mapEffect(mapInputChunksEffect(self, options.onInput), options.onDone)
539
- )
540
-
541
- /** @internal */
542
- export const drain: Sink.Sink<void, unknown> = new SinkImpl(
543
- channel.drain(channel.identityChannel())
544
- )
545
-
546
- /** @internal */
547
- export const drop = <In>(n: number): Sink.Sink<unknown, In, In> => suspend(() => new SinkImpl(dropLoop(n)))
548
-
549
- /** @internal */
550
- const dropLoop = <In>(
551
- n: number
552
- ): Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, never, never, unknown, unknown> =>
553
- core.readWith({
554
- onInput: (input: Chunk.Chunk<In>) => {
555
- const dropped = pipe(input, Chunk.drop(n))
556
- const leftover = Math.max(n - input.length, 0)
557
- const more = Chunk.isEmpty(input) || leftover > 0
558
- if (more) {
559
- return dropLoop(leftover)
560
- }
561
- return pipe(
562
- core.write(dropped),
563
- channel.zipRight(channel.identityChannel<Chunk.Chunk<In>, never, unknown>())
564
- )
565
- },
566
- onFailure: core.fail,
567
- onDone: () => core.void
568
- })
569
-
570
- /** @internal */
571
- export const dropUntil = <In>(predicate: Predicate<In>): Sink.Sink<unknown, In, In> =>
572
- new SinkImpl(
573
- pipe(toChannel(dropWhile((input: In) => !predicate(input))), channel.pipeToOrFail(toChannel(drop<In>(1))))
574
- )
575
-
576
- /** @internal */
577
- export const dropUntilEffect = <In, E, R>(
578
- predicate: (input: In) => Effect.Effect<boolean, E, R>
579
- ): Sink.Sink<unknown, In, In, E, R> => suspend(() => new SinkImpl(dropUntilEffectReader(predicate)))
580
-
581
- /** @internal */
582
- const dropUntilEffectReader = <In, R, E>(
583
- predicate: (input: In) => Effect.Effect<boolean, E, R>
584
- ): Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, E, E, unknown, unknown, R> =>
585
- core.readWith({
586
- onInput: (input: Chunk.Chunk<In>) =>
587
- pipe(
588
- input,
589
- Effect.dropUntil(predicate),
590
- Effect.map((leftover) => {
591
- const more = leftover.length === 0
592
- return more ?
593
- dropUntilEffectReader(predicate) :
594
- pipe(
595
- core.write(Chunk.unsafeFromArray(leftover)),
596
- channel.zipRight(channel.identityChannel<Chunk.Chunk<In>, E, unknown>())
597
- )
598
- }),
599
- channel.unwrap
600
- ),
601
- onFailure: core.fail,
602
- onDone: () => core.void
603
- })
604
-
605
- /** @internal */
606
- export const dropWhile = <In>(predicate: Predicate<In>): Sink.Sink<unknown, In, In> =>
607
- new SinkImpl(dropWhileReader(predicate))
608
-
609
- /** @internal */
610
- const dropWhileReader = <In>(
611
- predicate: Predicate<In>
612
- ): Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, never, never, unknown, unknown> =>
613
- core.readWith({
614
- onInput: (input: Chunk.Chunk<In>) => {
615
- const out = pipe(input, Chunk.dropWhile(predicate))
616
- if (Chunk.isEmpty(out)) {
617
- return dropWhileReader(predicate)
618
- }
619
- return pipe(core.write(out), channel.zipRight(channel.identityChannel<Chunk.Chunk<In>, never, unknown>()))
620
- },
621
- onFailure: core.fail,
622
- onDone: core.succeedNow
623
- })
624
-
625
- /** @internal */
626
- export const dropWhileEffect = <In, E, R>(
627
- predicate: (input: In) => Effect.Effect<boolean, E, R>
628
- ): Sink.Sink<unknown, In, In, E, R> => suspend(() => new SinkImpl(dropWhileEffectReader(predicate)))
629
-
630
- /** @internal */
631
- const dropWhileEffectReader = <In, R, E>(
632
- predicate: (input: In) => Effect.Effect<boolean, E, R>
633
- ): Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, E, E, unknown, unknown, R> =>
634
- core.readWith({
635
- onInput: (input: Chunk.Chunk<In>) =>
636
- pipe(
637
- input,
638
- Effect.dropWhile(predicate),
639
- Effect.map((leftover) => {
640
- const more = leftover.length === 0
641
- return more ?
642
- dropWhileEffectReader(predicate) :
643
- pipe(
644
- core.write(Chunk.unsafeFromArray(leftover)),
645
- channel.zipRight(channel.identityChannel<Chunk.Chunk<In>, E, unknown>())
646
- )
647
- }),
648
- channel.unwrap
649
- ),
650
- onFailure: core.fail,
651
- onDone: () => core.void
652
- })
653
-
654
- /** @internal */
655
- export const ensuring = dual<
656
- <X, R2>(
657
- finalizer: Effect.Effect<X, never, R2>
658
- ) => <A, In, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In, L, E, R2 | R>,
659
- <A, In, L, E, R, X, R2>(
660
- self: Sink.Sink<A, In, L, E, R>,
661
- finalizer: Effect.Effect<X, never, R2>
662
- ) => Sink.Sink<A, In, L, E, R2 | R>
663
- >(
664
- 2,
665
- (self, finalizer) => new SinkImpl(pipe(self, toChannel, channel.ensuring(finalizer)))
666
- )
667
-
668
- /** @internal */
669
- export const ensuringWith = dual<
670
- <A, E, X, R2>(
671
- finalizer: (exit: Exit.Exit<A, E>) => Effect.Effect<X, never, R2>
672
- ) => <In, L, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In, L, E, R2 | R>,
673
- <A, In, L, E, R, X, R2>(
674
- self: Sink.Sink<A, In, L, E, R>,
675
- finalizer: (exit: Exit.Exit<A, E>) => Effect.Effect<X, never, R2>
676
- ) => Sink.Sink<A, In, L, E, R2 | R>
677
- >(
678
- 2,
679
- (self, finalizer) => new SinkImpl(pipe(self, toChannel, core.ensuringWith(finalizer)))
680
- )
681
-
682
- /** @internal */
683
- export const context = <R>(): Sink.Sink<Context.Context<R>, unknown, never, never, R> => fromEffect(Effect.context<R>())
684
-
685
- /** @internal */
686
- export const contextWith = <R, Z>(
687
- f: (context: Context.Context<R>) => Z
688
- ): Sink.Sink<Z, unknown, never, never, R> => pipe(context<R>(), map(f))
689
-
690
- /** @internal */
691
- export const contextWithEffect = <R0, A, E, R>(
692
- f: (context: Context.Context<R0>) => Effect.Effect<A, E, R>
693
- ): Sink.Sink<A, unknown, never, E, R0 | R> => pipe(context<R0>(), mapEffect(f))
694
-
695
- /** @internal */
696
- export const contextWithSink = <R0, A, In, L, E, R>(
697
- f: (context: Context.Context<R0>) => Sink.Sink<A, In, L, E, R>
698
- ): Sink.Sink<A, In, L, E, R0 | R> =>
699
- new SinkImpl(channel.unwrap(Effect.contextWith((context) => toChannel(f(context)))))
700
-
701
- /** @internal */
702
- export const every = <In>(predicate: Predicate<In>): Sink.Sink<boolean, In, In> =>
703
- fold(true, identity, (acc, input) => acc && predicate(input))
704
-
705
- /** @internal */
706
- export const fail = <E>(e: E): Sink.Sink<never, unknown, never, E> => new SinkImpl(core.fail(e))
707
-
708
- /** @internal */
709
- export const failSync = <E>(evaluate: LazyArg<E>): Sink.Sink<never, unknown, never, E> =>
710
- new SinkImpl(core.failSync(evaluate))
711
-
712
- /** @internal */
713
- export const failCause = <E>(cause: Cause.Cause<E>): Sink.Sink<never, unknown, never, E> =>
714
- new SinkImpl(core.failCause(cause))
715
-
716
- /** @internal */
717
- export const failCauseSync = <E>(evaluate: LazyArg<Cause.Cause<E>>): Sink.Sink<never, unknown, never, E> =>
718
- new SinkImpl(core.failCauseSync(evaluate))
719
-
720
- /** @internal */
721
- export const filterInput: {
722
- <In, In1 extends In, In2 extends In1>(
723
- f: Refinement<In1, In2>
724
- ): <A, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In2, L, E, R>
725
- <In, In1 extends In>(f: Predicate<In1>): <A, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In1, L, E, R>
726
- } = <In, In1 extends In>(f: Predicate<In1>) => {
727
- return <A, L, E, R>(self: Sink.Sink<A, In, L, E, R>): Sink.Sink<A, In1, L, E, R> =>
728
- pipe(self, mapInputChunks(Chunk.filter(f)))
729
- }
730
-
731
- /** @internal */
732
- export const filterInputEffect = dual<
733
- <In, In1 extends In, E2, R2>(
734
- f: (input: In1) => Effect.Effect<boolean, E2, R2>
735
- ) => <A, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In1, L, E2 | E, R2 | R>,
736
- <A, In, L, E, R, In1 extends In, E2, R2>(
737
- self: Sink.Sink<A, In, L, E, R>,
738
- f: (input: In1) => Effect.Effect<boolean, E2, R2>
739
- ) => Sink.Sink<A, In1, L, E2 | E, R2 | R>
740
- >(
741
- 2,
742
- (self, f) =>
743
- mapInputChunksEffect(
744
- self,
745
- (chunk) => Effect.map(Effect.filter(chunk, f), Chunk.unsafeFromArray)
746
- )
747
- )
748
-
749
- /** @internal */
750
- export const findEffect = dual<
751
- <A, E2, R2>(
752
- f: (a: A) => Effect.Effect<boolean, E2, R2>
753
- ) => <In, L extends In, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<Option.Option<A>, In, L, E2 | E, R2 | R>,
754
- <A, In, L extends In, E, R, E2, R2>(
755
- self: Sink.Sink<A, In, L, E, R>,
756
- f: (a: A) => Effect.Effect<boolean, E2, R2>
757
- ) => Sink.Sink<Option.Option<A>, In, L, E2 | E, R2 | R>
758
- >(
759
- 2,
760
- <A, In, L extends In, E, R, E2, R2>(
761
- self: Sink.Sink<A, In, L, E, R>,
762
- f: (a: A) => Effect.Effect<boolean, E2, R2>
763
- ): Sink.Sink<Option.Option<A>, In, L, E2 | E, R2 | R> => {
764
- const newChannel = pipe(
765
- core.fromEffect(pipe(
766
- Ref.make(Chunk.empty<In>()),
767
- Effect.zip(Ref.make(false))
768
- )),
769
- core.flatMap(([leftoversRef, upstreamDoneRef]) => {
770
- const upstreamMarker: Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, never, never, unknown, unknown> = core
771
- .readWith({
772
- onInput: (input) => pipe(core.write(input), core.flatMap(() => upstreamMarker)),
773
- onFailure: core.fail,
774
- onDone: (done) => pipe(core.fromEffect(Ref.set(upstreamDoneRef, true)), channel.as(done))
775
- })
776
- const loop: Channel.Channel<Chunk.Chunk<L>, Chunk.Chunk<In>, E | E2, never, Option.Option<A>, unknown, R | R2> =
777
- channel.foldChannel(core.collectElements(toChannel(self)), {
778
- onFailure: core.fail,
779
- onSuccess: ([leftovers, doneValue]) =>
780
- pipe(
781
- core.fromEffect(f(doneValue)),
782
- core.flatMap((satisfied) =>
783
- pipe(
784
- core.fromEffect(Ref.set(leftoversRef, Chunk.flatten(leftovers))),
785
- channel.zipRight(
786
- pipe(
787
- core.fromEffect(Ref.get(upstreamDoneRef)),
788
- core.flatMap((upstreamDone) => {
789
- if (satisfied) {
790
- return pipe(core.write(Chunk.flatten(leftovers)), channel.as(Option.some(doneValue)))
791
- }
792
- if (upstreamDone) {
793
- return pipe(core.write(Chunk.flatten(leftovers)), channel.as(Option.none()))
794
- }
795
- return loop
796
- })
797
- )
798
- )
799
- )
800
- )
801
- )
802
- })
803
- return pipe(upstreamMarker, core.pipeTo(channel.bufferChunk(leftoversRef)), core.pipeTo(loop))
804
- })
805
- )
806
- return new SinkImpl(newChannel)
807
- }
808
- )
809
-
810
- /** @internal */
811
- export const fold = <S, In>(
812
- s: S,
813
- contFn: Predicate<S>,
814
- f: (s: S, input: In) => S
815
- ): Sink.Sink<S, In, In> => suspend(() => new SinkImpl(foldReader(s, contFn, f)))
816
-
817
- /** @internal */
818
- const foldReader = <S, In>(
819
- s: S,
820
- contFn: Predicate<S>,
821
- f: (z: S, input: In) => S
822
- ): Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, never, never, S, unknown> => {
823
- if (!contFn(s)) {
824
- return core.succeedNow(s)
825
- }
826
- return core.readWith({
827
- onInput: (input: Chunk.Chunk<In>) => {
828
- const [nextS, leftovers] = foldChunkSplit(s, input, contFn, f, 0, input.length)
829
- if (Chunk.isNonEmpty(leftovers)) {
830
- return pipe(core.write(leftovers), channel.as(nextS))
831
- }
832
- return foldReader(nextS, contFn, f)
833
- },
834
- onFailure: core.fail,
835
- onDone: () => core.succeedNow(s)
836
- })
837
- }
838
-
839
- /** @internal */
840
- const foldChunkSplit = <S, In>(
841
- s: S,
842
- chunk: Chunk.Chunk<In>,
843
- contFn: Predicate<S>,
844
- f: (z: S, input: In) => S,
845
- index: number,
846
- length: number
847
- ): [S, Chunk.Chunk<In>] => {
848
- if (index === length) {
849
- return [s, Chunk.empty()]
850
- }
851
- const s1 = f(s, pipe(chunk, Chunk.unsafeGet(index)))
852
- if (contFn(s1)) {
853
- return foldChunkSplit(s1, chunk, contFn, f, index + 1, length)
854
- }
855
- return [s1, pipe(chunk, Chunk.drop(index + 1))]
856
- }
857
-
858
- /** @internal */
859
- export const foldSink = dual<
860
- <E, A1, In, In1 extends In, L1, E1, R1, A, A2, In2 extends In, L2, E2, R2>(
861
- options: {
862
- readonly onFailure: (err: E) => Sink.Sink<A1, In1, L1, E1, R1>
863
- readonly onSuccess: (a: A) => Sink.Sink<A2, In2, L2, E2, R2>
864
- }
865
- ) => <L, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A1 | A2, In1 & In2, L1 | L2, E1 | E2, R1 | R2 | R>,
866
- <A, In, L, E, R, A1, In1 extends In, L1, E1, R1, A2, In2 extends In, L2, E2, R2>(
867
- self: Sink.Sink<A, In, L, E, R>,
868
- options: {
869
- readonly onFailure: (err: E) => Sink.Sink<A1, In1, L1, E1, R1>
870
- readonly onSuccess: (a: A) => Sink.Sink<A2, In2, L2, E2, R2>
871
- }
872
- ) => Sink.Sink<A1 | A2, In1 & In2, L1 | L2, E1 | E2, R1 | R2 | R>
873
- >(
874
- 2,
875
- <A, In, L, E, R, A1, In1 extends In, L1, E1, R1, A2, In2 extends In, L2, E2, R2>(
876
- self: Sink.Sink<A, In, L, E, R>,
877
- options: {
878
- readonly onFailure: (err: E) => Sink.Sink<A1, In1, L1, E1, R1>
879
- readonly onSuccess: (z: A) => Sink.Sink<A2, In2, L2, E2, R2>
880
- }
881
- ): Sink.Sink<A1 | A2, In1 & In2, L1 | L2, E1 | E2, R | R1 | R2> => {
882
- const newChannel: Channel.Channel<
883
- Chunk.Chunk<L1 | L2>,
884
- Chunk.Chunk<In1 & In2>,
885
- E1 | E2,
886
- never,
887
- A1 | A2,
888
- unknown,
889
- R | R1 | R2
890
- > = pipe(
891
- toChannel(self),
892
- core.collectElements,
893
- channel.foldChannel({
894
- onFailure: (error) => toChannel(options.onFailure(error)),
895
- onSuccess: ([leftovers, z]) =>
896
- core.suspend(() => {
897
- const leftoversRef = {
898
- ref: pipe(leftovers, Chunk.filter(Chunk.isNonEmpty)) as Chunk.Chunk<Chunk.Chunk<L1 | L2>>
899
- }
900
- const refReader = pipe(
901
- core.sync(() => {
902
- const ref = leftoversRef.ref
903
- leftoversRef.ref = Chunk.empty()
904
- return ref
905
- }),
906
- // This cast is safe because of the L1 >: L <: In1 bound. It follows that
907
- // L <: In1 and therefore Chunk[L] can be safely cast to Chunk[In1].
908
- core.flatMap((chunk) => channel.writeChunk(chunk as Chunk.Chunk<Chunk.Chunk<In1 & In2>>))
909
- )
910
- const passthrough = channel.identityChannel<Chunk.Chunk<In1 & In2>, never, unknown>()
911
- const continuationSink = pipe(
912
- refReader,
913
- channel.zipRight(passthrough),
914
- core.pipeTo(toChannel(options.onSuccess(z)))
915
- )
916
- return core.flatMap(
917
- core.collectElements(continuationSink),
918
- ([newLeftovers, z1]) =>
919
- pipe(
920
- core.succeed(leftoversRef.ref),
921
- core.flatMap(channel.writeChunk),
922
- channel.zipRight(channel.writeChunk(newLeftovers)),
923
- channel.as(z1)
924
- )
925
- )
926
- })
927
- })
928
- )
929
- return new SinkImpl(newChannel)
930
- }
931
- )
932
-
933
- /** @internal */
934
- export const foldChunks = <S, In>(
935
- s: S,
936
- contFn: Predicate<S>,
937
- f: (s: S, chunk: Chunk.Chunk<In>) => S
938
- ): Sink.Sink<S, In> => suspend(() => new SinkImpl(foldChunksReader(s, contFn, f)))
939
-
940
- /** @internal */
941
- const foldChunksReader = <S, In>(
942
- s: S,
943
- contFn: Predicate<S>,
944
- f: (s: S, chunk: Chunk.Chunk<In>) => S
945
- ): Channel.Channel<never, Chunk.Chunk<In>, never, never, S, unknown> => {
946
- if (!contFn(s)) {
947
- return core.succeedNow(s)
948
- }
949
- return core.readWith({
950
- onInput: (input: Chunk.Chunk<In>) => foldChunksReader(f(s, input), contFn, f),
951
- onFailure: core.fail,
952
- onDone: () => core.succeedNow(s)
953
- })
954
- }
955
-
956
- /** @internal */
957
- export const foldChunksEffect = <S, In, E, R>(
958
- s: S,
959
- contFn: Predicate<S>,
960
- f: (s: S, chunk: Chunk.Chunk<In>) => Effect.Effect<S, E, R>
961
- ): Sink.Sink<S, In, In, E, R> => suspend(() => new SinkImpl(foldChunksEffectReader(s, contFn, f)))
962
-
963
- /** @internal */
964
- const foldChunksEffectReader = <S, R, E, In>(
965
- s: S,
966
- contFn: Predicate<S>,
967
- f: (s: S, chunk: Chunk.Chunk<In>) => Effect.Effect<S, E, R>
968
- ): Channel.Channel<never, Chunk.Chunk<In>, E, E, S, unknown, R> => {
969
- if (!contFn(s)) {
970
- return core.succeedNow(s)
971
- }
972
- return core.readWith({
973
- onInput: (input: Chunk.Chunk<In>) =>
974
- pipe(
975
- core.fromEffect(f(s, input)),
976
- core.flatMap((s) => foldChunksEffectReader(s, contFn, f))
977
- ),
978
- onFailure: core.fail,
979
- onDone: () => core.succeedNow(s)
980
- })
981
- }
982
-
983
- /** @internal */
984
- export const foldEffect = <S, In, E, R>(
985
- s: S,
986
- contFn: Predicate<S>,
987
- f: (s: S, input: In) => Effect.Effect<S, E, R>
988
- ): Sink.Sink<S, In, In, E, R> => suspend(() => new SinkImpl(foldEffectReader(s, contFn, f)))
989
-
990
- /** @internal */
991
- const foldEffectReader = <S, In, R, E>(
992
- s: S,
993
- contFn: Predicate<S>,
994
- f: (s: S, input: In) => Effect.Effect<S, E, R>
995
- ): Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, E, E, S, unknown, R> => {
996
- if (!contFn(s)) {
997
- return core.succeedNow(s)
998
- }
999
- return core.readWith({
1000
- onInput: (input: Chunk.Chunk<In>) =>
1001
- pipe(
1002
- core.fromEffect(foldChunkSplitEffect(s, input, contFn, f)),
1003
- core.flatMap(([nextS, leftovers]) =>
1004
- pipe(
1005
- leftovers,
1006
- Option.match({
1007
- onNone: () => foldEffectReader(nextS, contFn, f),
1008
- onSome: (leftover) => pipe(core.write(leftover), channel.as(nextS))
1009
- })
1010
- )
1011
- )
1012
- ),
1013
- onFailure: core.fail,
1014
- onDone: () => core.succeedNow(s)
1015
- })
1016
- }
1017
-
1018
- /** @internal */
1019
- const foldChunkSplitEffect = <S, R, E, In>(
1020
- s: S,
1021
- chunk: Chunk.Chunk<In>,
1022
- contFn: Predicate<S>,
1023
- f: (s: S, input: In) => Effect.Effect<S, E, R>
1024
- ): Effect.Effect<[S, Option.Option<Chunk.Chunk<In>>], E, R> =>
1025
- foldChunkSplitEffectInternal(s, chunk, 0, chunk.length, contFn, f)
1026
-
1027
- /** @internal */
1028
- const foldChunkSplitEffectInternal = <S, R, E, In>(
1029
- s: S,
1030
- chunk: Chunk.Chunk<In>,
1031
- index: number,
1032
- length: number,
1033
- contFn: Predicate<S>,
1034
- f: (s: S, input: In) => Effect.Effect<S, E, R>
1035
- ): Effect.Effect<[S, Option.Option<Chunk.Chunk<In>>], E, R> => {
1036
- if (index === length) {
1037
- return Effect.succeed([s, Option.none()])
1038
- }
1039
- return pipe(
1040
- f(s, pipe(chunk, Chunk.unsafeGet(index))),
1041
- Effect.flatMap((s1) =>
1042
- contFn(s1) ?
1043
- foldChunkSplitEffectInternal(s1, chunk, index + 1, length, contFn, f) :
1044
- Effect.succeed([s1, Option.some(pipe(chunk, Chunk.drop(index + 1)))])
1045
- )
1046
- )
1047
- }
1048
-
1049
- /** @internal */
1050
- export const foldLeft = <S, In>(s: S, f: (s: S, input: In) => S): Sink.Sink<S, In> =>
1051
- ignoreLeftover(fold(s, constTrue, f))
1052
-
1053
- /** @internal */
1054
- export const foldLeftChunks = <S, In>(
1055
- s: S,
1056
- f: (s: S, chunk: Chunk.Chunk<In>) => S
1057
- ): Sink.Sink<S, In> => foldChunks(s, constTrue, f)
1058
-
1059
- /** @internal */
1060
- export const foldLeftChunksEffect = <S, In, E, R>(
1061
- s: S,
1062
- f: (s: S, chunk: Chunk.Chunk<In>) => Effect.Effect<S, E, R>
1063
- ): Sink.Sink<S, In, never, E, R> => ignoreLeftover(foldChunksEffect(s, constTrue, f))
1064
-
1065
- /** @internal */
1066
- export const foldLeftEffect = <S, In, E, R>(
1067
- s: S,
1068
- f: (s: S, input: In) => Effect.Effect<S, E, R>
1069
- ): Sink.Sink<S, In, In, E, R> => foldEffect(s, constTrue, f)
1070
-
1071
- /** @internal */
1072
- export const foldUntil = <S, In>(s: S, max: number, f: (s: S, input: In) => S): Sink.Sink<S, In, In> =>
1073
- pipe(
1074
- fold<[S, number], In>(
1075
- [s, 0],
1076
- (tuple) => tuple[1] < max,
1077
- ([output, count], input) => [f(output, input), count + 1]
1078
- ),
1079
- map((tuple) => tuple[0])
1080
- )
1081
-
1082
- /** @internal */
1083
- export const foldUntilEffect = <S, In, E, R>(
1084
- s: S,
1085
- max: number,
1086
- f: (s: S, input: In) => Effect.Effect<S, E, R>
1087
- ): Sink.Sink<S, In, In, E, R> =>
1088
- pipe(
1089
- foldEffect(
1090
- [s, 0 as number] as const,
1091
- (tuple) => tuple[1] < max,
1092
- ([output, count], input: In) => pipe(f(output, input), Effect.map((s) => [s, count + 1] as const))
1093
- ),
1094
- map((tuple) => tuple[0])
1095
- )
1096
-
1097
- /** @internal */
1098
- export const foldWeighted = <S, In>(
1099
- options: {
1100
- readonly initial: S
1101
- readonly maxCost: number
1102
- readonly cost: (s: S, input: In) => number
1103
- readonly body: (s: S, input: In) => S
1104
- }
1105
- ): Sink.Sink<S, In, In> =>
1106
- foldWeightedDecompose({
1107
- ...options,
1108
- decompose: Chunk.of
1109
- })
1110
-
1111
- /** @internal */
1112
- export const foldWeightedDecompose = <S, In>(
1113
- options: {
1114
- readonly initial: S
1115
- readonly maxCost: number
1116
- readonly cost: (s: S, input: In) => number
1117
- readonly decompose: (input: In) => Chunk.Chunk<In>
1118
- readonly body: (s: S, input: In) => S
1119
- }
1120
- ): Sink.Sink<S, In, In> =>
1121
- suspend(() =>
1122
- new SinkImpl(
1123
- foldWeightedDecomposeLoop(
1124
- options.initial,
1125
- 0,
1126
- false,
1127
- options.maxCost,
1128
- options.cost,
1129
- options.decompose,
1130
- options.body
1131
- )
1132
- )
1133
- )
1134
-
1135
- /** @internal */
1136
- const foldWeightedDecomposeLoop = <S, In>(
1137
- s: S,
1138
- cost: number,
1139
- dirty: boolean,
1140
- max: number,
1141
- costFn: (s: S, input: In) => number,
1142
- decompose: (input: In) => Chunk.Chunk<In>,
1143
- f: (s: S, input: In) => S
1144
- ): Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, never, never, S, unknown> =>
1145
- core.readWith({
1146
- onInput: (input: Chunk.Chunk<In>) => {
1147
- const [nextS, nextCost, nextDirty, leftovers] = foldWeightedDecomposeFold(
1148
- input,
1149
- s,
1150
- cost,
1151
- dirty,
1152
- max,
1153
- costFn,
1154
- decompose,
1155
- f
1156
- )
1157
- if (Chunk.isNonEmpty(leftovers)) {
1158
- return pipe(core.write(leftovers), channel.zipRight(core.succeedNow(nextS)))
1159
- }
1160
- if (cost > max) {
1161
- return core.succeedNow(nextS)
1162
- }
1163
- return foldWeightedDecomposeLoop(nextS, nextCost, nextDirty, max, costFn, decompose, f)
1164
- },
1165
- onFailure: core.fail,
1166
- onDone: () => core.succeedNow(s)
1167
- })
1168
-
1169
- /** @internal */
1170
- const foldWeightedDecomposeFold = <In, S>(
1171
- input: Chunk.Chunk<In>,
1172
- s: S,
1173
- cost: number,
1174
- dirty: boolean,
1175
- max: number,
1176
- costFn: (s: S, input: In) => number,
1177
- decompose: (input: In) => Chunk.Chunk<In>,
1178
- f: (s: S, input: In) => S
1179
- ): [S, number, boolean, Chunk.Chunk<In>] => {
1180
- for (let index = 0; index < input.length; index++) {
1181
- const elem = Chunk.unsafeGet(input, index)
1182
- const prevCost = cost
1183
- cost = cost + costFn(s, elem)
1184
- if (cost <= max) {
1185
- s = f(s, elem)
1186
- dirty = true
1187
- continue
1188
- }
1189
- const decomposed = decompose(elem)
1190
- if (decomposed.length <= 1 && !dirty) {
1191
- // If `elem` cannot be decomposed, we need to cross the `max` threshold. To
1192
- // minimize "injury", we only allow this when we haven't added anything else
1193
- // to the aggregate (dirty = false).
1194
- return [f(s, elem), cost, true, Chunk.drop(input, index + 1)]
1195
- }
1196
- if (decomposed.length <= 1 && dirty) {
1197
- // If the state is dirty and `elem` cannot be decomposed, we stop folding
1198
- // and include `elem` in the leftovers.
1199
- return [s, prevCost, dirty, Chunk.drop(input, index)]
1200
- }
1201
- // `elem` got decomposed, so we will recurse with the decomposed elements pushed
1202
- // into the chunk we're processing and see if we can aggregate further.
1203
- input = Chunk.appendAll(decomposed, Chunk.drop(input, index + 1))
1204
- cost = prevCost
1205
- index = -1
1206
- }
1207
- return [s, cost, dirty, Chunk.empty<In>()]
1208
- }
1209
-
1210
- /** @internal */
1211
- export const foldWeightedDecomposeEffect = <S, In, E, R, E2, R2, E3, R3>(
1212
- options: {
1213
- readonly initial: S
1214
- readonly maxCost: number
1215
- readonly cost: (s: S, input: In) => Effect.Effect<number, E, R>
1216
- readonly decompose: (input: In) => Effect.Effect<Chunk.Chunk<In>, E2, R2>
1217
- readonly body: (s: S, input: In) => Effect.Effect<S, E3, R3>
1218
- }
1219
- ): Sink.Sink<S, In, In, E | E2 | E3, R | R2 | R3> =>
1220
- suspend(() =>
1221
- new SinkImpl(
1222
- foldWeightedDecomposeEffectLoop(
1223
- options.initial,
1224
- options.maxCost,
1225
- options.cost,
1226
- options.decompose,
1227
- options.body,
1228
- 0,
1229
- false
1230
- )
1231
- )
1232
- )
1233
-
1234
- /** @internal */
1235
- export const foldWeightedEffect = <S, In, E, R, E2, R2>(
1236
- options: {
1237
- readonly initial: S
1238
- readonly maxCost: number
1239
- readonly cost: (s: S, input: In) => Effect.Effect<number, E, R>
1240
- readonly body: (s: S, input: In) => Effect.Effect<S, E2, R2>
1241
- }
1242
- ): Sink.Sink<S, In, In, E | E2, R | R2> =>
1243
- foldWeightedDecomposeEffect({
1244
- ...options,
1245
- decompose: (input) => Effect.succeed(Chunk.of(input))
1246
- })
1247
-
1248
- const foldWeightedDecomposeEffectLoop = <S, In, E, R, E2, R2, E3, R3>(
1249
- s: S,
1250
- max: number,
1251
- costFn: (s: S, input: In) => Effect.Effect<number, E, R>,
1252
- decompose: (input: In) => Effect.Effect<Chunk.Chunk<In>, E2, R2>,
1253
- f: (s: S, input: In) => Effect.Effect<S, E3, R3>,
1254
- cost: number,
1255
- dirty: boolean
1256
- ): Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, E | E2 | E3, E | E2 | E3, S, unknown, R | R2 | R3> =>
1257
- core.readWith({
1258
- onInput: (input: Chunk.Chunk<In>) =>
1259
- pipe(
1260
- core.fromEffect(foldWeightedDecomposeEffectFold(s, max, costFn, decompose, f, input, dirty, cost, 0)),
1261
- core.flatMap(([nextS, nextCost, nextDirty, leftovers]) => {
1262
- if (Chunk.isNonEmpty(leftovers)) {
1263
- return pipe(core.write(leftovers), channel.zipRight(core.succeedNow(nextS)))
1264
- }
1265
- if (cost > max) {
1266
- return core.succeedNow(nextS)
1267
- }
1268
- return foldWeightedDecomposeEffectLoop(nextS, max, costFn, decompose, f, nextCost, nextDirty)
1269
- })
1270
- ),
1271
- onFailure: core.fail,
1272
- onDone: () => core.succeedNow(s)
1273
- })
1274
-
1275
- /** @internal */
1276
- const foldWeightedDecomposeEffectFold = <S, In, E, R, E2, R2, E3, R3>(
1277
- s: S,
1278
- max: number,
1279
- costFn: (s: S, input: In) => Effect.Effect<number, E, R>,
1280
- decompose: (input: In) => Effect.Effect<Chunk.Chunk<In>, E2, R2>,
1281
- f: (s: S, input: In) => Effect.Effect<S, E3, R3>,
1282
- input: Chunk.Chunk<In>,
1283
- dirty: boolean,
1284
- cost: number,
1285
- index: number
1286
- ): Effect.Effect<[S, number, boolean, Chunk.Chunk<In>], E | E2 | E3, R | R2 | R3> => {
1287
- if (index === input.length) {
1288
- return Effect.succeed([s, cost, dirty, Chunk.empty<In>()])
1289
- }
1290
- const elem = pipe(input, Chunk.unsafeGet(index))
1291
- return pipe(
1292
- costFn(s, elem),
1293
- Effect.map((newCost) => cost + newCost),
1294
- Effect.flatMap((total) => {
1295
- if (total <= max) {
1296
- return pipe(
1297
- f(s, elem),
1298
- Effect.flatMap((s) =>
1299
- foldWeightedDecomposeEffectFold(s, max, costFn, decompose, f, input, true, total, index + 1)
1300
- )
1301
- )
1302
- }
1303
- return pipe(
1304
- decompose(elem),
1305
- Effect.flatMap((decomposed) => {
1306
- if (decomposed.length <= 1 && !dirty) {
1307
- // If `elem` cannot be decomposed, we need to cross the `max` threshold. To
1308
- // minimize "injury", we only allow this when we haven't added anything else
1309
- // to the aggregate (dirty = false).
1310
- return pipe(
1311
- f(s, elem),
1312
- Effect.map((s) => [s, total, true, pipe(input, Chunk.drop(index + 1))])
1313
- )
1314
- }
1315
- if (decomposed.length <= 1 && dirty) {
1316
- // If the state is dirty and `elem` cannot be decomposed, we stop folding
1317
- // and include `elem` in th leftovers.
1318
- return Effect.succeed([s, cost, dirty, pipe(input, Chunk.drop(index))])
1319
- }
1320
- // `elem` got decomposed, so we will recurse with the decomposed elements pushed
1321
- // into the chunk we're processing and see if we can aggregate further.
1322
- const next = pipe(decomposed, Chunk.appendAll(pipe(input, Chunk.drop(index + 1))))
1323
- return foldWeightedDecomposeEffectFold(s, max, costFn, decompose, f, next, dirty, cost, 0)
1324
- })
1325
- )
1326
- })
1327
- )
1328
- }
1329
-
1330
- /** @internal */
1331
- export const flatMap = dual<
1332
- <A, A1, In, In1 extends In, L1, E1, R1>(
1333
- f: (a: A) => Sink.Sink<A1, In1, L1, E1, R1>
1334
- ) => <L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A1, In & In1, L | L1, E1 | E, R1 | R>,
1335
- <A, In, L, E, R, A1, In1 extends In, L1, E1, R1>(
1336
- self: Sink.Sink<A, In, L, E, R>,
1337
- f: (a: A) => Sink.Sink<A1, In1, L1, E1, R1>
1338
- ) => Sink.Sink<A1, In & In1, L | L1, E1 | E, R1 | R>
1339
- >(
1340
- 2,
1341
- (self, f) => foldSink(self, { onFailure: fail, onSuccess: f })
1342
- )
1343
-
1344
- /** @internal */
1345
- export const forEach = <In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink.Sink<void, In, never, E, R> => {
1346
- const process: Channel.Channel<never, Chunk.Chunk<In>, E, E, void, unknown, R> = core.readWithCause({
1347
- onInput: (input: Chunk.Chunk<In>) =>
1348
- pipe(core.fromEffect(Effect.forEach(input, (v) => f(v), { discard: true })), core.flatMap(() => process)),
1349
- onFailure: core.failCause,
1350
- onDone: () => core.void
1351
- })
1352
- return new SinkImpl(process)
1353
- }
1354
-
1355
- /** @internal */
1356
- export const forEachChunk = <In, X, E, R>(
1357
- f: (input: Chunk.Chunk<In>) => Effect.Effect<X, E, R>
1358
- ): Sink.Sink<void, In, never, E, R> => {
1359
- const process: Channel.Channel<never, Chunk.Chunk<In>, E, E, void, unknown, R> = core.readWithCause({
1360
- onInput: (input: Chunk.Chunk<In>) => pipe(core.fromEffect(f(input)), core.flatMap(() => process)),
1361
- onFailure: core.failCause,
1362
- onDone: () => core.void
1363
- })
1364
- return new SinkImpl(process)
1365
- }
1366
-
1367
- /** @internal */
1368
- export const forEachWhile = <In, E, R>(
1369
- f: (input: In) => Effect.Effect<boolean, E, R>
1370
- ): Sink.Sink<void, In, In, E, R> => {
1371
- const process: Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, E, E, void, unknown, R> = core.readWithCause({
1372
- onInput: (input: Chunk.Chunk<In>) => forEachWhileReader(f, input, 0, input.length, process),
1373
- onFailure: core.failCause,
1374
- onDone: () => core.void
1375
- })
1376
- return new SinkImpl(process)
1377
- }
1378
-
1379
- /** @internal */
1380
- const forEachWhileReader = <In, E, R>(
1381
- f: (input: In) => Effect.Effect<boolean, E, R>,
1382
- input: Chunk.Chunk<In>,
1383
- index: number,
1384
- length: number,
1385
- cont: Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, E, E, void, unknown, R>
1386
- ): Channel.Channel<Chunk.Chunk<In>, Chunk.Chunk<In>, E, E, void, unknown, R> => {
1387
- if (index === length) {
1388
- return cont
1389
- }
1390
- return pipe(
1391
- core.fromEffect(f(pipe(input, Chunk.unsafeGet(index)))),
1392
- core.flatMap((bool) =>
1393
- bool ?
1394
- forEachWhileReader(f, input, index + 1, length, cont) :
1395
- core.write(pipe(input, Chunk.drop(index)))
1396
- ),
1397
- channel.catchAll((error) => pipe(core.write(pipe(input, Chunk.drop(index))), channel.zipRight(core.fail(error))))
1398
- )
1399
- }
1400
-
1401
- /** @internal */
1402
- export const forEachChunkWhile = <In, E, R>(
1403
- f: (input: Chunk.Chunk<In>) => Effect.Effect<boolean, E, R>
1404
- ): Sink.Sink<void, In, In, E, R> => {
1405
- const reader: Channel.Channel<never, Chunk.Chunk<In>, E, E, void, unknown, R> = core.readWith({
1406
- onInput: (input: Chunk.Chunk<In>) =>
1407
- pipe(
1408
- core.fromEffect(f(input)),
1409
- core.flatMap((cont) => cont ? reader : core.void)
1410
- ),
1411
- onFailure: core.fail,
1412
- onDone: () => core.void
1413
- })
1414
- return new SinkImpl(reader)
1415
- }
1416
-
1417
- /** @internal */
1418
- export const fromChannel = <L, In, E, A, R>(
1419
- channel: Channel.Channel<Chunk.Chunk<L>, Chunk.Chunk<In>, E, never, A, unknown, R>
1420
- ): Sink.Sink<A, In, L, E, R> => new SinkImpl(channel)
1421
-
1422
- /** @internal */
1423
- export const fromEffect = <A, E, R>(effect: Effect.Effect<A, E, R>): Sink.Sink<A, unknown, never, E, R> =>
1424
- new SinkImpl(core.fromEffect(effect))
1425
-
1426
- /** @internal */
1427
- export const fromPubSub = <In>(
1428
- pubsub: PubSub.PubSub<In>,
1429
- options?: {
1430
- readonly shutdown?: boolean | undefined
1431
- }
1432
- ): Sink.Sink<void, In> => fromQueue(pubsub, options)
1433
-
1434
- /** @internal */
1435
- export const fromPush = <In, L0, R0, L, R>(
1436
- push: Effect.Effect<
1437
- (_: Option.Option<Chunk.Chunk<In>>) => Effect.Effect<void, readonly [Either.Either<R0, L0>, Chunk.Chunk<L>], R>,
1438
- never,
1439
- R
1440
- >
1441
- ): Sink.Sink<R0, In, L, L0, Exclude<R, Scope.Scope>> =>
1442
- new SinkImpl(channel.unwrapScoped(pipe(push, Effect.map(fromPushPull))))
1443
-
1444
- const fromPushPull = <In, Z, E, L, R>(
1445
- push: (
1446
- option: Option.Option<Chunk.Chunk<In>>
1447
- ) => Effect.Effect<void, readonly [Either.Either<Z, E>, Chunk.Chunk<L>], R>
1448
- ): Channel.Channel<Chunk.Chunk<L>, Chunk.Chunk<In>, E, never, Z, unknown, R> =>
1449
- core.readWith({
1450
- onInput: (input: Chunk.Chunk<In>) =>
1451
- channel.foldChannel(core.fromEffect(push(Option.some(input))), {
1452
- onFailure: ([either, leftovers]) =>
1453
- Either.match(either, {
1454
- onLeft: (error) => pipe(core.write(leftovers), channel.zipRight(core.fail(error))),
1455
- onRight: (z) => pipe(core.write(leftovers), channel.zipRight(core.succeedNow(z)))
1456
- }),
1457
- onSuccess: () => fromPushPull(push)
1458
- }),
1459
- onFailure: core.fail,
1460
- onDone: () =>
1461
- channel.foldChannel(core.fromEffect(push(Option.none())), {
1462
- onFailure: ([either, leftovers]) =>
1463
- Either.match(either, {
1464
- onLeft: (error) => pipe(core.write(leftovers), channel.zipRight(core.fail(error))),
1465
- onRight: (z) => pipe(core.write(leftovers), channel.zipRight(core.succeedNow(z)))
1466
- }),
1467
- onSuccess: () =>
1468
- core.fromEffect(
1469
- Effect.dieMessage(
1470
- "BUG: Sink.fromPush - please report an issue at https://github.com/Effect-TS/effect/issues"
1471
- )
1472
- )
1473
- })
1474
- })
1475
-
1476
- /** @internal */
1477
- export const fromQueue = <In>(
1478
- queue: Queue.Enqueue<In>,
1479
- options?: {
1480
- readonly shutdown?: boolean | undefined
1481
- }
1482
- ): Sink.Sink<void, In> =>
1483
- options?.shutdown ?
1484
- unwrapScoped(
1485
- Effect.map(
1486
- Effect.acquireRelease(Effect.succeed(queue), Queue.shutdown),
1487
- fromQueue
1488
- )
1489
- ) :
1490
- forEachChunk((input: Chunk.Chunk<In>) => Queue.offerAll(queue, input))
1491
-
1492
- /** @internal */
1493
- export const head = <In>(): Sink.Sink<Option.Option<In>, In, In> =>
1494
- fold(
1495
- Option.none() as Option.Option<In>,
1496
- Option.isNone,
1497
- (option, input) =>
1498
- Option.match(option, {
1499
- onNone: () => Option.some(input),
1500
- onSome: () => option
1501
- })
1502
- )
1503
-
1504
- /** @internal */
1505
- export const ignoreLeftover = <A, In, L, E, R>(self: Sink.Sink<A, In, L, E, R>): Sink.Sink<A, In, never, E, R> =>
1506
- new SinkImpl(channel.drain(toChannel(self)))
1507
-
1508
- /** @internal */
1509
- export const last = <In>(): Sink.Sink<Option.Option<In>, In, In> =>
1510
- foldLeftChunks(Option.none<In>(), (s, input) => Option.orElse(Chunk.last(input), () => s))
1511
-
1512
- /** @internal */
1513
- export const leftover = <L>(chunk: Chunk.Chunk<L>): Sink.Sink<void, unknown, L> =>
1514
- new SinkImpl(core.suspend(() => core.write(chunk)))
1515
-
1516
- /** @internal */
1517
- export const map = dual<
1518
- <A, A2>(f: (a: A) => A2) => <In, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A2, In, L, E, R>,
1519
- <A, In, L, E, R, A2>(self: Sink.Sink<A, In, L, E, R>, f: (a: A) => A2) => Sink.Sink<A2, In, L, E, R>
1520
- >(2, (self, f) => {
1521
- return new SinkImpl(pipe(toChannel(self), channel.map(f)))
1522
- })
1523
-
1524
- /** @internal */
1525
- export const mapEffect = dual<
1526
- <A, A2, E2, R2>(
1527
- f: (a: A) => Effect.Effect<A2, E2, R2>
1528
- ) => <In, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A2, In, L, E2 | E, R2 | R>,
1529
- <A, In, L, E, R, A2, E2, R2>(
1530
- self: Sink.Sink<A, In, L, E, R>,
1531
- f: (a: A) => Effect.Effect<A2, E2, R2>
1532
- ) => Sink.Sink<A2, In, L, E2 | E, R2 | R>
1533
- >(
1534
- 2,
1535
- (self, f) => new SinkImpl(pipe(toChannel(self), channel.mapEffect(f)))
1536
- )
1537
-
1538
- /** @internal */
1539
- export const mapError = dual<
1540
- <E, E2>(f: (error: E) => E2) => <A, In, L, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In, L, E2, R>,
1541
- <A, In, L, E, R, E2>(self: Sink.Sink<A, In, L, E, R>, f: (error: E) => E2) => Sink.Sink<A, In, L, E2, R>
1542
- >(
1543
- 2,
1544
- (self, f) => new SinkImpl(pipe(toChannel(self), channel.mapError(f)))
1545
- )
1546
-
1547
- /** @internal */
1548
- export const mapLeftover = dual<
1549
- <L, L2>(f: (leftover: L) => L2) => <A, In, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In, L2, E, R>,
1550
- <A, In, L, E, R, L2>(self: Sink.Sink<A, In, L, E, R>, f: (leftover: L) => L2) => Sink.Sink<A, In, L2, E, R>
1551
- >(
1552
- 2,
1553
- (self, f) => new SinkImpl(pipe(toChannel(self), channel.mapOut(Chunk.map(f))))
1554
- )
1555
-
1556
- /** @internal */
1557
- export const never: Sink.Sink<never, unknown> = fromEffect(Effect.never)
1558
-
1559
- /** @internal */
1560
- export const orElse = dual<
1561
- <A2, In2, L2, E2, R2>(
1562
- that: LazyArg<Sink.Sink<A2, In2, L2, E2, R2>>
1563
- ) => <A, In, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A2 | A, In & In2, L2 | L, E2 | E, R2 | R>,
1564
- <A, In, L, E, R, A2, In2, L2, E2, R2>(
1565
- self: Sink.Sink<A, In, L, E, R>,
1566
- that: LazyArg<Sink.Sink<A2, In2, L2, E2, R2>>
1567
- ) => Sink.Sink<A2 | A, In & In2, L2 | L, E2 | E, R2 | R>
1568
- >(
1569
- 2,
1570
- <A, In, L, E, R, A2, In2, L2, E2, R2>(
1571
- self: Sink.Sink<A, In, L, E, R>,
1572
- that: LazyArg<Sink.Sink<A2, In2, L2, E2, R2>>
1573
- ): Sink.Sink<A | A2, In & In2, L | L2, E | E2, R | R2> =>
1574
- new SinkImpl<A | A2, In & In2, L | L2, E | E2, R | R2>(
1575
- pipe(toChannel(self), channel.orElse(() => toChannel(that())))
1576
- )
1577
- )
1578
-
1579
- /** @internal */
1580
- export const provideContext = dual<
1581
- <R>(context: Context.Context<R>) => <A, In, L, E>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In, L, E>,
1582
- <A, In, L, E, R>(self: Sink.Sink<A, In, L, E, R>, context: Context.Context<R>) => Sink.Sink<A, In, L, E>
1583
- >(
1584
- 2,
1585
- (self, context) => new SinkImpl(pipe(toChannel(self), core.provideContext(context)))
1586
- )
1587
-
1588
- /** @internal */
1589
- export const race = dual<
1590
- <R1, E1, In1, L1, A1>(
1591
- that: Sink.Sink<A1, In1, L1, E1, R1>
1592
- ) => <A, In, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A1 | A, In & In1, L1 | L, E1 | E, R1 | R>,
1593
- <A, In, L, E, R, A1, In1, L1, E1, R1>(
1594
- self: Sink.Sink<A, In, L, E, R>,
1595
- that: Sink.Sink<A1, In1, L1, E1, R1>
1596
- ) => Sink.Sink<A1 | A, In & In1, L1 | L, E1 | E, R1 | R>
1597
- >(
1598
- 2,
1599
- (self, that) => pipe(self, raceBoth(that), map(Either.merge))
1600
- )
1601
-
1602
- /** @internal */
1603
- export const raceBoth = dual<
1604
- <A1, In1, L1, E1, R1>(
1605
- that: Sink.Sink<A1, In1, L1, E1, R1>,
1606
- options?: {
1607
- readonly capacity?: number | undefined
1608
- }
1609
- ) => <A, In, L, E, R>(
1610
- self: Sink.Sink<A, In, L, E, R>
1611
- ) => Sink.Sink<Either.Either<A1, A>, In & In1, L1 | L, E1 | E, R1 | R>,
1612
- <A, In, L, E, R, A1, In1, L1, E1, R1>(
1613
- self: Sink.Sink<A, In, L, E, R>,
1614
- that: Sink.Sink<A1, In1, L1, E1, R1>,
1615
- options?: {
1616
- readonly capacity?: number | undefined
1617
- }
1618
- ) => Sink.Sink<Either.Either<A1, A>, In & In1, L1 | L, E1 | E, R1 | R>
1619
- >(
1620
- (args) => isSink(args[1]),
1621
- (self, that, options) =>
1622
- raceWith(self, {
1623
- other: that,
1624
- onSelfDone: (selfDone) => mergeDecision.Done(Effect.map(selfDone, Either.left)),
1625
- onOtherDone: (thatDone) => mergeDecision.Done(Effect.map(thatDone, Either.right)),
1626
- capacity: options?.capacity ?? 16
1627
- })
1628
- )
1629
-
1630
- /** @internal */
1631
- export const raceWith = dual<
1632
- <A2, In2, L2, E2, R2, A, E, A3, A4>(
1633
- options: {
1634
- readonly other: Sink.Sink<A2, In2, L2, E2, R2>
1635
- readonly onSelfDone: (exit: Exit.Exit<A, E>) => MergeDecision.MergeDecision<R2, E2, A2, E2 | E, A3>
1636
- readonly onOtherDone: (exit: Exit.Exit<A2, E2>) => MergeDecision.MergeDecision<R2, E, A, E2 | E, A4>
1637
- readonly capacity?: number | undefined
1638
- }
1639
- ) => <In, L, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A3 | A4, In & In2, L2 | L, E2 | E, R2 | R>,
1640
- <A, In, L, E, R, A2, In2, L2, E2, R2, A3, A4>(
1641
- self: Sink.Sink<A, In, L, E, R>,
1642
- options: {
1643
- readonly other: Sink.Sink<A2, In2, L2, E2, R2>
1644
- readonly onSelfDone: (exit: Exit.Exit<A, E>) => MergeDecision.MergeDecision<R2, E2, A2, E2 | E, A3>
1645
- readonly onOtherDone: (exit: Exit.Exit<A2, E2>) => MergeDecision.MergeDecision<R2, E, A, E2 | E, A4>
1646
- readonly capacity?: number | undefined
1647
- }
1648
- ) => Sink.Sink<A3 | A4, In & In2, L2 | L, E2 | E, R2 | R>
1649
- >(
1650
- 2,
1651
- <A, In, L, E, R, A2, In2, L2, E2, R2, A3, A4>(
1652
- self: Sink.Sink<A, In, L, E, R>,
1653
- options: {
1654
- readonly other: Sink.Sink<A2, In2, L2, E2, R2>
1655
- readonly onSelfDone: (exit: Exit.Exit<A, E>) => MergeDecision.MergeDecision<R2, E2, A2, E2 | E, A3>
1656
- readonly onOtherDone: (exit: Exit.Exit<A2, E2>) => MergeDecision.MergeDecision<R2, E, A, E2 | E, A4>
1657
- readonly capacity?: number | undefined
1658
- }
1659
- ): Sink.Sink<A3 | A4, In & In2, L2 | L, E2 | E, R2 | R> => {
1660
- function race(scope: Scope.Scope) {
1661
- return Effect.gen(function*() {
1662
- const pubsub = yield* PubSub.bounded<
1663
- Either.Either<Chunk.Chunk<In & In2>, Exit.Exit<unknown>>
1664
- >(options?.capacity ?? 16)
1665
- const subscription1 = yield* Scope.extend(PubSub.subscribe(pubsub), scope)
1666
- const subscription2 = yield* Scope.extend(PubSub.subscribe(pubsub), scope)
1667
- const reader = channel.toPubSub(pubsub)
1668
- const writer = channel.fromQueue(subscription1).pipe(
1669
- core.pipeTo(toChannel(self)),
1670
- channel.zipLeft(core.fromEffect(Queue.shutdown(subscription1))),
1671
- channel.mergeWith({
1672
- other: channel.fromQueue(subscription2).pipe(
1673
- core.pipeTo(toChannel(options.other)),
1674
- channel.zipLeft(core.fromEffect(Queue.shutdown(subscription2)))
1675
- ),
1676
- onSelfDone: options.onSelfDone,
1677
- onOtherDone: options.onOtherDone
1678
- })
1679
- )
1680
- const racedChannel = channel.mergeWith(reader, {
1681
- other: writer,
1682
- onSelfDone: () => mergeDecision.Await(identity),
1683
- onOtherDone: (exit) => mergeDecision.Done(exit)
1684
- }) as Channel.Channel<
1685
- Chunk.Chunk<L | L2>,
1686
- Chunk.Chunk<In & In2>,
1687
- E | E2,
1688
- never,
1689
- A3 | A4,
1690
- unknown,
1691
- R | R2
1692
- >
1693
- return new SinkImpl(racedChannel)
1694
- })
1695
- }
1696
- return unwrapScopedWith(race)
1697
- }
1698
- )
1699
-
1700
- /** @internal */
1701
- export const refineOrDie = dual<
1702
- <E, E2>(
1703
- pf: (error: E) => Option.Option<E2>
1704
- ) => <A, In, L, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In, L, E2, R>,
1705
- <A, In, L, E, R, E2>(
1706
- self: Sink.Sink<A, In, L, E, R>,
1707
- pf: (error: E) => Option.Option<E2>
1708
- ) => Sink.Sink<A, In, L, E2, R>
1709
- >(
1710
- 2,
1711
- (self, pf) => pipe(self, refineOrDieWith(pf, identity))
1712
- )
1713
-
1714
- /** @internal */
1715
- export const refineOrDieWith = dual<
1716
- <E, E2>(
1717
- pf: (error: E) => Option.Option<E2>,
1718
- f: (error: E) => unknown
1719
- ) => <A, In, L, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In, L, E2, R>,
1720
- <A, In, L, E, R, E2>(
1721
- self: Sink.Sink<A, In, L, E, R>,
1722
- pf: (error: E) => Option.Option<E2>,
1723
- f: (error: E) => unknown
1724
- ) => Sink.Sink<A, In, L, E2, R>
1725
- >(
1726
- 3,
1727
- (self, pf, f) => {
1728
- const newChannel = pipe(
1729
- self,
1730
- toChannel,
1731
- channel.catchAll((error) =>
1732
- Option.match(pf(error), {
1733
- onNone: () => core.failCauseSync(() => Cause.die(f(error))),
1734
- onSome: core.fail
1735
- })
1736
- )
1737
- )
1738
- return new SinkImpl(newChannel)
1739
- }
1740
- )
1741
-
1742
- /** @internal */
1743
- export const service = <I, S>(
1744
- tag: Context.Tag<I, S>
1745
- ): Sink.Sink<S, unknown, never, never, I> => serviceWith(tag, identity)
1746
-
1747
- /** @internal */
1748
- export const serviceWith = <I, S, Z>(
1749
- tag: Context.Tag<I, S>,
1750
- f: (service: Types.NoInfer<S>) => Z
1751
- ): Sink.Sink<Z, unknown, never, never, I> => fromEffect(Effect.map(tag, f))
1752
-
1753
- /** @internal */
1754
- export const serviceWithEffect = <I, S, R, E, Z>(
1755
- tag: Context.Tag<I, S>,
1756
- f: (service: Types.NoInfer<S>) => Effect.Effect<Z, E, R>
1757
- ): Sink.Sink<Z, unknown, never, E, R | I> => fromEffect(Effect.flatMap(tag, f))
1758
-
1759
- /** @internal */
1760
- export const serviceWithSink = <I, S, R, E, In, L, Z>(
1761
- tag: Context.Tag<I, S>,
1762
- f: (service: Types.NoInfer<S>) => Sink.Sink<Z, In, L, E, R>
1763
- ): Sink.Sink<Z, In, L, E, R | I> =>
1764
- new SinkImpl(pipe(Effect.map(tag, (service) => toChannel(f(service))), channel.unwrap))
1765
-
1766
- /** @internal */
1767
- export const some = <In>(predicate: Predicate<In>): Sink.Sink<boolean, In, In> =>
1768
- fold(false, (bool) => !bool, (acc, input) => acc || predicate(input))
1769
-
1770
- /** @internal */
1771
- export const splitWhere = dual<
1772
- <In>(f: Predicate<In>) => <A, L extends In, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In, In, E, R>,
1773
- <A, In, L extends In, E, R>(self: Sink.Sink<A, In, L, E, R>, f: Predicate<In>) => Sink.Sink<A, In, In, E, R>
1774
- >(2, <A, In, L extends In, E, R>(self: Sink.Sink<A, In, L, E, R>, f: Predicate<In>): Sink.Sink<A, In, In, E, R> => {
1775
- const newChannel = pipe(
1776
- core.fromEffect(Ref.make(Chunk.empty<In>())),
1777
- core.flatMap((ref) =>
1778
- pipe(
1779
- splitWhereSplitter<In, E>(false, ref, f),
1780
- channel.pipeToOrFail(toChannel(self)),
1781
- core.collectElements,
1782
- core.flatMap(([leftovers, z]) =>
1783
- pipe(
1784
- core.fromEffect(Ref.get(ref)),
1785
- core.flatMap((leftover) =>
1786
- pipe(
1787
- core.write<Chunk.Chunk<In>>(pipe(leftover, Chunk.appendAll(Chunk.flatten(leftovers)))),
1788
- channel.zipRight(core.succeed(z))
1789
- )
1790
- )
1791
- )
1792
- )
1793
- )
1794
- )
1795
- )
1796
- return new SinkImpl(newChannel)
1797
- })
1798
-
1799
- /** @internal */
1800
- const splitWhereSplitter = <A, E>(
1801
- written: boolean,
1802
- leftovers: Ref.Ref<Chunk.Chunk<A>>,
1803
- f: Predicate<A>
1804
- ): Channel.Channel<Chunk.Chunk<A>, Chunk.Chunk<A>, E, never, unknown, unknown> =>
1805
- core.readWithCause({
1806
- onInput: (input) => {
1807
- if (Chunk.isEmpty(input)) {
1808
- return splitWhereSplitter(written, leftovers, f)
1809
- }
1810
- if (written) {
1811
- const index = indexWhere(input, f)
1812
- if (index === -1) {
1813
- return channel.zipRight(
1814
- core.write(input),
1815
- splitWhereSplitter<A, E>(true, leftovers, f)
1816
- )
1817
- }
1818
- const [left, right] = Chunk.splitAt(input, index)
1819
- return channel.zipRight(
1820
- core.write(left),
1821
- core.fromEffect(Ref.set(leftovers, right))
1822
- )
1823
- }
1824
- const index = indexWhere(input, f, 1)
1825
- if (index === -1) {
1826
- return channel.zipRight(
1827
- core.write(input),
1828
- splitWhereSplitter<A, E>(true, leftovers, f)
1829
- )
1830
- }
1831
- const [left, right] = pipe(input, Chunk.splitAt(Math.max(index, 1)))
1832
- return channel.zipRight(core.write(left), core.fromEffect(Ref.set(leftovers, right)))
1833
- },
1834
- onFailure: core.failCause,
1835
- onDone: core.succeed
1836
- })
1837
-
1838
- /** @internal */
1839
- const indexWhere = <A>(self: Chunk.Chunk<A>, predicate: Predicate<A>, from = 0): number => {
1840
- const iterator = self[Symbol.iterator]()
1841
- let index = 0
1842
- let result = -1
1843
- let next: IteratorResult<A, any>
1844
- while (result < 0 && (next = iterator.next()) && !next.done) {
1845
- const a = next.value
1846
- if (index >= from && predicate(a)) {
1847
- result = index
1848
- }
1849
- index = index + 1
1850
- }
1851
- return result
1852
- }
1853
-
1854
- /** @internal */
1855
- export const succeed = <A>(a: A): Sink.Sink<A, unknown> => new SinkImpl(core.succeed(a))
1856
-
1857
- /** @internal */
1858
- export const sum: Sink.Sink<number, number> = foldLeftChunks(
1859
- 0,
1860
- (acc, chunk) => acc + Chunk.reduce(chunk, 0, (s, a) => s + a)
1861
- )
1862
-
1863
- /** @internal */
1864
- export const summarized = dual<
1865
- <A2, E2, R2, A3>(
1866
- summary: Effect.Effect<A2, E2, R2>,
1867
- f: (start: A2, end: A2) => A3
1868
- ) => <A, In, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<[A, A3], In, L, E2 | E, R2 | R>,
1869
- <A, In, L, E, R, A2, E2, R2, A3>(
1870
- self: Sink.Sink<A, In, L, E, R>,
1871
- summary: Effect.Effect<A2, E2, R2>,
1872
- f: (start: A2, end: A2) => A3
1873
- ) => Sink.Sink<[A, A3], In, L, E2 | E, R2 | R>
1874
- >(
1875
- 3,
1876
- (self, summary, f) => {
1877
- const newChannel = pipe(
1878
- core.fromEffect(summary),
1879
- core.flatMap((start) =>
1880
- pipe(
1881
- self,
1882
- toChannel,
1883
- core.flatMap((done) =>
1884
- pipe(
1885
- core.fromEffect(summary),
1886
- channel.map((end) => [done, f(start, end)])
1887
- )
1888
- )
1889
- )
1890
- )
1891
- )
1892
- return new SinkImpl(newChannel)
1893
- }
1894
- )
1895
-
1896
- /** @internal */
1897
- export const sync = <A>(evaluate: LazyArg<A>): Sink.Sink<A, unknown> => new SinkImpl(core.sync(evaluate))
1898
-
1899
- /** @internal */
1900
- export const take = <In>(n: number): Sink.Sink<Chunk.Chunk<In>, In, In> =>
1901
- pipe(
1902
- foldChunks<Chunk.Chunk<In>, In>(
1903
- Chunk.empty(),
1904
- (chunk) => chunk.length < n,
1905
- (acc, chunk) => pipe(acc, Chunk.appendAll(chunk))
1906
- ),
1907
- flatMap((acc) => {
1908
- const [taken, leftover] = pipe(acc, Chunk.splitAt(n))
1909
- return new SinkImpl(pipe(core.write(leftover), channel.zipRight(core.succeedNow(taken))))
1910
- })
1911
- )
1912
-
1913
- /** @internal */
1914
- export const toChannel = <A, In, L, E, R>(
1915
- self: Sink.Sink<A, In, L, E, R>
1916
- ): Channel.Channel<Chunk.Chunk<L>, Chunk.Chunk<In>, E, never, A, unknown, R> =>
1917
- Effect.isEffect(self) ?
1918
- toChannel(fromEffect(self as Effect.Effect<A, E, R>)) :
1919
- (self as SinkImpl<A, In, L, E, R>).channel
1920
-
1921
- /** @internal */
1922
- export const unwrap = <A, In, L, E2, R2, E, R>(
1923
- effect: Effect.Effect<Sink.Sink<A, In, L, E2, R2>, E, R>
1924
- ): Sink.Sink<A, In, L, E | E2, R | R2> =>
1925
- new SinkImpl(
1926
- channel.unwrap(pipe(effect, Effect.map((sink) => toChannel(sink))))
1927
- )
1928
-
1929
- /** @internal */
1930
- export const unwrapScoped = <A, In, L, E, R>(
1931
- effect: Effect.Effect<Sink.Sink<A, In, L, E, R>, E, R>
1932
- ): Sink.Sink<A, In, L, E, Exclude<R, Scope.Scope>> =>
1933
- new SinkImpl(
1934
- channel.unwrapScoped(effect.pipe(
1935
- Effect.map((sink) => toChannel(sink))
1936
- ))
1937
- )
1938
-
1939
- /** @internal */
1940
- export const unwrapScopedWith = <A, In, L, E, R>(
1941
- f: (scope: Scope.Scope) => Effect.Effect<Sink.Sink<A, In, L, E, R>, E, R>
1942
- ): Sink.Sink<A, In, L, E, R> =>
1943
- new SinkImpl(
1944
- channel.unwrapScopedWith((scope) =>
1945
- f(scope).pipe(
1946
- Effect.map((sink) => toChannel(sink))
1947
- )
1948
- )
1949
- )
1950
-
1951
- /** @internal */
1952
- export const withDuration = <A, In, L, E, R>(
1953
- self: Sink.Sink<A, In, L, E, R>
1954
- ): Sink.Sink<[A, Duration.Duration], In, L, E, R> =>
1955
- pipe(self, summarized(Clock.currentTimeMillis, (start, end) => Duration.millis(end - start)))
1956
-
1957
- /** @internal */
1958
- export const zip = dual<
1959
- <A2, In, In2 extends In, L2, E2, R2>(
1960
- that: Sink.Sink<A2, In2, L2, E2, R2>,
1961
- options?: {
1962
- readonly concurrent?: boolean | undefined
1963
- }
1964
- ) => <A, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<[A, A2], In & In2, L | L2, E2 | E, R2 | R>,
1965
- <A, In, L, E, R, A2, In2 extends In, L2, E2, R2>(
1966
- self: Sink.Sink<A, In, L, E, R>,
1967
- that: Sink.Sink<A2, In2, L2, E2, R2>,
1968
- options?: {
1969
- readonly concurrent?: boolean | undefined
1970
- }
1971
- ) => Sink.Sink<[A, A2], In & In2, L | L2, E2 | E, R2 | R>
1972
- >(
1973
- (args) => isSink(args[1]),
1974
- <A, In, L, E, R, A2, In2 extends In, L2, E2, R2>(
1975
- self: Sink.Sink<A, In, L, E, R>,
1976
- that: Sink.Sink<A2, In2, L2, E2, R2>,
1977
- options?: {
1978
- readonly concurrent?: boolean | undefined
1979
- }
1980
- ): Sink.Sink<[A, A2], In & In2, L | L2, E2 | E, R2 | R> => zipWith(self, that, (z, z2) => [z, z2], options)
1981
- )
1982
-
1983
- /** @internal */
1984
- export const zipLeft = dual<
1985
- <A2, In, In2 extends In, L2, E2, R2>(
1986
- that: Sink.Sink<A2, In2, L2, E2, R2>,
1987
- options?: {
1988
- readonly concurrent?: boolean | undefined
1989
- }
1990
- ) => <A, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A, In & In2, L | L2, E2 | E, R2 | R>,
1991
- <A, In, L, E, R, A2, In2 extends In, L2, E2, R2>(
1992
- self: Sink.Sink<A, In, L, E, R>,
1993
- that: Sink.Sink<A2, In2, L2, E2, R2>,
1994
- options?: {
1995
- readonly concurrent?: boolean | undefined
1996
- }
1997
- ) => Sink.Sink<A, In & In2, L | L2, E2 | E, R2 | R>
1998
- >(
1999
- (args) => isSink(args[1]),
2000
- <A, In, L, E, R, A2, In2 extends In, L2, E2, R2>(
2001
- self: Sink.Sink<A, In, L, E, R>,
2002
- that: Sink.Sink<A2, In2, L2, E2, R2>,
2003
- options?: {
2004
- readonly concurrent?: boolean | undefined
2005
- }
2006
- ): Sink.Sink<A, In & In2, L | L2, E2 | E, R2 | R> => zipWith(self, that, (z, _) => z, options)
2007
- )
2008
-
2009
- /** @internal */
2010
- export const zipRight = dual<
2011
- <A2, In, In2 extends In, L2, E2, R2>(
2012
- that: Sink.Sink<A2, In2, L2, E2, R2>,
2013
- options?: {
2014
- readonly concurrent?: boolean | undefined
2015
- }
2016
- ) => <A, L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A2, In & In2, L | L2, E2 | E, R2 | R>,
2017
- <A, In, L, E, R, A2, In2 extends In, L2, E2, R2>(
2018
- self: Sink.Sink<A, In, L, E, R>,
2019
- that: Sink.Sink<A2, In2, L2, E2, R2>,
2020
- options?: {
2021
- readonly concurrent?: boolean | undefined
2022
- }
2023
- ) => Sink.Sink<A2, In & In2, L | L2, E2 | E, R2 | R>
2024
- >(
2025
- (args) => isSink(args[1]),
2026
- <A, In, L, E, R, A2, In2 extends In, L2, E2, R2>(
2027
- self: Sink.Sink<A, In, L, E, R>,
2028
- that: Sink.Sink<A2, In2, L2, E2, R2>,
2029
- options?: {
2030
- readonly concurrent?: boolean | undefined
2031
- }
2032
- ): Sink.Sink<A2, In & In2, L | L2, E2 | E, R2 | R> => zipWith(self, that, (_, z2) => z2, options)
2033
- )
2034
-
2035
- /** @internal */
2036
- export const zipWith = dual<
2037
- <A2, In, In2 extends In, L2, E2, R2, A, A3>(
2038
- that: Sink.Sink<A2, In2, L2, E2, R2>,
2039
- f: (a: A, a2: A2) => A3,
2040
- options?: {
2041
- readonly concurrent?: boolean | undefined
2042
- }
2043
- ) => <L, E, R>(self: Sink.Sink<A, In, L, E, R>) => Sink.Sink<A3, In & In2, L | L2, E2 | E, R2 | R>,
2044
- <A, In, L, E, R, A2, In2 extends In, L2, E2, R2, A3>(
2045
- self: Sink.Sink<A, In, L, E, R>,
2046
- that: Sink.Sink<A2, In2, L2, E2, R2>,
2047
- f: (a: A, a2: A2) => A3,
2048
- options?: {
2049
- readonly concurrent?: boolean | undefined
2050
- }
2051
- ) => Sink.Sink<A3, In & In2, L | L2, E2 | E, R2 | R>
2052
- >(
2053
- (args) => isSink(args[1]),
2054
- <A, In, L, E, R, A2, In2 extends In, L2, E2, R2, A3>(
2055
- self: Sink.Sink<A, In, L, E, R>,
2056
- that: Sink.Sink<A2, In2, L2, E2, R2>,
2057
- f: (a: A, a2: A2) => A3,
2058
- options?: {
2059
- readonly concurrent?: boolean | undefined
2060
- }
2061
- ): Sink.Sink<A3, In & In2, L | L2, E2 | E, R2 | R> =>
2062
- options?.concurrent ?
2063
- raceWith(self, {
2064
- other: that,
2065
- onSelfDone: Exit.match({
2066
- onFailure: (cause) => mergeDecision.Done(Effect.failCause(cause)),
2067
- onSuccess: (leftZ) =>
2068
- mergeDecision.Await<R | R2, E2, A2, E | E2, A3>(
2069
- Exit.match({
2070
- onFailure: Effect.failCause,
2071
- onSuccess: (rightZ) => Effect.succeed(f(leftZ, rightZ))
2072
- })
2073
- )
2074
- }),
2075
- onOtherDone: Exit.match({
2076
- onFailure: (cause) => mergeDecision.Done(Effect.failCause(cause)),
2077
- onSuccess: (rightZ) =>
2078
- mergeDecision.Await<R | R2, E, A, E | E2, A3>(
2079
- Exit.match({
2080
- onFailure: Effect.failCause,
2081
- onSuccess: (leftZ) => Effect.succeed(f(leftZ, rightZ))
2082
- })
2083
- )
2084
- })
2085
- }) :
2086
- flatMap(self, (z) => map(that, (z2) => f(z, z2)))
2087
- )
2088
-
2089
- // Circular with Channel
2090
-
2091
- /** @internal */
2092
- export const channelToSink = <OutElem, InElem, OutErr, InErr, OutDone, Env>(
2093
- self: Channel.Channel<Chunk.Chunk<OutElem>, Chunk.Chunk<InElem>, OutErr, InErr, OutDone, unknown, Env>
2094
- ): Sink.Sink<OutDone, InElem, OutElem, OutErr, Env> => new SinkImpl(self)
2095
-
2096
- // Constants
2097
-
2098
- /** @internal */
2099
- export const count: Sink.Sink<number, unknown> = foldLeftChunks(
2100
- 0,
2101
- (acc, chunk) => acc + chunk.length
2102
- )
2103
-
2104
- /** @internal */
2105
- export const mkString: Sink.Sink<string, unknown> = suspend(() => {
2106
- const strings: Array<string> = []
2107
- return pipe(
2108
- foldLeftChunks<void, unknown>(void 0, (_, elems) =>
2109
- Chunk.map(elems, (elem) => {
2110
- strings.push(String(elem))
2111
- })),
2112
- map(() => strings.join(""))
2113
- )
2114
- })
2115
-
2116
- /** @internal */
2117
- export const timed: Sink.Sink<Duration.Duration, unknown> = pipe(
2118
- withDuration(drain),
2119
- map((tuple) => tuple[1])
2120
- )