1/**
2 * The `Take` module provides the stored representation of one pull result from
3 * a stream-like producer. A `Take<A, E, Done>` is either a non-empty batch of
4 * emitted values, a failed `Exit`, or a successful `Exit` carrying the
5 * completion value.
6 *
7 * @since 2.0.0
8 */
9import type { NonEmptyReadonlyArray } from "./Array.ts"
10import * as Cause from "./Cause.ts"
11import * as Effect from "./Effect.ts"
12import * as Exit from "./Exit.ts"
13import type * as Pull from "./Pull.ts"
14
15/**
16 * Represents one pull result: either a non-empty batch of values, a failure
17 * `Exit`, or a successful `Exit` that signals completion with a `Done` value.
18 *
19 * **When to use**
20 *
21 * Use to store, transfer, or interpret pull results later while preserving
22 * emitted values, failures, and normal completion.
23 *
24 * @see {@link toPull} for interpreting a `Take` as a `Pull` step
25 *
26 * @category models
27 * @since 2.0.0
28 */
29export type Take<A, E = never, Done = void> = NonEmptyReadonlyArray<A> | Exit.Exit<Done, E>
30
31/**
32 * Converts a `Take` into a `Pull`, succeeding with value batches, failing with
33 * failure exits, and translating successful exits into pull completion.
34 *
35 * **When to use**
36 *
37 * Use to interpret a stored or transferred `Take` as a `Pull` step while
38 * preserving emitted batches, ordinary failures, and completion values.
39 *
40 * @category converting
41 * @since 4.0.0
42 */
43export const toPull = <A, E, Done>(take: Take<A, E, Done>): Pull.Pull<NonEmptyReadonlyArray<A>, E, Done> =>
44 Exit.isExit(take)
45 ? Exit.isSuccess(take) ? Cause.done(take.value) : (take as Exit.Exit<never, E>)
46 : Effect.succeed(take)