@clayroach/effect 3.19.14-source-capture.8 → 3.19.14-source-trace.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/SourceLocation/package.json +6 -0
- package/dist/cjs/Effect.js +2 -28
- package/dist/cjs/Effect.js.map +1 -1
- package/dist/cjs/FiberRef.js +12 -1
- package/dist/cjs/FiberRef.js.map +1 -1
- package/dist/cjs/Layer.js +2 -24
- package/dist/cjs/Layer.js.map +1 -1
- package/dist/cjs/RuntimeFlags.js +1 -29
- package/dist/cjs/RuntimeFlags.js.map +1 -1
- package/dist/cjs/SourceLocation.js +60 -0
- package/dist/cjs/SourceLocation.js.map +1 -0
- package/dist/cjs/Tracer.js +1 -15
- package/dist/cjs/Tracer.js.map +1 -1
- package/dist/cjs/Utils.js +1 -1
- package/dist/cjs/Utils.js.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/internal/clock.js +1 -1
- package/dist/cjs/internal/clock.js.map +1 -1
- package/dist/cjs/internal/core.js +17 -50
- package/dist/cjs/internal/core.js.map +1 -1
- package/dist/cjs/internal/effect/circular.js +18 -30
- package/dist/cjs/internal/effect/circular.js.map +1 -1
- package/dist/cjs/internal/fiberRuntime.js +16 -65
- package/dist/cjs/internal/fiberRuntime.js.map +1 -1
- package/dist/cjs/internal/layer/circular.js +1 -5
- package/dist/cjs/internal/layer/circular.js.map +1 -1
- package/dist/cjs/internal/layer.js +1 -3
- package/dist/cjs/internal/layer.js.map +1 -1
- package/dist/cjs/internal/logger.js +25 -2
- package/dist/cjs/internal/logger.js.map +1 -1
- package/dist/cjs/internal/runtimeFlags.js +2 -11
- package/dist/cjs/internal/runtimeFlags.js.map +1 -1
- package/dist/cjs/internal/tracer.js +1 -114
- package/dist/cjs/internal/tracer.js.map +1 -1
- package/dist/dts/Config.d.ts +2 -2
- package/dist/dts/Config.d.ts.map +1 -1
- package/dist/dts/Effect.d.ts +8 -29
- package/dist/dts/Effect.d.ts.map +1 -1
- package/dist/dts/FiberRef.d.ts +12 -0
- package/dist/dts/FiberRef.d.ts.map +1 -1
- package/dist/dts/Layer.d.ts +0 -22
- package/dist/dts/Layer.d.ts.map +1 -1
- package/dist/dts/RuntimeFlags.d.ts +0 -28
- package/dist/dts/RuntimeFlags.d.ts.map +1 -1
- package/dist/dts/SourceLocation.d.ts +88 -0
- package/dist/dts/SourceLocation.d.ts.map +1 -0
- package/dist/dts/Tracer.d.ts +0 -15
- package/dist/dts/Tracer.d.ts.map +1 -1
- package/dist/dts/index.d.ts +6 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/internal/core.d.ts.map +1 -1
- package/dist/dts/internal/layer.d.ts.map +1 -1
- package/dist/dts/internal/runtimeFlags.d.ts.map +1 -1
- package/dist/esm/Effect.js +0 -26
- package/dist/esm/Effect.js.map +1 -1
- package/dist/esm/FiberRef.js +11 -0
- package/dist/esm/FiberRef.js.map +1 -1
- package/dist/esm/Layer.js +0 -22
- package/dist/esm/Layer.js.map +1 -1
- package/dist/esm/RuntimeFlags.js +0 -28
- package/dist/esm/RuntimeFlags.js.map +1 -1
- package/dist/esm/SourceLocation.js +51 -0
- package/dist/esm/SourceLocation.js.map +1 -0
- package/dist/esm/Tracer.js +0 -14
- package/dist/esm/Tracer.js.map +1 -1
- package/dist/esm/Utils.js +1 -1
- package/dist/esm/Utils.js.map +1 -1
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internal/clock.js +1 -1
- package/dist/esm/internal/clock.js.map +1 -1
- package/dist/esm/internal/core.js +12 -45
- package/dist/esm/internal/core.js.map +1 -1
- package/dist/esm/internal/effect/circular.js +18 -30
- package/dist/esm/internal/effect/circular.js.map +1 -1
- package/dist/esm/internal/fiberRuntime.js +13 -60
- package/dist/esm/internal/fiberRuntime.js.map +1 -1
- package/dist/esm/internal/layer/circular.js +0 -4
- package/dist/esm/internal/layer/circular.js.map +1 -1
- package/dist/esm/internal/layer.js +0 -2
- package/dist/esm/internal/layer.js.map +1 -1
- package/dist/esm/internal/logger.js +25 -2
- package/dist/esm/internal/logger.js.map +1 -1
- package/dist/esm/internal/runtimeFlags.js +1 -9
- package/dist/esm/internal/runtimeFlags.js.map +1 -1
- package/dist/esm/internal/tracer.js +0 -111
- package/dist/esm/internal/tracer.js.map +1 -1
- package/package.json +12 -1
- package/src/Arbitrary.ts +1101 -0
- package/src/Array.ts +3589 -0
- package/src/BigDecimal.ts +1349 -0
- package/src/BigInt.ts +643 -0
- package/src/Boolean.ts +287 -0
- package/src/Brand.ts +360 -0
- package/src/Cache.ts +281 -0
- package/src/Cause.ts +1555 -0
- package/src/Channel.ts +2355 -0
- package/src/ChildExecutorDecision.ts +146 -0
- package/src/Chunk.ts +1495 -0
- package/src/Clock.ts +111 -0
- package/src/Config.ts +542 -0
- package/src/ConfigError.ts +270 -0
- package/src/ConfigProvider.ts +333 -0
- package/src/ConfigProviderPathPatch.ts +100 -0
- package/src/Console.ts +226 -0
- package/src/Context.ts +585 -0
- package/src/Cron.ts +706 -0
- package/src/Data.ts +596 -0
- package/src/DateTime.ts +1686 -0
- package/src/DefaultServices.ts +34 -0
- package/src/Deferred.ts +301 -0
- package/src/Differ.ts +450 -0
- package/src/Duration.ts +1000 -0
- package/src/Effect.ts +14817 -0
- package/src/Effectable.ts +107 -0
- package/src/Either.ts +1040 -0
- package/src/Encoding.ts +195 -0
- package/src/Equal.ts +98 -0
- package/src/Equivalence.ts +235 -0
- package/src/ExecutionPlan.ts +308 -0
- package/src/ExecutionStrategy.ts +119 -0
- package/src/Exit.ts +467 -0
- package/src/FastCheck.ts +9 -0
- package/src/Fiber.ts +744 -0
- package/src/FiberHandle.ts +540 -0
- package/src/FiberId.ts +195 -0
- package/src/FiberMap.ts +656 -0
- package/src/FiberRef.ts +444 -0
- package/src/FiberRefs.ts +204 -0
- package/src/FiberRefsPatch.ts +105 -0
- package/src/FiberSet.ts +491 -0
- package/src/FiberStatus.ts +108 -0
- package/src/Function.ts +1222 -0
- package/src/GlobalValue.ts +53 -0
- package/src/Graph.ts +3732 -0
- package/src/GroupBy.ts +103 -0
- package/src/HKT.ts +45 -0
- package/src/Hash.ts +195 -0
- package/src/HashMap.ts +519 -0
- package/src/HashRing.ts +317 -0
- package/src/HashSet.ts +2346 -0
- package/src/Inspectable.ts +287 -0
- package/src/Iterable.ts +1119 -0
- package/src/JSONSchema.ts +1044 -0
- package/src/KeyedPool.ts +167 -0
- package/src/Layer.ts +1228 -0
- package/src/LayerMap.ts +436 -0
- package/src/List.ts +977 -0
- package/src/LogLevel.ts +285 -0
- package/src/LogSpan.ts +25 -0
- package/src/Logger.ts +702 -0
- package/src/Mailbox.ts +268 -0
- package/src/ManagedRuntime.ts +180 -0
- package/src/Match.ts +1477 -0
- package/src/MergeDecision.ts +95 -0
- package/src/MergeState.ts +172 -0
- package/src/MergeStrategy.ts +107 -0
- package/src/Metric.ts +780 -0
- package/src/MetricBoundaries.ts +69 -0
- package/src/MetricHook.ts +151 -0
- package/src/MetricKey.ts +224 -0
- package/src/MetricKeyType.ts +262 -0
- package/src/MetricLabel.ts +47 -0
- package/src/MetricPair.ts +71 -0
- package/src/MetricPolling.ts +148 -0
- package/src/MetricRegistry.ts +48 -0
- package/src/MetricState.ts +257 -0
- package/src/Micro.ts +4405 -0
- package/src/ModuleVersion.ts +18 -0
- package/src/MutableHashMap.ts +411 -0
- package/src/MutableHashSet.ts +706 -0
- package/src/MutableList.ts +297 -0
- package/src/MutableQueue.ts +227 -0
- package/src/MutableRef.ts +202 -0
- package/src/NonEmptyIterable.ts +32 -0
- package/src/Number.ts +1071 -0
- package/src/Option.ts +2170 -0
- package/src/Order.ts +373 -0
- package/src/Ordering.ts +111 -0
- package/src/ParseResult.ts +2031 -0
- package/src/PartitionedSemaphore.ts +200 -0
- package/src/Pipeable.ts +566 -0
- package/src/Pool.ts +204 -0
- package/src/Predicate.ts +1405 -0
- package/src/Pretty.ts +205 -0
- package/src/PrimaryKey.ts +23 -0
- package/src/PubSub.ts +182 -0
- package/src/Queue.ts +644 -0
- package/src/Random.ts +204 -0
- package/src/RateLimiter.ts +138 -0
- package/src/RcMap.ts +141 -0
- package/src/RcRef.ts +122 -0
- package/src/Readable.ts +93 -0
- package/src/Record.ts +1274 -0
- package/src/RedBlackTree.ts +421 -0
- package/src/Redacted.ts +144 -0
- package/src/Ref.ts +180 -0
- package/src/RegExp.ts +38 -0
- package/src/Reloadable.ts +127 -0
- package/src/Request.ts +347 -0
- package/src/RequestBlock.ts +118 -0
- package/src/RequestResolver.ts +366 -0
- package/src/Resource.ts +119 -0
- package/src/Runtime.ts +383 -0
- package/src/RuntimeFlags.ts +336 -0
- package/src/RuntimeFlagsPatch.ts +183 -0
- package/src/STM.ts +2045 -0
- package/src/Schedule.ts +2219 -0
- package/src/ScheduleDecision.ts +62 -0
- package/src/ScheduleInterval.ts +151 -0
- package/src/ScheduleIntervals.ts +122 -0
- package/src/Scheduler.ts +353 -0
- package/src/Schema.ts +10914 -0
- package/src/SchemaAST.ts +3043 -0
- package/src/Scope.ts +204 -0
- package/src/ScopedCache.ts +151 -0
- package/src/ScopedRef.ts +117 -0
- package/src/Secret.ts +88 -0
- package/src/SingleProducerAsyncInput.ts +67 -0
- package/src/Sink.ts +1461 -0
- package/src/SortedMap.ts +287 -0
- package/src/SortedSet.ts +390 -0
- package/src/SourceLocation.ts +108 -0
- package/src/Stream.ts +6468 -0
- package/src/StreamEmit.ts +136 -0
- package/src/StreamHaltStrategy.ts +123 -0
- package/src/Streamable.ts +45 -0
- package/src/String.ts +778 -0
- package/src/Struct.ts +243 -0
- package/src/Subscribable.ts +100 -0
- package/src/SubscriptionRef.ts +298 -0
- package/src/Supervisor.ts +240 -0
- package/src/Symbol.ts +29 -0
- package/src/SynchronizedRef.ts +270 -0
- package/src/TArray.ts +495 -0
- package/src/TDeferred.ts +100 -0
- package/src/TMap.ts +515 -0
- package/src/TPriorityQueue.ts +223 -0
- package/src/TPubSub.ts +200 -0
- package/src/TQueue.ts +432 -0
- package/src/TRandom.ts +129 -0
- package/src/TReentrantLock.ts +224 -0
- package/src/TRef.ts +178 -0
- package/src/TSemaphore.ts +129 -0
- package/src/TSet.ts +365 -0
- package/src/TSubscriptionRef.ts +192 -0
- package/src/Take.ts +258 -0
- package/src/TestAnnotation.ts +158 -0
- package/src/TestAnnotationMap.ts +119 -0
- package/src/TestAnnotations.ts +117 -0
- package/src/TestClock.ts +556 -0
- package/src/TestConfig.ts +47 -0
- package/src/TestContext.ts +36 -0
- package/src/TestLive.ts +53 -0
- package/src/TestServices.ts +390 -0
- package/src/TestSized.ts +55 -0
- package/src/Tracer.ts +182 -0
- package/src/Trie.ts +840 -0
- package/src/Tuple.ts +305 -0
- package/src/Types.ts +353 -0
- package/src/Unify.ts +113 -0
- package/src/UpstreamPullRequest.ts +117 -0
- package/src/UpstreamPullStrategy.ts +121 -0
- package/src/Utils.ts +809 -0
- package/src/index.ts +1568 -0
- package/src/internal/array.ts +8 -0
- package/src/internal/blockedRequests.ts +520 -0
- package/src/internal/cache.ts +733 -0
- package/src/internal/cause.ts +1050 -0
- package/src/internal/channel/channelExecutor.ts +1200 -0
- package/src/internal/channel/channelState.ts +134 -0
- package/src/internal/channel/childExecutorDecision.ts +96 -0
- package/src/internal/channel/continuation.ts +200 -0
- package/src/internal/channel/mergeDecision.ts +113 -0
- package/src/internal/channel/mergeState.ts +120 -0
- package/src/internal/channel/mergeStrategy.ts +72 -0
- package/src/internal/channel/singleProducerAsyncInput.ts +259 -0
- package/src/internal/channel/subexecutor.ts +229 -0
- package/src/internal/channel/upstreamPullRequest.ts +84 -0
- package/src/internal/channel/upstreamPullStrategy.ts +87 -0
- package/src/internal/channel.ts +2603 -0
- package/src/internal/clock.ts +95 -0
- package/src/internal/completedRequestMap.ts +9 -0
- package/src/internal/concurrency.ts +54 -0
- package/src/internal/config.ts +716 -0
- package/src/internal/configError.ts +304 -0
- package/src/internal/configProvider/pathPatch.ts +97 -0
- package/src/internal/configProvider.ts +799 -0
- package/src/internal/console.ts +153 -0
- package/src/internal/context.ts +337 -0
- package/src/internal/core-effect.ts +2293 -0
- package/src/internal/core-stream.ts +998 -0
- package/src/internal/core.ts +3189 -0
- package/src/internal/data.ts +36 -0
- package/src/internal/dataSource.ts +327 -0
- package/src/internal/dateTime.ts +1277 -0
- package/src/internal/defaultServices/console.ts +100 -0
- package/src/internal/defaultServices.ts +163 -0
- package/src/internal/deferred.ts +46 -0
- package/src/internal/differ/chunkPatch.ts +211 -0
- package/src/internal/differ/contextPatch.ts +232 -0
- package/src/internal/differ/hashMapPatch.ts +220 -0
- package/src/internal/differ/hashSetPatch.ts +176 -0
- package/src/internal/differ/orPatch.ts +311 -0
- package/src/internal/differ/readonlyArrayPatch.ts +210 -0
- package/src/internal/differ.ts +200 -0
- package/src/internal/doNotation.ts +80 -0
- package/src/internal/effect/circular.ts +895 -0
- package/src/internal/effectable.ts +131 -0
- package/src/internal/either.ts +110 -0
- package/src/internal/encoding/base64.ts +286 -0
- package/src/internal/encoding/base64Url.ts +29 -0
- package/src/internal/encoding/common.ts +51 -0
- package/src/internal/encoding/hex.ts +315 -0
- package/src/internal/errors.ts +7 -0
- package/src/internal/executionPlan.ts +114 -0
- package/src/internal/executionStrategy.ts +74 -0
- package/src/internal/fiber.ts +388 -0
- package/src/internal/fiberId.ts +267 -0
- package/src/internal/fiberMessage.ts +82 -0
- package/src/internal/fiberRefs/patch.ts +144 -0
- package/src/internal/fiberRefs.ts +297 -0
- package/src/internal/fiberRuntime.ts +3842 -0
- package/src/internal/fiberScope.ts +71 -0
- package/src/internal/fiberStatus.ts +119 -0
- package/src/internal/groupBy.ts +530 -0
- package/src/internal/hashMap/array.ts +49 -0
- package/src/internal/hashMap/bitwise.ts +32 -0
- package/src/internal/hashMap/config.ts +14 -0
- package/src/internal/hashMap/keySet.ts +8 -0
- package/src/internal/hashMap/node.ts +391 -0
- package/src/internal/hashMap.ts +586 -0
- package/src/internal/hashSet.ts +323 -0
- package/src/internal/keyedPool.ts +244 -0
- package/src/internal/layer/circular.ts +214 -0
- package/src/internal/layer.ts +1483 -0
- package/src/internal/logSpan.ts +20 -0
- package/src/internal/logger-circular.ts +24 -0
- package/src/internal/logger.ts +522 -0
- package/src/internal/mailbox.ts +561 -0
- package/src/internal/managedRuntime/circular.ts +6 -0
- package/src/internal/managedRuntime.ts +134 -0
- package/src/internal/matcher.ts +652 -0
- package/src/internal/metric/boundaries.ts +75 -0
- package/src/internal/metric/hook.ts +483 -0
- package/src/internal/metric/key.ts +167 -0
- package/src/internal/metric/keyType.ts +238 -0
- package/src/internal/metric/label.ts +41 -0
- package/src/internal/metric/pair.ts +48 -0
- package/src/internal/metric/polling.ts +149 -0
- package/src/internal/metric/registry.ts +187 -0
- package/src/internal/metric/state.ts +290 -0
- package/src/internal/metric.ts +577 -0
- package/src/internal/opCodes/cause.ts +35 -0
- package/src/internal/opCodes/channel.ts +83 -0
- package/src/internal/opCodes/channelChildExecutorDecision.ts +17 -0
- package/src/internal/opCodes/channelMergeDecision.ts +11 -0
- package/src/internal/opCodes/channelMergeState.ts +17 -0
- package/src/internal/opCodes/channelMergeStrategy.ts +11 -0
- package/src/internal/opCodes/channelState.ts +23 -0
- package/src/internal/opCodes/channelUpstreamPullRequest.ts +11 -0
- package/src/internal/opCodes/channelUpstreamPullStrategy.ts +11 -0
- package/src/internal/opCodes/config.ts +65 -0
- package/src/internal/opCodes/configError.ts +35 -0
- package/src/internal/opCodes/continuation.ts +11 -0
- package/src/internal/opCodes/deferred.ts +11 -0
- package/src/internal/opCodes/effect.ts +89 -0
- package/src/internal/opCodes/layer.ts +59 -0
- package/src/internal/opCodes/streamHaltStrategy.ts +23 -0
- package/src/internal/option.ts +80 -0
- package/src/internal/pool.ts +432 -0
- package/src/internal/pubsub.ts +1762 -0
- package/src/internal/query.ts +204 -0
- package/src/internal/queue.ts +766 -0
- package/src/internal/random.ts +161 -0
- package/src/internal/rateLimiter.ts +93 -0
- package/src/internal/rcMap.ts +285 -0
- package/src/internal/rcRef.ts +192 -0
- package/src/internal/redBlackTree/iterator.ts +200 -0
- package/src/internal/redBlackTree/node.ts +68 -0
- package/src/internal/redBlackTree.ts +1245 -0
- package/src/internal/redacted.ts +73 -0
- package/src/internal/ref.ts +171 -0
- package/src/internal/reloadable.ts +140 -0
- package/src/internal/request.ts +177 -0
- package/src/internal/resource.ts +76 -0
- package/src/internal/ringBuffer.ts +68 -0
- package/src/internal/runtime.ts +558 -0
- package/src/internal/runtimeFlags.ts +178 -0
- package/src/internal/runtimeFlagsPatch.ts +103 -0
- package/src/internal/schedule/decision.ts +47 -0
- package/src/internal/schedule/interval.ts +101 -0
- package/src/internal/schedule/intervals.ts +180 -0
- package/src/internal/schedule.ts +2199 -0
- package/src/internal/schema/errors.ts +191 -0
- package/src/internal/schema/schemaId.ts +106 -0
- package/src/internal/schema/util.ts +50 -0
- package/src/internal/scopedCache.ts +644 -0
- package/src/internal/scopedRef.ts +118 -0
- package/src/internal/secret.ts +89 -0
- package/src/internal/singleShotGen.ts +35 -0
- package/src/internal/sink.ts +2120 -0
- package/src/internal/stack.ts +10 -0
- package/src/internal/stm/core.ts +817 -0
- package/src/internal/stm/entry.ts +59 -0
- package/src/internal/stm/journal.ts +123 -0
- package/src/internal/stm/opCodes/stm.ts +71 -0
- package/src/internal/stm/opCodes/stmState.ts +17 -0
- package/src/internal/stm/opCodes/strategy.ts +17 -0
- package/src/internal/stm/opCodes/tExit.ts +29 -0
- package/src/internal/stm/opCodes/tryCommit.ts +11 -0
- package/src/internal/stm/stm.ts +1453 -0
- package/src/internal/stm/stmState.ts +136 -0
- package/src/internal/stm/tArray.ts +550 -0
- package/src/internal/stm/tDeferred.ts +81 -0
- package/src/internal/stm/tExit.ts +190 -0
- package/src/internal/stm/tMap.ts +824 -0
- package/src/internal/stm/tPriorityQueue.ts +267 -0
- package/src/internal/stm/tPubSub.ts +551 -0
- package/src/internal/stm/tQueue.ts +393 -0
- package/src/internal/stm/tRandom.ts +140 -0
- package/src/internal/stm/tReentrantLock.ts +352 -0
- package/src/internal/stm/tRef.ts +195 -0
- package/src/internal/stm/tSemaphore.ts +113 -0
- package/src/internal/stm/tSet.ts +259 -0
- package/src/internal/stm/tSubscriptionRef.ts +286 -0
- package/src/internal/stm/tryCommit.ts +34 -0
- package/src/internal/stm/txnId.ts +14 -0
- package/src/internal/stm/versioned.ts +4 -0
- package/src/internal/stream/debounceState.ts +57 -0
- package/src/internal/stream/emit.ts +123 -0
- package/src/internal/stream/haltStrategy.ts +94 -0
- package/src/internal/stream/handoff.ts +187 -0
- package/src/internal/stream/handoffSignal.ts +59 -0
- package/src/internal/stream/pull.ts +34 -0
- package/src/internal/stream/sinkEndReason.ts +30 -0
- package/src/internal/stream/zipAllState.ts +88 -0
- package/src/internal/stream/zipChunksState.ts +56 -0
- package/src/internal/stream.ts +8801 -0
- package/src/internal/string-utils.ts +107 -0
- package/src/internal/subscriptionRef.ts +138 -0
- package/src/internal/supervisor/patch.ts +190 -0
- package/src/internal/supervisor.ts +303 -0
- package/src/internal/synchronizedRef.ts +114 -0
- package/src/internal/take.ts +199 -0
- package/src/internal/testing/sleep.ts +27 -0
- package/src/internal/testing/suspendedWarningData.ts +85 -0
- package/src/internal/testing/warningData.ts +94 -0
- package/src/internal/tracer.ts +150 -0
- package/src/internal/trie.ts +722 -0
- package/src/internal/version.ts +7 -0
|
@@ -0,0 +1,1453 @@
|
|
|
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
|
+
))
|