"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resetForkScope = exports.overrideForkScope_ = exports.overrideForkScope = exports.forkScopeWith = exports.forkInReport_ = exports.forkInReport = exports.forkIn_ = exports.forkIn = exports.forkDaemonReport_ = exports.forkDaemonReport = exports.forkDaemon = exports.transplant = exports.raceWith = exports.raceWith_ = exports.raceWithScope = exports.raceWithScope_ = exports.forkScopeMask = exports.forkScopeMask_ = exports.ForkScopeRestore = exports.forkScope = void 0;
const O = __importStar(require("../Option"));
const Scope_1 = require("../Scope");
const core_1 = require("./core");
const primitives_1 = require("./primitives");
/**
 * Retrieves the scope that will be used to supervise forked effects.
 */
exports.forkScope = new primitives_1.IGetForkScope(core_1.succeed);
class ForkScopeRestore {
    constructor(scope) {
        this.scope = scope;
        this.restore = (fa, __trace) => new primitives_1.IOverrideForkScope(fa, O.some(this.scope), __trace);
    }
}
exports.ForkScopeRestore = ForkScopeRestore;
/**
 * 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.
 */
function forkScopeMask_(newScope, f, __trace) {
    return forkScopeWith((scope) => new primitives_1.IOverrideForkScope(f(new ForkScopeRestore(scope)), O.some(newScope)), __trace);
}
exports.forkScopeMask_ = forkScopeMask_;
/**
 * 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_
 */
function forkScopeMask(f, __trace) {
    return (newScope) => forkScopeMask_(newScope, f, __trace);
}
exports.forkScopeMask = forkScopeMask;
/**
 * 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.
 */
function raceWithScope_(left, right, leftWins, rightWins, scope, __trace) {
    return new primitives_1.IRaceWith(left, right, leftWins, rightWins, O.some(scope), __trace);
}
exports.raceWithScope_ = raceWithScope_;
/**
 * 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_
 */
function raceWithScope(right, leftWins, rightWins, scope, __trace) {
    return (left) => raceWithScope_(left, right, leftWins, rightWins, scope, __trace);
}
exports.raceWithScope = raceWithScope;
/**
 * 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.
 */
function raceWith_(left, right, leftWins, rightWins, __trace) {
    return new primitives_1.IRaceWith(left, right, leftWins, rightWins, O.none, __trace);
}
exports.raceWith_ = raceWith_;
/**
 * 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_
 */
function raceWith(right, leftWins, rightWins, __trace) {
    return (left) => raceWith_(left, right, leftWins, rightWins, __trace);
}
exports.raceWith = raceWith;
/**
 * 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.
 */
function transplant(f, __trace) {
    return forkScopeWith((scope) => f((e, __trace) => new primitives_1.IOverrideForkScope(e, O.some(scope), __trace)), __trace);
}
exports.transplant = transplant;
/**
 * 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.
 */
function forkDaemon(value, __trace) {
    return new primitives_1.IFork(value, O.some(Scope_1.globalScope), O.none, __trace);
}
exports.forkDaemon = forkDaemon;
/**
 * 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_
 */
function forkDaemonReport(reportFailure, __trace) {
    return (value) => forkDaemonReport_(value, reportFailure, __trace);
}
exports.forkDaemonReport = forkDaemonReport;
/**
 * 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.
 */
function forkDaemonReport_(value, reportFailure, __trace) {
    return new primitives_1.IFork(value, O.some(Scope_1.globalScope), O.some(reportFailure), __trace);
}
exports.forkDaemonReport_ = forkDaemonReport_;
/**
 * 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_
 */
function forkIn(scope, __trace) {
    return (value) => forkIn_(value, scope, __trace);
}
exports.forkIn = forkIn;
/**
 * 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.
 */
function forkIn_(value, scope, __trace) {
    return new primitives_1.IFork(value, O.some(scope), O.none, __trace);
}
exports.forkIn_ = forkIn_;
/**
 * 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_
 */
function forkInReport(scope, reportFailure, __trace) {
    return (value) => new primitives_1.IFork(value, O.some(scope), O.some(reportFailure), __trace);
}
exports.forkInReport = forkInReport;
/**
 * 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.
 */
function forkInReport_(value, scope, reportFailure, __trace) {
    return new primitives_1.IFork(value, O.some(scope), O.some(reportFailure), __trace);
}
exports.forkInReport_ = forkInReport_;
/**
 * Retrieves the scope that will be used to supervise forked effects.
 */
function forkScopeWith(f, __trace) {
    return new primitives_1.IGetForkScope(f, __trace);
}
exports.forkScopeWith = forkScopeWith;
/**
 * Returns a new effect that will utilize the specified scope to supervise
 * any fibers forked within the original effect.
 *
 * @ets_data_first overrideForkScope_
 */
function overrideForkScope(scope, __trace) {
    return (self) => new primitives_1.IOverrideForkScope(self, O.some(scope), __trace);
}
exports.overrideForkScope = overrideForkScope;
/**
 * Returns a new effect that will utilize the specified scope to supervise
 * any fibers forked within the original effect.
 */
function overrideForkScope_(self, scope, __trace) {
    return new primitives_1.IOverrideForkScope(self, O.some(scope), __trace);
}
exports.overrideForkScope_ = overrideForkScope_;
/**
 * Returns a new effect that will utilize the default scope (fiber scope) to
 * supervise any fibers forked within the original effect.
 */
function resetForkScope(self, __trace) {
    return new primitives_1.IOverrideForkScope(self, O.none, __trace);
}
exports.resetForkScope = resetForkScope;
//# sourceMappingURL=core-scope.js.map