variable / @typed/fx/Fx

onInterrupt

Runs a finalizer when the Fx reports or externally receives interruption.

Package version
2.0.0-beta.7
Category
Resource lifetime
Since
1.0.0

Follow one execution

Runs a finalizer when the Fx reports or externally receives interruption.

Fx timelineonInterrupt forwards prior values and runs cancellation cleanup only for interruptiononInterrupt
OperatoronInterrupt(abort)
Read this diagram

Follow each lane from left to right. Events stacked vertically share a tick; the green cursor marks the current time across every lane.

  • A value
    The text inside the pill is the emitted value.
  • Work starts
    The raised chevron starts an inner run (^ in the source).
  • The run returns
    The vertical bar ends this lane’s run.
  • A cause is delivered
    The exclamation mark belongs to this lane.
  • Work is interrupted
    The cross marks cancellation of this run.
  • Current time
    The line and diamond move together across all lanes.
  • Happening now
    A highlighted event is at the current tick.
  • Still ahead
    Muted, dashed values have not happened yet.
  • Time continues
    The lane’s arrow is not a return marker. An empty stretch can be quiet work that is still running.

Illustrated ticks start at 0. At 1×, one illustrated tick takes one second; captions specify real durations when timing matters. A cause or interruption belongs to its lane, and other work may continue. Scroll horizontally to inspect the rest of a long timeline.

An interrupt-only Cause reported by the source invokes the finalizer before that Cause is delivered; finalizer failure is combined with it. External interruption of the running fiber also invokes the finalizer, suppressing its failure to preserve cancellation. Those paths are tracked separately: if an external interruption arrives while reported-Cause handling is still active, the finalizer can run once for each path. Successful and non-interrupt failures do not invoke it. A finalizer captured while constructing an Fx is shared by every run of that Fx; create mutable cleanup state lazily inside gen or genScoped when each subscription must own a distinct resource.

Source implementation · Learn the surrounding model

Compare all Fx timelines →

Import

import { onInterrupt } from "@typed/fx/Fx";

Signatures

export declare const onInterrupt: {
    <XE, XR>(finalizer: Effect.Effect<void, XE, XR> | ((interruptors: ReadonlySet<number>) => Effect.Effect<void, XE, XR>)): <A, E, R>(self: Fx<A, E, R>) => Fx<A, E | XE, R | XR>;
    <A, E, R, XE, XR>(self: Fx<A, E, R>, finalizer: Effect.Effect<void, XE, XR> | ((interruptors: ReadonlySet<number>) => Effect.Effect<void, XE, XR>)): Fx<A, E | XE, R | XR>;
};

Why

Cancellation-specific cleanup should not run for success or ordinary typed failure. The interruptor IDs retain Effect’s information about which fibers requested cancellation.

Ownership and lifetime

An interrupt-only Cause reported by the source invokes the finalizer before that Cause is delivered; finalizer failure is combined with it. External interruption of the running fiber also invokes the finalizer, suppressing its failure to preserve cancellation. Those paths are tracked separately: if an external interruption arrives while reported-Cause handling is still active, the finalizer can run once for each path. Successful and non-interrupt failures do not invoke it. A finalizer captured while constructing an Fx is shared by every run of that Fx; create mutable cleanup state lazily inside gen or genScoped when each subscription must own a distinct resource.

Examples

import { Effect } from "effect"
import { onInterrupt } from "@typed/fx/Fx"
import { interrupt } from "@typed/fx/Fx"

const observed = onInterrupt(
  interrupt(1),
  (ids) => Effect.log(`interrupted by ${ids.size}`)
)
Keep the finalizer idempotent when both interrupt paths are possible
```ts
import { Effect } from "effect"
import { gen } from "@typed/fx/Fx"
import { onInterrupt } from "@typed/fx/Fx"
import { interrupt } from "@typed/fx/Fx"

const observed = gen(function* () {
// `gen` creates this controller separately for every subscription.
const controller = yield* Effect.sync(() => new AbortController())
return onInterrupt(
interrupt(1),
Effect.sync(() => controller.abort()) // `abort` is idempotent.
)
})
```

Other public imports

These import paths expose the same declaration. Each page retains its own public name and signature.

Source