import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal" /** * If this stream is empty, produce the specified element or chunk of elements, * or switch to the specified stream. * * @tsplus static effect/core/stream/Stream.Aspects defaultIfEmpty * @tsplus pipeable effect/core/stream/Stream defaultIfEmpty */ export function defaultIfEmpty( stream: Stream ): (self: Stream) => Stream export function defaultIfEmpty( chunk: Chunk ): (self: Stream) => Stream export function defaultIfEmpty( value: A1 ): (self: Stream) => Stream export function defaultIfEmpty( emptyValue: A1 | Chunk | Stream ) { return (self: Stream): Stream => { if (Chunk.isChunk(emptyValue)) { return defaultIfEmptyChunk(self, emptyValue) } if (Stream.isStream(emptyValue)) { return defaultIfEmptyStream(self, emptyValue) } return defaultIfEmptyValue(self, emptyValue) } } /** * Produces the specified element if this stream is empty. */ function defaultIfEmptyValue( self: Stream, a: A1 ): Stream { return defaultIfEmptyChunk(self, Chunk.single(a)) } /** * Produces the specified chunk if this stream is empty. */ function defaultIfEmptyChunk( self: Stream, chunk: Chunk ): Stream { return defaultIfEmptyStream(self, new StreamInternal(Channel.write(chunk))) } /** * Switches to the provided stream in case this one is empty. */ function defaultIfEmptyStream( self: Stream, stream: Stream ): Stream { const writer: Channel< R1, E, Chunk, unknown, E | E1, Chunk, any > = Channel.readWith( (input: Chunk) => input.isEmpty ? writer : Channel.write(input).flatMap(() => Channel.identity, unknown>()), (e) => Channel.fail(e), () => { concreteStream(stream) return stream.channel } ) concreteStream(self) return new StreamInternal(self.channel >> writer) }