// ets_tracing: off import "../../Operator/index.js" import * as C from "../../Cause/core.js" import * as A from "../../Collections/Immutable/Chunk/index.js" import * as E from "../../Exit/api.js" import { pipe } from "../../Function/index.js" import * as O from "../../Option/index.js" import * as T from "../_internal/effect.js" import type { Pull } from "../Pull/index.js" export type Take = E.Exit, A.Chunk> export function chunk(as: A.Chunk): Take { return E.succeed(as) } export function halt(cause: C.Cause): Take { return E.halt(pipe(cause, C.map(O.some))) } export const end: Take = E.fail(O.none) export function done(take: Take) { return T.done(take) } export function fromPull(pull: Pull): T.Effect> { return pipe( pull, T.foldCause( (c) => pipe( C.sequenceCauseOption(c), O.fold(() => end, halt) ), chunk ) ) } export function tap_( take: Take, f: (as: A.Chunk) => T.Effect ): T.Effect { return T.asUnit(E.forEach_(take, f)) } export function tap( f: (as: A.Chunk) => T.Effect ): (take: E.Exit, A.Chunk>) => T.Effect { return (take) => tap_(take, f) } /** * Folds over the failure cause, success value and end-of-stream marker to * yield a value. */ export function fold_( take: Take, end: Z, error: (cause: C.Cause) => Z, value: (chunk: A.Chunk) => Z ): Z { return E.fold_( take, (x) => pipe( x, C.sequenceCauseOption, O.fold(() => end, error) ), value ) } /** * Folds over the failure cause, success value and end-of-stream marker to * yield a value. */ export function fold( end: Z, error: (cause: C.Cause) => Z, value: (chunk: A.Chunk) => Z ) { return (take: Take) => fold_(take, end, error, value) } /** * Effectful version of `Take#fold`. * * Folds over the failure cause, success value and end-of-stream marker to * yield an effect. */ export function foldM_( take: Take, end: () => T.Effect, error: (cause: C.Cause) => T.Effect, value: (chunk: A.Chunk) => T.Effect ): T.Effect { return E.foldM_( take, (x) => pipe(x, C.sequenceCauseOption, O.fold(end, error)), value ) } /** * Effectful version of `Take#fold`. * * Folds over the failure cause, success value and end-of-stream marker to * yield an effect. */ export function foldM( end: () => T.Effect, error: (cause: C.Cause) => T.Effect, value: (chunk: A.Chunk) => T.Effect ): (take: Take) => T.Effect { return (take) => foldM_(take, end, error, value) } export function map_(take: Take, f: (a: A) => B): Take { return E.map_(take, A.map(f)) } export function map( f: (a: A) => B ): (take: E.Exit, A.Chunk>) => E.Exit, A.Chunk> { return (take) => map_(take, f) }