Skip to content

Glossary ​

The terms of @sekhmet/lifetime's ubiquitous language, each named the way the code names it. The concepts guide explains how they fit together. Terms Sekhmet's packages share, such as Panic, panic runtime and boundary, double fault, Outcome, Vow, Option, Result, Poll, enum and variant, Struct, and main and mini, live in the root glossary and aren't redefined here.

RAII's two edges ​

  • RAII: resource acquisition is initialization. Acquiring a resource makes a value, and the value's end releases it, so a resource can't outlive its owner or be released twice. The package splits it into a rising edge (Resource) and a falling edge (Lifetime).
  • Resource (Resource<I, T, C>): the rising edge, a recipe that pairs init with deinit. Each start acquires one value and hands back its lifetime.
  • Init (init(input, setup)): a resource's acquisition, sync or async, turning an input into a value. A throw or rejection is a panic, wrapped as Panic (InitFailed).
  • Deinit (deinit(value, cause)): a resource's release, deferred on the lifetime start makes, so it runs with the cause that lifetime ends with.
  • Start (resource.start({ input, panics, disposeCause? })): runs init in the panic boundary and returns a Vow of an Outcome: Ok of the new lifetime, or Panicked with init's panic, never Err. It settles at once when init is sync.
  • Setup (setup): the lifetime init receives for what it acquires along the way. It sits beneath deinit on the new lifetime's stack, so what init acquired outlives deinit.
  • Setup end (SetupEnd<C>): why a setup was torn down, its cause: Ended(cause) after a normal end, or InitPanicked(panic) when init panicked before the resource existed.
  • Setup teardown failure (SetupTeardownFailure, isSetupTeardownPanic): the Panic (SetupTeardownFailed) a start settles with when init panicked and setup's teardown failed too. Its cause holds init's panic and each of setup's cleanup panics; it carries them and is never reported itself.
  • Lifetime (Lifetime<T, C>): the falling edge. It owns an acquired value of type T and guarantees its release, ending with a cause of type C. It is invariant in C.

Phases and transitions ​

  • Phase (Phase<T, C>): where a lifetime is, read from its two signals, never kept by hand: Alive(value), Ending(cause) while its teardown runs, or Ended(report). There is no phase before Alive.
  • Cause (C): why a lifetime ends, the value end takes. Each cleanup receives it, and the first cause wins.
  • End (end(cause)): starts a lifetime's teardown with a cause and returns its ended signal. It is one-way, idempotent and memoised: a later call changes nothing and returns the same signal.
  • Ending (lifetime.ending): the DispatchSignal that dispatches the cause when the lifetime starts ending, before any cleanup runs. Its toAbortSignal() cancels platform work such as fetch. Its listeners are notifications, synchronous and never awaited.
  • Ended (lifetime.ended): the DispatchSignal that dispatches the end report once the teardown has finished.
  • End report (EndReport<C>): how a teardown went, a main Result: Ok(cause) when every cleanup succeeded, or Err of an end failure.
  • End failure (EndFailure<C>): why an end wasn't clean: the cause, and the panic of every cleanup that threw or rejected, in the order they ran. A child's failures are part of its parent's.
  • Value access (value(), with(fn)): the only ways to reach a lifetime's value, each handing out a main Option that is Some only while Alive. with keeps the value inside its callback.

The teardown stack ​

  • Teardown: a lifetime's release, the draining of its one stack in reverse acquisition order. An async entry is awaited before the next one runs.
  • Teardown stack: the one stack a lifetime's cleanups and adopted children share, pushed in acquisition order; the last acquired is the first released, like RAII.
  • Cleanup (Cleanup<C>): a function a lifetime runs with its cause as it ends, sync or async. A throw or rejection becomes a Panic (CleanupFailed) in the end report and never stops the others.
  • Defer (defer(cleanup)): registers a cleanup on the teardown stack and returns its registration.
  • Registration (Registration): what defer returns, saying when the cleanup will run: Registered(unregister) while the lifetime is alive, DuringEnd when it joins a teardown already running, or AfterEnd(vow) when it ran at once because the lifetime had ended, with a vow of how it ran: Ok, or Err of its panic.
  • Unregister (the Unsubscribe that Registered holds): removes a cleanup from the stack while the lifetime is alive, reporting whether it removed anything.
  • Waiter: the place a child that ended on its own keeps on its former parent's stack until its teardown finishes, so the parent's end still awaits it there. Once the child has finished, the waiter is gone.

Trees and ownership ​

  • Root: a lifetime with no owner. new Lifetime(...) makes one, and a child whose owner released it is a root again.
  • Owner (parent): the lifetime that holds a child on its teardown stack and ends it with its own cause. A lifetime has one owner at most.
  • Child: a lifetime on another's teardown stack, ended when its owner ends unless it ended first. parent.child({ value }) makes one; adopt takes one built separately.
  • Adopt (adopt({ child, fromParent? })): puts an existing lifetime on a parent's stack and returns an adoption.
  • Adoption (Adoption): what adopt returns: Adopted, or why not: AlreadyOwned, WouldCycle (the child is the parent or one of its owners), ChildEnded, or ParentEnded when the parent is ending or has ended and the child was ended at once.
  • Born dead: a child adopted into a parent that is ending or has ended. It is ended at once with the parent's cause, so it never leaks, and a parent still ending awaits it.
  • Cause mapping (fromParent: (cause: C) => D): maps a parent's cause into a child's own cause type, run only when the parent's end reaches the child, and for the parent's dispose cause. A throw is a Panic (CauseMappingFailed), and the parent releases the child as a root.
  • Release (of a child): what a parent does to a child whose cause mapping threw: it lets go of the child, which stays Alive as a root for another lifetime to adopt.
  • End when (endWhen(signal)): links a lifetime to an outside DispatchSignal, ending it with the signal's value as the cause. It returns an Unsubscribe, and the link is released when the lifetime ends.

using and await using ​

  • Dispose cause (disposeCause): the cause using and await using end a lifetime with. A child without one maps its owner's through fromParent, looked up when it is disposed; disposing a live lifetime with none is a Panic (NoDisposeCause).
  • The using boundary ([Symbol.dispose], [Symbol.asyncDispose]): the one place a failed end report becomes a throw: one panic as it is, several as a SuppressedError chain, the latest outermost. It throws only for a teardown it started itself. Sync using refuses an async cleanup with a Panic (AsyncCleanupUnderUsing).
  • Suppressed error chain (SuppressedError): how the using boundary throws several panics, as the explicit resource management spec chains disposal errors. Where the platform lacks the class, an Error of the same shape stands in, and no global is installed.