GraphSession
Runs one thread of a graph for a screen: the run is a StateFlow of GraphSnapshots that a UI draws, and the screen starts, resumes and cancels it with plain function calls. It is the unidirectional data flow of an app (state down, events up) with a graph behind it.
class ApprovalViewModel(graph: CompiledGraph<Order>, checkpointer: Checkpointer<Order>) : ViewModel() {
val session = GraphSession(graph, GraphConfig("order-42", checkpointer, interruptBefore = setOf("pay")), viewModelScope)
init {
session.restore() // after the app was killed: back to where the run stopped
}
fun approve() = session.resume { it.copy(approved = true) }
}
// In a composable
when (val snapshot = viewModel.session.snapshot.collectAsState().value) {
is GraphSnapshot.Interrupted -> ApproveButton(snapshot.state, onClick = viewModel::approve)
...
}The session runs everything in scope, so the run ends with the scope, for example when a ViewModel is cleared. With a checkpointer in config, nothing is lost: restore in the next session finds the run where its last step left it, and resume continues it.
One command runs at a time. start, resume, restore and cancel stop what the session is doing before they begin, in the order they were called, and can be called from any thread. Each returns a Job that completes when its work is over, which a test can wait for.
A failure of the run does not reach scope: it becomes a GraphSnapshot.Stopped with the exception. An Error that is no failure of the code, such as an AssertionError of a test, fails the scope, as it fails CompiledGraph.invoke.
The nodes run in the coroutines of scope. A node that computes for long, rather than waiting for a model or the network, moves that work off the main thread with withContext.
Parameters
the graph to run.
the thread this session runs, and how. resume and restore need its GraphConfig.checkpointer.
where the session runs, such as viewModelScope.
Properties
Every GraphEvent of the runs of this session, as CompiledGraph.stream emits it: what nodes report with reportProgress, such as the text a model writes, retries and the events of subgraphs. An event reaches the collectors that are there when it happens, after snapshot has taken it in, and the run waits until they have it. Nothing is kept for a later collector.
Where the run stands, for a screen to draw. It starts as GraphSnapshot.Idle.
Functions
Stops the run. snapshot becomes GraphSnapshot.Stopped without a cause when a run was going on, and stays as it is otherwise. With a checkpointer, resume continues from the last step that finished.
Reads where the run of the thread stopped, without running anything, as CompiledGraph.lastResult does. Call it when a screen opens, for example after the operating system ended the app: snapshot is GraphSnapshot.Restoring until the checkpoint is read, and then GraphSnapshot.Idle when the thread has none, GraphSnapshot.Completed, or GraphSnapshot.Interrupted when resume can continue it. A run that the app lost in the middle of a step counts as interrupted before that step.
Continues the run of the thread from its last checkpoint, as CompiledGraph.streamResume does: after a GraphSnapshot.Interrupted, or after a GraphSnapshot.Stopped to try the failed step again.
Starts a new run of the thread with input, as CompiledGraph.stream does: an earlier run of the thread is replaced. snapshot is GraphSnapshot.Running with input at once.