// tracing: off
import * as P from "../../Promise"
import * as T from "../_internal/effect"
import * as M from "../_internal/managed"
import * as chain from "./chain"
import * as crossRight from "./crossRight"
import * as forEach from "./forEach"
import * as fromEffect from "./fromEffect"
import * as interruptWhenP from "./interruptWhenP"
import { managed } from "./managed"
/**
 * Drains the provided stream in the background for as long as this stream is running.
 * If this stream ends before `other`, `other` will be interrupted. If `other` fails,
 * this stream will fail with that error.
 */

export function drainFork_(self, other) {
  return chain.chain_(fromEffect.fromEffect(P.make()), (bgDied) =>
    crossRight.crossRight_(
      managed(
        M.fork(
          M.catchAllCause_(
            forEach.forEachManaged_(other, (_) => T.unit),
            (_) => T.toManaged(P.halt_(bgDied, _))
          )
        )
      ),
      interruptWhenP.interruptWhenP_(self, bgDied)
    )
  )
}
/**
 * Drains the provided stream in the background for as long as this stream is running.
 * If this stream ends before `other`, `other` will be interrupted. If `other` fails,
 * this stream will fail with that error.
 */

export function drainFork(other) {
  return (self) => drainFork_(self, other)
}
//# sourceMappingURL=drainFork.js.map
