@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,1453 +0,0 @@
1
- import * as RA from "../../Array.js"
2
- import * as Cause from "../../Cause.js"
3
- import * as Chunk from "../../Chunk.js"
4
- import * as Context from "../../Context.js"
5
- import * as Effect from "../../Effect.js"
6
- import * as Either from "../../Either.js"
7
- import * as Exit from "../../Exit.js"
8
- import type * as FiberId from "../../FiberId.js"
9
- import type { LazyArg } from "../../Function.js"
10
- import { constFalse, constTrue, constVoid, dual, identity, pipe } from "../../Function.js"
11
- import * as Option from "../../Option.js"
12
- import type { Predicate, Refinement } from "../../Predicate.js"
13
- import * as predicate from "../../Predicate.js"
14
- import type * as STM from "../../STM.js"
15
- import type * as Types from "../../Types.js"
16
- import { yieldWrapGet } from "../../Utils.js"
17
- import * as effectCore from "../core.js"
18
- import * as core from "./core.js"
19
- import * as Journal from "./journal.js"
20
- import * as STMState from "./stmState.js"
21
-
22
- /** @internal */
23
- export const acquireUseRelease = dual<
24
- <A, A2, E2, R2, A3, E3, R3>(
25
- use: (resource: A) => STM.STM<A2, E2, R2>,
26
- release: (resource: A) => STM.STM<A3, E3, R3>
27
- ) => <E, R>(
28
- acquire: STM.STM<A, E, R>
29
- ) => Effect.Effect<A2, E | E2 | E3, R | R2 | R3>,
30
- <A, E, R, A2, E2, R2, A3, E3, R3>(
31
- acquire: STM.STM<A, E, R>,
32
- use: (resource: A) => STM.STM<A2, E2, R2>,
33
- release: (resource: A) => STM.STM<A3, E3, R3>
34
- ) => Effect.Effect<A2, E | E2 | E3, R | R2 | R3>
35
- >(3, <A, E, R, A2, E2, R2, A3, E3, R3>(
36
- acquire: STM.STM<A, E, R>,
37
- use: (resource: A) => STM.STM<A2, E2, R2>,
38
- release: (resource: A) => STM.STM<A3, E3, R3>
39
- ): Effect.Effect<A2, E | E2 | E3, R | R2 | R3> =>
40
- Effect.uninterruptibleMask((restore) => {
41
- let state: STMState.STMState<A, E> = STMState.running
42
- return pipe(
43
- restore(
44
- core.unsafeAtomically(
45
- acquire,
46
- (exit) => {
47
- state = STMState.done(exit)
48
- },
49
- () => {
50
- state = STMState.interrupted
51
- }
52
- )
53
- ),
54
- Effect.matchCauseEffect({
55
- onFailure: (cause) => {
56
- if (STMState.isDone(state) && Exit.isSuccess(state.exit)) {
57
- return pipe(
58
- release(state.exit.value),
59
- Effect.matchCauseEffect({
60
- onFailure: (cause2) => Effect.failCause(Cause.parallel(cause, cause2)),
61
- onSuccess: () => Effect.failCause(cause)
62
- })
63
- )
64
- }
65
- return Effect.failCause(cause)
66
- },
67
- onSuccess: (a) =>
68
- pipe(
69
- restore(use(a)),
70
- Effect.matchCauseEffect({
71
- onFailure: (cause) =>
72
- pipe(
73
- release(a),
74
- Effect.matchCauseEffect({
75
- onFailure: (cause2) => Effect.failCause(Cause.parallel(cause, cause2)),
76
- onSuccess: () => Effect.failCause(cause)
77
- })
78
- ),
79
- onSuccess: (a2) => pipe(release(a), Effect.as(a2))
80
- })
81
- )
82
- })
83
- )
84
- }))
85
-
86
- /** @internal */
87
- export const as = dual<
88
- <A2>(value: A2) => <A, E, R>(self: STM.STM<A, E, R>) => STM.STM<A2, E, R>,
89
- <A, E, R, A2>(self: STM.STM<A, E, R>, value: A2) => STM.STM<A2, E, R>
90
- >(2, (self, value) => pipe(self, core.map(() => value)))
91
-
92
- /** @internal */
93
- export const asSome = <A, E, R>(self: STM.STM<A, E, R>): STM.STM<Option.Option<A>, E, R> =>
94
- pipe(self, core.map(Option.some))
95
-
96
- /** @internal */
97
- export const asSomeError = <A, E, R>(self: STM.STM<A, E, R>): STM.STM<A, Option.Option<E>, R> =>
98
- pipe(self, mapError(Option.some))
99
-
100
- /** @internal */
101
- export const asVoid = <A, E, R>(self: STM.STM<A, E, R>): STM.STM<void, E, R> => pipe(self, core.map(constVoid))
102
-
103
- /** @internal */
104
- export const attempt = <A>(evaluate: LazyArg<A>): STM.STM<A, unknown> =>
105
- suspend(() => {
106
- try {
107
- return core.succeed(evaluate())
108
- } catch (defect) {
109
- return core.fail(defect)
110
- }
111
- })
112
-
113
- export const bind = dual<
114
- <N extends string, K, A, E2, R2>(
115
- tag: Exclude<N, keyof K>,
116
- f: (_: K) => STM.STM<A, E2, R2>
117
- ) => <E, R>(self: STM.STM<K, E, R>) => STM.STM<Types.MergeRecord<K, { [k in N]: A }>, E | E2, R | R2>,
118
- <K, E, R, N extends string, A, E2, R2>(
119
- self: STM.STM<K, E, R>,
120
- tag: Exclude<N, keyof K>,
121
- f: (_: K) => STM.STM<A, E2, R2>
122
- ) => STM.STM<Types.MergeRecord<K, { [k in N]: A }>, E | E2, R | R2>
123
- >(3, <K, E, R, N extends string, A, E2, R2>(
124
- self: STM.STM<K, E, R>,
125
- tag: Exclude<N, keyof K>,
126
- f: (_: K) => STM.STM<A, E2, R2>
127
- ) =>
128
- core.flatMap(self, (k) =>
129
- core.map(
130
- f(k),
131
- (a): Types.MergeRecord<K, { [k in N]: A }> => ({ ...k, [tag]: a } as any)
132
- )))
133
-
134
- /* @internal */
135
- export const bindTo = dual<
136
- <N extends string>(tag: N) => <A, E, R>(self: STM.STM<A, E, R>) => STM.STM<
137
- Record<N, A>,
138
- E,
139
- R
140
- >,
141
- <A, E, R, N extends string>(
142
- self: STM.STM<A, E, R>,
143
- tag: N
144
- ) => STM.STM<
145
- Record<N, A>,
146
- E,
147
- R
148
- >
149
- >(
150
- 2,
151
- <A, E, R, N extends string>(self: STM.STM<A, E, R>, tag: N): STM.STM<Record<N, A>, E, R> =>
152
- core.map(self, (a) => ({ [tag]: a } as Record<N, A>))
153
- )
154
-
155
- /* @internal */
156
- export const let_ = dual<
157
- <N extends string, K, A>(
158
- tag: Exclude<N, keyof K>,
159
- f: (_: K) => A
160
- ) => <E, R>(self: STM.STM<K, E, R>) => STM.STM<
161
- Types.MergeRecord<K, { [k in N]: A }>,
162
- E,
163
- R
164
- >,
165
- <K, E, R, N extends string, A>(
166
- self: STM.STM<K, E, R>,
167
- tag: Exclude<N, keyof K>,
168
- f: (_: K) => A
169
- ) => STM.STM<
170
- Types.MergeRecord<K, { [k in N]: A }>,
171
- E,
172
- R
173
- >
174
- >(3, <K, E, R, N extends string, A>(self: STM.STM<K, E, R>, tag: Exclude<N, keyof K>, f: (_: K) => A) =>
175
- core.map(
176
- self,
177
- (k): Types.MergeRecord<K, { [k in N]: A }> => ({ ...k, [tag]: f(k) } as any)
178
- ))
179
-
180
- /** @internal */
181
- export const catchSome = dual<
182
- <E, A2, E2, R2>(
183
- pf: (error: E) => Option.Option<STM.STM<A2, E2, R2>>
184
- ) => <A, R>(
185
- self: STM.STM<A, E, R>
186
- ) => STM.STM<A2 | A, E | E2, R2 | R>,
187
- <A, E, R, A2, E2, R2>(
188
- self: STM.STM<A, E, R>,
189
- pf: (error: E) => Option.Option<STM.STM<A2, E2, R2>>
190
- ) => STM.STM<A2 | A, E | E2, R2 | R>
191
- >(2, <A, E, R, A2, E2, R2>(
192
- self: STM.STM<A, E, R>,
193
- pf: (error: E) => Option.Option<STM.STM<A2, E2, R2>>
194
- ): STM.STM<A2 | A, E | E2, R2 | R> =>
195
- core.catchAll(
196
- self,
197
- (e): STM.STM<A | A2, E | E2, R | R2> => Option.getOrElse(pf(e), () => core.fail(e))
198
- ))
199
-
200
- /** @internal */
201
- export const catchTag = dual<
202
- <K extends E["_tag"] & string, E extends { _tag: string }, A1, E1, R1>(
203
- k: K,
204
- f: (e: Extract<E, { _tag: K }>) => STM.STM<A1, E1, R1>
205
- ) => <A, R>(self: STM.STM<A, E, R>) => STM.STM<A | A1, Exclude<E, { _tag: K }> | E1, R | R1>,
206
- <A, E extends { _tag: string }, R, K extends E["_tag"] & string, A1, E1, R1>(
207
- self: STM.STM<A, E, R>,
208
- k: K,
209
- f: (e: Extract<E, { _tag: K }>) => STM.STM<A1, E1, R1>
210
- ) => STM.STM<A | A1, Exclude<E, { _tag: K }> | E1, R | R1>
211
- >(3, (self, k, f) =>
212
- core.catchAll(self, (e) => {
213
- if ("_tag" in e && e["_tag"] === k) {
214
- return f(e as any)
215
- }
216
- return core.fail(e as any)
217
- }))
218
-
219
- /** @internal */
220
- export const catchTags: {
221
- <
222
- E extends { _tag: string },
223
- Cases extends {
224
- [K in E["_tag"]]+?: (error: Extract<E, { _tag: K }>) => STM.STM<any, any, any>
225
- }
226
- >(
227
- cases: Cases
228
- ): <A, R>(self: STM.STM<A, E, R>) => STM.STM<
229
- | A
230
- | {
231
- [K in keyof Cases]: Cases[K] extends ((...args: Array<any>) => STM.STM<infer A, any, any>) ? A : never
232
- }[keyof Cases],
233
- | Exclude<E, { _tag: keyof Cases }>
234
- | {
235
- [K in keyof Cases]: Cases[K] extends ((...args: Array<any>) => STM.STM<any, infer E, any>) ? E : never
236
- }[keyof Cases],
237
- | R
238
- | {
239
- [K in keyof Cases]: Cases[K] extends ((...args: Array<any>) => STM.STM<any, any, infer R>) ? R : never
240
- }[keyof Cases]
241
- >
242
- <
243
- R,
244
- E extends { _tag: string },
245
- A,
246
- Cases extends {
247
- [K in E["_tag"]]+?: (error: Extract<E, { _tag: K }>) => STM.STM<any, any, any>
248
- }
249
- >(
250
- self: STM.STM<A, E, R>,
251
- cases: Cases
252
- ): STM.STM<
253
- | A
254
- | {
255
- [K in keyof Cases]: Cases[K] extends ((...args: Array<any>) => STM.STM<infer A, any, any>) ? A : never
256
- }[keyof Cases],
257
- | Exclude<E, { _tag: keyof Cases }>
258
- | {
259
- [K in keyof Cases]: Cases[K] extends ((...args: Array<any>) => STM.STM<any, infer E, any>) ? E : never
260
- }[keyof Cases],
261
- | R
262
- | {
263
- [K in keyof Cases]: Cases[K] extends ((...args: Array<any>) => STM.STM<any, any, infer R>) ? R : never
264
- }[keyof Cases]
265
- >
266
- } = dual(2, (self, cases) =>
267
- core.catchAll(self, (e: any) => {
268
- const keys = Object.keys(cases)
269
- if ("_tag" in e && keys.includes(e["_tag"])) {
270
- return cases[e["_tag"]](e as any)
271
- }
272
- return core.fail(e as any)
273
- }))
274
-
275
- /** @internal */
276
- export const check = (predicate: LazyArg<boolean>): STM.STM<void> => suspend(() => predicate() ? void_ : core.retry)
277
-
278
- /** @internal */
279
- export const collect = dual<
280
- <A, A2>(pf: (a: A) => Option.Option<A2>) => <E, R>(self: STM.STM<A, E, R>) => STM.STM<A2, E, R>,
281
- <A, E, R, A2>(self: STM.STM<A, E, R>, pf: (a: A) => Option.Option<A2>) => STM.STM<A2, E, R>
282
- >(2, (self, pf) =>
283
- collectSTM(
284
- self,
285
- (a) => Option.map(pf(a), core.succeed)
286
- ))
287
-
288
- /** @internal */
289
- export const collectSTM = dual<
290
- <A, A2, E2, R2>(
291
- pf: (a: A) => Option.Option<STM.STM<A2, E2, R2>>
292
- ) => <E, R>(
293
- self: STM.STM<A, E, R>
294
- ) => STM.STM<A2, E2 | E, R2 | R>,
295
- <A, E, R, A2, E2, R2>(
296
- self: STM.STM<A, E, R>,
297
- pf: (a: A) => Option.Option<STM.STM<A2, E2, R2>>
298
- ) => STM.STM<A2, E2 | E, R2 | R>
299
- >(2, (self, pf) =>
300
- core.matchSTM(self, {
301
- onFailure: core.fail,
302
- onSuccess: (a) => {
303
- const option = pf(a)
304
- return Option.isSome(option) ? option.value : core.retry
305
- }
306
- }))
307
-
308
- /** @internal */
309
- export const commitEither = <A, E, R>(self: STM.STM<A, E, R>): Effect.Effect<A, E, R> =>
310
- Effect.flatten(core.commit(either(self)))
311
-
312
- /** @internal */
313
- export const cond = <A, E>(
314
- predicate: LazyArg<boolean>,
315
- error: LazyArg<E>,
316
- result: LazyArg<A>
317
- ): STM.STM<A, E> => {
318
- return suspend(
319
- () => predicate() ? core.sync(result) : core.failSync(error)
320
- )
321
- }
322
-
323
- /** @internal */
324
- export const either = <A, E, R>(self: STM.STM<A, E, R>): STM.STM<Either.Either<A, E>, never, R> =>
325
- match(self, { onFailure: Either.left, onSuccess: Either.right })
326
-
327
- /** @internal */
328
- export const eventually = <A, E, R>(self: STM.STM<A, E, R>): STM.STM<A, E, R> =>
329
- core.matchSTM(self, { onFailure: () => eventually(self), onSuccess: core.succeed })
330
-
331
- /** @internal */
332
- export const every = dual<
333
- <A, R, E>(
334
- predicate: (a: Types.NoInfer<A>) => STM.STM<boolean, E, R>
335
- ) => (iterable: Iterable<A>) => STM.STM<boolean, E, R>,
336
- <A, R, E>(iterable: Iterable<A>, predicate: (a: A) => STM.STM<boolean, E, R>) => STM.STM<boolean, E, R>
337
- >(
338
- 2,
339
- <A, R, E>(
340
- iterable: Iterable<A>,
341
- predicate: (a: A) => STM.STM<boolean, E, R>
342
- ): STM.STM<boolean, E, R> =>
343
- core.flatMap(core.sync(() => iterable[Symbol.iterator]()), (iterator) => {
344
- const loop: STM.STM<boolean, E, R> = suspend(() => {
345
- const next = iterator.next()
346
- if (next.done) {
347
- return core.succeed(true)
348
- }
349
- return pipe(
350
- predicate(next.value),
351
- core.flatMap((bool) => bool ? loop : core.succeed(bool))
352
- )
353
- })
354
- return loop
355
- })
356
- )
357
-
358
- /** @internal */
359
- export const exists = dual<
360
- <A, R, E>(
361
- predicate: (a: Types.NoInfer<A>) => STM.STM<boolean, E, R>
362
- ) => (iterable: Iterable<A>) => STM.STM<boolean, E, R>,
363
- <A, R, E>(iterable: Iterable<A>, predicate: (a: A) => STM.STM<boolean, E, R>) => STM.STM<boolean, E, R>
364
- >(
365
- 2,
366
- <A, R, E>(iterable: Iterable<A>, predicate: (a: A) => STM.STM<boolean, E, R>): STM.STM<boolean, E, R> =>
367
- core.flatMap(core.sync(() => iterable[Symbol.iterator]()), (iterator) => {
368
- const loop: STM.STM<boolean, E, R> = suspend(() => {
369
- const next = iterator.next()
370
- if (next.done) {
371
- return core.succeed(false)
372
- }
373
- return core.flatMap(
374
- predicate(next.value),
375
- (bool) => bool ? core.succeed(bool) : loop
376
- )
377
- })
378
- return loop
379
- })
380
- )
381
-
382
- /** @internal */
383
- export const fiberId: STM.STM<FiberId.FiberId> = core.effect<never, FiberId.FiberId>((_, fiberId) => fiberId)
384
-
385
- /** @internal */
386
- export const filter = dual<
387
- <A, R, E>(
388
- predicate: (a: Types.NoInfer<A>) => STM.STM<boolean, E, R>
389
- ) => (iterable: Iterable<A>) => STM.STM<Array<A>, E, R>,
390
- <A, R, E>(iterable: Iterable<A>, predicate: (a: A) => STM.STM<boolean, E, R>) => STM.STM<Array<A>, E, R>
391
- >(
392
- 2,
393
- <A, R, E>(iterable: Iterable<A>, predicate: (a: A) => STM.STM<boolean, E, R>): STM.STM<Array<A>, E, R> =>
394
- Array.from(iterable).reduce(
395
- (acc, curr) =>
396
- pipe(
397
- acc,
398
- core.zipWith(predicate(curr), (as, p) => {
399
- if (p) {
400
- as.push(curr)
401
- return as
402
- }
403
- return as
404
- })
405
- ),
406
- core.succeed([]) as STM.STM<Array<A>, E, R>
407
- )
408
- )
409
-
410
- /** @internal */
411
- export const filterNot = dual<
412
- <A, R, E>(
413
- predicate: (a: Types.NoInfer<A>) => STM.STM<boolean, E, R>
414
- ) => (iterable: Iterable<A>) => STM.STM<Array<A>, E, R>,
415
- <A, R, E>(iterable: Iterable<A>, predicate: (a: A) => STM.STM<boolean, E, R>) => STM.STM<Array<A>, E, R>
416
- >(
417
- 2,
418
- <A, R, E>(iterable: Iterable<A>, predicate: (a: A) => STM.STM<boolean, E, R>): STM.STM<Array<A>, E, R> =>
419
- filter(iterable, (a) => negate(predicate(a)))
420
- )
421
-
422
- /** @internal */
423
- export const filterOrDie: {
424
- <A, B extends A>(
425
- refinement: Refinement<Types.NoInfer<A>, B>,
426
- defect: LazyArg<unknown>
427
- ): <E, R>(self: STM.STM<A, E, R>) => STM.STM<B, E, R>
428
- <A>(
429
- predicate: Predicate<Types.NoInfer<A>>,
430
- defect: LazyArg<unknown>
431
- ): <E, R>(self: STM.STM<A, E, R>) => STM.STM<A, E, R>
432
- <A, E, R, B extends A>(
433
- self: STM.STM<A, E, R>,
434
- refinement: Refinement<A, B>,
435
- defect: LazyArg<unknown>
436
- ): STM.STM<B, E, R>
437
- <A, E, R>(self: STM.STM<A, E, R>, predicate: Predicate<A>, defect: LazyArg<unknown>): STM.STM<A, E, R>
438
- } = dual(
439
- 3,
440
- <A, E, R>(self: STM.STM<A, E, R>, predicate: Predicate<A>, defect: LazyArg<unknown>): STM.STM<A, E, R> =>
441
- filterOrElse(self, predicate, () => core.dieSync(defect))
442
- )
443
-
444
- /** @internal */
445
- export const filterOrDieMessage: {
446
- <A, B extends A>(
447
- refinement: Refinement<Types.NoInfer<A>, B>,
448
- message: string
449
- ): <E, R>(self: STM.STM<A, E, R>) => STM.STM<B, E, R>
450
- <A>(predicate: Predicate<Types.NoInfer<A>>, message: string): <E, R>(self: STM.STM<A, E, R>) => STM.STM<A, E, R>
451
- <A, E, R, B extends A>(self: STM.STM<A, E, R>, refinement: Refinement<A, B>, message: string): STM.STM<B, E, R>
452
- <A, E, R>(self: STM.STM<A, E, R>, predicate: Predicate<A>, message: string): STM.STM<A, E, R>
453
- } = dual(
454
- 3,
455
- <A, E, R>(self: STM.STM<A, E, R>, predicate: Predicate<A>, message: string): STM.STM<A, E, R> =>
456
- filterOrElse(self, predicate, () => core.dieMessage(message))
457
- )
458
-
459
- /** @internal */
460
- export const filterOrElse: {
461
- <A, B extends A, C, E2, R2>(
462
- refinement: Refinement<Types.NoInfer<A>, B>,
463
- orElse: (a: Types.NoInfer<A>) => STM.STM<C, E2, R2>
464
- ): <E, R>(self: STM.STM<A, E, R>) => STM.STM<B | C, E2 | E, R2 | R>
465
- <A, B, E2, R2>(
466
- predicate: Predicate<Types.NoInfer<A>>,
467
- orElse: (a: Types.NoInfer<A>) => STM.STM<B, E2, R2>
468
- ): <E, R>(self: STM.STM<A, E, R>) => STM.STM<A | B, E2 | E, R2 | R>
469
- <A, E, R, B extends A, C, E2, R2>(
470
- self: STM.STM<A, E, R>,
471
- refinement: Refinement<A, B>,
472
- orElse: (a: A) => STM.STM<C, E2, R2>
473
- ): STM.STM<B | C, E | E2, R | R2>
474
- <A, E, R, B, E2, R2>(
475
- self: STM.STM<A, E, R>,
476
- predicate: Predicate<A>,
477
- orElse: (a: A) => STM.STM<B, E2, R2>
478
- ): STM.STM<A | B, E | E2, R | R2>
479
- } = dual(
480
- 3,
481
- <A, E, R, B, E2, R2>(
482
- self: STM.STM<A, E, R>,
483
- predicate: Predicate<A>,
484
- orElse: (a: A) => STM.STM<B, E2, R2>
485
- ): STM.STM<A | B, E | E2, R | R2> =>
486
- core.flatMap(self, (a): STM.STM<A | B, E2, R2> => predicate(a) ? core.succeed(a) : orElse(a))
487
- )
488
-
489
- /** @internal */
490
- export const filterOrFail: {
491
- <A, B extends A, E2>(
492
- refinement: Refinement<Types.NoInfer<A>, B>,
493
- orFailWith: (a: Types.NoInfer<A>) => E2
494
- ): <E, R>(self: STM.STM<A, E, R>) => STM.STM<B, E2 | E, R>
495
- <A, E2>(
496
- predicate: Predicate<Types.NoInfer<A>>,
497
- orFailWith: (a: Types.NoInfer<A>) => E2
498
- ): <E, R>(self: STM.STM<A, E, R>) => STM.STM<A, E2 | E, R>
499
- <A, E, R, B extends A, E2>(
500
- self: STM.STM<A, E, R>,
501
- refinement: Refinement<A, B>,
502
- orFailWith: (a: A) => E2
503
- ): STM.STM<B, E | E2, R>
504
- <A, E, R, E2>(self: STM.STM<A, E, R>, predicate: Predicate<A>, orFailWith: (a: A) => E2): STM.STM<A, E | E2, R>
505
- } = dual(
506
- 3,
507
- <A, E, R, E2>(self: STM.STM<A, E, R>, predicate: Predicate<A>, orFailWith: (a: A) => E2): STM.STM<A, E | E2, R> =>
508
- filterOrElse(
509
- self,
510
- predicate,
511
- (a) => core.failSync(() => orFailWith(a))
512
- )
513
- )
514
-
515
- /** @internal */
516
- export const flatten = <A, E2, R2, E, R>(self: STM.STM<STM.STM<A, E2, R2>, E, R>): STM.STM<A, E | E2, R | R2> =>
517
- core.flatMap(self, identity)
518
-
519
- /** @internal */
520
- export const flip = <A, E, R>(self: STM.STM<A, E, R>): STM.STM<E, A, R> =>
521
- core.matchSTM(self, { onFailure: core.succeed, onSuccess: core.fail })
522
-
523
- /** @internal */
524
- export const flipWith = dual<
525
- <E, A, R, E2, A2, R2>(
526
- f: (stm: STM.STM<E, A, R>) => STM.STM<E2, A2, R2>
527
- ) => (
528
- self: STM.STM<A, E, R>
529
- ) => STM.STM<A | A2, E | E2, R | R2>,
530
- <A, E, R, E2, A2, R2>(
531
- self: STM.STM<A, E, R>,
532
- f: (stm: STM.STM<E, A, R>) => STM.STM<E2, A2, R2>
533
- ) => STM.STM<A | A2, E | E2, R | R2>
534
- >(2, (self, f) => flip(f(flip(self))))
535
-
536
- /** @internal */
537
- export const match = dual<
538
- <E, A2, A, A3>(options: {
539
- readonly onFailure: (error: E) => A2
540
- readonly onSuccess: (value: A) => A3
541
- }) => <R>(self: STM.STM<A, E, R>) => STM.STM<A2 | A3, never, R>,
542
- <A, E, R, A2, A3>(self: STM.STM<A, E, R>, options: {
543
- readonly onFailure: (error: E) => A2
544
- readonly onSuccess: (value: A) => A3
545
- }) => STM.STM<A2 | A3, never, R>
546
- >(2, (self, { onFailure, onSuccess }) =>
547
- core.matchSTM(self, {
548
- onFailure: (e) => core.succeed(onFailure(e)),
549
- onSuccess: (a) => core.succeed(onSuccess(a))
550
- }))
551
-
552
- /** @internal */
553
- export const forEach = dual<
554
- {
555
- <A, A2, E, R>(f: (a: A) => STM.STM<A2, E, R>, options?: {
556
- readonly discard?: false | undefined
557
- }): (elements: Iterable<A>) => STM.STM<Array<A2>, E, R>
558
- <A, A2, E, R>(f: (a: A) => STM.STM<A2, E, R>, options: {
559
- readonly discard: true
560
- }): (elements: Iterable<A>) => STM.STM<void, E, R>
561
- },
562
- {
563
- <A, A2, E, R>(elements: Iterable<A>, f: (a: A) => STM.STM<A2, E, R>, options?: {
564
- readonly discard?: false | undefined
565
- }): STM.STM<Array<A2>, E, R>
566
- <A, A2, E, R>(elements: Iterable<A>, f: (a: A) => STM.STM<A2, E, R>, options: {
567
- readonly discard: true
568
- }): STM.STM<void, E, R>
569
- }
570
- >(
571
- (args) => predicate.isIterable(args[0]),
572
- <A, A2, E, R>(iterable: Iterable<A>, f: (a: A) => STM.STM<A2, E, R>, options?: {
573
- readonly discard?: boolean | undefined
574
- }): STM.STM<any, E, R> => {
575
- if (options?.discard) {
576
- return pipe(
577
- core.sync(() => iterable[Symbol.iterator]()),
578
- core.flatMap((iterator) => {
579
- const loop: STM.STM<void, E, R> = suspend(() => {
580
- const next = iterator.next()
581
- if (next.done) {
582
- return void_
583
- }
584
- return pipe(f(next.value), core.flatMap(() => loop))
585
- })
586
- return loop
587
- })
588
- )
589
- }
590
-
591
- return suspend(() =>
592
- RA.fromIterable(iterable).reduce(
593
- (acc, curr) =>
594
- core.zipWith(acc, f(curr), (array, elem) => {
595
- array.push(elem)
596
- return array
597
- }),
598
- core.succeed([]) as STM.STM<Array<A2>, E, R>
599
- )
600
- )
601
- }
602
- )
603
-
604
- /** @internal */
605
- export const fromEither = <A, E>(either: Either.Either<A, E>): STM.STM<A, E> => {
606
- switch (either._tag) {
607
- case "Left": {
608
- return core.fail(either.left)
609
- }
610
- case "Right": {
611
- return core.succeed(either.right)
612
- }
613
- }
614
- }
615
-
616
- /** @internal */
617
- export const fromOption = <A>(option: Option.Option<A>): STM.STM<A, Option.Option<never>> =>
618
- Option.match(option, {
619
- onNone: () => core.fail(Option.none()),
620
- onSome: core.succeed
621
- })
622
-
623
- /**
624
- * Inspired by https://github.com/tusharmath/qio/pull/22 (revised)
625
- * @internal
626
- */
627
- export const gen: typeof STM.gen = (...args) =>
628
- suspend(() => {
629
- const f = (args.length === 1)
630
- ? args[0]
631
- : args[1].bind(args[0])
632
- const iterator = f(pipe)
633
- const state = iterator.next()
634
- const run = (
635
- state: IteratorYieldResult<any> | IteratorReturnResult<any>
636
- ): STM.STM<any, any, any> =>
637
- state.done ?
638
- core.succeed(state.value) :
639
- core.flatMap(yieldWrapGet(state.value) as any, (val: any) => run(iterator.next(val as never)))
640
- return run(state)
641
- })
642
-
643
- /** @internal */
644
- export const head = <A, E, R>(self: STM.STM<Iterable<A>, E, R>): STM.STM<A, Option.Option<E>, R> =>
645
- pipe(
646
- self,
647
- core.matchSTM({
648
- onFailure: (e) => core.fail(Option.some(e)),
649
- onSuccess: (a) => {
650
- const i = a[Symbol.iterator]()
651
- const res = i.next()
652
- if (res.done) {
653
- return core.fail(Option.none())
654
- } else {
655
- return core.succeed(res.value)
656
- }
657
- }
658
- })
659
- )
660
-
661
- /** @internal */
662
- export const if_ = dual<
663
- <A, E1, R1, A2, E2, R2>(
664
- options: {
665
- readonly onTrue: STM.STM<A, E1, R1>
666
- readonly onFalse: STM.STM<A2, E2, R2>
667
- }
668
- ) => <E = never, R = never>(
669
- self: STM.STM<boolean, E, R> | boolean
670
- ) => STM.STM<A | A2, E1 | E2 | E, R1 | R2 | R>,
671
- {
672
- <A, E1, R1, A2, E2, R2, E = never, R = never>(
673
- self: boolean,
674
- options: {
675
- readonly onTrue: STM.STM<A, E1, R1>
676
- readonly onFalse: STM.STM<A2, E2, R2>
677
- }
678
- ): STM.STM<A | A2, E1 | E2 | E, R1 | R2 | R>
679
- <E, R, A, E1, R1, A2, E2, R2>(
680
- self: STM.STM<boolean, E, R>,
681
- options: {
682
- readonly onTrue: STM.STM<A, E1, R1>
683
- readonly onFalse: STM.STM<A2, E2, R2>
684
- }
685
- ): STM.STM<A | A2, E1 | E2 | E, R1 | R2 | R>
686
- }
687
- >(
688
- (args) => typeof args[0] === "boolean" || core.isSTM(args[0]),
689
- <E, R, A, E1, R1, A2, E2, R2>(
690
- self: STM.STM<boolean, E, R> | boolean,
691
- { onFalse, onTrue }: {
692
- readonly onTrue: STM.STM<A, E1, R1>
693
- readonly onFalse: STM.STM<A2, E2, R2>
694
- }
695
- ) => {
696
- if (typeof self === "boolean") {
697
- return self ? onTrue : onFalse
698
- }
699
-
700
- return core.flatMap(self, (bool): STM.STM<A | A2, E1 | E2 | E, R1 | R2 | R> => bool ? onTrue : onFalse)
701
- }
702
- )
703
-
704
- /** @internal */
705
- export const ignore = <A, E, R>(self: STM.STM<A, E, R>): STM.STM<void, never, R> =>
706
- match(self, { onFailure: () => void_, onSuccess: () => void_ })
707
-
708
- /** @internal */
709
- export const isFailure = <A, E, R>(self: STM.STM<A, E, R>): STM.STM<boolean, never, R> =>
710
- match(self, { onFailure: constTrue, onSuccess: constFalse })
711
-
712
- /** @internal */
713
- export const isSuccess = <A, E, R>(self: STM.STM<A, E, R>): STM.STM<boolean, never, R> =>
714
- match(self, { onFailure: constFalse, onSuccess: constTrue })
715
-
716
- /** @internal */
717
- export const iterate = <Z, E, R>(
718
- initial: Z,
719
- options: {
720
- readonly while: (z: Z) => boolean
721
- readonly body: (z: Z) => STM.STM<Z, E, R>
722
- }
723
- ): STM.STM<Z, E, R> => iterateLoop(initial, options.while, options.body)
724
-
725
- const iterateLoop = <Z, E, R>(
726
- initial: Z,
727
- cont: (z: Z) => boolean,
728
- body: (z: Z) => STM.STM<Z, E, R>
729
- ): STM.STM<Z, E, R> => {
730
- if (cont(initial)) {
731
- return pipe(
732
- body(initial),
733
- core.flatMap((z) => iterateLoop(z, cont, body))
734
- )
735
- }
736
- return core.succeed(initial)
737
- }
738
-
739
- /** @internal */
740
- export const loop: {
741
- <Z, A, E, R>(
742
- initial: Z,
743
- options: {
744
- readonly while: (z: Z) => boolean
745
- readonly step: (z: Z) => Z
746
- readonly body: (z: Z) => STM.STM<A, E, R>
747
- readonly discard?: false | undefined
748
- }
749
- ): STM.STM<Array<A>, E, R>
750
- <Z, A, E, R>(
751
- initial: Z,
752
- options: {
753
- readonly while: (z: Z) => boolean
754
- readonly step: (z: Z) => Z
755
- readonly body: (z: Z) => STM.STM<A, E, R>
756
- readonly discard: true
757
- }
758
- ): STM.STM<void, E, R>
759
- } = <Z, A, E, R>(
760
- initial: Z,
761
- options: {
762
- readonly while: (z: Z) => boolean
763
- readonly step: (z: Z) => Z
764
- readonly body: (z: Z) => STM.STM<A, E, R>
765
- readonly discard?: boolean | undefined
766
- }
767
- ): STM.STM<any, E, R> =>
768
- options.discard ?
769
- loopDiscardLoop(initial, options.while, options.step, options.body) :
770
- core.map(loopLoop(initial, options.while, options.step, options.body), (a) => Array.from(a))
771
-
772
- const loopLoop = <Z, A, E, R>(
773
- initial: Z,
774
- cont: (z: Z) => boolean,
775
- inc: (z: Z) => Z,
776
- body: (z: Z) => STM.STM<A, E, R>
777
- ): STM.STM<Chunk.Chunk<A>, E, R> => {
778
- if (cont(initial)) {
779
- return pipe(
780
- body(initial),
781
- core.flatMap((a) => pipe(loopLoop(inc(initial), cont, inc, body), core.map(Chunk.append(a))))
782
- )
783
- }
784
- return core.succeed(Chunk.empty<A>())
785
- }
786
-
787
- const loopDiscardLoop = <Z, R, E, X>(
788
- initial: Z,
789
- cont: (z: Z) => boolean,
790
- inc: (z: Z) => Z,
791
- body: (z: Z) => STM.STM<X, E, R>
792
- ): STM.STM<void, E, R> => {
793
- if (cont(initial)) {
794
- return pipe(
795
- body(initial),
796
- core.flatMap(() => loopDiscardLoop(inc(initial), cont, inc, body))
797
- )
798
- }
799
- return void_
800
- }
801
-
802
- /** @internal */
803
- export const mapAttempt = dual<
804
- <A, B>(f: (a: A) => B) => <E, R>(self: STM.STM<A, E, R>) => STM.STM<B, unknown, R>,
805
- <A, E, R, B>(self: STM.STM<A, E, R>, f: (a: A) => B) => STM.STM<B, unknown, R>
806
- >(2, <A, E, R, B>(self: STM.STM<A, E, R>, f: (a: A) => B): STM.STM<B, unknown, R> =>
807
- core.matchSTM(self, {
808
- onFailure: (e) => core.fail(e),
809
- onSuccess: (a) => attempt(() => f(a))
810
- }))
811
-
812
- /** @internal */
813
- export const mapBoth = dual<
814
- <E, E2, A, A2>(options: {
815
- readonly onFailure: (error: E) => E2
816
- readonly onSuccess: (value: A) => A2
817
- }) => <R>(self: STM.STM<A, E, R>) => STM.STM<A2, E2, R>,
818
- <A, E, R, E2, A2>(self: STM.STM<A, E, R>, options: {
819
- readonly onFailure: (error: E) => E2
820
- readonly onSuccess: (value: A) => A2
821
- }) => STM.STM<A2, E2, R>
822
- >(2, (self, { onFailure, onSuccess }) =>
823
- core.matchSTM(self, {
824
- onFailure: (e) => core.fail(onFailure(e)),
825
- onSuccess: (a) => core.succeed(onSuccess(a))
826
- }))
827
-
828
- /** @internal */
829
- export const mapError = dual<
830
- <E, E2>(f: (error: E) => E2) => <A, R>(self: STM.STM<A, E, R>) => STM.STM<A, E2, R>,
831
- <A, E, R, E2>(self: STM.STM<A, E, R>, f: (error: E) => E2) => STM.STM<A, E2, R>
832
- >(2, (self, f) =>
833
- core.matchSTM(self, {
834
- onFailure: (e) => core.fail(f(e)),
835
- onSuccess: core.succeed
836
- }))
837
-
838
- /** @internal */
839
- export const merge = <A, E, R>(self: STM.STM<A, E, R>): STM.STM<E | A, never, R> =>
840
- core.matchSTM(self, { onFailure: (e) => core.succeed(e), onSuccess: core.succeed })
841
-
842
- /** @internal */
843
- export const mergeAll = dual<
844
- <A2, A>(zero: A2, f: (a2: A2, a: A) => A2) => <E, R>(iterable: Iterable<STM.STM<A, E, R>>) => STM.STM<A2, E, R>,
845
- <A, E, R, A2>(iterable: Iterable<STM.STM<A, E, R>>, zero: A2, f: (a2: A2, a: A) => A2) => STM.STM<A2, E, R>
846
- >(
847
- 3,
848
- <A, E, R, A2>(iterable: Iterable<STM.STM<A, E, R>>, zero: A2, f: (a2: A2, a: A) => A2): STM.STM<A2, E, R> =>
849
- suspend(() =>
850
- Array.from(iterable).reduce(
851
- (acc, curr) => pipe(acc, core.zipWith(curr, f)),
852
- core.succeed(zero) as STM.STM<A2, E, R>
853
- )
854
- )
855
- )
856
-
857
- /** @internal */
858
- export const negate = <E, R>(self: STM.STM<boolean, E, R>): STM.STM<boolean, E, R> => pipe(self, core.map((b) => !b))
859
-
860
- /** @internal */
861
- export const none = <A, E, R>(self: STM.STM<Option.Option<A>, E, R>): STM.STM<void, Option.Option<E>, R> =>
862
- core.matchSTM(self, {
863
- onFailure: (e) => core.fail(Option.some(e)),
864
- onSuccess: Option.match({
865
- onNone: () => void_,
866
- onSome: () => core.fail(Option.none())
867
- })
868
- })
869
-
870
- /** @internal */
871
- export const option = <A, E, R>(self: STM.STM<A, E, R>): STM.STM<Option.Option<A>, never, R> =>
872
- match(self, { onFailure: () => Option.none(), onSuccess: Option.some })
873
-
874
- /** @internal */
875
- export const orDie = <A, E, R>(self: STM.STM<A, E, R>): STM.STM<A, never, R> => pipe(self, orDieWith(identity))
876
-
877
- /** @internal */
878
- export const orDieWith = dual<
879
- <E>(f: (error: E) => unknown) => <A, R>(self: STM.STM<A, E, R>) => STM.STM<A, never, R>,
880
- <A, E, R>(self: STM.STM<A, E, R>, f: (error: E) => unknown) => STM.STM<A, never, R>
881
- >(2, (self, f) => pipe(self, mapError(f), core.catchAll(core.die)))
882
-
883
- /** @internal */
884
- export const orElse = dual<
885
- <A2, E2, R2>(that: LazyArg<STM.STM<A2, E2, R2>>) => <A, E, R>(self: STM.STM<A, E, R>) => STM.STM<A2 | A, E2, R2 | R>,
886
- <A, E, R, A2, E2, R2>(self: STM.STM<A, E, R>, that: LazyArg<STM.STM<A2, E2, R2>>) => STM.STM<A2 | A, E2, R2 | R>
887
- >(
888
- 2,
889
- <A, E, R, A2, E2, R2>(self: STM.STM<A, E, R>, that: LazyArg<STM.STM<A2, E2, R2>>): STM.STM<A2 | A, E2, R2 | R> =>
890
- core.flatMap(core.effect<R, LazyArg<void>>((journal) => Journal.prepareResetJournal(journal)), (reset) =>
891
- pipe(
892
- core.orTry(self, () => core.flatMap(core.sync(reset), that)),
893
- core.catchAll(() => core.flatMap(core.sync(reset), that))
894
- ))
895
- )
896
-
897
- /** @internal */
898
- export const orElseEither = dual<
899
- <A2, E2, R2>(
900
- that: LazyArg<STM.STM<A2, E2, R2>>
901
- ) => <A, E, R>(
902
- self: STM.STM<A, E, R>
903
- ) => STM.STM<Either.Either<A2, A>, E2, R2 | R>,
904
- <A, E, R, A2, E2, R2>(
905
- self: STM.STM<A, E, R>,
906
- that: LazyArg<STM.STM<A2, E2, R2>>
907
- ) => STM.STM<Either.Either<A2, A>, E2, R2 | R>
908
- >(
909
- 2,
910
- <A, E, R, A2, E2, R2>(
911
- self: STM.STM<A, E, R>,
912
- that: LazyArg<STM.STM<A2, E2, R2>>
913
- ): STM.STM<Either.Either<A2, A>, E2, R2 | R> =>
914
- orElse(core.map(self, Either.left), () => core.map(that(), Either.right))
915
- )
916
-
917
- /** @internal */
918
- export const orElseFail = dual<
919
- <E2>(error: LazyArg<E2>) => <A, E, R>(self: STM.STM<A, E, R>) => STM.STM<A, E2, R>,
920
- <A, E, R, E2>(self: STM.STM<A, E, R>, error: LazyArg<E2>) => STM.STM<A, E2, R>
921
- >(
922
- 2,
923
- <A, E, R, E2>(self: STM.STM<A, E, R>, error: LazyArg<E2>): STM.STM<A, E2, R> =>
924
- orElse(self, () => core.failSync(error))
925
- )
926
-
927
- /** @internal */
928
- export const orElseOptional = dual<
929
- <A2, E2, R2>(
930
- that: LazyArg<STM.STM<A2, Option.Option<E2>, R2>>
931
- ) => <A, E, R>(
932
- self: STM.STM<A, Option.Option<E>, R>
933
- ) => STM.STM<A2 | A, Option.Option<E2 | E>, R2 | R>,
934
- <A, E, R, A2, E2, R2>(
935
- self: STM.STM<A, Option.Option<E>, R>,
936
- that: LazyArg<STM.STM<A2, Option.Option<E2>, R2>>
937
- ) => STM.STM<A2 | A, Option.Option<E2 | E>, R2 | R>
938
- >(
939
- 2,
940
- <A, E, R, A2, E2, R2>(
941
- self: STM.STM<A, Option.Option<E>, R>,
942
- that: LazyArg<STM.STM<A2, Option.Option<E2>, R2>>
943
- ): STM.STM<A2 | A, Option.Option<E2 | E>, R2 | R> =>
944
- core.catchAll(
945
- self,
946
- Option.match({
947
- onNone: that,
948
- onSome: (e) => core.fail(Option.some<E | E2>(e))
949
- })
950
- )
951
- )
952
-
953
- /** @internal */
954
- export const orElseSucceed = dual<
955
- <A2>(value: LazyArg<A2>) => <A, E, R>(self: STM.STM<A, E, R>) => STM.STM<A2 | A, never, R>,
956
- <A, E, R, A2>(self: STM.STM<A, E, R>, value: LazyArg<A2>) => STM.STM<A2 | A, never, R>
957
- >(
958
- 2,
959
- <A, E, R, A2>(self: STM.STM<A, E, R>, value: LazyArg<A2>): STM.STM<A2 | A, never, R> =>
960
- orElse(self, () => core.sync(value))
961
- )
962
-
963
- /** @internal */
964
- export const provideContext = dual<
965
- <R>(env: Context.Context<R>) => <A, E>(self: STM.STM<A, E, R>) => STM.STM<A, E>,
966
- <A, E, R>(self: STM.STM<A, E, R>, env: Context.Context<R>) => STM.STM<A, E>
967
- >(2, (self, env) => core.mapInputContext(self, (_: Context.Context<never>) => env))
968
-
969
- /** @internal */
970
- export const provideSomeContext = dual<
971
- <R>(context: Context.Context<R>) => <R1, E, A>(self: STM.STM<A, E, R1>) => STM.STM<A, E, Exclude<R1, R>>,
972
- <R, R1, E, A>(self: STM.STM<A, E, R1>, context: Context.Context<R>) => STM.STM<A, E, Exclude<R1, R>>
973
- >(2, <R, R1, E, A>(
974
- self: STM.STM<A, E, R1>,
975
- context: Context.Context<R>
976
- ): STM.STM<A, E, Exclude<R1, R>> =>
977
- core.mapInputContext(
978
- self,
979
- (parent: Context.Context<Exclude<R1, R>>): Context.Context<R1> => Context.merge(parent, context) as any
980
- ))
981
-
982
- /** @internal */
983
- export const provideService = dual<
984
- <I, S>(
985
- tag: Context.Tag<I, S>,
986
- resource: Types.NoInfer<S>
987
- ) => <A, E, R>(
988
- self: STM.STM<A, E, R>
989
- ) => STM.STM<A, E, Exclude<R, I>>,
990
- <A, E, R, I, S>(
991
- self: STM.STM<A, E, R>,
992
- tag: Context.Tag<I, S>,
993
- resource: Types.NoInfer<S>
994
- ) => STM.STM<A, E, Exclude<R, I>>
995
- >(3, (self, tag, resource) => provideServiceSTM(self, tag, core.succeed(resource)))
996
-
997
- /** @internal */
998
- export const provideServiceSTM = dual<
999
- <I, S, E1, R1>(
1000
- tag: Context.Tag<I, S>,
1001
- stm: STM.STM<Types.NoInfer<S>, E1, R1>
1002
- ) => <A, E, R>(
1003
- self: STM.STM<A, E, R>
1004
- ) => STM.STM<A, E1 | E, R1 | Exclude<R, I>>,
1005
- <A, E, R, I, S, E1, R1>(
1006
- self: STM.STM<A, E, R>,
1007
- tag: Context.Tag<I, S>,
1008
- stm: STM.STM<Types.NoInfer<S>, E1, R1>
1009
- ) => STM.STM<A, E1 | E, R1 | Exclude<R, I>>
1010
- >(3, <A, E, R, I, S, E1, R1>(
1011
- self: STM.STM<A, E, R>,
1012
- tag: Context.Tag<I, S>,
1013
- stm: STM.STM<Types.NoInfer<S>, E1, R1>
1014
- ): STM.STM<A, E1 | E, R1 | Exclude<R, I>> =>
1015
- core.contextWithSTM((env: Context.Context<R1 | Exclude<R, I>>) =>
1016
- core.flatMap(
1017
- stm,
1018
- (service) =>
1019
- provideContext(
1020
- self,
1021
- Context.add(env, tag, service) as Context.Context<R | R1>
1022
- )
1023
- )
1024
- ))
1025
-
1026
- /** @internal */
1027
- export const reduce = dual<
1028
- <S, A, E, R>(zero: S, f: (s: S, a: A) => STM.STM<S, E, R>) => (iterable: Iterable<A>) => STM.STM<S, E, R>,
1029
- <S, A, E, R>(iterable: Iterable<A>, zero: S, f: (s: S, a: A) => STM.STM<S, E, R>) => STM.STM<S, E, R>
1030
- >(
1031
- 3,
1032
- <S, A, R, E>(iterable: Iterable<A>, zero: S, f: (s: S, a: A) => STM.STM<S, E, R>): STM.STM<S, E, R> =>
1033
- suspend(() =>
1034
- Array.from(iterable).reduce(
1035
- (acc, curr) => pipe(acc, core.flatMap((s) => f(s, curr))),
1036
- core.succeed(zero) as STM.STM<S, E, R>
1037
- )
1038
- )
1039
- )
1040
-
1041
- /** @internal */
1042
- export const reduceAll = dual<
1043
- <A, E2, R2>(
1044
- initial: STM.STM<A, E2, R2>,
1045
- f: (x: A, y: A) => A
1046
- ) => <E, R>(
1047
- iterable: Iterable<STM.STM<A, E, R>>
1048
- ) => STM.STM<A, E2 | E, R2 | R>,
1049
- <A, E, R, E2, R2>(
1050
- iterable: Iterable<STM.STM<A, E, R>>,
1051
- initial: STM.STM<A, E2, R2>,
1052
- f: (x: A, y: A) => A
1053
- ) => STM.STM<A, E2 | E, R2 | R>
1054
- >(3, <A, E, R, E2, R2>(
1055
- iterable: Iterable<STM.STM<A, E, R>>,
1056
- initial: STM.STM<A, E2, R2>,
1057
- f: (x: A, y: A) => A
1058
- ): STM.STM<A, E2 | E, R2 | R> =>
1059
- suspend(() =>
1060
- Array.from(iterable).reduce(
1061
- (acc, curr) => pipe(acc, core.zipWith(curr, f)),
1062
- initial as STM.STM<A, E | E2, R | R2>
1063
- )
1064
- ))
1065
-
1066
- /** @internal */
1067
- export const reduceRight = dual<
1068
- <S, A, R, E>(zero: S, f: (s: S, a: A) => STM.STM<S, E, R>) => (iterable: Iterable<A>) => STM.STM<S, E, R>,
1069
- <S, A, R, E>(iterable: Iterable<A>, zero: S, f: (s: S, a: A) => STM.STM<S, E, R>) => STM.STM<S, E, R>
1070
- >(
1071
- 3,
1072
- <S, A, R, E>(iterable: Iterable<A>, zero: S, f: (s: S, a: A) => STM.STM<S, E, R>): STM.STM<S, E, R> =>
1073
- suspend(() =>
1074
- Array.from(iterable).reduceRight(
1075
- (acc, curr) => pipe(acc, core.flatMap((s) => f(s, curr))),
1076
- core.succeed(zero) as STM.STM<S, E, R>
1077
- )
1078
- )
1079
- )
1080
-
1081
- /** @internal */
1082
- export const refineOrDie = dual<
1083
- <E, E2>(pf: (error: E) => Option.Option<E2>) => <A, R>(self: STM.STM<A, E, R>) => STM.STM<A, E2, R>,
1084
- <A, E, R, E2>(self: STM.STM<A, E, R>, pf: (error: E) => Option.Option<E2>) => STM.STM<A, E2, R>
1085
- >(2, (self, pf) => refineOrDieWith(self, pf, identity))
1086
-
1087
- /** @internal */
1088
- export const refineOrDieWith = dual<
1089
- <E, E2>(
1090
- pf: (error: E) => Option.Option<E2>,
1091
- f: (error: E) => unknown
1092
- ) => <A, R>(
1093
- self: STM.STM<A, E, R>
1094
- ) => STM.STM<A, E2, R>,
1095
- <A, E, R, E2>(
1096
- self: STM.STM<A, E, R>,
1097
- pf: (error: E) => Option.Option<E2>,
1098
- f: (error: E) => unknown
1099
- ) => STM.STM<A, E2, R>
1100
- >(3, (self, pf, f) =>
1101
- core.catchAll(
1102
- self,
1103
- (e) =>
1104
- Option.match(pf(e), {
1105
- onNone: () => core.die(f(e)),
1106
- onSome: core.fail
1107
- })
1108
- ))
1109
-
1110
- /** @internal */
1111
- export const reject = dual<
1112
- <A, E2>(pf: (a: A) => Option.Option<E2>) => <E, R>(self: STM.STM<A, E, R>) => STM.STM<A, E2 | E, R>,
1113
- <A, E, R, E2>(self: STM.STM<A, E, R>, pf: (a: A) => Option.Option<E2>) => STM.STM<A, E2 | E, R>
1114
- >(2, (self, pf) =>
1115
- rejectSTM(
1116
- self,
1117
- (a) => Option.map(pf(a), core.fail)
1118
- ))
1119
-
1120
- /** @internal */
1121
- export const rejectSTM = dual<
1122
- <A, E2, R2>(
1123
- pf: (a: A) => Option.Option<STM.STM<E2, E2, R2>>
1124
- ) => <E, R>(
1125
- self: STM.STM<A, E, R>
1126
- ) => STM.STM<A, E2 | E, R2 | R>,
1127
- <A, E, R, E2, R2>(
1128
- self: STM.STM<A, E, R>,
1129
- pf: (a: A) => Option.Option<STM.STM<E2, E2, R2>>
1130
- ) => STM.STM<A, E2 | E, R2 | R>
1131
- >(2, (self, pf) =>
1132
- core.flatMap(self, (a) =>
1133
- Option.match(pf(a), {
1134
- onNone: () => core.succeed(a),
1135
- onSome: core.flatMap(core.fail)
1136
- })))
1137
-
1138
- /** @internal */
1139
- export const repeatUntil = dual<
1140
- <A>(predicate: Predicate<A>) => <E, R>(self: STM.STM<A, E, R>) => STM.STM<A, E, R>,
1141
- <A, E, R>(self: STM.STM<A, E, R>, predicate: Predicate<A>) => STM.STM<A, E, R>
1142
- >(2, (self, predicate) => repeatUntilLoop(self, predicate))
1143
-
1144
- const repeatUntilLoop = <A, E, R>(self: STM.STM<A, E, R>, predicate: Predicate<A>): STM.STM<A, E, R> =>
1145
- core.flatMap(self, (a) =>
1146
- predicate(a) ?
1147
- core.succeed(a) :
1148
- repeatUntilLoop(self, predicate))
1149
-
1150
- /** @internal */
1151
- export const repeatWhile = dual<
1152
- <A>(predicate: Predicate<A>) => <E, R>(self: STM.STM<A, E, R>) => STM.STM<A, E, R>,
1153
- <A, E, R>(self: STM.STM<A, E, R>, predicate: Predicate<A>) => STM.STM<A, E, R>
1154
- >(2, (self, predicate) => repeatWhileLoop(self, predicate))
1155
-
1156
- const repeatWhileLoop = <A, E, R>(self: STM.STM<A, E, R>, predicate: Predicate<A>): STM.STM<A, E, R> =>
1157
- core.flatMap(self, (a) =>
1158
- predicate(a) ?
1159
- repeatWhileLoop(self, predicate) :
1160
- core.succeed(a))
1161
-
1162
- /** @internal */
1163
- export const replicate = dual<
1164
- (n: number) => <A, E, R>(self: STM.STM<A, E, R>) => Array<STM.STM<A, E, R>>,
1165
- <A, E, R>(self: STM.STM<A, E, R>, n: number) => Array<STM.STM<A, E, R>>
1166
- >(2, (self, n) => Array.from({ length: n }, () => self))
1167
-
1168
- /** @internal */
1169
- export const replicateSTM = dual<
1170
- (n: number) => <A, E, R>(self: STM.STM<A, E, R>) => STM.STM<Array<A>, E, R>,
1171
- <A, E, R>(self: STM.STM<A, E, R>, n: number) => STM.STM<Array<A>, E, R>
1172
- >(2, (self, n) => all(replicate(self, n)))
1173
-
1174
- /** @internal */
1175
- export const replicateSTMDiscard = dual<
1176
- (n: number) => <A, E, R>(self: STM.STM<A, E, R>) => STM.STM<void, E, R>,
1177
- <A, E, R>(self: STM.STM<A, E, R>, n: number) => STM.STM<void, E, R>
1178
- >(2, (self, n) => all(replicate(self, n), { discard: true }))
1179
-
1180
- /** @internal */
1181
- export const retryUntil = dual<
1182
- {
1183
- <A, B extends A>(refinement: Refinement<Types.NoInfer<A>, B>): <E, R>(self: STM.STM<A, E, R>) => STM.STM<B, E, R>
1184
- <A>(predicate: Predicate<A>): <E, R>(self: STM.STM<A, E, R>) => STM.STM<A, E, R>
1185
- },
1186
- {
1187
- <A, E, R, B extends A>(self: STM.STM<A, E, R>, refinement: Refinement<A, B>): STM.STM<B, E, R>
1188
- <A, E, R>(self: STM.STM<A, E, R>, predicate: Predicate<A>): STM.STM<A, E, R>
1189
- }
1190
- >(
1191
- 2,
1192
- <A, E, R>(self: STM.STM<A, E, R>, predicate: Predicate<A>) =>
1193
- core.matchSTM(self, { onFailure: core.fail, onSuccess: (a) => predicate(a) ? core.succeed(a) : core.retry })
1194
- )
1195
-
1196
- /** @internal */
1197
- export const retryWhile = dual<
1198
- <A>(predicate: Predicate<A>) => <E, R>(self: STM.STM<A, E, R>) => STM.STM<A, E, R>,
1199
- <A, E, R>(self: STM.STM<A, E, R>, predicate: Predicate<A>) => STM.STM<A, E, R>
1200
- >(
1201
- 2,
1202
- (self, predicate) =>
1203
- core.matchSTM(self, { onFailure: core.fail, onSuccess: (a) => !predicate(a) ? core.succeed(a) : core.retry })
1204
- )
1205
-
1206
- /** @internal */
1207
- export const partition = dual<
1208
- <A, A2, E, R>(
1209
- f: (a: A) => STM.STM<A2, E, R>
1210
- ) => (
1211
- elements: Iterable<A>
1212
- ) => STM.STM<[excluded: Array<E>, satisfying: Array<A2>], never, R>,
1213
- <A, A2, E, R>(
1214
- elements: Iterable<A>,
1215
- f: (a: A) => STM.STM<A2, E, R>
1216
- ) => STM.STM<[excluded: Array<E>, satisfying: Array<A2>], never, R>
1217
- >(2, (elements, f) =>
1218
- pipe(
1219
- forEach(elements, (a) => either(f(a))),
1220
- core.map((as) => effectCore.partitionMap(as, identity))
1221
- ))
1222
-
1223
- /** @internal */
1224
- export const some = <A, E, R>(self: STM.STM<Option.Option<A>, E, R>): STM.STM<A, Option.Option<E>, R> =>
1225
- core.matchSTM(self, {
1226
- onFailure: (e) => core.fail(Option.some(e)),
1227
- onSuccess: Option.match({
1228
- onNone: () => core.fail(Option.none()),
1229
- onSome: core.succeed
1230
- })
1231
- })
1232
-
1233
- /* @internal */
1234
- export const all = ((
1235
- input: Iterable<STM.All.STMAny> | Record<string, STM.All.STMAny>,
1236
- options?: STM.All.Options
1237
- ): STM.STM<any, any, any> => {
1238
- if (Symbol.iterator in input) {
1239
- return forEach(input, identity, options as any)
1240
- } else if (options?.discard) {
1241
- return forEach(Object.values(input), identity, options as any)
1242
- }
1243
-
1244
- return core.map(
1245
- forEach(
1246
- Object.entries(input),
1247
- ([_, e]) => core.map(e, (a) => [_, a] as const)
1248
- ),
1249
- (values) => {
1250
- const res = {}
1251
- for (const [k, v] of values) {
1252
- ;(res as any)[k] = v
1253
- }
1254
- return res
1255
- }
1256
- )
1257
- }) as STM.All.Signature
1258
-
1259
- /** @internal */
1260
- export const succeedNone: STM.STM<Option.Option<never>> = core.succeed(Option.none())
1261
-
1262
- /** @internal */
1263
- export const succeedSome = <A>(value: A): STM.STM<Option.Option<A>> => core.succeed(Option.some(value))
1264
-
1265
- /** @internal */
1266
- export const summarized = dual<
1267
- <A2, E2, R2, A3>(
1268
- summary: STM.STM<A2, E2, R2>,
1269
- f: (before: A2, after: A2) => A3
1270
- ) => <A, E, R>(
1271
- self: STM.STM<A, E, R>
1272
- ) => STM.STM<[A3, A], E2 | E, R2 | R>,
1273
- <A, E, R, A2, E2, R2, A3>(
1274
- self: STM.STM<A, E, R>,
1275
- summary: STM.STM<A2, E2, R2>,
1276
- f: (before: A2, after: A2) => A3
1277
- ) => STM.STM<[A3, A], E2 | E, R2 | R>
1278
- >(3, (self, summary, f) =>
1279
- core.flatMap(summary, (start) =>
1280
- core.flatMap(self, (value) =>
1281
- core.map(
1282
- summary,
1283
- (end) => [f(start, end), value]
1284
- ))))
1285
-
1286
- /** @internal */
1287
- export const suspend = <A, E, R>(evaluate: LazyArg<STM.STM<A, E, R>>): STM.STM<A, E, R> => flatten(core.sync(evaluate))
1288
-
1289
- /** @internal */
1290
- export const tap: {
1291
- <A, X, E2, R2>(f: (a: A) => STM.STM<X, E2, R2>): <E, R>(self: STM.STM<A, E, R>) => STM.STM<A, E2 | E, R2 | R>
1292
- <A, E, R, X, E2, R2>(self: STM.STM<A, E, R>, f: (a: A) => STM.STM<X, E2, R2>): STM.STM<A, E | E2, R | R2>
1293
- } = dual(
1294
- 2,
1295
- <A, E, R, X, E2, R2>(self: STM.STM<A, E, R>, f: (a: A) => STM.STM<X, E2, R2>): STM.STM<A, E | E2, R | R2> =>
1296
- core.flatMap(self, (a) => as(f(a), a))
1297
- )
1298
-
1299
- /** @internal */
1300
- export const tapBoth = dual<
1301
- <XE extends E, A2, E2, R2, XA extends A, A3, E3, R3, A, E>(
1302
- options: {
1303
- readonly onFailure: (error: XE) => STM.STM<A2, E2, R2>
1304
- readonly onSuccess: (value: XA) => STM.STM<A3, E3, R3>
1305
- }
1306
- ) => <R>(
1307
- self: STM.STM<A, E, R>
1308
- ) => STM.STM<A, E | E2 | E3, R2 | R3 | R>,
1309
- <A, E, R, XE extends E, A2, E2, R2, XA extends A, A3, E3, R3>(
1310
- self: STM.STM<A, E, R>,
1311
- options: {
1312
- readonly onFailure: (error: XE) => STM.STM<A2, E2, R2>
1313
- readonly onSuccess: (value: XA) => STM.STM<A3, E3, R3>
1314
- }
1315
- ) => STM.STM<A, E | E2 | E3, R2 | R3 | R>
1316
- >(2, (self, { onFailure, onSuccess }) =>
1317
- core.matchSTM(self, {
1318
- onFailure: (e) => pipe(onFailure(e as any), core.zipRight(core.fail(e))),
1319
- onSuccess: (a) => pipe(onSuccess(a as any), as(a))
1320
- }))
1321
-
1322
- /** @internal */
1323
- export const tapError: {
1324
- <E, X, E2, R2>(
1325
- f: (error: Types.NoInfer<E>) => STM.STM<X, E2, R2>
1326
- ): <A, R>(self: STM.STM<A, E, R>) => STM.STM<A, E | E2, R2 | R>
1327
- <A, E, R, X, E2, R2>(self: STM.STM<A, E, R>, f: (error: E) => STM.STM<X, E2, R2>): STM.STM<A, E | E2, R | R2>
1328
- } = dual(
1329
- 2,
1330
- <A, E, R, X, E2, R2>(self: STM.STM<A, E, R>, f: (error: E) => STM.STM<X, E2, R2>): STM.STM<A, E | E2, R | R2> =>
1331
- core.matchSTM(self, {
1332
- onFailure: (e) => core.zipRight(f(e), core.fail(e)),
1333
- onSuccess: core.succeed
1334
- })
1335
- )
1336
-
1337
- /** @internal */
1338
- export const try_: {
1339
- <A, E>(options: {
1340
- readonly try: LazyArg<A>
1341
- readonly catch: (u: unknown) => E
1342
- }): STM.STM<A, E>
1343
- <A>(try_: LazyArg<A>): STM.STM<A, unknown>
1344
- } = <A, E>(
1345
- arg: LazyArg<A> | {
1346
- readonly try: LazyArg<A>
1347
- readonly catch: (u: unknown) => E
1348
- }
1349
- ) => {
1350
- const evaluate = typeof arg === "function" ? arg : arg.try
1351
- return suspend(() => {
1352
- try {
1353
- return core.succeed(evaluate())
1354
- } catch (error) {
1355
- return core.fail("catch" in arg ? arg.catch(error) : error)
1356
- }
1357
- })
1358
- }
1359
-
1360
- /** @internal */
1361
- const void_: STM.STM<void> = core.succeed(void 0)
1362
- export {
1363
- /** @internal */
1364
- void_ as void
1365
- }
1366
-
1367
- /** @internal */
1368
- export const unless = dual<
1369
- (predicate: LazyArg<boolean>) => <A, E, R>(self: STM.STM<A, E, R>) => STM.STM<Option.Option<A>, E, R>,
1370
- <A, E, R>(self: STM.STM<A, E, R>, predicate: LazyArg<boolean>) => STM.STM<Option.Option<A>, E, R>
1371
- >(2, (self, predicate) =>
1372
- suspend(
1373
- () => predicate() ? succeedNone : asSome(self)
1374
- ))
1375
-
1376
- /** @internal */
1377
- export const unlessSTM = dual<
1378
- <E2, R2>(
1379
- predicate: STM.STM<boolean, E2, R2>
1380
- ) => <A, E, R>(
1381
- self: STM.STM<A, E, R>
1382
- ) => STM.STM<Option.Option<A>, E2 | E, R2 | R>,
1383
- <A, E, R, E2, R2>(
1384
- self: STM.STM<A, E, R>,
1385
- predicate: STM.STM<boolean, E2, R2>
1386
- ) => STM.STM<Option.Option<A>, E2 | E, R2 | R>
1387
- >(2, (self, predicate) =>
1388
- core.flatMap(
1389
- predicate,
1390
- (bool) => bool ? succeedNone : asSome(self)
1391
- ))
1392
-
1393
- /** @internal */
1394
- export const unsome = <A, E, R>(self: STM.STM<A, Option.Option<E>, R>): STM.STM<Option.Option<A>, E, R> =>
1395
- core.matchSTM(self, {
1396
- onFailure: Option.match({
1397
- onNone: () => core.succeed(Option.none()),
1398
- onSome: core.fail
1399
- }),
1400
- onSuccess: (a) => core.succeed(Option.some(a))
1401
- })
1402
-
1403
- /** @internal */
1404
- export const validateAll = dual<
1405
- <A, B, E, R>(
1406
- f: (a: A) => STM.STM<B, E, R>
1407
- ) => (
1408
- elements: Iterable<A>
1409
- ) => STM.STM<Array<B>, RA.NonEmptyArray<E>, R>,
1410
- <A, B, E, R>(
1411
- elements: Iterable<A>,
1412
- f: (a: A) => STM.STM<B, E, R>
1413
- ) => STM.STM<Array<B>, RA.NonEmptyArray<E>, R>
1414
- >(
1415
- 2,
1416
- (elements, f) =>
1417
- core.flatMap(partition(elements, f), ([errors, values]) =>
1418
- RA.isNonEmptyArray(errors) ?
1419
- core.fail(errors) :
1420
- core.succeed(values))
1421
- )
1422
-
1423
- /** @internal */
1424
- export const validateFirst = dual<
1425
- <A, B, E, R>(f: (a: A) => STM.STM<B, E, R>) => (elements: Iterable<A>) => STM.STM<B, Array<E>, R>,
1426
- <A, B, E, R>(elements: Iterable<A>, f: (a: A) => STM.STM<B, E, R>) => STM.STM<B, Array<E>, R>
1427
- >(2, (elements, f) => flip(forEach(elements, (a) => flip(f(a)))))
1428
-
1429
- /** @internal */
1430
- export const when = dual<
1431
- (predicate: LazyArg<boolean>) => <A, E, R>(self: STM.STM<A, E, R>) => STM.STM<Option.Option<A>, E, R>,
1432
- <A, E, R>(self: STM.STM<A, E, R>, predicate: LazyArg<boolean>) => STM.STM<Option.Option<A>, E, R>
1433
- >(2, (self, predicate) =>
1434
- suspend(
1435
- () => predicate() ? asSome(self) : succeedNone
1436
- ))
1437
-
1438
- /** @internal */
1439
- export const whenSTM = dual<
1440
- <E2, R2>(
1441
- predicate: STM.STM<boolean, E2, R2>
1442
- ) => <A, E, R>(
1443
- self: STM.STM<A, E, R>
1444
- ) => STM.STM<Option.Option<A>, E2 | E, R2 | R>,
1445
- <A, E, R, E2, R2>(
1446
- self: STM.STM<A, E, R>,
1447
- predicate: STM.STM<boolean, E2, R2>
1448
- ) => STM.STM<Option.Option<A>, E2 | E, R2 | R>
1449
- >(2, (self, predicate) =>
1450
- core.flatMap(
1451
- predicate,
1452
- (bool) => bool ? asSome(self) : succeedNone
1453
- ))