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