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

Function addFinalizer

packages/effect/src/internal/fiberRuntime.ts:1710–1749  ·  view source on GitHub ↗
(
  finalizer: (exit: Exit.Exit<unknown, unknown>) => Effect.Effect<X, never, R>
)

Source from the content-addressed store, hash-verified

1708
1709/* @internal */
1710export const addFinalizer = <X, R>(
1711 finalizer: (exit: Exit.Exit<unknown, unknown>) => Effect.Effect<X, never, R>
1712): Effect.Effect<void, never, R | Scope.Scope> =>
1713 core.withFiberRuntime(
1714 (runtime) => {
1715 const acquireRefs = runtime.getFiberRefs()
1716 const acquireFlags = runtimeFlags_.disable(runtime.currentRuntimeFlags, runtimeFlags_.Interruption)
1717 return core.flatMap(scope, (scope) =>
1718 core.scopeAddFinalizerExit(scope, (exit) =>
1719 core.withFiberRuntime((runtimeFinalizer) => {
1720 const preRefs = runtimeFinalizer.getFiberRefs()
1721 const preFlags = runtimeFinalizer.currentRuntimeFlags
1722 const interruptedCause = preRefs.locals.get(core.currentInterruptedCause)
1723 const locals = new Map(acquireRefs.locals)
1724 if (interruptedCause === undefined) {
1725 locals.delete(core.currentInterruptedCause)
1726 } else {
1727 locals.set(core.currentInterruptedCause, interruptedCause)
1728 }
1729 const refs = fiberRefs.unsafeMake(locals)
1730 const patchFlags = runtimeFlags_.diff(preFlags, acquireFlags)
1731 const inverseRefs = FiberRefsPatch.diff(refs, preRefs)
1732 runtimeFinalizer.setFiberRefs(refs)
1733
1734 return ensuring(
1735 core.withRuntimeFlags(
1736 core.suspend(() =>
1737 finalizer(exit) as Effect.Effect<X>
1738 ),
1739 patchFlags
1740 ),
1741 core.sync(() => {
1742 runtimeFinalizer.setFiberRefs(
1743 FiberRefsPatch.patch(inverseRefs, runtimeFinalizer.id(), runtimeFinalizer.getFiberRefs())
1744 )
1745 })
1746 )
1747 })))
1748 }
1749 )
1750
1751/* @internal */
1752export const daemonChildren = <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> => {

Callers 2

fiberRuntime.tsFile · 0.70
withRuntimeFlagsScopedFunction · 0.70

Calls 10

finFunction · 0.85
setFiberRefsMethod · 0.80
syncMethod · 0.80
finalizerFunction · 0.70
getFiberRefsMethod · 0.65
getMethod · 0.65
setMethod · 0.65
diffMethod · 0.65
patchMethod · 0.65
idMethod · 0.65

Tested by

no test coverage detected