MCPcopy Create free account
hub / github.com/Effect-TS/effect / asyncEffect

Function asyncEffect

packages/effect/src/internal/stream.ts:542–597  ·  view source on GitHub ↗
(
  register: (emit: Emit.Emit<R, E, A, void>) => Effect.Effect<unknown, E, R>,
  bufferSize?: number | "unbounded" | {
    readonly bufferSize?: number | undefined
    readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
  } | undefined
)

Source from the content-addressed store, hash-verified

540
541/** @internal */
542export const asyncEffect = <A, E = never, R = never>(
543 register: (emit: Emit.Emit<R, E, A, void>) => Effect.Effect<unknown, E, R>,
544 bufferSize?: number | "unbounded" | {
545 readonly bufferSize?: number | undefined
546 readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
547 } | undefined
548): Stream.Stream<A, E, R> =>
549 pipe(
550 Effect.acquireRelease(
551 queueFromBufferOptions<A, E>(bufferSize),
552 (queue) => Queue.shutdown(queue)
553 ),
554 Effect.flatMap((output) =>
555 pipe(
556 Effect.runtime<R>(),
557 Effect.flatMap((runtime) =>
558 pipe(
559 register(
560 emit.make((k) =>
561 pipe(
562 InternalTake.fromPull(k),
563 Effect.flatMap((take) => Queue.offer(output, take)),
564 Effect.asVoid,
565 Runtime.runPromiseExit(runtime)
566 ).then((exit) => {
567 if (Exit.isFailure(exit)) {
568 if (!Cause.isInterrupted(exit.cause)) {
569 throw Cause.squash(exit.cause)
570 }
571 }
572 })
573 )
574 ),
575 Effect.map(() => {
576 const loop: Channel.Channel<Chunk.Chunk<A>, unknown, E, unknown, void, unknown> = pipe(
577 Queue.take(output),
578 Effect.flatMap(InternalTake.done),
579 Effect.match({
580 onFailure: (maybeError) =>
581 pipe(
582 core.fromEffect(Queue.shutdown(output)),
583 channel.zipRight(Option.match(maybeError, { onNone: () => core.void, onSome: core.fail }))
584 ),
585 onSuccess: (chunk) => pipe(core.write(chunk), core.flatMap(() => loop))
586 }),
587 channel.unwrap
588 )
589 return loop
590 })
591 )
592 )
593 )
594 ),
595 channel.unwrapScoped,
596 fromChannel
597 )
598
599const mailboxFromBufferOptionsPush = <A, E>(

Callers

nothing calls this directly

Calls 12

queueFromBufferOptionsFunction · 0.85
runtimeMethod · 0.80
isInterruptedMethod · 0.80
fromEffectMethod · 0.80
pipeFunction · 0.70
registerFunction · 0.70
makeMethod · 0.65
offerMethod · 0.65
mapMethod · 0.65
takeMethod · 0.65
writeMethod · 0.65
shutdownMethod · 0.45

Tested by

no test coverage detected