import * as Arr from "../Array.js" import * as Cause from "../Cause.js" import type * as Channel from "../Channel.js" import * as Chunk from "../Chunk.js" import * as Clock from "../Clock.js" import type * as Context from "../Context.js" import * as Duration from "../Duration.js" import * as Effect from "../Effect.js" import * as Either from "../Either.js" import * as Exit from "../Exit.js" import { constTrue, dual, identity, pipe } from "../Function.js" import type { LazyArg } from "../Function.js" import * as HashMap from "../HashMap.js" import * as HashSet from "../HashSet.js" import type * as MergeDecision from "../MergeDecision.js" import * as Option from "../Option.js" import { pipeArguments } from "../Pipeable.js" import { hasProperty, type Predicate, type Refinement } from "../Predicate.js" import * as PubSub from "../PubSub.js" import * as Queue from "../Queue.js" import * as Ref from "../Ref.js" import * as Scope from "../Scope.js" import type * as Sink from "../Sink.js" import type * as Types from "../Types.js" import * as channel from "./channel.js" import * as mergeDecision from "./channel/mergeDecision.js" import * as core from "./core-stream.js" /** @internal */ export const SinkTypeId: Sink.SinkTypeId = Symbol.for("effect/Sink") as Sink.SinkTypeId const sinkVariance = { /* c8 ignore next */ _A: (_: never) => _, /* c8 ignore next */ _In: (_: unknown) => _, /* c8 ignore next */ _L: (_: never) => _, /* c8 ignore next */ _E: (_: never) => _, /* c8 ignore next */ _R: (_: never) => _ } /** @internal */ export class SinkImpl implements Sink.Sink { readonly [SinkTypeId] = sinkVariance constructor( readonly channel: Channel.Channel, Chunk.Chunk, E, never, A, unknown, R> ) { } pipe() { return pipeArguments(this, arguments) } } /** @internal */ export const isSink = (u: unknown): u is Sink.Sink => hasProperty(u, SinkTypeId) /** @internal */ export const suspend = (evaluate: LazyArg>): Sink.Sink => new SinkImpl(core.suspend(() => toChannel(evaluate()))) /** @internal */ export const as = dual< (a: A2) => (self: Sink.Sink) => Sink.Sink, (self: Sink.Sink, a: A2) => Sink.Sink >( 2, (self, a) => pipe(self, map(() => a)) ) /** @internal */ export const collectAll = (): Sink.Sink, In> => new SinkImpl(collectAllLoop(Chunk.empty())) /** @internal */ const collectAllLoop = ( acc: Chunk.Chunk ): Channel.Channel, never, never, Chunk.Chunk, unknown> => core.readWithCause({ onInput: (chunk: Chunk.Chunk) => collectAllLoop(pipe(acc, Chunk.appendAll(chunk))), onFailure: core.failCause, onDone: () => core.succeed(acc) }) /** @internal */ export const collectAllN = (n: number): Sink.Sink, In, In> => suspend(() => fromChannel(collectAllNLoop(n, Chunk.empty()))) /** @internal */ const collectAllNLoop = ( n: number, acc: Chunk.Chunk ): Channel.Channel, Chunk.Chunk, never, never, Chunk.Chunk, unknown> => core.readWithCause({ onInput: (chunk: Chunk.Chunk) => { const [collected, leftovers] = Chunk.splitAt(chunk, n) if (collected.length < n) { return collectAllNLoop(n - collected.length, Chunk.appendAll(acc, collected)) } if (Chunk.isEmpty(leftovers)) { return core.succeed(Chunk.appendAll(acc, collected)) } return core.flatMap(core.write(leftovers), () => core.succeed(Chunk.appendAll(acc, collected))) }, onFailure: core.failCause, onDone: () => core.succeed(acc) }) /** @internal */ export const collectAllFrom = ( self: Sink.Sink ): Sink.Sink, In, L, E, R> => collectAllWhileWith(self, { initial: Chunk.empty(), while: constTrue, body: (chunk, a) => pipe(chunk, Chunk.append(a)) }) /** @internal */ export const collectAllToMap = ( key: (input: In) => K, merge: (x: In, y: In) => In ): Sink.Sink, In> => { return foldLeftChunks(HashMap.empty(), (map, chunk) => pipe( chunk, Chunk.reduce(map, (map, input) => { const k: K = key(input) const v: In = pipe(map, HashMap.has(k)) ? merge(pipe(map, HashMap.unsafeGet(k)), input) : input return pipe(map, HashMap.set(k, v)) }) )) } /** @internal */ export const collectAllToMapN = ( n: number, key: (input: In) => K, merge: (x: In, y: In) => In ): Sink.Sink, In, In> => { return foldWeighted, In>({ initial: HashMap.empty(), maxCost: n, cost: (acc, input) => pipe(acc, HashMap.has(key(input))) ? 0 : 1, body: (acc, input) => { const k: K = key(input) const v: In = pipe(acc, HashMap.has(k)) ? merge(pipe(acc, HashMap.unsafeGet(k)), input) : input return pipe(acc, HashMap.set(k, v)) } }) } /** @internal */ export const collectAllToSet = (): Sink.Sink, In> => foldLeftChunks, In>( HashSet.empty(), (acc, chunk) => pipe(chunk, Chunk.reduce(acc, (acc, input) => pipe(acc, HashSet.add(input)))) ) /** @internal */ export const collectAllToSetN = (n: number): Sink.Sink, In, In> => foldWeighted, In>({ initial: HashSet.empty(), maxCost: n, cost: (acc, input) => HashSet.has(acc, input) ? 0 : 1, body: (acc, input) => HashSet.add(acc, input) }) /** @internal */ export const collectAllUntil = (p: Predicate): Sink.Sink, In, In> => { return pipe( fold<[Chunk.Chunk, boolean], In>( [Chunk.empty(), true], (tuple) => tuple[1], ([chunk, _], input) => [pipe(chunk, Chunk.append(input)), !p(input)] ), map((tuple) => tuple[0]) ) } /** @internal */ export const collectAllUntilEffect = (p: (input: In) => Effect.Effect) => { return pipe( foldEffect<[Chunk.Chunk, boolean], In, E, R>( [Chunk.empty(), true], (tuple) => tuple[1], ([chunk, _], input) => pipe(p(input), Effect.map((bool) => [pipe(chunk, Chunk.append(input)), !bool])) ), map((tuple) => tuple[0]) ) } /** @internal */ export const collectAllWhile: { (refinement: Refinement): Sink.Sink, In, In> (predicate: Predicate): Sink.Sink, In, In> } = (predicate: Predicate): Sink.Sink, In, In> => fromChannel(collectAllWhileReader(predicate, Chunk.empty())) /** @internal */ const collectAllWhileReader = ( predicate: Predicate, done: Chunk.Chunk ): Channel.Channel, Chunk.Chunk, never, never, Chunk.Chunk, unknown> => core.readWith({ onInput: (input: Chunk.Chunk) => { const [collected, leftovers] = pipe(Chunk.toReadonlyArray(input), Arr.span(predicate)) if (leftovers.length === 0) { return collectAllWhileReader( predicate, pipe(done, Chunk.appendAll(Chunk.unsafeFromArray(collected))) ) } return pipe( core.write(Chunk.unsafeFromArray(leftovers)), channel.zipRight(core.succeed(pipe(done, Chunk.appendAll(Chunk.unsafeFromArray(collected))))) ) }, onFailure: core.fail, onDone: () => core.succeed(done) }) /** @internal */ export const collectAllWhileEffect = ( predicate: (input: In) => Effect.Effect ): Sink.Sink, In, In, E, R> => fromChannel(collectAllWhileEffectReader(predicate, Chunk.empty())) /** @internal */ const collectAllWhileEffectReader = ( predicate: (input: In) => Effect.Effect, done: Chunk.Chunk ): Channel.Channel, Chunk.Chunk, E, never, Chunk.Chunk, unknown, R> => core.readWith({ onInput: (input: Chunk.Chunk) => pipe( core.fromEffect(pipe(input, Effect.takeWhile(predicate), Effect.map(Chunk.unsafeFromArray))), core.flatMap((collected) => { const leftovers = pipe(input, Chunk.drop(collected.length)) if (Chunk.isEmpty(leftovers)) { return collectAllWhileEffectReader(predicate, pipe(done, Chunk.appendAll(collected))) } return pipe(core.write(leftovers), channel.zipRight(core.succeed(pipe(done, Chunk.appendAll(collected))))) }) ), onFailure: core.fail, onDone: () => core.succeed(done) }) /** @internal */ export const collectAllWhileWith: { ( options: { readonly initial: S readonly while: Predicate readonly body: (s: S, a: A) => S } ): (self: Sink.Sink) => Sink.Sink ( self: Sink.Sink, options: { readonly initial: S readonly while: Predicate readonly body: (s: S, a: A) => S } ): Sink.Sink } = dual( 2, ( self: Sink.Sink, options: { readonly initial: S readonly while: Predicate readonly body: (s: S, a: A) => S } ): Sink.Sink => { const refs = pipe( Ref.make(Chunk.empty()), Effect.zip(Ref.make(false)) ) const newChannel = pipe( core.fromEffect(refs), core.flatMap(([leftoversRef, upstreamDoneRef]) => { const upstreamMarker: Channel.Channel, Chunk.Chunk, never, never, unknown, unknown> = core .readWith({ onInput: (input) => pipe(core.write(input), core.flatMap(() => upstreamMarker)), onFailure: core.fail, onDone: (done) => pipe(core.fromEffect(Ref.set(upstreamDoneRef, true)), channel.as(done)) }) return pipe( upstreamMarker, core.pipeTo(channel.bufferChunk(leftoversRef)), core.pipeTo( collectAllWhileWithLoop(self, leftoversRef, upstreamDoneRef, options.initial, options.while, options.body) ) ) }) ) return new SinkImpl(newChannel) } ) const collectAllWhileWithLoop = ( self: Sink.Sink, leftoversRef: Ref.Ref>, upstreamDoneRef: Ref.Ref, currentResult: S, p: Predicate, f: (s: S, z: Z) => S ): Channel.Channel, Chunk.Chunk, E, never, S, unknown, R> => { return pipe( toChannel(self), channel.doneCollect, channel.foldChannel({ onFailure: core.fail, onSuccess: ([leftovers, doneValue]) => p(doneValue) ? pipe( core.fromEffect( Ref.set(leftoversRef, Chunk.flatten(leftovers as Chunk.Chunk>)) ), core.flatMap(() => pipe( core.fromEffect(Ref.get(upstreamDoneRef)), core.flatMap((upstreamDone) => { const accumulatedResult = f(currentResult, doneValue) return upstreamDone ? pipe(core.write(Chunk.flatten(leftovers)), channel.as(accumulatedResult)) : collectAllWhileWithLoop(self, leftoversRef, upstreamDoneRef, accumulatedResult, p, f) }) ) ) ) : pipe(core.write(Chunk.flatten(leftovers)), channel.as(currentResult)) }) ) } /** @internal */ export const collectLeftover = ( self: Sink.Sink ): Sink.Sink<[A, Chunk.Chunk], In, never, E, R> => new SinkImpl(pipe(core.collectElements(toChannel(self)), channel.map(([chunks, z]) => [z, Chunk.flatten(chunks)]))) /** @internal */ export const mapInput = dual< (f: (input: In0) => In) => (self: Sink.Sink) => Sink.Sink, (self: Sink.Sink, f: (input: In0) => In) => Sink.Sink >( 2, (self: Sink.Sink, f: (input: In0) => In): Sink.Sink => pipe(self, mapInputChunks(Chunk.map(f))) ) /** @internal */ export const mapInputEffect = dual< ( f: (input: In0) => Effect.Effect ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, f: (input: In0) => Effect.Effect ) => Sink.Sink >( 2, ( self: Sink.Sink, f: (input: In0) => Effect.Effect ): Sink.Sink => mapInputChunksEffect( self, (chunk) => Effect.map( Effect.forEach(chunk, (v) => f(v)), Chunk.unsafeFromArray ) ) ) /** @internal */ export const mapInputChunks = dual< ( f: (chunk: Chunk.Chunk) => Chunk.Chunk ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, f: (chunk: Chunk.Chunk) => Chunk.Chunk ) => Sink.Sink >( 2, ( self: Sink.Sink, f: (chunk: Chunk.Chunk) => Chunk.Chunk ): Sink.Sink => { const loop: Channel.Channel, Chunk.Chunk, never, never, unknown, unknown, R> = core.readWith({ onInput: (chunk) => pipe(core.write(f(chunk)), core.flatMap(() => loop)), onFailure: core.fail, onDone: core.succeed }) return new SinkImpl(pipe(loop, core.pipeTo(toChannel(self)))) } ) /** @internal */ export const mapInputChunksEffect = dual< ( f: (chunk: Chunk.Chunk) => Effect.Effect, E2, R2> ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, f: (chunk: Chunk.Chunk) => Effect.Effect, E2, R2> ) => Sink.Sink >( 2, ( self: Sink.Sink, f: (chunk: Chunk.Chunk) => Effect.Effect, E2, R2> ): Sink.Sink => { const loop: Channel.Channel, Chunk.Chunk, E2, never, unknown, unknown, R | R2> = core .readWith({ onInput: (chunk) => pipe(core.fromEffect(f(chunk)), core.flatMap(core.write), core.flatMap(() => loop)), onFailure: core.fail, onDone: core.succeed }) return new SinkImpl(pipe(loop, channel.pipeToOrFail(toChannel(self)))) } ) /** @internal */ export const die = (defect: unknown): Sink.Sink => failCause(Cause.die(defect)) /** @internal */ export const dieMessage = (message: string): Sink.Sink => failCause(Cause.die(new Cause.RuntimeException(message))) /** @internal */ export const dieSync = (evaluate: LazyArg): Sink.Sink => failCauseSync(() => Cause.die(evaluate())) /** @internal */ export const dimap = dual< ( options: { readonly onInput: (input: In0) => In readonly onDone: (a: A) => A2 } ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, options: { readonly onInput: (input: In0) => In readonly onDone: (a: A) => A2 } ) => Sink.Sink >( 2, ( self: Sink.Sink, options: { readonly onInput: (input: In0) => In readonly onDone: (a: A) => A2 } ): Sink.Sink => map(mapInput(self, options.onInput), options.onDone) ) /** @internal */ export const dimapEffect = dual< ( options: { readonly onInput: (input: In0) => Effect.Effect readonly onDone: (a: A) => Effect.Effect } ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, options: { readonly onInput: (input: In0) => Effect.Effect readonly onDone: (a: A) => Effect.Effect } ) => Sink.Sink >( 2, (self, options) => mapEffect( mapInputEffect(self, options.onInput), options.onDone ) ) /** @internal */ export const dimapChunks = dual< ( options: { readonly onInput: (chunk: Chunk.Chunk) => Chunk.Chunk readonly onDone: (a: A) => A2 } ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, options: { readonly onInput: (chunk: Chunk.Chunk) => Chunk.Chunk readonly onDone: (a: A) => A2 } ) => Sink.Sink >( 2, (self, options) => map( mapInputChunks(self, options.onInput), options.onDone ) ) /** @internal */ export const dimapChunksEffect = dual< ( options: { readonly onInput: (chunk: Chunk.Chunk) => Effect.Effect, E2, R2> readonly onDone: (a: A) => Effect.Effect } ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, options: { readonly onInput: (chunk: Chunk.Chunk) => Effect.Effect, E2, R2> readonly onDone: (a: A) => Effect.Effect } ) => Sink.Sink >( 2, (self, options) => mapEffect(mapInputChunksEffect(self, options.onInput), options.onDone) ) /** @internal */ export const drain: Sink.Sink = new SinkImpl( channel.drain(channel.identityChannel()) ) /** @internal */ export const drop = (n: number): Sink.Sink => suspend(() => new SinkImpl(dropLoop(n))) /** @internal */ const dropLoop = ( n: number ): Channel.Channel, Chunk.Chunk, never, never, unknown, unknown> => core.readWith({ onInput: (input: Chunk.Chunk) => { const dropped = pipe(input, Chunk.drop(n)) const leftover = Math.max(n - input.length, 0) const more = Chunk.isEmpty(input) || leftover > 0 if (more) { return dropLoop(leftover) } return pipe( core.write(dropped), channel.zipRight(channel.identityChannel, never, unknown>()) ) }, onFailure: core.fail, onDone: () => core.void }) /** @internal */ export const dropUntil = (predicate: Predicate): Sink.Sink => new SinkImpl( pipe(toChannel(dropWhile((input: In) => !predicate(input))), channel.pipeToOrFail(toChannel(drop(1)))) ) /** @internal */ export const dropUntilEffect = ( predicate: (input: In) => Effect.Effect ): Sink.Sink => suspend(() => new SinkImpl(dropUntilEffectReader(predicate))) /** @internal */ const dropUntilEffectReader = ( predicate: (input: In) => Effect.Effect ): Channel.Channel, Chunk.Chunk, E, E, unknown, unknown, R> => core.readWith({ onInput: (input: Chunk.Chunk) => pipe( input, Effect.dropUntil(predicate), Effect.map((leftover) => { const more = leftover.length === 0 return more ? dropUntilEffectReader(predicate) : pipe( core.write(Chunk.unsafeFromArray(leftover)), channel.zipRight(channel.identityChannel, E, unknown>()) ) }), channel.unwrap ), onFailure: core.fail, onDone: () => core.void }) /** @internal */ export const dropWhile = (predicate: Predicate): Sink.Sink => new SinkImpl(dropWhileReader(predicate)) /** @internal */ const dropWhileReader = ( predicate: Predicate ): Channel.Channel, Chunk.Chunk, never, never, unknown, unknown> => core.readWith({ onInput: (input: Chunk.Chunk) => { const out = pipe(input, Chunk.dropWhile(predicate)) if (Chunk.isEmpty(out)) { return dropWhileReader(predicate) } return pipe(core.write(out), channel.zipRight(channel.identityChannel, never, unknown>())) }, onFailure: core.fail, onDone: core.succeedNow }) /** @internal */ export const dropWhileEffect = ( predicate: (input: In) => Effect.Effect ): Sink.Sink => suspend(() => new SinkImpl(dropWhileEffectReader(predicate))) /** @internal */ const dropWhileEffectReader = ( predicate: (input: In) => Effect.Effect ): Channel.Channel, Chunk.Chunk, E, E, unknown, unknown, R> => core.readWith({ onInput: (input: Chunk.Chunk) => pipe( input, Effect.dropWhile(predicate), Effect.map((leftover) => { const more = leftover.length === 0 return more ? dropWhileEffectReader(predicate) : pipe( core.write(Chunk.unsafeFromArray(leftover)), channel.zipRight(channel.identityChannel, E, unknown>()) ) }), channel.unwrap ), onFailure: core.fail, onDone: () => core.void }) /** @internal */ export const ensuring = dual< ( finalizer: Effect.Effect ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, finalizer: Effect.Effect ) => Sink.Sink >( 2, (self, finalizer) => new SinkImpl(pipe(self, toChannel, channel.ensuring(finalizer))) ) /** @internal */ export const ensuringWith = dual< ( finalizer: (exit: Exit.Exit) => Effect.Effect ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, finalizer: (exit: Exit.Exit) => Effect.Effect ) => Sink.Sink >( 2, (self, finalizer) => new SinkImpl(pipe(self, toChannel, core.ensuringWith(finalizer))) ) /** @internal */ export const context = (): Sink.Sink, unknown, never, never, R> => fromEffect(Effect.context()) /** @internal */ export const contextWith = ( f: (context: Context.Context) => Z ): Sink.Sink => pipe(context(), map(f)) /** @internal */ export const contextWithEffect = ( f: (context: Context.Context) => Effect.Effect ): Sink.Sink => pipe(context(), mapEffect(f)) /** @internal */ export const contextWithSink = ( f: (context: Context.Context) => Sink.Sink ): Sink.Sink => new SinkImpl(channel.unwrap(Effect.contextWith((context) => toChannel(f(context))))) /** @internal */ export const every = (predicate: Predicate): Sink.Sink => fold(true, identity, (acc, input) => acc && predicate(input)) /** @internal */ export const fail = (e: E): Sink.Sink => new SinkImpl(core.fail(e)) /** @internal */ export const failSync = (evaluate: LazyArg): Sink.Sink => new SinkImpl(core.failSync(evaluate)) /** @internal */ export const failCause = (cause: Cause.Cause): Sink.Sink => new SinkImpl(core.failCause(cause)) /** @internal */ export const failCauseSync = (evaluate: LazyArg>): Sink.Sink => new SinkImpl(core.failCauseSync(evaluate)) /** @internal */ export const filterInput: { ( f: Refinement ): (self: Sink.Sink) => Sink.Sink (f: Predicate): (self: Sink.Sink) => Sink.Sink } = (f: Predicate) => { return (self: Sink.Sink): Sink.Sink => pipe(self, mapInputChunks(Chunk.filter(f))) } /** @internal */ export const filterInputEffect = dual< ( f: (input: In1) => Effect.Effect ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, f: (input: In1) => Effect.Effect ) => Sink.Sink >( 2, (self, f) => mapInputChunksEffect( self, (chunk) => Effect.map(Effect.filter(chunk, f), Chunk.unsafeFromArray) ) ) /** @internal */ export const findEffect = dual< ( f: (a: A) => Effect.Effect ) => (self: Sink.Sink) => Sink.Sink, In, L, E2 | E, R2 | R>, ( self: Sink.Sink, f: (a: A) => Effect.Effect ) => Sink.Sink, In, L, E2 | E, R2 | R> >( 2, ( self: Sink.Sink, f: (a: A) => Effect.Effect ): Sink.Sink, In, L, E2 | E, R2 | R> => { const newChannel = pipe( core.fromEffect(pipe( Ref.make(Chunk.empty()), Effect.zip(Ref.make(false)) )), core.flatMap(([leftoversRef, upstreamDoneRef]) => { const upstreamMarker: Channel.Channel, Chunk.Chunk, never, never, unknown, unknown> = core .readWith({ onInput: (input) => pipe(core.write(input), core.flatMap(() => upstreamMarker)), onFailure: core.fail, onDone: (done) => pipe(core.fromEffect(Ref.set(upstreamDoneRef, true)), channel.as(done)) }) const loop: Channel.Channel, Chunk.Chunk, E | E2, never, Option.Option, unknown, R | R2> = channel.foldChannel(core.collectElements(toChannel(self)), { onFailure: core.fail, onSuccess: ([leftovers, doneValue]) => pipe( core.fromEffect(f(doneValue)), core.flatMap((satisfied) => pipe( core.fromEffect(Ref.set(leftoversRef, Chunk.flatten(leftovers))), channel.zipRight( pipe( core.fromEffect(Ref.get(upstreamDoneRef)), core.flatMap((upstreamDone) => { if (satisfied) { return pipe(core.write(Chunk.flatten(leftovers)), channel.as(Option.some(doneValue))) } if (upstreamDone) { return pipe(core.write(Chunk.flatten(leftovers)), channel.as(Option.none())) } return loop }) ) ) ) ) ) }) return pipe(upstreamMarker, core.pipeTo(channel.bufferChunk(leftoversRef)), core.pipeTo(loop)) }) ) return new SinkImpl(newChannel) } ) /** @internal */ export const fold = ( s: S, contFn: Predicate, f: (s: S, input: In) => S ): Sink.Sink => suspend(() => new SinkImpl(foldReader(s, contFn, f))) /** @internal */ const foldReader = ( s: S, contFn: Predicate, f: (z: S, input: In) => S ): Channel.Channel, Chunk.Chunk, never, never, S, unknown> => { if (!contFn(s)) { return core.succeedNow(s) } return core.readWith({ onInput: (input: Chunk.Chunk) => { const [nextS, leftovers] = foldChunkSplit(s, input, contFn, f, 0, input.length) if (Chunk.isNonEmpty(leftovers)) { return pipe(core.write(leftovers), channel.as(nextS)) } return foldReader(nextS, contFn, f) }, onFailure: core.fail, onDone: () => core.succeedNow(s) }) } /** @internal */ const foldChunkSplit = ( s: S, chunk: Chunk.Chunk, contFn: Predicate, f: (z: S, input: In) => S, index: number, length: number ): [S, Chunk.Chunk] => { if (index === length) { return [s, Chunk.empty()] } const s1 = f(s, pipe(chunk, Chunk.unsafeGet(index))) if (contFn(s1)) { return foldChunkSplit(s1, chunk, contFn, f, index + 1, length) } return [s1, pipe(chunk, Chunk.drop(index + 1))] } /** @internal */ export const foldSink = dual< ( options: { readonly onFailure: (err: E) => Sink.Sink readonly onSuccess: (a: A) => Sink.Sink } ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, options: { readonly onFailure: (err: E) => Sink.Sink readonly onSuccess: (a: A) => Sink.Sink } ) => Sink.Sink >( 2, ( self: Sink.Sink, options: { readonly onFailure: (err: E) => Sink.Sink readonly onSuccess: (z: A) => Sink.Sink } ): Sink.Sink => { const newChannel: Channel.Channel< Chunk.Chunk, Chunk.Chunk, E1 | E2, never, A1 | A2, unknown, R | R1 | R2 > = pipe( toChannel(self), core.collectElements, channel.foldChannel({ onFailure: (error) => toChannel(options.onFailure(error)), onSuccess: ([leftovers, z]) => core.suspend(() => { const leftoversRef = { ref: pipe(leftovers, Chunk.filter(Chunk.isNonEmpty)) as Chunk.Chunk> } const refReader = pipe( core.sync(() => { const ref = leftoversRef.ref leftoversRef.ref = Chunk.empty() return ref }), // This cast is safe because of the L1 >: L <: In1 bound. It follows that // L <: In1 and therefore Chunk[L] can be safely cast to Chunk[In1]. core.flatMap((chunk) => channel.writeChunk(chunk as Chunk.Chunk>)) ) const passthrough = channel.identityChannel, never, unknown>() const continuationSink = pipe( refReader, channel.zipRight(passthrough), core.pipeTo(toChannel(options.onSuccess(z))) ) return core.flatMap( core.collectElements(continuationSink), ([newLeftovers, z1]) => pipe( core.succeed(leftoversRef.ref), core.flatMap(channel.writeChunk), channel.zipRight(channel.writeChunk(newLeftovers)), channel.as(z1) ) ) }) }) ) return new SinkImpl(newChannel) } ) /** @internal */ export const foldChunks = ( s: S, contFn: Predicate, f: (s: S, chunk: Chunk.Chunk) => S ): Sink.Sink => suspend(() => new SinkImpl(foldChunksReader(s, contFn, f))) /** @internal */ const foldChunksReader = ( s: S, contFn: Predicate, f: (s: S, chunk: Chunk.Chunk) => S ): Channel.Channel, never, never, S, unknown> => { if (!contFn(s)) { return core.succeedNow(s) } return core.readWith({ onInput: (input: Chunk.Chunk) => foldChunksReader(f(s, input), contFn, f), onFailure: core.fail, onDone: () => core.succeedNow(s) }) } /** @internal */ export const foldChunksEffect = ( s: S, contFn: Predicate, f: (s: S, chunk: Chunk.Chunk) => Effect.Effect ): Sink.Sink => suspend(() => new SinkImpl(foldChunksEffectReader(s, contFn, f))) /** @internal */ const foldChunksEffectReader = ( s: S, contFn: Predicate, f: (s: S, chunk: Chunk.Chunk) => Effect.Effect ): Channel.Channel, E, E, S, unknown, R> => { if (!contFn(s)) { return core.succeedNow(s) } return core.readWith({ onInput: (input: Chunk.Chunk) => pipe( core.fromEffect(f(s, input)), core.flatMap((s) => foldChunksEffectReader(s, contFn, f)) ), onFailure: core.fail, onDone: () => core.succeedNow(s) }) } /** @internal */ export const foldEffect = ( s: S, contFn: Predicate, f: (s: S, input: In) => Effect.Effect ): Sink.Sink => suspend(() => new SinkImpl(foldEffectReader(s, contFn, f))) /** @internal */ const foldEffectReader = ( s: S, contFn: Predicate, f: (s: S, input: In) => Effect.Effect ): Channel.Channel, Chunk.Chunk, E, E, S, unknown, R> => { if (!contFn(s)) { return core.succeedNow(s) } return core.readWith({ onInput: (input: Chunk.Chunk) => pipe( core.fromEffect(foldChunkSplitEffect(s, input, contFn, f)), core.flatMap(([nextS, leftovers]) => pipe( leftovers, Option.match({ onNone: () => foldEffectReader(nextS, contFn, f), onSome: (leftover) => pipe(core.write(leftover), channel.as(nextS)) }) ) ) ), onFailure: core.fail, onDone: () => core.succeedNow(s) }) } /** @internal */ const foldChunkSplitEffect = ( s: S, chunk: Chunk.Chunk, contFn: Predicate, f: (s: S, input: In) => Effect.Effect ): Effect.Effect<[S, Option.Option>], E, R> => foldChunkSplitEffectInternal(s, chunk, 0, chunk.length, contFn, f) /** @internal */ const foldChunkSplitEffectInternal = ( s: S, chunk: Chunk.Chunk, index: number, length: number, contFn: Predicate, f: (s: S, input: In) => Effect.Effect ): Effect.Effect<[S, Option.Option>], E, R> => { if (index === length) { return Effect.succeed([s, Option.none()]) } return pipe( f(s, pipe(chunk, Chunk.unsafeGet(index))), Effect.flatMap((s1) => contFn(s1) ? foldChunkSplitEffectInternal(s1, chunk, index + 1, length, contFn, f) : Effect.succeed([s1, Option.some(pipe(chunk, Chunk.drop(index + 1)))]) ) ) } /** @internal */ export const foldLeft = (s: S, f: (s: S, input: In) => S): Sink.Sink => ignoreLeftover(fold(s, constTrue, f)) /** @internal */ export const foldLeftChunks = ( s: S, f: (s: S, chunk: Chunk.Chunk) => S ): Sink.Sink => foldChunks(s, constTrue, f) /** @internal */ export const foldLeftChunksEffect = ( s: S, f: (s: S, chunk: Chunk.Chunk) => Effect.Effect ): Sink.Sink => ignoreLeftover(foldChunksEffect(s, constTrue, f)) /** @internal */ export const foldLeftEffect = ( s: S, f: (s: S, input: In) => Effect.Effect ): Sink.Sink => foldEffect(s, constTrue, f) /** @internal */ export const foldUntil = (s: S, max: number, f: (s: S, input: In) => S): Sink.Sink => pipe( fold<[S, number], In>( [s, 0], (tuple) => tuple[1] < max, ([output, count], input) => [f(output, input), count + 1] ), map((tuple) => tuple[0]) ) /** @internal */ export const foldUntilEffect = ( s: S, max: number, f: (s: S, input: In) => Effect.Effect ): Sink.Sink => pipe( foldEffect( [s, 0 as number] as const, (tuple) => tuple[1] < max, ([output, count], input: In) => pipe(f(output, input), Effect.map((s) => [s, count + 1] as const)) ), map((tuple) => tuple[0]) ) /** @internal */ export const foldWeighted = ( options: { readonly initial: S readonly maxCost: number readonly cost: (s: S, input: In) => number readonly body: (s: S, input: In) => S } ): Sink.Sink => foldWeightedDecompose({ ...options, decompose: Chunk.of }) /** @internal */ export const foldWeightedDecompose = ( options: { readonly initial: S readonly maxCost: number readonly cost: (s: S, input: In) => number readonly decompose: (input: In) => Chunk.Chunk readonly body: (s: S, input: In) => S } ): Sink.Sink => suspend(() => new SinkImpl( foldWeightedDecomposeLoop( options.initial, 0, false, options.maxCost, options.cost, options.decompose, options.body ) ) ) /** @internal */ const foldWeightedDecomposeLoop = ( s: S, cost: number, dirty: boolean, max: number, costFn: (s: S, input: In) => number, decompose: (input: In) => Chunk.Chunk, f: (s: S, input: In) => S ): Channel.Channel, Chunk.Chunk, never, never, S, unknown> => core.readWith({ onInput: (input: Chunk.Chunk) => { const [nextS, nextCost, nextDirty, leftovers] = foldWeightedDecomposeFold( input, s, cost, dirty, max, costFn, decompose, f ) if (Chunk.isNonEmpty(leftovers)) { return pipe(core.write(leftovers), channel.zipRight(core.succeedNow(nextS))) } if (cost > max) { return core.succeedNow(nextS) } return foldWeightedDecomposeLoop(nextS, nextCost, nextDirty, max, costFn, decompose, f) }, onFailure: core.fail, onDone: () => core.succeedNow(s) }) /** @internal */ const foldWeightedDecomposeFold = ( input: Chunk.Chunk, s: S, cost: number, dirty: boolean, max: number, costFn: (s: S, input: In) => number, decompose: (input: In) => Chunk.Chunk, f: (s: S, input: In) => S ): [S, number, boolean, Chunk.Chunk] => { for (let index = 0; index < input.length; index++) { const elem = Chunk.unsafeGet(input, index) const prevCost = cost cost = cost + costFn(s, elem) if (cost <= max) { s = f(s, elem) dirty = true continue } const decomposed = decompose(elem) if (decomposed.length <= 1 && !dirty) { // If `elem` cannot be decomposed, we need to cross the `max` threshold. To // minimize "injury", we only allow this when we haven't added anything else // to the aggregate (dirty = false). return [f(s, elem), cost, true, Chunk.drop(input, index + 1)] } if (decomposed.length <= 1 && dirty) { // If the state is dirty and `elem` cannot be decomposed, we stop folding // and include `elem` in the leftovers. return [s, prevCost, dirty, Chunk.drop(input, index)] } // `elem` got decomposed, so we will recurse with the decomposed elements pushed // into the chunk we're processing and see if we can aggregate further. input = Chunk.appendAll(decomposed, Chunk.drop(input, index + 1)) cost = prevCost index = -1 } return [s, cost, dirty, Chunk.empty()] } /** @internal */ export const foldWeightedDecomposeEffect = ( options: { readonly initial: S readonly maxCost: number readonly cost: (s: S, input: In) => Effect.Effect readonly decompose: (input: In) => Effect.Effect, E2, R2> readonly body: (s: S, input: In) => Effect.Effect } ): Sink.Sink => suspend(() => new SinkImpl( foldWeightedDecomposeEffectLoop( options.initial, options.maxCost, options.cost, options.decompose, options.body, 0, false ) ) ) /** @internal */ export const foldWeightedEffect = ( options: { readonly initial: S readonly maxCost: number readonly cost: (s: S, input: In) => Effect.Effect readonly body: (s: S, input: In) => Effect.Effect } ): Sink.Sink => foldWeightedDecomposeEffect({ ...options, decompose: (input) => Effect.succeed(Chunk.of(input)) }) const foldWeightedDecomposeEffectLoop = ( s: S, max: number, costFn: (s: S, input: In) => Effect.Effect, decompose: (input: In) => Effect.Effect, E2, R2>, f: (s: S, input: In) => Effect.Effect, cost: number, dirty: boolean ): Channel.Channel, Chunk.Chunk, E | E2 | E3, E | E2 | E3, S, unknown, R | R2 | R3> => core.readWith({ onInput: (input: Chunk.Chunk) => pipe( core.fromEffect(foldWeightedDecomposeEffectFold(s, max, costFn, decompose, f, input, dirty, cost, 0)), core.flatMap(([nextS, nextCost, nextDirty, leftovers]) => { if (Chunk.isNonEmpty(leftovers)) { return pipe(core.write(leftovers), channel.zipRight(core.succeedNow(nextS))) } if (cost > max) { return core.succeedNow(nextS) } return foldWeightedDecomposeEffectLoop(nextS, max, costFn, decompose, f, nextCost, nextDirty) }) ), onFailure: core.fail, onDone: () => core.succeedNow(s) }) /** @internal */ const foldWeightedDecomposeEffectFold = ( s: S, max: number, costFn: (s: S, input: In) => Effect.Effect, decompose: (input: In) => Effect.Effect, E2, R2>, f: (s: S, input: In) => Effect.Effect, input: Chunk.Chunk, dirty: boolean, cost: number, index: number ): Effect.Effect<[S, number, boolean, Chunk.Chunk], E | E2 | E3, R | R2 | R3> => { if (index === input.length) { return Effect.succeed([s, cost, dirty, Chunk.empty()]) } const elem = pipe(input, Chunk.unsafeGet(index)) return pipe( costFn(s, elem), Effect.map((newCost) => cost + newCost), Effect.flatMap((total) => { if (total <= max) { return pipe( f(s, elem), Effect.flatMap((s) => foldWeightedDecomposeEffectFold(s, max, costFn, decompose, f, input, true, total, index + 1) ) ) } return pipe( decompose(elem), Effect.flatMap((decomposed) => { if (decomposed.length <= 1 && !dirty) { // If `elem` cannot be decomposed, we need to cross the `max` threshold. To // minimize "injury", we only allow this when we haven't added anything else // to the aggregate (dirty = false). return pipe( f(s, elem), Effect.map((s) => [s, total, true, pipe(input, Chunk.drop(index + 1))]) ) } if (decomposed.length <= 1 && dirty) { // If the state is dirty and `elem` cannot be decomposed, we stop folding // and include `elem` in th leftovers. return Effect.succeed([s, cost, dirty, pipe(input, Chunk.drop(index))]) } // `elem` got decomposed, so we will recurse with the decomposed elements pushed // into the chunk we're processing and see if we can aggregate further. const next = pipe(decomposed, Chunk.appendAll(pipe(input, Chunk.drop(index + 1)))) return foldWeightedDecomposeEffectFold(s, max, costFn, decompose, f, next, dirty, cost, 0) }) ) }) ) } /** @internal */ export const flatMap = dual< ( f: (a: A) => Sink.Sink ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, f: (a: A) => Sink.Sink ) => Sink.Sink >( 2, (self, f) => foldSink(self, { onFailure: fail, onSuccess: f }) ) /** @internal */ export const forEach = (f: (input: In) => Effect.Effect): Sink.Sink => { const process: Channel.Channel, E, E, void, unknown, R> = core.readWithCause({ onInput: (input: Chunk.Chunk) => pipe(core.fromEffect(Effect.forEach(input, (v) => f(v), { discard: true })), core.flatMap(() => process)), onFailure: core.failCause, onDone: () => core.void }) return new SinkImpl(process) } /** @internal */ export const forEachChunk = ( f: (input: Chunk.Chunk) => Effect.Effect ): Sink.Sink => { const process: Channel.Channel, E, E, void, unknown, R> = core.readWithCause({ onInput: (input: Chunk.Chunk) => pipe(core.fromEffect(f(input)), core.flatMap(() => process)), onFailure: core.failCause, onDone: () => core.void }) return new SinkImpl(process) } /** @internal */ export const forEachWhile = ( f: (input: In) => Effect.Effect ): Sink.Sink => { const process: Channel.Channel, Chunk.Chunk, E, E, void, unknown, R> = core.readWithCause({ onInput: (input: Chunk.Chunk) => forEachWhileReader(f, input, 0, input.length, process), onFailure: core.failCause, onDone: () => core.void }) return new SinkImpl(process) } /** @internal */ const forEachWhileReader = ( f: (input: In) => Effect.Effect, input: Chunk.Chunk, index: number, length: number, cont: Channel.Channel, Chunk.Chunk, E, E, void, unknown, R> ): Channel.Channel, Chunk.Chunk, E, E, void, unknown, R> => { if (index === length) { return cont } return pipe( core.fromEffect(f(pipe(input, Chunk.unsafeGet(index)))), core.flatMap((bool) => bool ? forEachWhileReader(f, input, index + 1, length, cont) : core.write(pipe(input, Chunk.drop(index))) ), channel.catchAll((error) => pipe(core.write(pipe(input, Chunk.drop(index))), channel.zipRight(core.fail(error)))) ) } /** @internal */ export const forEachChunkWhile = ( f: (input: Chunk.Chunk) => Effect.Effect ): Sink.Sink => { const reader: Channel.Channel, E, E, void, unknown, R> = core.readWith({ onInput: (input: Chunk.Chunk) => pipe( core.fromEffect(f(input)), core.flatMap((cont) => cont ? reader : core.void) ), onFailure: core.fail, onDone: () => core.void }) return new SinkImpl(reader) } /** @internal */ export const fromChannel = ( channel: Channel.Channel, Chunk.Chunk, E, never, A, unknown, R> ): Sink.Sink => new SinkImpl(channel) /** @internal */ export const fromEffect = (effect: Effect.Effect): Sink.Sink => new SinkImpl(core.fromEffect(effect)) /** @internal */ export const fromPubSub = ( pubsub: PubSub.PubSub, options?: { readonly shutdown?: boolean | undefined } ): Sink.Sink => fromQueue(pubsub, options) /** @internal */ export const fromPush = ( push: Effect.Effect< (_: Option.Option>) => Effect.Effect, Chunk.Chunk], R>, never, R > ): Sink.Sink> => new SinkImpl(channel.unwrapScoped(pipe(push, Effect.map(fromPushPull)))) const fromPushPull = ( push: ( option: Option.Option> ) => Effect.Effect, Chunk.Chunk], R> ): Channel.Channel, Chunk.Chunk, E, never, Z, unknown, R> => core.readWith({ onInput: (input: Chunk.Chunk) => channel.foldChannel(core.fromEffect(push(Option.some(input))), { onFailure: ([either, leftovers]) => Either.match(either, { onLeft: (error) => pipe(core.write(leftovers), channel.zipRight(core.fail(error))), onRight: (z) => pipe(core.write(leftovers), channel.zipRight(core.succeedNow(z))) }), onSuccess: () => fromPushPull(push) }), onFailure: core.fail, onDone: () => channel.foldChannel(core.fromEffect(push(Option.none())), { onFailure: ([either, leftovers]) => Either.match(either, { onLeft: (error) => pipe(core.write(leftovers), channel.zipRight(core.fail(error))), onRight: (z) => pipe(core.write(leftovers), channel.zipRight(core.succeedNow(z))) }), onSuccess: () => core.fromEffect( Effect.dieMessage( "BUG: Sink.fromPush - please report an issue at https://github.com/Effect-TS/effect/issues" ) ) }) }) /** @internal */ export const fromQueue = ( queue: Queue.Enqueue, options?: { readonly shutdown?: boolean | undefined } ): Sink.Sink => options?.shutdown ? unwrapScoped( Effect.map( Effect.acquireRelease(Effect.succeed(queue), Queue.shutdown), fromQueue ) ) : forEachChunk((input: Chunk.Chunk) => Queue.offerAll(queue, input)) /** @internal */ export const head = (): Sink.Sink, In, In> => fold( Option.none() as Option.Option, Option.isNone, (option, input) => Option.match(option, { onNone: () => Option.some(input), onSome: () => option }) ) /** @internal */ export const ignoreLeftover = (self: Sink.Sink): Sink.Sink => new SinkImpl(channel.drain(toChannel(self))) /** @internal */ export const last = (): Sink.Sink, In, In> => foldLeftChunks(Option.none(), (s, input) => Option.orElse(Chunk.last(input), () => s)) /** @internal */ export const leftover = (chunk: Chunk.Chunk): Sink.Sink => new SinkImpl(core.suspend(() => core.write(chunk))) /** @internal */ export const map = dual< (f: (a: A) => A2) => (self: Sink.Sink) => Sink.Sink, (self: Sink.Sink, f: (a: A) => A2) => Sink.Sink >(2, (self, f) => { return new SinkImpl(pipe(toChannel(self), channel.map(f))) }) /** @internal */ export const mapEffect = dual< ( f: (a: A) => Effect.Effect ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, f: (a: A) => Effect.Effect ) => Sink.Sink >( 2, (self, f) => new SinkImpl(pipe(toChannel(self), channel.mapEffect(f))) ) /** @internal */ export const mapError = dual< (f: (error: E) => E2) => (self: Sink.Sink) => Sink.Sink, (self: Sink.Sink, f: (error: E) => E2) => Sink.Sink >( 2, (self, f) => new SinkImpl(pipe(toChannel(self), channel.mapError(f))) ) /** @internal */ export const mapLeftover = dual< (f: (leftover: L) => L2) => (self: Sink.Sink) => Sink.Sink, (self: Sink.Sink, f: (leftover: L) => L2) => Sink.Sink >( 2, (self, f) => new SinkImpl(pipe(toChannel(self), channel.mapOut(Chunk.map(f)))) ) /** @internal */ export const never: Sink.Sink = fromEffect(Effect.never) /** @internal */ export const orElse = dual< ( that: LazyArg> ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, that: LazyArg> ) => Sink.Sink >( 2, ( self: Sink.Sink, that: LazyArg> ): Sink.Sink => new SinkImpl( pipe(toChannel(self), channel.orElse(() => toChannel(that()))) ) ) /** @internal */ export const provideContext = dual< (context: Context.Context) => (self: Sink.Sink) => Sink.Sink, (self: Sink.Sink, context: Context.Context) => Sink.Sink >( 2, (self, context) => new SinkImpl(pipe(toChannel(self), core.provideContext(context))) ) /** @internal */ export const race = dual< ( that: Sink.Sink ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, that: Sink.Sink ) => Sink.Sink >( 2, (self, that) => pipe(self, raceBoth(that), map(Either.merge)) ) /** @internal */ export const raceBoth = dual< ( that: Sink.Sink, options?: { readonly capacity?: number | undefined } ) => ( self: Sink.Sink ) => Sink.Sink, In & In1, L1 | L, E1 | E, R1 | R>, ( self: Sink.Sink, that: Sink.Sink, options?: { readonly capacity?: number | undefined } ) => Sink.Sink, In & In1, L1 | L, E1 | E, R1 | R> >( (args) => isSink(args[1]), (self, that, options) => raceWith(self, { other: that, onSelfDone: (selfDone) => mergeDecision.Done(Effect.map(selfDone, Either.left)), onOtherDone: (thatDone) => mergeDecision.Done(Effect.map(thatDone, Either.right)), capacity: options?.capacity ?? 16 }) ) /** @internal */ export const raceWith = dual< ( options: { readonly other: Sink.Sink readonly onSelfDone: (exit: Exit.Exit) => MergeDecision.MergeDecision readonly onOtherDone: (exit: Exit.Exit) => MergeDecision.MergeDecision readonly capacity?: number | undefined } ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, options: { readonly other: Sink.Sink readonly onSelfDone: (exit: Exit.Exit) => MergeDecision.MergeDecision readonly onOtherDone: (exit: Exit.Exit) => MergeDecision.MergeDecision readonly capacity?: number | undefined } ) => Sink.Sink >( 2, ( self: Sink.Sink, options: { readonly other: Sink.Sink readonly onSelfDone: (exit: Exit.Exit) => MergeDecision.MergeDecision readonly onOtherDone: (exit: Exit.Exit) => MergeDecision.MergeDecision readonly capacity?: number | undefined } ): Sink.Sink => { function race(scope: Scope.Scope) { return Effect.gen(function*() { const pubsub = yield* PubSub.bounded< Either.Either, Exit.Exit> >(options?.capacity ?? 16) const subscription1 = yield* Scope.extend(PubSub.subscribe(pubsub), scope) const subscription2 = yield* Scope.extend(PubSub.subscribe(pubsub), scope) const reader = channel.toPubSub(pubsub) const writer = channel.fromQueue(subscription1).pipe( core.pipeTo(toChannel(self)), channel.zipLeft(core.fromEffect(Queue.shutdown(subscription1))), channel.mergeWith({ other: channel.fromQueue(subscription2).pipe( core.pipeTo(toChannel(options.other)), channel.zipLeft(core.fromEffect(Queue.shutdown(subscription2))) ), onSelfDone: options.onSelfDone, onOtherDone: options.onOtherDone }) ) const racedChannel = channel.mergeWith(reader, { other: writer, onSelfDone: () => mergeDecision.Await(identity), onOtherDone: (exit) => mergeDecision.Done(exit) }) as Channel.Channel< Chunk.Chunk, Chunk.Chunk, E | E2, never, A3 | A4, unknown, R | R2 > return new SinkImpl(racedChannel) }) } return unwrapScopedWith(race) } ) /** @internal */ export const refineOrDie = dual< ( pf: (error: E) => Option.Option ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, pf: (error: E) => Option.Option ) => Sink.Sink >( 2, (self, pf) => pipe(self, refineOrDieWith(pf, identity)) ) /** @internal */ export const refineOrDieWith = dual< ( pf: (error: E) => Option.Option, f: (error: E) => unknown ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, pf: (error: E) => Option.Option, f: (error: E) => unknown ) => Sink.Sink >( 3, (self, pf, f) => { const newChannel = pipe( self, toChannel, channel.catchAll((error) => Option.match(pf(error), { onNone: () => core.failCauseSync(() => Cause.die(f(error))), onSome: core.fail }) ) ) return new SinkImpl(newChannel) } ) /** @internal */ export const service = ( tag: Context.Tag ): Sink.Sink => serviceWith(tag, identity) /** @internal */ export const serviceWith = ( tag: Context.Tag, f: (service: Types.NoInfer) => Z ): Sink.Sink => fromEffect(Effect.map(tag, f)) /** @internal */ export const serviceWithEffect = ( tag: Context.Tag, f: (service: Types.NoInfer) => Effect.Effect ): Sink.Sink => fromEffect(Effect.flatMap(tag, f)) /** @internal */ export const serviceWithSink = ( tag: Context.Tag, f: (service: Types.NoInfer) => Sink.Sink ): Sink.Sink => new SinkImpl(pipe(Effect.map(tag, (service) => toChannel(f(service))), channel.unwrap)) /** @internal */ export const some = (predicate: Predicate): Sink.Sink => fold(false, (bool) => !bool, (acc, input) => acc || predicate(input)) /** @internal */ export const splitWhere = dual< (f: Predicate) => (self: Sink.Sink) => Sink.Sink, (self: Sink.Sink, f: Predicate) => Sink.Sink >(2, (self: Sink.Sink, f: Predicate): Sink.Sink => { const newChannel = pipe( core.fromEffect(Ref.make(Chunk.empty())), core.flatMap((ref) => pipe( splitWhereSplitter(false, ref, f), channel.pipeToOrFail(toChannel(self)), core.collectElements, core.flatMap(([leftovers, z]) => pipe( core.fromEffect(Ref.get(ref)), core.flatMap((leftover) => pipe( core.write>(pipe(leftover, Chunk.appendAll(Chunk.flatten(leftovers)))), channel.zipRight(core.succeed(z)) ) ) ) ) ) ) ) return new SinkImpl(newChannel) }) /** @internal */ const splitWhereSplitter = ( written: boolean, leftovers: Ref.Ref>, f: Predicate ): Channel.Channel, Chunk.Chunk, E, never, unknown, unknown> => core.readWithCause({ onInput: (input) => { if (Chunk.isEmpty(input)) { return splitWhereSplitter(written, leftovers, f) } if (written) { const index = indexWhere(input, f) if (index === -1) { return channel.zipRight( core.write(input), splitWhereSplitter(true, leftovers, f) ) } const [left, right] = Chunk.splitAt(input, index) return channel.zipRight( core.write(left), core.fromEffect(Ref.set(leftovers, right)) ) } const index = indexWhere(input, f, 1) if (index === -1) { return channel.zipRight( core.write(input), splitWhereSplitter(true, leftovers, f) ) } const [left, right] = pipe(input, Chunk.splitAt(Math.max(index, 1))) return channel.zipRight(core.write(left), core.fromEffect(Ref.set(leftovers, right))) }, onFailure: core.failCause, onDone: core.succeed }) /** @internal */ const indexWhere = (self: Chunk.Chunk, predicate: Predicate, from = 0): number => { const iterator = self[Symbol.iterator]() let index = 0 let result = -1 let next: IteratorResult while (result < 0 && (next = iterator.next()) && !next.done) { const a = next.value if (index >= from && predicate(a)) { result = index } index = index + 1 } return result } /** @internal */ export const succeed = (a: A): Sink.Sink => new SinkImpl(core.succeed(a)) /** @internal */ export const sum: Sink.Sink = foldLeftChunks( 0, (acc, chunk) => acc + Chunk.reduce(chunk, 0, (s, a) => s + a) ) /** @internal */ export const summarized = dual< ( summary: Effect.Effect, f: (start: A2, end: A2) => A3 ) => (self: Sink.Sink) => Sink.Sink<[A, A3], In, L, E2 | E, R2 | R>, ( self: Sink.Sink, summary: Effect.Effect, f: (start: A2, end: A2) => A3 ) => Sink.Sink<[A, A3], In, L, E2 | E, R2 | R> >( 3, (self, summary, f) => { const newChannel = pipe( core.fromEffect(summary), core.flatMap((start) => pipe( self, toChannel, core.flatMap((done) => pipe( core.fromEffect(summary), channel.map((end) => [done, f(start, end)]) ) ) ) ) ) return new SinkImpl(newChannel) } ) /** @internal */ export const sync = (evaluate: LazyArg): Sink.Sink => new SinkImpl(core.sync(evaluate)) /** @internal */ export const take = (n: number): Sink.Sink, In, In> => pipe( foldChunks, In>( Chunk.empty(), (chunk) => chunk.length < n, (acc, chunk) => pipe(acc, Chunk.appendAll(chunk)) ), flatMap((acc) => { const [taken, leftover] = pipe(acc, Chunk.splitAt(n)) return new SinkImpl(pipe(core.write(leftover), channel.zipRight(core.succeedNow(taken)))) }) ) /** @internal */ export const toChannel = ( self: Sink.Sink ): Channel.Channel, Chunk.Chunk, E, never, A, unknown, R> => Effect.isEffect(self) ? toChannel(fromEffect(self as Effect.Effect)) : (self as SinkImpl).channel /** @internal */ export const unwrap = ( effect: Effect.Effect, E, R> ): Sink.Sink => new SinkImpl( channel.unwrap(pipe(effect, Effect.map((sink) => toChannel(sink)))) ) /** @internal */ export const unwrapScoped = ( effect: Effect.Effect, E, R> ): Sink.Sink> => new SinkImpl( channel.unwrapScoped(effect.pipe( Effect.map((sink) => toChannel(sink)) )) ) /** @internal */ export const unwrapScopedWith = ( f: (scope: Scope.Scope) => Effect.Effect, E, R> ): Sink.Sink => new SinkImpl( channel.unwrapScopedWith((scope) => f(scope).pipe( Effect.map((sink) => toChannel(sink)) ) ) ) /** @internal */ export const withDuration = ( self: Sink.Sink ): Sink.Sink<[A, Duration.Duration], In, L, E, R> => pipe(self, summarized(Clock.currentTimeMillis, (start, end) => Duration.millis(end - start))) /** @internal */ export const zip = dual< ( that: Sink.Sink, options?: { readonly concurrent?: boolean | undefined } ) => (self: Sink.Sink) => Sink.Sink<[A, A2], In & In2, L | L2, E2 | E, R2 | R>, ( self: Sink.Sink, that: Sink.Sink, options?: { readonly concurrent?: boolean | undefined } ) => Sink.Sink<[A, A2], In & In2, L | L2, E2 | E, R2 | R> >( (args) => isSink(args[1]), ( self: Sink.Sink, that: Sink.Sink, options?: { readonly concurrent?: boolean | undefined } ): Sink.Sink<[A, A2], In & In2, L | L2, E2 | E, R2 | R> => zipWith(self, that, (z, z2) => [z, z2], options) ) /** @internal */ export const zipLeft = dual< ( that: Sink.Sink, options?: { readonly concurrent?: boolean | undefined } ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, that: Sink.Sink, options?: { readonly concurrent?: boolean | undefined } ) => Sink.Sink >( (args) => isSink(args[1]), ( self: Sink.Sink, that: Sink.Sink, options?: { readonly concurrent?: boolean | undefined } ): Sink.Sink => zipWith(self, that, (z, _) => z, options) ) /** @internal */ export const zipRight = dual< ( that: Sink.Sink, options?: { readonly concurrent?: boolean | undefined } ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, that: Sink.Sink, options?: { readonly concurrent?: boolean | undefined } ) => Sink.Sink >( (args) => isSink(args[1]), ( self: Sink.Sink, that: Sink.Sink, options?: { readonly concurrent?: boolean | undefined } ): Sink.Sink => zipWith(self, that, (_, z2) => z2, options) ) /** @internal */ export const zipWith = dual< ( that: Sink.Sink, f: (a: A, a2: A2) => A3, options?: { readonly concurrent?: boolean | undefined } ) => (self: Sink.Sink) => Sink.Sink, ( self: Sink.Sink, that: Sink.Sink, f: (a: A, a2: A2) => A3, options?: { readonly concurrent?: boolean | undefined } ) => Sink.Sink >( (args) => isSink(args[1]), ( self: Sink.Sink, that: Sink.Sink, f: (a: A, a2: A2) => A3, options?: { readonly concurrent?: boolean | undefined } ): Sink.Sink => options?.concurrent ? raceWith(self, { other: that, onSelfDone: Exit.match({ onFailure: (cause) => mergeDecision.Done(Effect.failCause(cause)), onSuccess: (leftZ) => mergeDecision.Await( Exit.match({ onFailure: Effect.failCause, onSuccess: (rightZ) => Effect.succeed(f(leftZ, rightZ)) }) ) }), onOtherDone: Exit.match({ onFailure: (cause) => mergeDecision.Done(Effect.failCause(cause)), onSuccess: (rightZ) => mergeDecision.Await( Exit.match({ onFailure: Effect.failCause, onSuccess: (leftZ) => Effect.succeed(f(leftZ, rightZ)) }) ) }) }) : flatMap(self, (z) => map(that, (z2) => f(z, z2))) ) // Circular with Channel /** @internal */ export const channelToSink = ( self: Channel.Channel, Chunk.Chunk, OutErr, InErr, OutDone, unknown, Env> ): Sink.Sink => new SinkImpl(self) // Constants /** @internal */ export const count: Sink.Sink = foldLeftChunks( 0, (acc, chunk) => acc + chunk.length ) /** @internal */ export const mkString: Sink.Sink = suspend(() => { const strings: Array = [] return pipe( foldLeftChunks(void 0, (_, elems) => Chunk.map(elems, (elem) => { strings.push(String(elem)) })), map(() => strings.join("")) ) }) /** @internal */ export const timed: Sink.Sink = pipe( withDuration(drain), map((tuple) => tuple[1]) )