import * as Boolean from "@effect/data/Boolean" import * as Chunk from "@effect/data/Chunk" import * as Context from "@effect/data/Context" import type * as Either from "@effect/data/Either" import type { LazyArg } from "@effect/data/Function" import { dual, identity, pipe } from "@effect/data/Function" import * as HashSet from "@effect/data/HashSet" import * as List from "@effect/data/List" import * as MRef from "@effect/data/MutableRef" import * as Option from "@effect/data/Option" import { pipeArguments } from "@effect/data/Pipeable" import * as Predicate from "@effect/data/Predicate" import * as RA from "@effect/data/ReadonlyArray" import type * as Cause from "@effect/io/Cause" import type * as Clock from "@effect/io/Clock" import type { Concurrency } from "@effect/io/Concurrency" import type { ConfigProvider } from "@effect/io/ConfigProvider" import * as Deferred from "@effect/io/Deferred" import type * as Effect from "@effect/io/Effect" import * as ExecutionStrategy from "@effect/io/ExecutionStrategy" import type * as Exit from "@effect/io/Exit" import type * as Fiber from "@effect/io/Fiber" import * as FiberId from "@effect/io/FiberId" import type * as FiberRef from "@effect/io/FiberRef" import * as FiberRefs from "@effect/io/FiberRefs" import * as FiberRefsPatch from "@effect/io/FiberRefsPatch" import * as FiberStatus from "@effect/io/FiberStatus" import * as _RequestBlock from "@effect/io/internal/blockedRequests" import * as internalCause from "@effect/io/internal/cause" import * as clock from "@effect/io/internal/clock" import { currentRequestMap } from "@effect/io/internal/completedRequestMap" import * as concurrency from "@effect/io/internal/concurrency" import { configProviderTag } from "@effect/io/internal/configProvider" import * as core from "@effect/io/internal/core" import * as defaultServices from "@effect/io/internal/defaultServices" import { consoleTag } from "@effect/io/internal/defaultServices/console" import * as internalEffect from "@effect/io/internal/effect" import * as executionStrategy from "@effect/io/internal/executionStrategy" import * as internalFiber from "@effect/io/internal/fiber" import * as FiberMessage from "@effect/io/internal/fiberMessage" import * as fiberRefs from "@effect/io/internal/fiberRefs" import * as fiberScope from "@effect/io/internal/fiberScope" import * as internalLogger from "@effect/io/internal/logger" import * as metric from "@effect/io/internal/metric" import * as metricBoundaries from "@effect/io/internal/metric/boundaries" import * as metricLabel from "@effect/io/internal/metric/label" import * as OpCodes from "@effect/io/internal/opCodes/effect" import { complete } from "@effect/io/internal/request" import * as _runtimeFlags from "@effect/io/internal/runtimeFlags" import { OpSupervision } from "@effect/io/internal/runtimeFlags" import * as supervisor from "@effect/io/internal/supervisor" import * as SupervisorPatch from "@effect/io/internal/supervisor/patch" import * as tracer from "@effect/io/internal/tracer" import type { Logger } from "@effect/io/Logger" import * as LogLevel from "@effect/io/LogLevel" import type * as MetricLabel from "@effect/io/MetricLabel" import * as Ref from "@effect/io/Ref" import type { Entry, Request } from "@effect/io/Request" import type * as RequestBlock from "@effect/io/RequestBlock" import type * as RuntimeFlags from "@effect/io/RuntimeFlags" import * as RuntimeFlagsPatch from "@effect/io/RuntimeFlagsPatch" import { currentScheduler, type Scheduler } from "@effect/io/Scheduler" import type * as Scope from "@effect/io/Scope" import type * as Supervisor from "@effect/io/Supervisor" import type * as Tracer from "@effect/io/Tracer" /** @internal */ export const fiberStarted = metric.counter("effect_fiber_started") /** @internal */ export const fiberActive = metric.counter("effect_fiber_active") /** @internal */ export const fiberSuccesses = metric.counter("effect_fiber_successes") /** @internal */ export const fiberFailures = metric.counter("effect_fiber_failures") /** @internal */ export const fiberLifetimes = metric.tagged( metric.histogram( "effect_fiber_lifetimes", metricBoundaries.exponential({ start: 1.0, factor: 1.3, count: 100 }) ), "time_unit", "milliseconds" ) /** @internal */ type EvaluationSignal = | EvaluationSignalContinue | EvaluationSignalDone | EvaluationSignalYieldNow /** @internal */ const EvaluationSignalContinue = "Continue" as const /** @internal */ type EvaluationSignalContinue = typeof EvaluationSignalContinue /** @internal */ const EvaluationSignalDone = "Done" as const /** @internal */ type EvaluationSignalDone = typeof EvaluationSignalDone /** @internal */ const EvaluationSignalYieldNow = "Yield" as const /** @internal */ type EvaluationSignalYieldNow = typeof EvaluationSignalYieldNow /** @internal */ export const runtimeFiberVariance = { _E: (_: never) => _, _A: (_: never) => _ } const absurd = (_: never): never => { throw new Error( `BUG: FiberRuntime - ${JSON.stringify(_)} - please report an issue at https://github.com/Effect-TS/io/issues` ) } const contOpSuccess = { [OpCodes.OP_ON_SUCCESS]: ( _: FiberRuntime, cont: core.OnSuccess, value: unknown ) => { return cont.i1(value) }, ["OnStep"]: ( _: FiberRuntime, cont: core.OnStep, value: unknown ) => { return cont.i1(core.exitSucceed(value)) }, [OpCodes.OP_ON_SUCCESS_AND_FAILURE]: ( _: FiberRuntime, cont: core.OnSuccessAndFailure, value: unknown ) => { return cont.i2(value) }, [OpCodes.OP_REVERT_FLAGS]: ( self: FiberRuntime, cont: core.RevertFlags, value: unknown ) => { self.patchRuntimeFlags(self._runtimeFlags, cont.patch) if (_runtimeFlags.interruptible(self._runtimeFlags) && self.isInterrupted()) { return core.exitFailCause(self.getInterruptedCause()) } else { return core.exitSucceed(value) } }, [OpCodes.OP_WHILE]: ( self: FiberRuntime, cont: core.While, value: unknown ) => { cont.i2(value) if (cont.i0()) { self.pushStack(cont) return cont.i1() } else { return core.unit } } } const drainQueueWhileRunningTable = { [FiberMessage.OP_INTERRUPT_SIGNAL]: ( self: FiberRuntime, runtimeFlags: RuntimeFlags.RuntimeFlags, cur: Effect.Effect, message: FiberMessage.FiberMessage & { _tag: FiberMessage.OP_INTERRUPT_SIGNAL } ) => { self.processNewInterruptSignal(message.cause) return _runtimeFlags.interruptible(runtimeFlags) ? core.exitFailCause(message.cause) : cur }, [FiberMessage.OP_RESUME]: ( _self: FiberRuntime, _runtimeFlags: RuntimeFlags.RuntimeFlags, _cur: Effect.Effect, _message: FiberMessage.FiberMessage ) => { throw new Error("It is illegal to have multiple concurrent run loops in a single fiber") }, [FiberMessage.OP_STATEFUL]: ( self: FiberRuntime, runtimeFlags: RuntimeFlags.RuntimeFlags, cur: Effect.Effect, message: FiberMessage.FiberMessage & { _tag: FiberMessage.OP_STATEFUL } ) => { message.onFiber(self, FiberStatus.running(runtimeFlags)) return cur }, [FiberMessage.OP_YIELD_NOW]: ( _self: FiberRuntime, _runtimeFlags: RuntimeFlags.RuntimeFlags, cur: Effect.Effect, _message: FiberMessage.FiberMessage & { _tag: FiberMessage.OP_YIELD_NOW } ) => { return core.flatMap(core.yieldNow(), () => cur) } } /** * Executes all requests, submitting requests to each data source in parallel. */ const runBlockedRequests = (self: RequestBlock.RequestBlock) => core.forEachSequentialDiscard( _RequestBlock.flatten(self), (requestsByRequestResolver) => forEachParUnboundedDiscard( _RequestBlock.sequentialCollectionToChunk(requestsByRequestResolver), ([dataSource, sequential]) => { const map = new Map, Entry>() for (const block of sequential) { for (const entry of block) { map.set(entry.request as Request, entry) } } return core.fiberRefLocally( invokeWithInterrupt(dataSource.runAll(sequential), sequential.flat()), currentRequestMap, map ) }, false ) ) /** @internal */ export class FiberRuntime implements Fiber.RuntimeFiber { readonly [internalFiber.FiberTypeId] = internalFiber.fiberVariance readonly [internalFiber.RuntimeFiberTypeId] = runtimeFiberVariance pipe() { return pipeArguments(this, arguments) } private _fiberRefs: FiberRefs.FiberRefs private _fiberId: FiberId.Runtime public _runtimeFlags: RuntimeFlags.RuntimeFlags private _queue = new Array() private _children: Set> | null = null private _observers = new Array<(exit: Exit.Exit) => void>() private _running = false private _stack: Array = [] private _asyncInterruptor: ((effect: Effect.Effect) => any) | null = null private _asyncBlockingOn: FiberId.FiberId | null = null private _exitValue: Exit.Exit | null = null private _steps: Array = [false] public _supervisor: Supervisor.Supervisor public _scheduler: Scheduler private _tracer: Tracer.Tracer public currentOpCount: number = 0 private isYielding = false constructor( fiberId: FiberId.Runtime, fiberRefs0: FiberRefs.FiberRefs, runtimeFlags0: RuntimeFlags.RuntimeFlags ) { this._runtimeFlags = runtimeFlags0 this._fiberId = fiberId this._fiberRefs = fiberRefs0 this._supervisor = this.getFiberRef(currentSupervisor) this._scheduler = this.getFiberRef(currentScheduler) if (_runtimeFlags.runtimeMetrics(runtimeFlags0)) { const tags = this.getFiberRef(core.currentMetricLabels) fiberStarted.unsafeUpdate(1, tags) fiberActive.unsafeUpdate(1, tags) } this._tracer = Context.get(this.getFiberRef(defaultServices.currentServices), tracer.tracerTag) } /** * The identity of the fiber. */ id(): FiberId.Runtime { return this._fiberId } /** * Begins execution of the effect associated with this fiber on in the * background. This can be called to "kick off" execution of a fiber after * it has been created. */ resume(effect: Effect.Effect): void { this.tell(FiberMessage.resume(effect)) } /** * The status of the fiber. */ status(): Effect.Effect { return this.ask((_, status) => status) } /** * Gets the fiber runtime flags. */ runtimeFlags(): Effect.Effect { return this.ask((state, status) => { if (FiberStatus.isDone(status)) { return state._runtimeFlags } return status.runtimeFlags }) } /** * Returns the current `FiberScope` for the fiber. */ scope(): fiberScope.FiberScope { return fiberScope.unsafeMake(this) } /** * Retrieves the immediate children of the fiber. */ children(): Effect.Effect>> { return this.ask((fiber) => Array.from(fiber.getChildren())) } /** * Gets the fiber's set of children. */ getChildren(): Set> { if (this._children === null) { this._children = new Set() } return this._children } /** * Retrieves the interrupted cause of the fiber, which will be `Cause.empty` * if the fiber has not been interrupted. * * **NOTE**: This method is safe to invoke on any fiber, but if not invoked * on this fiber, then values derived from the fiber's state (including the * log annotations and log level) may not be up-to-date. */ getInterruptedCause() { return this.getFiberRef(core.currentInterruptedCause) } /** * Retrieves the whole set of fiber refs. */ fiberRefs(): Effect.Effect { return this.ask((fiber) => fiber.getFiberRefs()) } /** * Returns an effect that will contain information computed from the fiber * state and status while running on the fiber. * * This allows the outside world to interact safely with mutable fiber state * without locks or immutable data. */ ask( f: (runtime: FiberRuntime, status: FiberStatus.FiberStatus) => Z ): Effect.Effect { return core.suspend(() => { const deferred = core.deferredUnsafeMake(this._fiberId) this.tell( FiberMessage.stateful((fiber, status) => { core.deferredUnsafeDone(deferred, core.sync(() => f(fiber, status))) }) ) return core.deferredAwait(deferred) }) } /** * Adds a message to be processed by the fiber on the fiber. */ tell(message: FiberMessage.FiberMessage): void { this._queue.push(message) if (!this._running) { this._running = true this.drainQueueLaterOnExecutor() } } await(): Effect.Effect> { return core.async>((resume) => { const cb = (exit: Exit.Exit) => resume(core.succeed(exit)) this.tell( FiberMessage.stateful((fiber, _) => { if (fiber._exitValue !== null) { cb(this._exitValue!) } else { fiber.addObserver(cb) } }) ) return core.sync(() => this.tell( FiberMessage.stateful((fiber, _) => { fiber.removeObserver(cb) }) ) ) }, this.id()) } inheritAll(): Effect.Effect { return core.withFiberRuntime((parentFiber, parentStatus) => { const parentFiberId = parentFiber.id() const parentFiberRefs = parentFiber.getFiberRefs() const parentRuntimeFlags = parentStatus.runtimeFlags const childFiberRefs = this.getFiberRefs() const updatedFiberRefs = fiberRefs.joinAs(parentFiberRefs, parentFiberId, childFiberRefs) parentFiber.setFiberRefs(updatedFiberRefs) const updatedRuntimeFlags = parentFiber.getFiberRef(currentRuntimeFlags) const patch = pipe( _runtimeFlags.diff(parentRuntimeFlags, updatedRuntimeFlags), // Do not inherit WindDown or Interruption! RuntimeFlagsPatch.exclude(_runtimeFlags.Interruption), RuntimeFlagsPatch.exclude(_runtimeFlags.WindDown) ) return core.updateRuntimeFlags(patch) }) } /** * Tentatively observes the fiber, but returns immediately if it is not * already done. */ poll(): Effect.Effect>> { return core.sync(() => Option.fromNullable(this._exitValue)) } /** * Unsafely observes the fiber, but returns immediately if it is not * already done. */ unsafePoll(): Exit.Exit | null { return this._exitValue } /** * In the background, interrupts the fiber as if interrupted from the specified fiber. */ interruptAsFork(fiberId: FiberId.FiberId): Effect.Effect { return core.sync(() => this.tell(FiberMessage.interruptSignal(internalCause.interrupt(fiberId)))) } /** * Adds an observer to the list of observers. * * **NOTE**: This method must be invoked by the fiber itself. */ addObserver(observer: (exit: Exit.Exit) => void): void { if (this._exitValue !== null) { observer(this._exitValue!) } else { this._observers.push(observer) } } /** * Removes the specified observer from the list of observers that will be * notified when the fiber exits. * * **NOTE**: This method must be invoked by the fiber itself. */ removeObserver(observer: (exit: Exit.Exit) => void): void { this._observers = this._observers.filter((o) => o !== observer) } /** * Retrieves all fiber refs of the fiber. * * **NOTE**: This method is safe to invoke on any fiber, but if not invoked * on this fiber, then values derived from the fiber's state (including the * log annotations and log level) may not be up-to-date. */ getFiberRefs(): FiberRefs.FiberRefs { this.setFiberRef(currentRuntimeFlags, this._runtimeFlags) return this._fiberRefs } /** * Deletes the specified fiber ref. * * **NOTE**: This method must be invoked by the fiber itself. */ unsafeDeleteFiberRef(fiberRef: FiberRef.FiberRef): void { this._fiberRefs = fiberRefs.delete_(this._fiberRefs, fiberRef) } /** * Retrieves the state of the fiber ref, or else its initial value. * * **NOTE**: This method is safe to invoke on any fiber, but if not invoked * on this fiber, then values derived from the fiber's state (including the * log annotations and log level) may not be up-to-date. */ getFiberRef(fiberRef: FiberRef.FiberRef): X { if (this._fiberRefs.locals.has(fiberRef)) { return this._fiberRefs.locals.get(fiberRef)![0][1] as X } return fiberRef.initial } /** * Sets the fiber ref to the specified value. * * **NOTE**: This method must be invoked by the fiber itself. */ setFiberRef(fiberRef: FiberRef.FiberRef, value: X): void { this._fiberRefs = fiberRefs.updatedAs(this._fiberRefs, { fiberId: this._fiberId, fiberRef, value }) this.refreshRefCache() } refreshRefCache() { this._tracer = Context.get(this.getFiberRef(defaultServices.currentServices), tracer.tracerTag) this._supervisor = this.getFiberRef(currentSupervisor) this._scheduler = this.getFiberRef(currentScheduler) } /** * Wholesale replaces all fiber refs of this fiber. * * **NOTE**: This method must be invoked by the fiber itself. */ setFiberRefs(fiberRefs: FiberRefs.FiberRefs): void { this._fiberRefs = fiberRefs this.refreshRefCache() } /** * Adds a reference to the specified fiber inside the children set. * * **NOTE**: This method must be invoked by the fiber itself. */ addChild(child: FiberRuntime) { this.getChildren().add(child) } /** * Removes a reference to the specified fiber inside the children set. * * **NOTE**: This method must be invoked by the fiber itself. */ removeChild(child: FiberRuntime) { this.getChildren().delete(child) } /** * On the current thread, executes all messages in the fiber's inbox. This * method may return before all work is done, in the event the fiber executes * an asynchronous operation. * * **NOTE**: This method must be invoked by the fiber itself. */ drainQueueOnCurrentThread() { let recurse = true while (recurse) { let evaluationSignal: EvaluationSignal = EvaluationSignalContinue const prev = (globalThis as any)[internalFiber.currentFiberURI] ;(globalThis as any)[internalFiber.currentFiberURI] = this try { while (evaluationSignal === EvaluationSignalContinue) { evaluationSignal = this._queue.length === 0 ? EvaluationSignalDone : this.evaluateMessageWhileSuspended(this._queue.splice(0, 1)[0]!) } } finally { this._running = false ;(globalThis as any)[internalFiber.currentFiberURI] = prev } // Maybe someone added something to the queue between us checking, and us // giving up the drain. If so, we need to restart the draining, but only // if we beat everyone else to the restart: if (this._queue.length > 0 && !this._running) { this._running = true if (evaluationSignal === EvaluationSignalYieldNow) { this.drainQueueLaterOnExecutor() recurse = false } else { recurse = true } } else { recurse = false } } } /** * Schedules the execution of all messages in the fiber's inbox. * * This method will return immediately after the scheduling * operation is completed, but potentially before such messages have been * executed. * * **NOTE**: This method must be invoked by the fiber itself. */ drainQueueLaterOnExecutor() { this._scheduler.scheduleTask( this.run, this.getFiberRef(core.currentSchedulingPriority) ) } /** * Drains the fiber's message queue while the fiber is actively running, * returning the next effect to execute, which may be the input effect if no * additional effect needs to be executed. * * **NOTE**: This method must be invoked by the fiber itself. */ drainQueueWhileRunning( runtimeFlags: RuntimeFlags.RuntimeFlags, cur0: Effect.Effect ) { let cur = cur0 while (this._queue.length > 0) { const message = this._queue.splice(0, 1)[0] // @ts-expect-error cur = drainQueueWhileRunningTable[message._tag](this, runtimeFlags, cur, message) } return cur } /** * Determines if the fiber is interrupted. * * **NOTE**: This method is safe to invoke on any fiber, but if not invoked * on this fiber, then values derived from the fiber's state (including the * log annotations and log level) may not be up-to-date. */ isInterrupted(): boolean { return !internalCause.isEmpty(this.getFiberRef(core.currentInterruptedCause)) } /** * Adds an interruptor to the set of interruptors that are interrupting this * fiber. * * **NOTE**: This method must be invoked by the fiber itself. */ addInterruptedCause(cause: Cause.Cause) { const oldSC = this.getFiberRef(core.currentInterruptedCause) this.setFiberRef(core.currentInterruptedCause, internalCause.sequential(oldSC, cause)) } /** * Processes a new incoming interrupt signal. * * **NOTE**: This method must be invoked by the fiber itself. */ processNewInterruptSignal(cause: Cause.Cause): void { this.addInterruptedCause(cause) this.sendInterruptSignalToAllChildren() } /** * Interrupts all children of the current fiber, returning an effect that will * await the exit of the children. This method will return null if the fiber * has no children. * * **NOTE**: This method must be invoked by the fiber itself. */ sendInterruptSignalToAllChildren(): boolean { if (this._children === null || this._children.size === 0) { return false } let told = false for (const child of this._children) { child.tell(FiberMessage.interruptSignal(internalCause.interrupt(this.id()))) told = true } return told } /** * Interrupts all children of the current fiber, returning an effect that will * await the exit of the children. This method will return null if the fiber * has no children. * * **NOTE**: This method must be invoked by the fiber itself. */ interruptAllChildren() { if (this.sendInterruptSignalToAllChildren()) { const it = this._children!.values() this._children = null let isDone = false const body = () => { const next = it.next() if (!next.done) { return core.asUnit(next.value.await()) } else { return core.sync(() => { isDone = true }) } } return core.whileLoop({ while: () => !isDone, body, step: () => { // } }) } return null } reportExitValue(exit: Exit.Exit) { if (_runtimeFlags.runtimeMetrics(this._runtimeFlags)) { const tags = this.getFiberRef(core.currentMetricLabels) fiberActive.unsafeUpdate(-1, tags) switch (exit._tag) { case OpCodes.OP_SUCCESS: { fiberSuccesses.unsafeUpdate(1, tags) break } case OpCodes.OP_FAILURE: { fiberFailures.unsafeUpdate(1, tags) break } } } if (exit._tag === "Failure") { const level = this.getFiberRef(core.currentUnhandledErrorLogLevel) if (!internalCause.isInterruptedOnly(exit.cause) && level._tag === "Some") { this.log("Fiber terminated with a non handled error", exit.cause, level) } } } setExitValue(exit: Exit.Exit) { this._exitValue = exit if (_runtimeFlags.runtimeMetrics(this._runtimeFlags)) { const tags = this.getFiberRef(core.currentMetricLabels) const startTimeMillis = this.id().startTimeMillis const endTimeMillis = new Date().getTime() fiberLifetimes.unsafeUpdate(endTimeMillis - startTimeMillis, tags) } this.reportExitValue(exit) for (let i = this._observers.length - 1; i >= 0; i--) { this._observers[i](exit) } } getLoggers() { return this.getFiberRef(currentLoggers) } log( message: unknown, cause: Cause.Cause, overrideLogLevel: Option.Option ): void { const logLevel = Option.isSome(overrideLogLevel) ? overrideLogLevel.value : this.getFiberRef(core.currentLogLevel) const minimumLogLevel = this.getFiberRef(currentMinimumLogLevel) if (LogLevel.greaterThan(minimumLogLevel, logLevel)) { return } const spans = this.getFiberRef(core.currentLogSpan) const annotations = this.getFiberRef(core.currentLogAnnotations) const loggers = this.getLoggers() const contextMap = this.getFiberRefs() if (HashSet.size(loggers) > 0) { const clockService = Context.get(this.getFiberRef(defaultServices.currentServices), clock.clockTag) const date = new Date(clockService.unsafeCurrentTimeMillis()) for (const logger of loggers) { logger.log({ fiberId: this.id(), logLevel, message, cause, context: contextMap, spans, annotations, date }) } } } /** * Evaluates a single message on the current thread, while the fiber is * suspended. This method should only be called while evaluation of the * fiber's effect is suspended due to an asynchronous operation. * * **NOTE**: This method must be invoked by the fiber itself. */ evaluateMessageWhileSuspended(message: FiberMessage.FiberMessage): EvaluationSignal { switch (message._tag) { case FiberMessage.OP_YIELD_NOW: { return EvaluationSignalYieldNow } case FiberMessage.OP_INTERRUPT_SIGNAL: { this.processNewInterruptSignal(message.cause) if (this._asyncInterruptor !== null) { this._asyncInterruptor(core.exitFailCause(message.cause)) this._asyncInterruptor = null } return EvaluationSignalContinue } case FiberMessage.OP_RESUME: { this._asyncInterruptor = null this._asyncBlockingOn = null this.evaluateEffect(message.effect) return EvaluationSignalContinue } case FiberMessage.OP_STATEFUL: { message.onFiber( this, this._exitValue !== null ? FiberStatus.done : FiberStatus.suspended(this._runtimeFlags, this._asyncBlockingOn!) ) return EvaluationSignalContinue } default: { return absurd(message) } } } /** * Evaluates an effect until completion, potentially asynchronously. * * **NOTE**: This method must be invoked by the fiber itself. */ evaluateEffect(effect0: Effect.Effect) { this._supervisor.onResume(this) try { let effect: Effect.Effect | null = _runtimeFlags.interruptible(this._runtimeFlags) && this.isInterrupted() ? core.exitFailCause(this.getInterruptedCause()) : effect0 while (effect !== null) { try { const eff: Effect.Effect = effect const exit = this.runLoop(eff) this._runtimeFlags = pipe(this._runtimeFlags, _runtimeFlags.enable(_runtimeFlags.WindDown)) const interruption = this.interruptAllChildren() if (interruption !== null) { effect = core.flatMap(interruption, () => exit) } else { if (this._queue.length === 0) { // No more messages to process, so we will allow the fiber to end life: this.setExitValue(exit) } else { // There are messages, possibly added by the final op executed by // the fiber. To be safe, we should execute those now before we // allow the fiber to end life: this.tell(FiberMessage.resume(exit)) } effect = null } } catch (e) { if (core.isEffect(e)) { if ((e as core.Primitive)._tag === OpCodes.OP_YIELD) { if (_runtimeFlags.cooperativeYielding(this._runtimeFlags)) { this.tell(FiberMessage.yieldNow()) this.tell(FiberMessage.resume(core.exitUnit)) effect = null } else { effect = core.exitUnit } } else if ((e as core.Primitive)._tag === OpCodes.OP_ASYNC) { // Terminate this evaluation, async resumption will continue evaluation: effect = null } } else { throw e } } } } finally { this._supervisor.onSuspend(this) } } /** * Begins execution of the effect associated with this fiber on the current * thread. This can be called to "kick off" execution of a fiber after it has * been created, in hopes that the effect can be executed synchronously. * * This is not the normal way of starting a fiber, but it is useful when the * express goal of executing the fiber is to synchronously produce its exit. */ start(effect: Effect.Effect): void { if (!this._running) { this._running = true const prev = (globalThis as any)[internalFiber.currentFiberURI] ;(globalThis as any)[internalFiber.currentFiberURI] = this try { this.evaluateEffect(effect) } finally { this._running = false ;(globalThis as any)[internalFiber.currentFiberURI] = prev // Because we're special casing `start`, we have to be responsible // for spinning up the fiber if there were new messages added to // the queue between the completion of the effect and the transition // to the not running state. if (this._queue.length > 0) { this.drainQueueLaterOnExecutor() } } } else { this.tell(FiberMessage.resume(effect)) } } /** * Begins execution of the effect associated with this fiber on in the * background, and on the correct thread pool. This can be called to "kick * off" execution of a fiber after it has been created, in hopes that the * effect can be executed synchronously. */ startFork(effect: Effect.Effect): void { this.tell(FiberMessage.resume(effect)) } /** * Takes the current runtime flags, patches them to return the new runtime * flags, and then makes any changes necessary to fiber state based on the * specified patch. * * **NOTE**: This method must be invoked by the fiber itself. */ patchRuntimeFlags(oldRuntimeFlags: RuntimeFlags.RuntimeFlags, patch: RuntimeFlagsPatch.RuntimeFlagsPatch) { const newRuntimeFlags = _runtimeFlags.patch(oldRuntimeFlags, patch) ;(globalThis as any)[internalFiber.currentFiberURI] = this this._runtimeFlags = newRuntimeFlags return newRuntimeFlags } /** * Initiates an asynchronous operation, by building a callback that will * resume execution, and then feeding that callback to the registration * function, handling error cases and repeated resumptions appropriately. * * **NOTE**: This method must be invoked by the fiber itself. */ initiateAsync( runtimeFlags: RuntimeFlags.RuntimeFlags, asyncRegister: (resume: (effect: Effect.Effect) => void) => void ) { let alreadyCalled = false const callback = (effect: Effect.Effect) => { if (!alreadyCalled) { alreadyCalled = true this.tell(FiberMessage.resume(effect)) } } if (_runtimeFlags.interruptible(runtimeFlags)) { this._asyncInterruptor = callback } try { asyncRegister(callback) } catch (e) { callback(core.failCause(internalCause.die(e))) } } pushStack(cont: core.Continuation) { this._stack.push(cont) if (cont._tag === "OnStep") { this._steps.push(true) } if (cont._tag === "RevertFlags") { this._steps.push(false) } } popStack() { const item = this._stack.pop() if (item) { if (item._tag === "OnStep" || item._tag === "RevertFlags") { this._steps.pop() } return item } return } getNextSuccessCont() { let frame = this.popStack() while (frame) { if (frame._tag !== OpCodes.OP_ON_FAILURE) { return frame } frame = this.popStack() } } getNextFailCont() { let frame = this.popStack() while (frame) { if (frame._tag !== OpCodes.OP_ON_SUCCESS && frame._tag !== OpCodes.OP_WHILE) { return frame } frame = this.popStack() } } [OpCodes.OP_TAG](op: core.Primitive & { _tag: OpCodes.OP_SYNC }) { return core.map( core.fiberRefGet(core.currentContext), (context) => { try { return Context.unsafeGet(context, op as unknown as Context.Tag) } catch (e) { console.log(e) throw e } } ) } ["Left"](op: core.Primitive & { _tag: "Left" }) { return core.exitFail(op.left) } ["None"](_: core.Primitive & { _tag: "None" }) { return core.exitFail(internalCause.NoSuchElementException()) } ["Right"](op: core.Primitive & { _tag: "Right" }) { return core.exitSucceed(op.right) } ["Some"](op: core.Primitive & { _tag: "Some" }) { return core.exitSucceed(op.value) } [OpCodes.OP_SYNC](op: core.Primitive & { _tag: OpCodes.OP_SYNC }) { const value = op.i0() const cont = this.getNextSuccessCont() if (cont !== undefined) { if (!(cont._tag in contOpSuccess)) { // @ts-expect-error absurd(cont) } // @ts-expect-error return contOpSuccess[cont._tag](this, cont, value) } else { throw core.exitSucceed(value) } } [OpCodes.OP_SUCCESS](op: core.Primitive & { _tag: OpCodes.OP_SUCCESS }) { const oldCur = op const cont = this.getNextSuccessCont() if (cont !== undefined) { if (!(cont._tag in contOpSuccess)) { // @ts-expect-error absurd(cont) } // @ts-expect-error return contOpSuccess[cont._tag](this, cont, oldCur.i0) } else { throw oldCur } } [OpCodes.OP_FAILURE](op: core.Primitive & { _tag: OpCodes.OP_FAILURE }) { const cause = op.i0 const cont = this.getNextFailCont() if (cont !== undefined) { switch (cont._tag) { case OpCodes.OP_ON_FAILURE: case OpCodes.OP_ON_SUCCESS_AND_FAILURE: { if (!(_runtimeFlags.interruptible(this._runtimeFlags) && this.isInterrupted())) { return cont.i1(cause) } else { return core.exitFailCause(internalCause.stripFailures(cause)) } } case "OnStep": { if (!(_runtimeFlags.interruptible(this._runtimeFlags) && this.isInterrupted())) { return cont.i1(core.exitFailCause(cause)) } else { return core.exitFailCause(internalCause.stripFailures(cause)) } } case OpCodes.OP_REVERT_FLAGS: { this.patchRuntimeFlags(this._runtimeFlags, cont.patch) if (_runtimeFlags.interruptible(this._runtimeFlags) && this.isInterrupted()) { return core.exitFailCause(internalCause.sequential(cause, this.getInterruptedCause())) } else { return core.exitFailCause(cause) } } default: { absurd(cont) } } } else { throw core.exitFailCause(cause) } } [OpCodes.OP_WITH_RUNTIME](op: core.Primitive & { _tag: OpCodes.OP_WITH_RUNTIME }) { return op.i0( this as FiberRuntime, FiberStatus.running(this._runtimeFlags) as FiberStatus.Running ) } ["Blocked"](op: core.Primitive & { _tag: "Blocked" }) { if (this._steps[this._steps.length - 1]) { const nextOp = this.popStack() if (nextOp) { switch (nextOp._tag) { case "OnStep": { return nextOp.i1(op) } case "OnSuccess": { return core.blocked(op.i0, core.flatMap(op.i1, nextOp.i1)) } case "OnSuccessAndFailure": { return core.blocked( op.i0, core.matchCauseEffect(op.i1, { onFailure: nextOp.i1, onSuccess: nextOp.i2 }) ) } case "OnFailure": { return core.blocked(op.i0, core.catchAllCause(op.i1, nextOp.i1)) } case "While": { return core.blocked( op.i0, core.flatMap(op.i1, (a) => { nextOp.i2(a) if (nextOp.i0()) { return core.whileLoop({ while: nextOp.i0, body: nextOp.i1, step: nextOp.i2 }) } return core.unit }) ) } case "RevertFlags": { this.pushStack(nextOp) break } } } } return core.uninterruptibleMask((restore) => core.flatMap( fork(core.runRequestBlock(op.i0)), () => restore(op.i1) ) ) } ["RunBlocked"](op: core.Primitive & { _tag: "RunBlocked" }) { return runBlockedRequests(op.i0) } [OpCodes.OP_UPDATE_RUNTIME_FLAGS](op: core.Primitive & { _tag: OpCodes.OP_UPDATE_RUNTIME_FLAGS }) { const updateFlags = op.i0 const oldRuntimeFlags = this._runtimeFlags const newRuntimeFlags = _runtimeFlags.patch(oldRuntimeFlags, updateFlags) // One more chance to short circuit: if we're immediately going // to interrupt. Interruption will cause immediate reversion of // the flag, so as long as we "peek ahead", there's no need to // set them to begin with. if (_runtimeFlags.interruptible(newRuntimeFlags) && this.isInterrupted()) { return core.exitFailCause(this.getInterruptedCause()) } else { // Impossible to short circuit, so record the changes this.patchRuntimeFlags(this._runtimeFlags, updateFlags) if (op.i1) { // Since we updated the flags, we need to revert them const revertFlags = _runtimeFlags.diff(newRuntimeFlags, oldRuntimeFlags) this.pushStack(new core.RevertFlags(revertFlags, op)) return op.i1(oldRuntimeFlags) } else { return core.exitUnit } } } [OpCodes.OP_ON_SUCCESS](op: core.Primitive & { _tag: OpCodes.OP_ON_SUCCESS }) { this.pushStack(op) return op.i0 } ["OnStep"](op: core.Primitive & { _tag: "OnStep" }) { this.pushStack(op) return op.i0 } [OpCodes.OP_ON_FAILURE](op: core.Primitive & { _tag: OpCodes.OP_ON_FAILURE }) { this.pushStack(op) return op.i0 } [OpCodes.OP_ON_SUCCESS_AND_FAILURE](op: core.Primitive & { _tag: OpCodes.OP_ON_SUCCESS_AND_FAILURE }) { this.pushStack(op) return op.i0 } [OpCodes.OP_ASYNC](op: core.Primitive & { _tag: OpCodes.OP_ASYNC }) { this._asyncBlockingOn = op.i1 this.initiateAsync(this._runtimeFlags, op.i0) throw op } [OpCodes.OP_YIELD](op: core.Primitive & { op: OpCodes.OP_YIELD }) { this.isYielding = false throw op } [OpCodes.OP_WHILE](op: core.Primitive & { _tag: OpCodes.OP_WHILE }) { const check = op.i0 const body = op.i1 if (check()) { this.pushStack(op) return body() } else { return core.exitUnit } } [OpCodes.OP_COMMIT](op: core.Primitive & { _tag: OpCodes.OP_COMMIT }) { return op.commit() } /** * The main run-loop for evaluating effects. * * **NOTE**: This method must be invoked by the fiber itself. */ runLoop(effect0: Effect.Effect): Exit.Exit { let cur = effect0 this.currentOpCount = 0 // eslint-disable-next-line no-constant-condition while (true) { if ((this._runtimeFlags & OpSupervision) !== 0) { this._supervisor.onEffect(this, cur) } if (this._queue.length > 0) { cur = this.drainQueueWhileRunning(this._runtimeFlags, cur) } if (!this.isYielding) { const shouldYield = this._scheduler.shouldYield(this) if (shouldYield !== false) { this.isYielding = true this.currentOpCount = 0 const oldCur = cur cur = core.flatMap(core.yieldNow({ priority: shouldYield }), () => oldCur) } else { this.currentOpCount += 1 } } try { if (!((cur as core.Primitive)._tag in this)) { if (typeof cur === "function") { console.log((cur as any)()) } // @ts-expect-error absurd(cur) } // @ts-expect-error cur = this._tracer.context( // @ts-expect-error () => this[(cur as core.Primitive)._tag](cur as core.Primitive), this ) } catch (e) { if (core.isEffect(e)) { if ( (e as core.Primitive)._tag === OpCodes.OP_YIELD || (e as core.Primitive)._tag === OpCodes.OP_ASYNC ) { throw e } if ( (e as core.Primitive)._tag === OpCodes.OP_SUCCESS || (e as core.Primitive)._tag === OpCodes.OP_FAILURE ) { return e as Exit.Exit } } else { if (core.isEffectError(e)) { cur = core.exitFailCause(e.cause) } else if (internalCause.isInterruptedException(e)) { cur = core.exitFailCause( internalCause.sequential(internalCause.die(e), internalCause.interrupt(FiberId.none)) ) } else { cur = core.exitFailCause(internalCause.die(e)) } } } } } run = () => { this.drainQueueOnCurrentThread() } } // circular with Logger /** @internal */ export const currentMinimumLogLevel: FiberRef.FiberRef = core.fiberRefUnsafeMake( LogLevel.fromLiteral("Info") ) /** @internal */ export const getConsole = (refs: FiberRefs.FiberRefs) => { const defaultServicesValue = FiberRefs.getOrDefault(refs, defaultServices.currentServices) const cnsl = Context.get(defaultServicesValue, consoleTag) return cnsl.unsafe } /** @internal */ export const defaultLogger: Logger = internalLogger.makeLogger((options) => { const formatted = internalLogger.stringLogger.log(options) getConsole(options.context).log(formatted) }) /** @internal */ export const filterMinimumLogLevel: Logger = internalLogger.makeLogger((options) => { const formatted = internalLogger.stringLogger.log(options) getConsole(options.context).log(formatted) }) /** @internal */ export const logFmtLogger: Logger = internalLogger.makeLogger((options) => { const formatted = internalLogger.logfmtLogger.log(options) getConsole(options.context).log(formatted) }) /** @internal */ export const tracerLogger = internalLogger.makeLogger(({ annotations, cause, context, fiberId, logLevel, message }) => { const span = Option.flatMap(fiberRefs.get(context, core.currentTracerSpan), List.head) const clockService = Option.map( fiberRefs.get(context, defaultServices.currentServices), (_) => Context.get(_, clock.clockTag) ) if (span._tag === "None" || span.value._tag === "ExternalSpan" || clockService._tag === "None") { return } const attributes = Object.fromEntries(annotations) attributes["effect.fiberId"] = FiberId.threadName(fiberId) attributes["effect.logLevel"] = logLevel.label if (cause !== null && cause !== internalCause.empty) { attributes["effect.cause"] = internalCause.pretty(cause) } span.value.event( String(message), clockService.value.unsafeCurrentTimeNanos(), attributes ) }) /** @internal */ export const currentLoggers: FiberRef.FiberRef< HashSet.HashSet> > = core.fiberRefUnsafeMakeHashSet(HashSet.make(defaultLogger, tracerLogger)) // circular with Effect /* @internal */ export const acquireRelease: { ( release: (a: A, exit: Exit.Exit) => Effect.Effect ): (acquire: Effect.Effect) => Effect.Effect ( acquire: Effect.Effect, release: (a: A, exit: Exit.Exit) => Effect.Effect ): Effect.Effect } = dual< { ( release: (a: A, exit: Exit.Exit) => Effect.Effect ): (acquire: Effect.Effect) => Effect.Effect }, { ( acquire: Effect.Effect, release: (a: A, exit: Exit.Exit) => Effect.Effect ): Effect.Effect } >((args) => core.isEffect(args[0]), (acquire, release) => { return core.uninterruptible( core.tap(acquire, (a) => addFinalizer((exit) => release(a, exit))) ) }) /* @internal */ export const acquireReleaseInterruptible: { ( release: (exit: Exit.Exit) => Effect.Effect ): (acquire: Effect.Effect) => Effect.Effect ( acquire: Effect.Effect, release: (exit: Exit.Exit) => Effect.Effect ): Effect.Effect } = dual< { ( release: (exit: Exit.Exit) => Effect.Effect ): (acquire: Effect.Effect) => Effect.Effect }, { ( acquire: Effect.Effect, release: (exit: Exit.Exit) => Effect.Effect ): Effect.Effect } >((args) => core.isEffect(args[0]), (acquire, release) => { return ensuring( acquire, addFinalizer((exit) => release(exit)) ) }) /* @internal */ export const addFinalizer = ( finalizer: (exit: Exit.Exit) => Effect.Effect ): Effect.Effect => core.withFiberRuntime( (runtime) => { const acquireRefs = runtime.getFiberRefs() const acquireFlags = runtime._runtimeFlags return core.flatMap(scope, (scope) => core.scopeAddFinalizerExit(scope, (exit) => core.withFiberRuntime((runtimeFinalizer) => { const preRefs = runtimeFinalizer.getFiberRefs() const preFlags = runtimeFinalizer._runtimeFlags const patchRefs = FiberRefsPatch.diff(preRefs, acquireRefs) const patchFlags = _runtimeFlags.diff(preFlags, acquireFlags) const inverseRefs = FiberRefsPatch.diff(acquireRefs, preRefs) runtimeFinalizer.setFiberRefs( FiberRefsPatch.patch(patchRefs, runtimeFinalizer.id(), acquireRefs) ) return ensuring( core.withRuntimeFlags(finalizer(exit) as Effect.Effect, patchFlags), core.sync(() => { runtimeFinalizer.setFiberRefs( FiberRefsPatch.patch(inverseRefs, runtimeFinalizer.id(), runtimeFinalizer.getFiberRefs()) ) }) ) }))) } ) /* @internal */ export const daemonChildren = (self: Effect.Effect): Effect.Effect => { const forkScope = core.fiberRefLocally(core.currentForkScopeOverride, Option.some(fiberScope.globalScope)) return forkScope(self) } /** @internal */ const _existsParFound = Symbol("@effect/io/Effect/existsPar/found") /* @internal */ export const exists = dual< (f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" }) => (elements: Iterable) => Effect.Effect, (elements: Iterable, f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency: Concurrency readonly batching?: boolean | "inherit" }) => Effect.Effect >((args) => Predicate.isIterable(args[0]), (elements, f, options) => concurrency.matchSimple( options, () => core.suspend(() => existsLoop(elements[Symbol.iterator](), 0, f)), () => core.matchEffect( forEachOptions( elements, (a, i) => core.if_(f(a, i), { onTrue: core.fail(_existsParFound), onFalse: core.unit }), options ), { onFailure: (e) => e === _existsParFound ? core.succeed(true) : core.fail(e), onSuccess: () => core.succeed(false) } ) )) const existsLoop = ( iterator: Iterator, index: number, f: (a: A, i: number) => Effect.Effect ): Effect.Effect => { const next = iterator.next() if (next.done) { return core.succeed(false) } return pipe(core.flatMap( f(next.value, index), (b) => b ? core.succeed(b) : existsLoop(iterator, index + 1, f) )) } /* @internal */ export const filter = dual< ( f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly negate?: boolean } ) => (elements: Iterable) => Effect.Effect>, (elements: Iterable, f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly negate?: boolean }) => Effect.Effect> >( (args) => Predicate.isIterable(args[0]), (elements: Iterable, f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly negate?: boolean }) => { const predicate = options?.negate ? (a: A, i: number) => core.map(f(a, i), Boolean.not) : f return concurrency.matchSimple( options, () => core.suspend(() => RA.fromIterable(elements).reduceRight( (effect, a, i) => core.zipWith( effect, core.suspend(() => predicate(a, i)), (list, b) => b ? [a, ...list] : list ), core.sync(() => new Array()) as Effect.Effect> ) ), () => core.map( forEachOptions( elements, (a, i) => core.map(predicate(a, i), (b) => (b ? Option.some(a) : Option.none())), options ), RA.compact ) ) } ) // === all const allResolveInput = ( input: Iterable> | Record> ): readonly [Iterable>, Option.Option<(as: ReadonlyArray) => any>] => { if (Array.isArray(input) || Predicate.isIterable(input)) { return [input, Option.none()] } const keys = Object.keys(input) const size = keys.length return [ keys.map((k) => input[k]), Option.some((values: ReadonlyArray) => { const res = {} for (let i = 0; i < size; i++) { ;(res as any)[keys[i]] = values[i] } return res }) ] } const allValidate = ( effects: Iterable>, reconcile: Option.Option<(as: ReadonlyArray) => any>, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard?: boolean readonly mode?: "default" | "validate" | "either" } ) => { const eitherEffects: Array>> = [] for (const effect of effects) { eitherEffects.push(core.either(effect)) } return core.flatMap( forEachOptions(eitherEffects, identity, { concurrency: options?.concurrency, batching: options?.batching }), (eithers) => { const none = Option.none() const size = eithers.length const errors: Array = new Array(size) const successes: Array = new Array(size) let errored = false for (let i = 0; i < size; i++) { const either = eithers[i] as Either.Either if (either._tag === "Left") { errors[i] = Option.some(either.left) errored = true } else { successes[i] = either.right errors[i] = none } } if (errored) { return reconcile._tag === "Some" ? core.fail(reconcile.value(errors)) : core.fail(errors) } else if (options?.discard) { return core.unit } return reconcile._tag === "Some" ? core.succeed(reconcile.value(successes)) : core.succeed(successes) } ) } const allEither = ( effects: Iterable>, reconcile: Option.Option<(as: ReadonlyArray) => any>, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard?: boolean readonly mode?: "default" | "validate" | "either" } ) => { const eitherEffects: Array>> = [] for (const effect of effects) { eitherEffects.push(core.either(effect)) } if (options?.discard) { return forEachOptions(eitherEffects, identity, { concurrency: options?.concurrency, batching: options?.batching, discard: true }) } return core.map( forEachOptions(eitherEffects, identity, { concurrency: options?.concurrency, batching: options?.batching }), (eithers) => reconcile._tag === "Some" ? reconcile.value(eithers) : eithers ) } /* @internal */ export const all = < const Arg extends Iterable> | Record>, O extends { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard?: boolean readonly mode?: "default" | "validate" | "either" } >( arg: Arg, options?: O ): Effect.All.Return => { const [effects, reconcile] = allResolveInput(arg) if (options?.mode === "validate") { return allValidate(effects, reconcile, options) as any } else if (options?.mode === "either") { return allEither(effects, reconcile, options) as any } return reconcile._tag === "Some" ? core.map( forEachOptions(effects, identity, options as any), reconcile.value ) as any : forEachOptions(effects, identity, options as any) as any } /* @internal */ export const allWith = < O extends { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard?: boolean readonly mode?: "default" | "validate" | "either" } >(options?: O) => > | Record>>( arg: Arg ): Effect.All.Return => all(arg, options) /* @internal */ export const allSuccesses = ( elements: Iterable>, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" } ): Effect.Effect> => core.map( all(RA.fromIterable(elements).map(core.exit), options), RA.filterMap((exit) => core.exitIsSuccess(exit) ? Option.some(exit.i0) : Option.none()) ) /* @internal */ export const replicate = dual< (n: number) => (self: Effect.Effect) => Array>, (self: Effect.Effect, n: number) => Array> >(2, (self, n) => Array.from({ length: n }, () => self)) /* @internal */ export const replicateEffect = dual< { (n: number, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard?: false }): (self: Effect.Effect) => Effect.Effect> (n: number, options: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard: true }): (self: Effect.Effect) => Effect.Effect }, { (self: Effect.Effect, n: number, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard?: false }): Effect.Effect> (self: Effect.Effect, n: number, options: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard: true }): Effect.Effect } >( (args) => core.isEffect(args[0]), (self, n, options) => all( replicate(self, n), options as { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard?: boolean } ) ) // @ts-expect-error export const forEachOptions = dual< { (f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard?: false }): (self: Iterable) => Effect.Effect> (f: (a: A, i: number) => Effect.Effect, options: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard: true }): (self: Iterable) => Effect.Effect }, { (self: Iterable, f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard?: false }): Effect.Effect> (self: Iterable, f: (a: A, i: number) => Effect.Effect, options: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard: true }): Effect.Effect } >((args) => Predicate.isIterable(args[0]), (self, f, options) => core.withFiberRuntime((r) => { const requestBatchingEnabled = options?.batching === true || (options?.batching === "inherit" && r.getFiberRef(core.currentRequestBatching)) if (options?.discard) { return concurrency.match( options, () => finalizersMask(ExecutionStrategy.sequential)((restore) => requestBatchingEnabled ? forEachBatchedDiscard(self, (a, i) => restore(f(a, i))) : core.forEachSequentialDiscard(self, (a, i) => restore(f(a, i))) ), () => finalizersMask(ExecutionStrategy.parallel)((restore) => forEachParUnboundedDiscard(self, (a, i) => restore(f(a, i)), requestBatchingEnabled) ), (n) => finalizersMask(ExecutionStrategy.parallelN(n))((restore) => forEachParNDiscard(self, n, (a, i) => restore(f(a, i)), requestBatchingEnabled) ) ) } return concurrency.match( options, () => finalizersMask(ExecutionStrategy.sequential)((restore) => requestBatchingEnabled ? forEachParN(self, 1, (a, i) => restore(f(a, i)), true) : core.forEachSequential(self, (a, i) => restore(f(a, i))) ), () => finalizersMask(ExecutionStrategy.parallel)((restore) => forEachParUnbounded(self, (a, i) => restore(f(a, i)), requestBatchingEnabled) ), (n) => finalizersMask(ExecutionStrategy.parallelN(n))((restore) => forEachParN(self, n, (a, i) => restore(f(a, i)), requestBatchingEnabled) ) ) })) /* @internal */ export const forEachParUnbounded = ( self: Iterable, f: (a: A, i: number) => Effect.Effect, batching: boolean ): Effect.Effect> => core.suspend(() => { const as = RA.fromIterable(self) const array = new Array(as.length) const fn = (a: A, i: number) => core.flatMap(f(a, i), (b) => core.sync(() => array[i] = b)) return core.zipRight(forEachParUnboundedDiscard(as, fn, batching), core.succeed(array)) }) const forEachBatchedDiscard = ( self: Iterable, f: (a: A, i: number) => Effect.Effect ): Effect.Effect => core.suspend(() => { const as = RA.fromIterable(self) const size = as.length if (size === 0) { return core.unit } else if (size === 1) { return core.asUnit(f(as[0], 0)) } const effects = as.map(f) const blocked = new Array>() const loop = (i: number): Effect.Effect => i === effects.length ? core.suspend(() => { if (blocked.length > 0) { const requests = blocked.map((b) => b.i0).reduce(_RequestBlock.par) return core.blocked( requests, forEachBatchedDiscard(blocked.map((b) => b.i1), identity) ) } return core.unit }) : core.flatMapStep(effects[i], (s) => { if (s._tag === "Blocked") { blocked.push(s) return loop(i + 1) } else if (s._tag === "Failure") { return core.suspend(() => { if (blocked.length > 0) { const requests = blocked.map((b) => b.i0).reduce(_RequestBlock.par) return core.blocked( requests, core.flatMap(forEachBatchedDiscard(blocked.map((b) => b.i1), identity), () => s) ) } return core.unit }) } else { return loop(i + 1) } }) return loop(0) }) /* @internal */ export const forEachParUnboundedDiscard = ( self: Iterable, f: (a: A, i: number) => Effect.Effect, batching: boolean ): Effect.Effect => core.suspend(() => { const as = RA.fromIterable(self) const size = as.length if (size === 0) { return core.unit } else if (size === 1) { return core.asUnit(f(as[0], 0)) } return core.uninterruptibleMask((restore) => { const deferred = core.deferredUnsafeMake>(FiberId.none) let ref = 0 const residual: Array> = [] const joinOrder: Array> = [] const process = core.transplant((graft) => core.forEachSequential(as, (a, i) => pipe( graft(pipe( core.suspend(() => restore((batching ? core.step : core.exit)(f(a, i)))), core.flatMap( (exit) => { switch (exit._tag) { case "Failure": { if (residual.length > 0) { const requests = residual.map((blocked) => blocked.i0).reduce(_RequestBlock.par) const _continue = forEachParUnboundedDiscard(residual, (blocked) => blocked.i1, batching) return core.blocked( requests, core.matchCauseEffect(_continue, { onFailure: (cause) => core.zipRight( core.deferredFail(deferred, void 0), core.failCause(internalCause.parallel(cause, exit.cause)) ), onSuccess: () => core.zipRight( core.deferredFail(deferred, void 0), core.failCause(exit.cause) ) }) ) } return core.zipRight( core.deferredFail(deferred, void 0), core.failCause(exit.cause) ) } default: { if (exit._tag === "Blocked") { residual.push(exit) } if (ref + 1 === size) { if (residual.length > 0) { const requests = residual.map((blocked) => blocked.i0).reduce(_RequestBlock.par) const _continue = forEachParUnboundedDiscard(residual, (blocked) => blocked.i1, batching) return core.deferredSucceed(deferred, core.blocked(requests, _continue)) } else { core.deferredUnsafeDone(deferred, core.exitSucceed(core.exitUnit)) } } else { ref = ref + 1 } return core.unit } } } ) )), forkDaemon, core.map((fiber) => { fiber.addObserver(() => { joinOrder.push(fiber) }) return fiber }) )) ) return core.flatMap(process, (fibers) => core.matchCauseEffect( restore(core.deferredAwait(deferred)), { onFailure: (cause) => core.flatMap( forEachParUnbounded(fibers, core.interruptFiber, batching), (exits) => { const exit = core.exitCollectAll(exits, { parallel: true }) if (exit._tag === "Some" && core.exitIsFailure(exit.value)) { return core.failCause( internalCause.parallel(internalCause.stripFailures(cause), exit.value.i0) ) } else { return core.failCause(internalCause.stripFailures(cause)) } } ), onSuccess: (rest) => core.flatMap(rest, () => core.forEachSequentialDiscard(joinOrder, (f) => f.inheritAll())) } )) }) }) /* @internal */ export const forEachParN = ( self: Iterable, n: number, f: (a: A, i: number) => Effect.Effect, batching: boolean ): Effect.Effect> => core.suspend(() => { const as = RA.fromIterable(self) const array = new Array(as.length) const fn = (a: A, i: number) => core.map(f(a, i), (b) => array[i] = b) return core.zipRight(forEachParNDiscard(as, n, fn, batching), core.succeed(array)) }) /* @internal */ export const forEachParNDiscard = ( self: Iterable, n: number, f: (a: A, i: number) => Effect.Effect, batching: boolean ): Effect.Effect => core.suspend(() => { let i = 0 const iterator = self[Symbol.iterator]() const residual: Array> = [] const worker: Effect.Effect = core.flatMap( core.sync(() => iterator.next()), (next) => next.done ? core.unit : core.flatMap((batching ? core.step : core.exit)(core.asUnit(f(next.value, i++))), (res) => { switch (res._tag) { case "Blocked": { residual.push(res) return worker } case "Failure": { return res } case "Success": return worker } }) ) const effects: Array> = [] for (let i = 0; i < n; i++) { effects.push(worker) } return core.flatMap(core.exit(forEachParUnboundedDiscard(effects, identity, batching)), (exit) => { if (residual.length === 0) { return exit } const requests = residual.map((blocked) => blocked.i0).reduce(_RequestBlock.par) const _continue = forEachParNDiscard(residual, n, (blocked) => blocked.i1, batching) if (exit._tag === "Failure") { return core.blocked( requests, core.matchCauseEffect(_continue, { onFailure: (cause) => core.exitFailCause(internalCause.parallel(exit.cause, cause)), onSuccess: () => exit }) ) } return core.blocked(requests, _continue) }) }) /* @internal */ export const fork = (self: Effect.Effect): Effect.Effect> => core.withFiberRuntime>((state, status) => core.succeed(unsafeFork(self, state, status.runtimeFlags)) ) /* @internal */ export const forkDaemon = (self: Effect.Effect): Effect.Effect> => forkWithScopeOverride(self, fiberScope.globalScope) /* @internal */ export const forkWithErrorHandler = dual< ( handler: (e: E) => Effect.Effect ) => (self: Effect.Effect) => Effect.Effect>, ( self: Effect.Effect, handler: (e: E) => Effect.Effect ) => Effect.Effect> >(2, (self, handler) => fork(core.onError(self, (cause) => { const either = internalCause.failureOrCause(cause) switch (either._tag) { case "Left": { return handler(either.left) } case "Right": { return core.failCause(either.right) } } }))) /** @internal */ export const unsafeFork = ( effect: Effect.Effect, parentFiber: FiberRuntime, parentRuntimeFlags: RuntimeFlags.RuntimeFlags, overrideScope: fiberScope.FiberScope | null = null ): FiberRuntime => { const childFiber = unsafeMakeChildFiber(effect, parentFiber, parentRuntimeFlags, overrideScope) childFiber.resume(effect) return childFiber } /** @internal */ export const unsafeMakeChildFiber = ( effect: Effect.Effect, parentFiber: FiberRuntime, parentRuntimeFlags: RuntimeFlags.RuntimeFlags, overrideScope: fiberScope.FiberScope | null = null ): FiberRuntime => { const childId = FiberId.unsafeMake() const parentFiberRefs = parentFiber.getFiberRefs() const childFiberRefs = fiberRefs.forkAs(parentFiberRefs, childId) const childFiber = new FiberRuntime(childId, childFiberRefs, parentRuntimeFlags) const childContext = fiberRefs.getOrDefault( childFiberRefs, core.currentContext as unknown as FiberRef.FiberRef> ) const supervisor = childFiber._supervisor supervisor.onStart( childContext, effect, Option.some(parentFiber), childFiber ) childFiber.addObserver((exit) => supervisor.onEnd(exit, childFiber)) const parentScope = overrideScope !== null ? overrideScope : pipe( parentFiber.getFiberRef(core.currentForkScopeOverride), Option.getOrElse(() => parentFiber.scope()) ) parentScope.add(parentRuntimeFlags, childFiber) return childFiber } /* @internal */ const forkWithScopeOverride = ( self: Effect.Effect, scopeOverride: fiberScope.FiberScope ): Effect.Effect> => core.withFiberRuntime>((parentFiber, parentStatus) => core.succeed(unsafeFork(self, parentFiber, parentStatus.runtimeFlags, scopeOverride)) ) /* @internal */ export const mergeAll = dual< (zero: Z, f: (z: Z, a: A, i: number) => Z, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" }) => (elements: Iterable>) => Effect.Effect, (elements: Iterable>, zero: Z, f: (z: Z, a: A, i: number) => Z, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" }) => Effect.Effect >( (args) => Predicate.isIterable(args[0]), (elements: Iterable>, zero: Z, f: (z: Z, a: A, i: number) => Z, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" }) => concurrency.matchSimple( options, () => RA.fromIterable(elements).reduce( (acc, a, i) => core.zipWith(acc, a, (acc, a) => f(acc, a, i)), core.succeed(zero) as Effect.Effect ), () => core.flatMap(Ref.make(zero), (acc) => core.flatMap( forEachOptions( elements, (effect, i) => core.flatMap(effect, (a) => Ref.update(acc, (b) => f(b, a, i))), options ), () => Ref.get(acc) )) ) ) /* @internal */ export const partition = dual< ( f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" } ) => (elements: Iterable) => Effect.Effect, Array]>, ( elements: Iterable, f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" } ) => Effect.Effect, Array]> >((args) => Predicate.isIterable(args[0]), (elements, f, options) => pipe( forEachOptions(elements, (a, i) => core.either(f(a, i)), options), core.map((chunk) => core.partitionMap(chunk, identity)) )) /* @internal */ export const validateAll = dual< { ( f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard?: false } ): (elements: Iterable) => Effect.Effect, Array> ( f: (a: A, i: number) => Effect.Effect, options: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard: true } ): (elements: Iterable) => Effect.Effect, void> }, { ( elements: Iterable, f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard?: false } ): Effect.Effect, Array> ( elements: Iterable, f: (a: A, i: number) => Effect.Effect, options: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard: true } ): Effect.Effect, void> } >( (args) => Predicate.isIterable(args[0]), (elements: Iterable, f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" readonly discard?: boolean }): Effect.Effect, any> => core.flatMap( partition(elements, f, { concurrency: options?.concurrency, batching: options?.batching }), ([es, bs]) => es.length === 0 ? options?.discard ? core.unit : core.succeed(bs) : core.fail(es) ) ) /* @internal */ export const raceAll = (all: Iterable>) => { const list = Chunk.fromIterable(all) if (!Chunk.isNonEmpty(list)) { return core.dieSync(() => internalCause.IllegalArgumentException(`Received an empty collection of effects`)) } const self = Chunk.headNonEmpty(list) const effects = Chunk.tailNonEmpty(list) const inheritAll = (res: readonly [A, Fiber.Fiber]) => pipe( internalFiber.inheritAll(res[1]), core.as(res[0]) ) return pipe( core.deferredMake]>(), core.flatMap((done) => pipe( Ref.make(effects.length), core.flatMap((fails) => core.uninterruptibleMask((restore) => pipe( fork(core.interruptible(self)), core.flatMap((head) => pipe( effects, core.forEachSequential((effect) => fork(core.interruptible(effect))), core.map(Chunk.unsafeFromArray), core.map((tail) => pipe(tail, Chunk.prepend(head)) as Chunk.Chunk>), core.tap((fibers) => pipe( fibers, RA.reduce(core.unit, (effect, fiber) => pipe( effect, core.zipRight( pipe( internalFiber._await(fiber), core.flatMap(raceAllArbiter(fibers, fiber, done, fails)), fork, core.asUnit ) ) )) ) ), core.flatMap((fibers) => pipe( restore(pipe(Deferred.await(done), core.flatMap(inheritAll))), core.onInterrupt(() => pipe( fibers, RA.reduce( core.unit, (effect, fiber) => pipe(effect, core.zipLeft(core.interruptFiber(fiber))) ) ) ) ) ) ) ) ) ) ) ) ) ) } /* @internal */ const raceAllArbiter = ( fibers: Iterable>, winner: Fiber.Fiber, deferred: Deferred.Deferred]>, fails: Ref.Ref ) => (exit: Exit.Exit): Effect.Effect => core.exitMatchEffect(exit, { onFailure: (cause) => pipe( Ref.modify(fails, (fails) => [ fails === 0 ? pipe(core.deferredFailCause(deferred, cause), core.asUnit) : core.unit, fails - 1 ] as const), core.flatten ), onSuccess: (value): Effect.Effect => pipe( core.deferredSucceed(deferred, [value, winner] as const), core.flatMap((set) => set ? pipe( Chunk.fromIterable(fibers), RA.reduce( core.unit, (effect, fiber) => fiber === winner ? effect : pipe(effect, core.zipLeft(core.interruptFiber(fiber))) ) ) : core.unit ) ) }) /* @internal */ export const reduceEffect = dual< ( zero: Effect.Effect, f: (acc: A, a: A, i: number) => A, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" } ) => (elements: Iterable>) => Effect.Effect, ( elements: Iterable>, zero: Effect.Effect, f: (acc: A, a: A, i: number) => A, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" } ) => Effect.Effect >((args) => Predicate.isIterable(args[0]), ( elements: Iterable>, zero: Effect.Effect, f: (acc: A, a: A, i: number) => A, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" } ) => concurrency.matchSimple( options, () => RA.fromIterable(elements).reduce((acc, a, i) => core.zipWith(acc, a, (acc, a) => f(acc, a, i)), zero), () => core.suspend(() => pipe( mergeAll( [zero, ...elements], Option.none() as Option.Option, (acc, elem, i) => { switch (acc._tag) { case "None": { return Option.some(elem) } case "Some": { return Option.some(f(acc.value, elem, i)) } } }, options ), core.map((option) => { switch (option._tag) { case "None": { throw new Error( "BUG: Effect.reduceEffect - please report an issue at https://github.com/Effect-TS/io/issues" ) } case "Some": { return option.value } } }) ) ) )) /* @internal */ export const parallelFinalizers = (self: Effect.Effect): Effect.Effect => core.contextWithEffect((context) => Option.match(Context.getOption(context, scopeTag), { onNone: () => self, onSome: (scope) => { switch (scope.strategy._tag) { case "Parallel": return self case "Sequential": case "ParallelN": return core.flatMap( core.scopeFork(scope, ExecutionStrategy.parallel), (inner) => scopeExtend(self, inner) ) } } }) ) /* @internal */ export const parallelNFinalizers = (parallelism: number) => (self: Effect.Effect): Effect.Effect => core.contextWithEffect((context) => Option.match(Context.getOption(context, scopeTag), { onNone: () => self, onSome: (scope) => { if (scope.strategy._tag === "ParallelN" && scope.strategy.parallelism === parallelism) { return self } return core.flatMap( core.scopeFork(scope, ExecutionStrategy.parallelN(parallelism)), (inner) => scopeExtend(self, inner) ) } }) ) /* @internal */ export const finalizersMask = (strategy: ExecutionStrategy.ExecutionStrategy) => ( self: (restore: (self: Effect.Effect) => Effect.Effect) => Effect.Effect ): Effect.Effect => core.contextWithEffect((context) => Option.match(Context.getOption(context, scopeTag), { onNone: () => self(identity), onSome: (scope) => { const patch = strategy._tag === "Parallel" ? parallelFinalizers : strategy._tag === "Sequential" ? sequentialFinalizers : parallelNFinalizers(strategy.parallelism) switch (scope.strategy._tag) { case "Parallel": return patch(self(parallelFinalizers)) case "Sequential": return patch(self(sequentialFinalizers)) case "ParallelN": return patch(self(parallelNFinalizers(scope.strategy.parallelism))) } } }) ) /* @internal */ export const scopeWith = ( f: (scope: Scope.Scope) => Effect.Effect ): Effect.Effect => core.flatMap(scopeTag, f) /* @internal */ export const scopedEffect = (effect: Effect.Effect): Effect.Effect, E, A> => core.flatMap(scopeMake(), (scope) => scopeUse(scope)(effect)) /* @internal */ export const sequentialFinalizers = (self: Effect.Effect): Effect.Effect => core.contextWithEffect((context) => Option.match(Context.getOption(context, scopeTag), { onNone: () => self, onSome: (scope) => { switch (scope.strategy._tag) { case "Sequential": return self case "Parallel": case "ParallelN": return core.flatMap( core.scopeFork(scope, ExecutionStrategy.sequential), (inner) => scopeExtend(self, inner) ) } } }) ) /* @internal */ export const tagMetricsScoped = (key: string, value: string): Effect.Effect => labelMetricsScoped([metricLabel.make(key, value)]) /* @internal */ export const labelMetricsScoped = ( labels: ReadonlyArray ): Effect.Effect => labelMetricsScopedSet(HashSet.fromIterable(labels)) /* @internal */ export const labelMetricsScopedSet = ( labels: HashSet.HashSet ): Effect.Effect => fiberRefLocallyScopedWith(core.currentMetricLabels, (set) => pipe(set, HashSet.union(labels))) /* @internal */ export const using = dual< ( use: (a: A) => Effect.Effect ) => (self: Effect.Effect) => Effect.Effect | R2, E | E2, A2>, ( self: Effect.Effect, use: (a: A) => Effect.Effect ) => Effect.Effect | R2, E | E2, A2> >(2, (self, use) => core.acquireUseRelease( scopeMake(), (scope) => core.flatMap(scopeExtend(self, scope), use), (scope, exit) => core.scopeClose(scope, exit) )) /** @internal */ export const validate = dual< ( that: Effect.Effect, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ) => (self: Effect.Effect) => Effect.Effect, ( self: Effect.Effect, that: Effect.Effect, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ) => Effect.Effect >( (args) => core.isEffect(args[1]), (self, that, options) => validateWith(self, that, (a, b) => [a, b], options) ) /** @internal */ export const validateWith = dual< ( that: Effect.Effect, f: (a: A, b: B) => C, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ) => (self: Effect.Effect) => Effect.Effect, ( self: Effect.Effect, that: Effect.Effect, f: (a: A, b: B) => C, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ) => Effect.Effect >((args) => core.isEffect(args[1]), (self, that, f, options) => core.flatten(zipWithOptions( core.exit(self), core.exit(that), (ea, eb) => core.exitZipWith(ea, eb, { onSuccess: f, onFailure: (ca, cb) => options?.concurrent ? internalCause.parallel(ca, cb) : internalCause.sequential(ca, cb) }), options ))) /* @internal */ export const validateAllPar = dual< ( f: (a: A) => Effect.Effect ) => (elements: Iterable) => Effect.Effect, Array>, ( elements: Iterable, f: (a: A) => Effect.Effect ) => Effect.Effect, Array> >(2, (elements, f) => core.flatMap( partition(elements, f), ([es, bs]) => es.length === 0 ? core.succeed(bs) : core.fail(es) )) /* @internal */ export const validateAllParDiscard = dual< ( f: (a: A) => Effect.Effect ) => (elements: Iterable) => Effect.Effect, void>, (elements: Iterable, f: (a: A) => Effect.Effect) => Effect.Effect, void> >(2, (elements, f) => core.flatMap( partition(elements, f), ([es, _]) => es.length === 0 ? core.unit : core.fail(es) )) /* @internal */ export const validateFirst = dual< (f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" }) => (elements: Iterable) => Effect.Effect, B>, (elements: Iterable, f: (a: A, i: number) => Effect.Effect, options?: { readonly concurrency?: Concurrency readonly batching?: boolean | "inherit" }) => Effect.Effect, B> >( (args) => Predicate.isIterable(args[0]), (elements, f, options) => core.flip(forEachOptions(elements, (a, i) => core.flip(f(a, i)), options)) ) /* @internal */ export const withClockScoped = (value: A) => fiberRefLocallyScopedWith(defaultServices.currentServices, Context.add(clock.clockTag, value)) /* @internal */ export const withConfigProviderScoped = (value: ConfigProvider) => fiberRefLocallyScopedWith(defaultServices.currentServices, Context.add(configProviderTag, value)) /* @internal */ export const withEarlyRelease = ( self: Effect.Effect ): Effect.Effect, A]> => scopeWith((parent) => core.flatMap(core.scopeFork(parent, executionStrategy.sequential), (child) => pipe( self, scopeExtend(child), core.map((value) => [ core.fiberIdWith((fiberId) => core.scopeClose(child, core.exitInterrupt(fiberId))), value ] as const ) )) ) /** @internal */ export const zipOptions = dual< ( that: Effect.Effect, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ) => ( self: Effect.Effect ) => Effect.Effect, ( self: Effect.Effect, that: Effect.Effect, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ) => Effect.Effect >((args) => core.isEffect(args[1]), ( self, that, options ) => zipWithOptions(self, that, (a, b) => [a, b], options)) /** @internal */ export const zipLeftOptions = dual< ( that: Effect.Effect, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ) => ( self: Effect.Effect ) => Effect.Effect, ( self: Effect.Effect, that: Effect.Effect, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ) => Effect.Effect >( (args) => core.isEffect(args[1]), (self, that, options) => zipWithOptions(self, that, (a, _) => a, options) ) /** @internal */ export const zipRightOptions = dual< ( that: Effect.Effect, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ) => (self: Effect.Effect) => Effect.Effect, ( self: Effect.Effect, that: Effect.Effect, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ) => Effect.Effect >((args) => core.isEffect(args[1]), (self, that, options) => zipWithOptions(self, that, (_, b) => b, options)) /** @internal */ export const zipWithOptions = dual< ( that: Effect.Effect, f: (a: A, b: A2) => B, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ) => ( self: Effect.Effect ) => Effect.Effect, ( self: Effect.Effect, that: Effect.Effect, f: (a: A, b: A2) => B, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ) => Effect.Effect >((args) => core.isEffect(args[1]), ( self: Effect.Effect, that: Effect.Effect, f: (a: A, b: A2) => B, options?: { readonly concurrent?: boolean readonly batching?: boolean | "inherit" } ): Effect.Effect => core.map( all([self, that], { concurrency: options?.concurrent ? 2 : 1, batching: options?.batching }), ([a, a2]) => f(a, a2) )) /* @internal */ export const withRuntimeFlagsScoped = ( update: RuntimeFlagsPatch.RuntimeFlagsPatch ): Effect.Effect => { if (update === RuntimeFlagsPatch.empty) { return core.unit } return pipe( core.runtimeFlags, core.flatMap((runtimeFlags) => { const updatedRuntimeFlags = _runtimeFlags.patch(runtimeFlags, update) const revertRuntimeFlags = _runtimeFlags.diff(updatedRuntimeFlags, runtimeFlags) return pipe( core.updateRuntimeFlags(update), core.zipRight(addFinalizer(() => core.updateRuntimeFlags(revertRuntimeFlags))), core.asUnit ) }), core.uninterruptible ) } // circular with ReleaseMap /* @internal */ export const releaseMapReleaseAll = ( strategy: ExecutionStrategy.ExecutionStrategy, exit: Exit.Exit ) => (self: core.ReleaseMap): Effect.Effect => core.suspend(() => { switch (self.state._tag) { case "Exited": { return core.unit } case "Running": { const finalizersMap = self.state.finalizers const update = self.state.update const finalizers = Array.from(finalizersMap.keys()).sort((a, b) => b - a).map((key) => finalizersMap.get(key)!) self.state = { _tag: "Exited", nextKey: self.state.nextKey, exit, update } return executionStrategy.isSequential(strategy) ? pipe( finalizers, core.forEachSequential((fin) => core.exit(update(fin)(exit))), core.flatMap((results) => pipe( core.exitCollectAll(results), Option.map(core.exitAsUnit), Option.getOrElse(() => core.exitUnit) ) ) ) : executionStrategy.isParallel(strategy) ? pipe( forEachParUnbounded(finalizers, (fin) => core.exit(update(fin)(exit)), false), core.flatMap((results) => pipe( core.exitCollectAll(results, { parallel: true }), Option.map(core.exitAsUnit), Option.getOrElse(() => core.exitUnit) ) ) ) : pipe( forEachParN(finalizers, strategy.parallelism, (fin) => core.exit(update(fin)(exit)), false), core.flatMap((results) => pipe( core.exitCollectAll(results, { parallel: true }), Option.map(core.exitAsUnit), Option.getOrElse(() => core.exitUnit) ) ) ) } } }) // circular with Scope /** @internal */ export const scopeTag = Context.Tag(core.ScopeTypeId) /* @internal */ export const scope: Effect.Effect = scopeTag /* @internal */ export const scopeMake = ( strategy: ExecutionStrategy.ExecutionStrategy = executionStrategy.sequential ): Effect.Effect => core.map(core.releaseMapMake, (rm): Scope.Scope.Closeable => ({ [core.ScopeTypeId]: core.ScopeTypeId, [core.CloseableScopeTypeId]: core.CloseableScopeTypeId, strategy, pipe() { return pipeArguments(this, arguments) }, fork: (strategy) => core.uninterruptible( pipe( scopeMake(strategy), core.flatMap((scope) => pipe( core.releaseMapAdd(rm, (exit) => core.scopeClose(scope, exit)), core.tap((fin) => core.scopeAddFinalizerExit(scope, fin)), core.as(scope) ) ) ) ), close: (exit) => core.asUnit(releaseMapReleaseAll(strategy, exit)(rm)), addFinalizer: (fin) => core.asUnit(core.releaseMapAdd(fin)(rm)) })) /* @internal */ export const scopeExtend = dual< (scope: Scope.Scope) => (effect: Effect.Effect) => Effect.Effect, E, A>, (effect: Effect.Effect, scope: Scope.Scope) => Effect.Effect, E, A> >( 2, (effect: Effect.Effect, scope: Scope.Scope) => core.mapInputContext, R, E, A>( effect, // @ts-expect-error Context.merge(Context.make(scopeTag, scope)) ) ) /* @internal */ export const scopeUse = dual< ( scope: Scope.Scope.Closeable ) => (effect: Effect.Effect) => Effect.Effect, E, A>, ( effect: Effect.Effect, scope: Scope.Scope.Closeable ) => Effect.Effect, E, A> >(2, (effect, scope) => pipe( effect, scopeExtend(scope), core.onExit((exit) => scope.close(exit)) )) // circular with Supervisor /** @internal */ export const fiberRefUnsafeMakeSupervisor = ( initial: Supervisor.Supervisor ): FiberRef.FiberRef> => core.fiberRefUnsafeMakePatch(initial, { differ: SupervisorPatch.differ, fork: SupervisorPatch.empty }) // circular with FiberRef /* @internal */ export const fiberRefLocallyScoped = dual< (value: A) => (self: FiberRef.FiberRef) => Effect.Effect, (self: FiberRef.FiberRef, value: A) => Effect.Effect >(2, (self, value) => core.asUnit( acquireRelease( core.flatMap( core.fiberRefGet(self), (oldValue) => core.as(core.fiberRefSet(self, value), oldValue) ), (oldValue) => core.fiberRefSet(self, oldValue) ) )) /* @internal */ export const fiberRefLocallyScopedWith = dual< (f: (a: A) => A) => (self: FiberRef.FiberRef) => Effect.Effect, (self: FiberRef.FiberRef, f: (a: A) => A) => Effect.Effect >(2, (self, f) => core.fiberRefGetWith(self, (a) => fiberRefLocallyScoped(self, f(a)))) /* @internal */ export const fiberRefMake = ( initial: A, options?: { readonly fork?: (a: A) => A readonly join?: (left: A, right: A) => A } ): Effect.Effect> => fiberRefMakeWith(() => core.fiberRefUnsafeMake(initial, options)) /* @internal */ export const fiberRefMakeWith = ( ref: LazyArg> ): Effect.Effect> => acquireRelease( core.tap(core.sync(ref), (ref) => core.fiberRefUpdate(ref, identity)), (fiberRef) => core.fiberRefDelete(fiberRef) ) /* @internal */ export const fiberRefMakeContext = ( initial: Context.Context ): Effect.Effect>> => fiberRefMakeWith(() => core.fiberRefUnsafeMakeContext(initial)) /* @internal */ export const fiberRefMakeRuntimeFlags = ( initial: RuntimeFlags.RuntimeFlags ): Effect.Effect> => fiberRefMakeWith(() => core.fiberRefUnsafeMakeRuntimeFlags(initial)) /** @internal */ export const currentRuntimeFlags: FiberRef.FiberRef = core.fiberRefUnsafeMakeRuntimeFlags( _runtimeFlags.none ) /** @internal */ export const currentSupervisor: FiberRef.FiberRef> = fiberRefUnsafeMakeSupervisor( supervisor.none ) // circular with Fiber /* @internal */ export const fiberAwaitAll = (fibers: Iterable>): Effect.Effect => core.asUnit(internalFiber._await(fiberAll(fibers))) /** @internal */ export const fiberAll = (fibers: Iterable>): Fiber.Fiber> => ({ [internalFiber.FiberTypeId]: internalFiber.fiberVariance, id: () => RA.fromIterable(fibers).reduce((id, fiber) => FiberId.combine(id, fiber.id()), FiberId.none), await: () => core.exit(forEachParUnbounded(fibers, (fiber) => core.flatten(fiber.await()), false)), children: () => core.map(forEachParUnbounded(fibers, (fiber) => fiber.children(), false), RA.flatten), inheritAll: () => core.forEachSequentialDiscard(fibers, (fiber) => fiber.inheritAll()), poll: () => core.map( core.forEachSequential(fibers, (fiber) => fiber.poll()), RA.reduceRight( Option.some>>(core.exitSucceed(new Array())), (optionB, optionA) => { switch (optionA._tag) { case "None": { return Option.none() } case "Some": { switch (optionB._tag) { case "None": { return Option.none() } case "Some": { return Option.some( core.exitZipWith(optionA.value, optionB.value, { onSuccess: (a, chunk) => [a, ...chunk], onFailure: internalCause.parallel }) ) } } } } } ) ), interruptAsFork: (fiberId) => core.forEachSequentialDiscard(fibers, (fiber) => fiber.interruptAsFork(fiberId)), pipe() { return pipeArguments(this, arguments) } }) /* @internal */ export const fiberInterruptFork = (self: Fiber.Fiber): Effect.Effect => core.asUnit(forkDaemon(core.interruptFiber(self))) /* @internal */ export const fiberJoinAll = (fibers: Iterable>): Effect.Effect => core.asUnit(internalFiber.join(fiberAll(fibers))) /* @internal */ export const fiberScoped = (self: Fiber.Fiber): Effect.Effect> => acquireRelease(core.succeed(self), core.interruptFiber) // // circular race // /** @internal */ export const raceWith = dual< ( other: Effect.Effect, options: { readonly onSelfDone: (exit: Exit.Exit, fiber: Fiber.Fiber) => Effect.Effect readonly onOtherDone: (exit: Exit.Exit, fiber: Fiber.Fiber) => Effect.Effect } ) => (self: Effect.Effect) => Effect.Effect, ( self: Effect.Effect, other: Effect.Effect, options: { readonly onSelfDone: (exit: Exit.Exit, fiber: Fiber.Fiber) => Effect.Effect readonly onOtherDone: (exit: Exit.Exit, fiber: Fiber.Fiber) => Effect.Effect } ) => Effect.Effect >(3, ( self: Effect.Effect, other: Effect.Effect, options: { readonly onSelfDone: (exit: Exit.Exit, fiber: Fiber.Fiber) => Effect.Effect readonly onOtherDone: (exit: Exit.Exit, fiber: Fiber.Fiber) => Effect.Effect } ) => raceFibersWith(self, other, { onSelfWin: (winner, loser) => core.flatMap(winner.await(), (exit) => { switch (exit._tag) { case OpCodes.OP_SUCCESS: { return core.flatMap( winner.inheritAll(), () => options.onSelfDone(exit, loser) ) } case OpCodes.OP_FAILURE: { return options.onSelfDone(exit, loser) } } }), onOtherWin: (winner, loser) => core.flatMap(winner.await(), (exit) => { switch (exit._tag) { case OpCodes.OP_SUCCESS: { return core.flatMap( winner.inheritAll(), () => options.onOtherDone(exit, loser) ) } case OpCodes.OP_FAILURE: { return options.onOtherDone(exit, loser) } } }) })) /** @internal */ export const disconnect = (self: Effect.Effect): Effect.Effect => core.uninterruptibleMask((restore) => core.fiberIdWith((fiberId) => core.flatMap(forkDaemon(restore(self)), (fiber) => pipe( restore(internalFiber.join(fiber)), core.onInterrupt(() => pipe(fiber, internalFiber.interruptAsFork(fiberId))) )) ) ) /** @internal */ export const race = dual< ( that: Effect.Effect ) => ( self: Effect.Effect ) => Effect.Effect, ( self: Effect.Effect, that: Effect.Effect ) => Effect.Effect >( 2, (self, that) => core.fiberIdWith((parentFiberId) => raceWith(self, that, { onSelfDone: (exit, right) => core.exitMatchEffect(exit, { onFailure: (cause) => pipe( internalFiber.join(right), internalEffect.mapErrorCause((cause2) => internalCause.parallel(cause, cause2)) ), onSuccess: (value) => pipe( right, core.interruptAsFiber(parentFiberId), core.as(value) ) }), onOtherDone: (exit, left) => core.exitMatchEffect(exit, { onFailure: (cause) => pipe( internalFiber.join(left), internalEffect.mapErrorCause((cause2) => internalCause.parallel(cause2, cause)) ), onSuccess: (value) => pipe( left, core.interruptAsFiber(parentFiberId), core.as(value) ) }) }) ) ) /** @internal */ export const raceFibersWith = dual< ( other: Effect.Effect, options: { readonly onSelfWin: ( winner: Fiber.RuntimeFiber, loser: Fiber.RuntimeFiber ) => Effect.Effect readonly onOtherWin: ( winner: Fiber.RuntimeFiber, loser: Fiber.RuntimeFiber ) => Effect.Effect readonly selfScope?: fiberScope.FiberScope readonly otherScope?: fiberScope.FiberScope } ) => (self: Effect.Effect) => Effect.Effect< R | R1 | R2 | R3, E2 | E3, A2 | A3 >, ( self: Effect.Effect, other: Effect.Effect, options: { readonly onSelfWin: ( winner: Fiber.RuntimeFiber, loser: Fiber.RuntimeFiber ) => Effect.Effect readonly onOtherWin: ( winner: Fiber.RuntimeFiber, loser: Fiber.RuntimeFiber ) => Effect.Effect readonly selfScope?: fiberScope.FiberScope readonly otherScope?: fiberScope.FiberScope } ) => Effect.Effect< R | R1 | R2 | R3, E2 | E3, A2 | A3 > >(3, ( self: Effect.Effect, other: Effect.Effect, options: { readonly onSelfWin: ( winner: Fiber.RuntimeFiber, loser: Fiber.RuntimeFiber ) => Effect.Effect readonly onOtherWin: ( winner: Fiber.RuntimeFiber, loser: Fiber.RuntimeFiber ) => Effect.Effect readonly selfScope?: fiberScope.FiberScope readonly otherScope?: fiberScope.FiberScope } ) => core.withFiberRuntime((parentFiber, parentStatus) => { const parentRuntimeFlags = parentStatus.runtimeFlags const raceIndicator = MRef.make(true) const leftFiber: FiberRuntime = unsafeMakeChildFiber( self, parentFiber, parentRuntimeFlags, options.selfScope ) const rightFiber: FiberRuntime = unsafeMakeChildFiber( other, parentFiber, parentRuntimeFlags, options.otherScope ) return core.async((cb) => { leftFiber.addObserver(() => completeRace(leftFiber, rightFiber, options.onSelfWin, raceIndicator, cb)) rightFiber.addObserver(() => completeRace(rightFiber, leftFiber, options.onOtherWin, raceIndicator, cb)) leftFiber.startFork(self) rightFiber.startFork(other) }, FiberId.combine(leftFiber.id(), rightFiber.id())) })) const completeRace = ( winner: Fiber.RuntimeFiber, loser: Fiber.RuntimeFiber, cont: (winner: Fiber.RuntimeFiber, loser: Fiber.RuntimeFiber) => Effect.Effect, ab: MRef.MutableRef, cb: (_: Effect.Effect) => void ): void => { if (MRef.compareAndSet(true, false)(ab)) { cb(cont(winner, loser)) } } /** @internal */ export const ensuring = dual< ( finalizer: Effect.Effect ) => ( self: Effect.Effect ) => Effect.Effect, ( self: Effect.Effect, finalizer: Effect.Effect ) => Effect.Effect >(2, (self, finalizer) => core.uninterruptibleMask((restore) => core.matchCauseEffect(restore(self), { onFailure: (cause1) => core.matchCauseEffect(finalizer, { onFailure: (cause2) => core.failCause(internalCause.sequential(cause1, cause2)), onSuccess: () => core.failCause(cause1) }), onSuccess: (a) => core.as(finalizer, a) }) )) /** @internal */ export const invokeWithInterrupt: ( self: Effect.Effect, entries: ReadonlyArray> ) => Effect.Effect = (dataSource: Effect.Effect, all: ReadonlyArray>) => core.fiberIdWith((id) => core.flatMap( core.flatMap( forkDaemon(core.interruptible(dataSource)), (processing) => core.async((cb) => { const counts = all.map((_) => _.listeners.count) const checkDone = () => { if (counts.every((count) => count === 0)) { cleanup.forEach((f) => f()) cb(core.interruptFiber(processing)) } } processing.addObserver((exit) => { cleanup.forEach((f) => f()) cb(exit) }) const cleanup = all.map((r, i) => { const observer = (count: number) => { counts[i] = count checkDone() } r.listeners.addObserver(observer) return () => r.listeners.removeObserver(observer) }) checkDone() return core.sync(() => { cleanup.forEach((f) => f()) }) }) ), () => core.suspend(() => { const residual = all.flatMap((entry) => { if (!entry.state.completed) { return [entry] } return [] }) return core.forEachSequentialDiscard( residual, (entry) => complete(entry.request as any, core.exitInterrupt(id)) ) }) ) ) /** @internal */ export const interruptWhenPossible = dual< (all: Iterable>) => ( self: Effect.Effect ) => Effect.Effect, ( self: Effect.Effect, all: Iterable> ) => Effect.Effect >(2, (self, all) => core.fiberRefGetWith( currentRequestMap, (map) => core.suspend(() => { const entries = RA.fromIterable(all).flatMap((_) => map.has(_) ? [map.get(_)!] : []) return invokeWithInterrupt(self, entries) }) )) // circular Tracer /** @internal */ export const useSpanScoped = ( name: string, options?: { readonly attributes?: Record readonly links?: ReadonlyArray readonly parent?: Tracer.ParentSpan readonly root?: boolean readonly context?: Context.Context } ): Effect.Effect => acquireRelease( internalEffect.makeSpan(name, options), (span, exit) => core.flatMap( internalEffect.currentTimeNanosTracing, (endTime) => core.sync(() => span.end(endTime, exit)) ) ) /* @internal */ export const withSpanScoped = ( name: string, options?: { readonly attributes?: Record readonly links?: ReadonlyArray readonly parent?: Tracer.ParentSpan readonly root?: boolean readonly context?: Context.Context } ): Effect.Effect => core.flatMap( useSpanScoped(name, options), (span) => fiberRefLocallyScopedWith( core.currentTracerSpan, List.prepend(span) ) ) /* @internal */ export const withTracerScoped = (value: Tracer.Tracer): Effect.Effect => fiberRefLocallyScopedWith(defaultServices.currentServices, Context.add(tracer.tracerTag, value)) /* @internal */ export const withParentSpanScoped = (span: Tracer.ParentSpan): Effect.Effect => fiberRefLocallyScopedWith(core.currentTracerSpan, List.prepend(span))