Lifetimes
Every derived or side-effecting node (Memo, MemoTable, Effect, Boundary, AsyncMemo, StreamMemo, Timed, Projection, Lookup) attaches at construction to Graph.CurrentOwner. Disposing a Graph tears down graph.Root and every child scope under it in time.
Root input cells — Signal<'T>, ReadOnlySignal<'T>, SignalPort<'T>, and AsyncSource<'T> — hold no Owner and are collected by the GC once unreferenced.
Activating a graph
Scope helpers (root, scope { ... }, cleanup, getOwner ()) resolve Graph.Current, whereas source-bound derivations (memo, effect, boundary, asyncMemo, stream) infer source.Graphdirectly from their Phase-1 let! / and! inputs and attach to source.Graph.CurrentOwner.
See The graph for full details on:
- Configuring
GraphOptions(Equality,ThreadAffinity,FlightPolicy,Dispatcher,Journal). - Activating
Graph.Currentwithgraph { ... },use _ = graph.Activate (),graph.Run, and automatic hosting inside turns. - Graph inference rules and single-graph / threading requirements.
Roots and owners
Create a disposable child Owner scope in inside an active graph with root { ... } (createRoot):
Unit body returns
Owner: aunitbody returns the newly createdOwnerdirectly (use scope = root { ... }).Custom return value (
root.owner): bindlet! o = root.ownerand end withreturn (o, result)when setup returns domain handles alongside the scope.Re-entering a captured owner (
scope { ... }): anyscope: Owneris itself a0 Bcomputation expression builder (scope { ... }, wrappingrunWithOwner scope) that attaches new nodes or cleanups toscope. BecauseOwnerstores no back-reference toGraph, ensure its graph is active when re-entering outside a turn (graph { scope { ... } }).Inspecting the current owner: call
getOwner ()inside an owning scope, ortryGetOwner ()(Owner voption) when helper code may also run inside a pure memo (wheretryGetOwner ()returnsValueNonewithout throwing).Attaching external disposables: call
scope.Attach disposableto dispose anyIDisposableresource automatically withscope.
open Ranvier
open Ranvier.CE
let mountPage (graph: Graph) (route: Signal<string>) =
graph {
return
root {
let! owner = root.owner
let title = memo { let! r = route in return $"Page: {r}" }
effect { let! t = title in printfn "%s" t }
return owner, title
}
}
Disposing an owner
Disposing an Owner tears down its entire subtree in time, unlinking every upstream observer edge1 so subsequent signal writes never wake disposed effects.2 Disposal is idempotent:3
Example code
let count = signal { return 0 }
let mutable runs = 0
let scope =
root {
effect {
let! n = count
runs <- runs + 1
printfn "count = %d" n
}
}
controls [
button "Write 1" (fun () -> count.Value <- 1)
|> describe "The root's effect responds while its owner is alive."
|> expect "Effect runs before disposal" (fun () -> runs = 2)
button "Dispose root" (fun () -> scope.Dispose ())
|> describe "Disposal detaches the effect from count."
|> expect "Disposal does not re-run the effect" (fun () -> runs = 2)
button "Write 2" (fun () -> count.Value <- 2)
|> describe "count updates, while the disposed effect stays silent."
|> expect "Disposed effect never runs again" (fun () -> runs = 2 && count.Peek = 2)
]
A disposed Memo<'T> detaches its upstream sources and stops recomputing, while answering reads with its last cached value.4 When cached payloads themselves must be freed on eviction or disposal, use a MemoTable<'K, 'V>.
Cleanup
Register teardown logic on the innermost active scope with cleanup { ... }(onCleanup f) in .
Inside an effect, boundary, or memo { owning; ... }, each registered cleanup runs untracked before the next re-run and on scope disposal, executing at most once:5
Example code
let user = signal { return "Ada" }
let mutable released = []
let scope =
root {
effect {
let! name = user
printfn "subscribe %s" name
cleanup {
released <- released @ [ name ]
printfn "unsubscribe %s" name
}
}
}
controls [
button "Switch to Grace" (fun () -> user.Value <- "Grace")
|> describe "Ada's cleanup runs before subscribing to Grace."
|> expect "Previous subscription released" (fun () -> released = [ "Ada" ])
button "Dispose" (fun () -> scope.Dispose ())
|> describe "Disposal releases Grace and detaches the effect."
|> expect "Final subscription released" (fun () -> released = [ "Ada"; "Grace" ])
]
Ordering, purity, and error containment
LIFO interleaving: cleanups and child scopes share one reverse-acquisition stack:6 a cleanup registered after a child node runs before that child is disposed.
Pure scope guard: calling
cleanupinside a purememoraisesInvalidOperationException.7 Add theowningheader (memo { owning; ... }) when a derived computation owns resources.Fault isolation: a throwing cleanup records its exception in
scope.Errors(orgraph.Root.Errorsfor computation scopes; inspect new entries viascope.ErrorCountandscope.ErrorsFrom idx) while remaining cleanups still run.8Writes during cleanup: signal writes inside a cleanup are visible to the incoming re-run without scheduling a duplicate run.9
See Benchmarks → Lifetimes for creation and teardown instruction counts.