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

Function invokeWithInterrupt

packages/effect/src/internal/fiberRuntime.ts:3729–3796  ·  view source on GitHub ↗
(
  self: Effect.Effect<A, E, R>,
  entries: ReadonlyArray<Entry<unknown>>,
  onInterrupt?: () => void
)

Source from the content-addressed store, hash-verified

3727 entries: ReadonlyArray<Entry<unknown>>,
3728 onInterrupt?: () => void
3729) => Effect.Effect<void, E, R> = <A, E, R>(
3730 self: Effect.Effect<A, E, R>,
3731 entries: ReadonlyArray<Entry<unknown>>,
3732 onInterrupt?: () => void
3733) =>
3734 core.fiberIdWith((id) =>
3735 ensuring(
3736 core.flatMap(
3737 forkDaemon(core.interruptible(self)),
3738 (processing) =>
3739 core.async<void, E>((cb) => {
3740 const counts = entries.map((_) => _.listeners.count)
3741 const checkDone = () => {
3742 if (counts.every((count) => count === 0)) {
3743 if (
3744 entries.every((_) => {
3745 if (_.result.state.current._tag === "Pending") {
3746 return true
3747 } else if (
3748 _.result.state.current._tag === "Done" &&
3749 core.exitIsExit(_.result.state.current.effect) &&
3750 _.result.state.current.effect._tag === "Failure" &&
3751 internalCause.isInterrupted(_.result.state.current.effect.cause)
3752 ) {
3753 return true
3754 } else {
3755 return false
3756 }
3757 })
3758 ) {
3759 cleanup.forEach((f) => f())
3760 onInterrupt?.()
3761 cb(core.interruptFiber(processing))
3762 }
3763 }
3764 }
3765 processing.addObserver((exit) => {
3766 cleanup.forEach((f) => f())
3767 cb(exit)
3768 })
3769 const cleanup = entries.map((r, i) => {
3770 const observer = (count: number) => {
3771 counts[i] = count
3772 checkDone()
3773 }
3774 r.listeners.addObserver(observer)
3775 return () => r.listeners.removeObserver(observer)
3776 })
3777 checkDone()
3778 return core.sync(() => {
3779 cleanup.forEach((f) => f())
3780 })
3781 })
3782 ),
3783 core.suspend(() => {
3784 const residual = entries.flatMap((entry) => {
3785 if (!entry.state.completed) {
3786 return [entry]

Callers 3

runBlockedRequestsFunction · 0.85
fiberRuntime.tsFile · 0.85
makeBatchedFunction · 0.85

Calls 8

checkDoneFunction · 0.85
syncMethod · 0.80
forkDaemonFunction · 0.70
completeFunction · 0.70
mapMethod · 0.65
addObserverMethod · 0.65
removeObserverMethod · 0.65
fFunction · 0.50

Tested by

no test coverage detected