// ets_tracing: off import * as CK from "../../../../Collections/Immutable/Chunk/index.js" import * as CH from "../../Channel/index.js" import * as C from "../core.js" /** * Switches to the provided stream in case this one is empty. */ function defaultIfEmptyStream( self: C.Stream, stream: C.Stream ): C.Stream { const writer = (): CH.Channel< R1, E, CK.Chunk, unknown, E | E1, CK.Chunk, any > => CH.readWith( (in_) => CK.isEmpty(in_) ? writer() : CH.zipRight_(CH.write(in_), CH.identity, any>()), (e) => CH.fail(e), (_) => stream.channel ) return new C.Stream(self.channel[">>>"](writer())) } /** * Produces the specified chunk if this stream is empty. */ function defaultIfEmptyChunk( self: C.Stream, chunk: CK.Chunk ): C.Stream { return defaultIfEmptyStream(self, new C.Stream(CH.write(chunk))) } /** * Produces the specified element if this stream is empty. */ function defaultIfEmptyValue( self: C.Stream, a: A1 ): C.Stream { return defaultIfEmptyChunk(self, CK.single(a)) } export function defaultIfEmpty_( self: C.Stream, stream: C.Stream ): C.Stream export function defaultIfEmpty_( self: C.Stream, chunk: CK.Chunk ): C.Stream export function defaultIfEmpty_( self: C.Stream, a: A1 ): C.Stream export function defaultIfEmpty_( self: C.Stream, emptyValue: A1 | CK.Chunk | C.Stream ): C.Stream { if (CK.isChunk(emptyValue)) { return defaultIfEmptyChunk(self, emptyValue) } if (C.isStream(emptyValue)) { return defaultIfEmptyStream(self, emptyValue) } return defaultIfEmptyValue(self, emptyValue) } /** * @ets_data_first defaultIfEmpty_ */ export function defaultIfEmpty( stream: C.Stream ): (self: C.Stream) => C.Stream export function defaultIfEmpty( chunk: CK.Chunk ): (self: C.Stream) => C.Stream export function defaultIfEmpty( a: A1 ): (self: C.Stream) => C.Stream export function defaultIfEmpty( emptyValue: unknown ): (self: C.Stream) => C.Stream { return (self: C.Stream) => defaultIfEmpty_(self, emptyValue) }