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

Function asyncScoped

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

Source from the content-addressed store, hash-verified

632
633/** @internal */
634export const asyncScoped = <A, E = never, R = never>(
635 register: (emit: Emit.Emit<R, E, A, void>) => Effect.Effect<unknown, E, R | Scope.Scope>,
636 bufferSize?: number | "unbounded" | {
637 readonly bufferSize?: number | undefined
638 readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
639 } | undefined
640): Stream.Stream<A, E, Exclude<R, Scope.Scope>> =>
641 pipe(
642 Effect.acquireRelease(
643 queueFromBufferOptions<A, E>(bufferSize),
644 (queue) => Queue.shutdown(queue)
645 ),
646 Effect.flatMap((output) =>
647 pipe(
648 Effect.runtime<R>(),
649 Effect.flatMap((runtime) =>
650 pipe(
651 register(
652 emit.make((k) =>
653 pipe(
654 InternalTake.fromPull(k),
655 Effect.flatMap((take) => Queue.offer(output, take)),
656 Effect.asVoid,
657 Runtime.runPromiseExit(runtime)
658 ).then((exit) => {
659 if (Exit.isFailure(exit)) {
660 if (!Cause.isInterrupted(exit.cause)) {
661 throw Cause.squash(exit.cause)
662 }
663 }
664 })
665 )
666 ),
667 Effect.zipRight(Ref.make(false)),
668 Effect.flatMap((ref) =>
669 pipe(
670 Ref.get(ref),
671 Effect.map((isDone) =>
672 isDone ?
673 pull.end() :
674 pipe(
675 Queue.take(output),
676 Effect.flatMap(InternalTake.done),
677 Effect.onError(() =>
678 pipe(
679 Ref.set(ref, true),
680 Effect.zipRight(Queue.shutdown(output))
681 )
682 )
683 )
684 )
685 )
686 )
687 )
688 )
689 )
690 ),
691 scoped,

Callers

nothing calls this directly

Calls 14

queueFromBufferOptionsFunction · 0.85
runtimeMethod · 0.80
isInterruptedMethod · 0.80
onErrorMethod · 0.80
pipeFunction · 0.70
registerFunction · 0.70
makeMethod · 0.65
offerMethod · 0.65
getMethod · 0.65
mapMethod · 0.65
endMethod · 0.65
takeMethod · 0.65

Tested by

no test coverage detected