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

Function _async

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

Source from the content-addressed store, hash-verified

483
484/** @internal */
485export const _async = <A, E = never, R = never>(
486 register: (
487 emit: Emit.Emit<R, E, A, void>
488 ) => Effect.Effect<void, never, R> | void,
489 bufferSize?: number | "unbounded" | {
490 readonly bufferSize?: number | undefined
491 readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
492 } | undefined
493): Stream.Stream<A, E, R> =>
494 Effect.acquireRelease(
495 queueFromBufferOptions<A, E>(bufferSize),
496 (queue) => Queue.shutdown(queue)
497 ).pipe(
498 Effect.flatMap((output) =>
499 Effect.runtime<R>().pipe(
500 Effect.flatMap((runtime) =>
501 Effect.sync(() => {
502 const runPromiseExit = Runtime.runPromiseExit(runtime)
503 const canceler = register(emit.make<R, E, A, void>((resume) =>
504 InternalTake.fromPull(resume).pipe(
505 Effect.flatMap((take) => Queue.offer(output, take)),
506 Effect.asVoid,
507 runPromiseExit
508 ).then((exit) => {
509 if (Exit.isFailure(exit)) {
510 if (!Cause.isInterrupted(exit.cause)) {
511 throw Cause.squash(exit.cause)
512 }
513 }
514 })
515 ))
516 return canceler
517 })
518 ),
519 Effect.map((value) => {
520 const loop: Channel.Channel<Chunk.Chunk<A>, unknown, E, unknown, void, unknown> = Queue.take(output).pipe(
521 Effect.flatMap((take) => InternalTake.done(take)),
522 Effect.match({
523 onFailure: (maybeError) =>
524 core.fromEffect(Queue.shutdown(output)).pipe(
525 channel.zipRight(Option.match(maybeError, {
526 onNone: () => core.void,
527 onSome: (error) => core.fail(error)
528 }))
529 ),
530 onSuccess: (chunk) => core.write(chunk).pipe(core.flatMap(() => loop))
531 }),
532 channel.unwrap
533 )
534 return fromChannel(loop).pipe(ensuring(value ?? Effect.void))
535 })
536 )
537 ),
538 unwrapScoped
539 )
540
541/** @internal */
542export const asyncEffect = <A, E = never, R = never>(

Callers

nothing calls this directly

Calls 15

queueFromBufferOptionsFunction · 0.85
runtimeMethod · 0.80
syncMethod · 0.80
isInterruptedMethod · 0.80
fromEffectMethod · 0.80
registerFunction · 0.70
fromChannelFunction · 0.70
pipeMethod · 0.65
makeMethod · 0.65
offerMethod · 0.65
mapMethod · 0.65
takeMethod · 0.65

Tested by

no test coverage detected