// tracing: off
import * as C from "../../Cause"
import { pipe } from "../../Function"
import * as O from "../../Option"
import * as Pull from "../../Stream/Pull"
import * as T from "../_internal/effect"
import * as M from "../_internal/managed"
import * as Ref from "../_internal/ref"
import { Stream } from "./definitions"
export function forever(self) {
  return new Stream(
    M.map_(
      M.tap_(
        M.bind_(
          M.bind_(M.do, "currStream", () => T.toManaged(Ref.makeRef(Pull.end))),
          "switchStream",
          () => M.switchable()
        ),
        ({ currStream, switchStream }) => {
          return T.toManaged(T.chain_(switchStream(self.proc), currStream.set))
        }
      ),
      ({ currStream, switchStream }) => {
        const go = T.catchAllCause_(T.flatten(currStream.get), (_) =>
          O.fold_(
            C.sequenceCauseOption(_),
            () => T.zipRight_(T.chain_(switchStream(self.proc), currStream.set), go),
            (e) => Pull.halt(e)
          )
        )
        return go
      }
    )
  )
}
//# sourceMappingURL=forever.js.map
