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

Function intoResult

packages/effect/src/unstable/workflow/Workflow.ts:656–710  ·  view source on GitHub ↗
(
  effect: Effect.Effect<A, E, R>
)

Source from the content-addressed store, hash-verified

654 * @since 4.0.0
655 */
656export const intoResult = <A, E, R>(
657 effect: Effect.Effect<A, E, R>
658): Effect.Effect<
659 Result<A, E>,
660 never,
661 Exclude<R, Scope.Scope> | WorkflowInstance
662> =>
663 Effect.contextWith((context: Context.Context<WorkflowInstance>) => {
664 const instance = Context.get(context, InstanceTag)
665 const captureDefects = Context.get(instance.workflow.annotations, CaptureDefects)
666 const suspendOnFailure = Context.get(instance.workflow.annotations, SuspendOnFailure)
667 return effect.pipe(
668 // so we can use external interruption to suspend the workflow
669 Effect.forkChild({ startImmediately: true }),
670 Effect.flatMap((fiber) => Effect.onInterrupt(Fiber.join(fiber), () => Fiber.interrupt(fiber))),
671 Effect.interruptible,
672 suspendOnFailure
673 ? Effect.catchCause((cause) => {
674 instance.suspended = true
675 if (!Cause.hasInterruptsOnly(cause)) {
676 instance.cause = Cause.die(Cause.squash(cause))
677 }
678 return Effect.interrupt
679 })
680 : identity,
681 Effect.scoped,
682 Effect.matchCauseEffect({
683 onSuccess: (value) => Effect.succeed(new Complete({ exit: Exit.succeed(value) })),
684 onFailure: (cause): Effect.Effect<Result<A, E>> => {
685 const [reasons, interrupts] = Arr.partition(
686 cause.reasons,
687 Filter.fromPredicate(Cause.isInterruptReason)
688 )
689 const hasInterruptsOnly = interrupts.length === cause.reasons.length
690 const filtered = reasons.length === 0 ? cause : Cause.fromReasons(reasons)
691 return instance.suspended && hasInterruptsOnly
692 ? Effect.succeed(new Suspended({ cause: instance.cause }))
693 : (!instance.interrupted && hasInterruptsOnly) ||
694 (!captureDefects && Cause.hasDies(cause))
695 ? Effect.failCause(filtered as Cause.Cause<never>)
696 : Effect.succeed(new Complete({ exit: Exit.failCause(filtered) }))
697 }
698 }),
699 (eff) =>
700 Effect.onExitPrimitive(eff, (exit) => {
701 if (Exit.isFailure(exit)) {
702 return Scope.close(instance.scope, exit)
703 } else if (exit.value._tag === "Complete") {
704 return Scope.close(instance.scope, exit.value.exit)
705 }
706 return Effect.void
707 }, true),
708 Effect.uninterruptible
709 )
710 })
711
712/**
713 * Wraps an activity-like effect so workflow suspension waits for currently

Callers

nothing calls this directly

Calls 7

onInterruptMethod · 0.80
joinMethod · 0.80
getMethod · 0.65
pipeMethod · 0.65
closeMethod · 0.65
interruptMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected