// ets_tracing: off import type { Exit } from "../Exit/exit.js" import type { Runtime } from "../Fiber/core.js" import type * as Fiber from "../Fiber/index.js" import * as O from "../Option/index.js" import type { Scope } from "../Scope/index.js" import { globalScope } from "../Scope/index.js" import { succeed } from "./core.js" import type { Effect, RIO, UIO } from "./effect.js" import type { FailureReporter } from "./primitives.js" import { IFork, IGetForkScope, IOverrideForkScope, IRaceWith } from "./primitives.js" /** * Retrieves the scope that will be used to supervise forked effects. */ export const forkScope: UIO>> = new IGetForkScope(succeed) export class ForkScopeRestore { constructor(private scope: Scope>) {} readonly restore = ( fa: Effect, __trace?: string ): Effect => new IOverrideForkScope(fa, O.some(this.scope), __trace) } /** * Captures the fork scope, before overriding it with the specified new * scope, passing a function that allows restoring the fork scope to * what it was originally. */ export function forkScopeMask_( newScope: Scope>, f: (restore: ForkScopeRestore) => Effect, __trace?: string ) { return forkScopeWith( (scope) => new IOverrideForkScope(f(new ForkScopeRestore(scope)), O.some(newScope)), __trace ) } /** * Captures the fork scope, before overriding it with the specified new * scope, passing a function that allows restoring the fork scope to * what it was originally. * * @ets_data_first forkScopeMask_ */ export function forkScopeMask( f: (restore: ForkScopeRestore) => Effect, __trace?: string ) { return (newScope: Scope>) => forkScopeMask_(newScope, f, __trace) } /** * Returns an effect that races this effect with the specified effect, calling * the specified finisher as soon as one result or the other has been computed. */ export function raceWithScope_( left: Effect, right: Effect, leftWins: (exit: Exit, fiber: Fiber.Fiber) => Effect, rightWins: (exit: Exit, fiber: Fiber.Fiber) => Effect, scope: Scope>, __trace?: string ): Effect { return new IRaceWith(left, right, leftWins, rightWins, O.some(scope), __trace) } /** * Returns an effect that races this effect with the specified effect, calling * the specified finisher as soon as one result or the other has been computed. * * @ets_data_first raceWithScope_ */ export function raceWithScope( right: Effect, leftWins: (exit: Exit, fiber: Fiber.Fiber) => Effect, rightWins: (exit: Exit, fiber: Fiber.Fiber) => Effect, scope: Scope>, __trace?: string ) { return (left: Effect) => raceWithScope_(left, right, leftWins, rightWins, scope, __trace) } /** * Returns an effect that races this effect with the specified effect, calling * the specified finisher as soon as one result or the other has been computed. */ export function raceWith_( left: Effect, right: Effect, leftWins: (exit: Exit, fiber: Fiber.Fiber) => Effect, rightWins: (exit: Exit, fiber: Fiber.Fiber) => Effect, __trace?: string ): Effect { return new IRaceWith(left, right, leftWins, rightWins, O.none, __trace) } /** * Returns an effect that races this effect with the specified effect, calling * the specified finisher as soon as one result or the other has been computed. * * @ets_data_first raceWith_ */ export function raceWith( right: Effect, leftWins: (exit: Exit, fiber: Fiber.Fiber) => Effect, rightWins: (exit: Exit, fiber: Fiber.Fiber) => Effect, __trace?: string ) { return (left: Effect) => raceWith_(left, right, leftWins, rightWins, __trace) } /** * Graft function */ export type Grafter = ( effect: Effect, __trace?: string ) => Effect /** * Transplants specified effects so that when those effects fork other * effects, the forked effects will be governed by the scope of the * fiber that executes this effect. * * This can be used to "graft" deep grandchildren onto a higher-level * scope, effectively extending their lifespans into the parent scope. */ export function transplant( f: (_: Grafter) => Effect, __trace?: string ) { return forkScopeWith( (scope) => f((e, __trace) => new IOverrideForkScope(e, O.some(scope), __trace)), __trace ) } /** * Forks the effect into a new fiber attached to the global scope. Because the * new fiber is attached to the global scope, when the fiber executing the * returned effect terminates, the forked fiber will continue running. */ export function forkDaemon( value: Effect, __trace?: string ): RIO> { return new IFork(value, O.some(globalScope), O.none, __trace) } /** * Forks the effect into a new fiber attached to the global scope. Because the * new fiber is attached to the global scope, when the fiber executing the * returned effect terminates, the forked fiber will continue running. * * @ets_data_first forkDaemonReport_ */ export function forkDaemonReport(reportFailure: FailureReporter, __trace?: string) { return (value: Effect): RIO> => forkDaemonReport_(value, reportFailure, __trace) } /** * Forks the effect into a new fiber attached to the global scope. Because the * new fiber is attached to the global scope, when the fiber executing the * returned effect terminates, the forked fiber will continue running. */ export function forkDaemonReport_( value: Effect, reportFailure: FailureReporter, __trace?: string ): RIO> { return new IFork(value, O.some(globalScope), O.some(reportFailure), __trace) } /** * Returns an effect that forks this effect into its own separate fiber, * returning the fiber immediately, without waiting for it to begin * executing the effect. * * The returned fiber can be used to interrupt the forked fiber, await its * result, or join the fiber. See `Fiber` for more information. * * The fiber is forked with interrupt supervision mode, meaning that when the * fiber that forks the child exits, the child will be interrupted. * * @ets_data_first forkIn_ */ export function forkIn(scope: Scope>, __trace?: string) { return (value: Effect): RIO> => forkIn_(value, scope, __trace) } /** * Returns an effect that forks this effect into its own separate fiber, * returning the fiber immediately, without waiting for it to begin * executing the effect. * * The returned fiber can be used to interrupt the forked fiber, await its * result, or join the fiber. See `Fiber` for more information. * * The fiber is forked with interrupt supervision mode, meaning that when the * fiber that forks the child exits, the child will be interrupted. */ export function forkIn_( value: Effect, scope: Scope>, __trace?: string ): RIO> { return new IFork(value, O.some(scope), O.none, __trace) } /** * Returns an effect that forks this effect into its own separate fiber, * returning the fiber immediately, without waiting for it to begin * executing the effect. * * The returned fiber can be used to interrupt the forked fiber, await its * result, or join the fiber. See `Fiber` for more information. * * The fiber is forked with interrupt supervision mode, meaning that when the * fiber that forks the child exits, the child will be interrupted. * * @ets_data_first forkInReport_ */ export function forkInReport( scope: Scope>, reportFailure: FailureReporter, __trace?: string ) { return (value: Effect): RIO> => new IFork(value, O.some(scope), O.some(reportFailure), __trace) } /** * Returns an effect that forks this effect into its own separate fiber, * returning the fiber immediately, without waiting for it to begin * executing the effect. * * The returned fiber can be used to interrupt the forked fiber, await its * result, or join the fiber. See `Fiber` for more information. * * The fiber is forked with interrupt supervision mode, meaning that when the * fiber that forks the child exits, the child will be interrupted. */ export function forkInReport_( value: Effect, scope: Scope>, reportFailure: FailureReporter, __trace?: string ): RIO> { return new IFork(value, O.some(scope), O.some(reportFailure), __trace) } /** * Retrieves the scope that will be used to supervise forked effects. */ export function forkScopeWith( f: (_: Scope>) => Effect, __trace?: string ): Effect { return new IGetForkScope(f, __trace) } /** * Returns a new effect that will utilize the specified scope to supervise * any fibers forked within the original effect. * * @ets_data_first overrideForkScope_ */ export function overrideForkScope(scope: Scope>, __trace?: string) { return (self: Effect): Effect => new IOverrideForkScope(self, O.some(scope), __trace) } /** * Returns a new effect that will utilize the specified scope to supervise * any fibers forked within the original effect. */ export function overrideForkScope_( self: Effect, scope: Scope>, __trace?: string ): Effect { return new IOverrideForkScope(self, O.some(scope), __trace) } /** * Returns a new effect that will utilize the default scope (fiber scope) to * supervise any fibers forked within the original effect. */ export function resetForkScope( self: Effect, __trace?: string ): Effect { return new IOverrideForkScope(self, O.none, __trace) }