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 pairsinitwithdeinit. Eachstartacquires 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 asPanic (InitFailed). - Deinit (
deinit(value, cause)): a resource's release, deferred on the lifetimestartmakes, so it runs with the cause that lifetime ends with. - Start (
resource.start({ input, panics, disposeCause? })): runsinitin the panic boundary and returns aVowof anOutcome:Okof the new lifetime, orPanickedwithinit's panic, neverErr. It settles at once wheninitis sync. - Setup (
setup): the lifetimeinitreceives for what it acquires along the way. It sits beneathdeiniton the new lifetime's stack, so whatinitacquired outlivesdeinit. - Setup end (
SetupEnd<C>): why a setup was torn down, its cause:Ended(cause)after a normal end, orInitPanicked(panic)wheninitpanicked before the resource existed. - Setup teardown failure (
SetupTeardownFailure,isSetupTeardownPanic): thePanic (SetupTeardownFailed)a start settles with wheninitpanicked and setup's teardown failed too. Its cause holdsinit'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 typeTand guarantees its release, ending with a cause of typeC. It is invariant inC.
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, orEnded(report). There is no phase beforeAlive. - Cause (
C): why a lifetime ends, the valueendtakes. Each cleanup receives it, and the first cause wins. - End (
end(cause)): starts a lifetime's teardown with a cause and returns itsendedsignal. It is one-way, idempotent and memoised: a later call changes nothing and returns the same signal. - Ending (
lifetime.ending): theDispatchSignalthat dispatches the cause when the lifetime starts ending, before any cleanup runs. ItstoAbortSignal()cancels platform work such asfetch. Its listeners are notifications, synchronous and never awaited. - Ended (
lifetime.ended): theDispatchSignalthat dispatches the end report once the teardown has finished. - End report (
EndReport<C>): how a teardown went, a mainResult:Ok(cause)when every cleanup succeeded, orErrof 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 mainOptionthat isSomeonly whileAlive.withkeeps 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 aPanic (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): whatdeferreturns, saying when the cleanup will run:Registered(unregister)while the lifetime is alive,DuringEndwhen it joins a teardown already running, orAfterEnd(vow)when it ran at once because the lifetime had ended, with a vow of how it ran:Ok, orErrof its panic. - Unregister (the
UnsubscribethatRegisteredholds): 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;adopttakes one built separately. - Adopt (
adopt({ child, fromParent? })): puts an existing lifetime on a parent's stack and returns an adoption. - Adoption (
Adoption): whatadoptreturns:Adopted, or why not:AlreadyOwned,WouldCycle(the child is the parent or one of its owners),ChildEnded, orParentEndedwhen 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 aPanic (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
Aliveas a root for another lifetime to adopt. - End when (
endWhen(signal)): links a lifetime to an outsideDispatchSignal, ending it with the signal's value as the cause. It returns anUnsubscribe, and the link is released when the lifetime ends.
using and await using
- Dispose cause (
disposeCause): the causeusingandawait usingend a lifetime with. A child without one maps its owner's throughfromParent, looked up when it is disposed; disposing a live lifetime with none is aPanic (NoDisposeCause). - The
usingboundary ([Symbol.dispose],[Symbol.asyncDispose]): the one place a failed end report becomes a throw: one panic as it is, several as aSuppressedErrorchain, the latest outermost. It throws only for a teardown it started itself. Syncusingrefuses an async cleanup with aPanic (AsyncCleanupUnderUsing). - Suppressed error chain (
SuppressedError): how theusingboundary throws several panics, as the explicit resource management spec chains disposal errors. Where the platform lacks the class, anErrorof the same shape stands in, and no global is installed.