API Reference
This page provides a detailed reference for the public API of the Anchor library.
Core API
Anchor<R, S, Err>
The main abstract class for state management. R is the Effect type, S is the ViewState type, and Err is the domain error type (use Nothing when no domain errors are needed).
state: S: The current state.reduce(reducer: S.() -> S): Updates the state.effect(coroutineContext, block: suspend R.() -> T): T: Executes a side effect.cancellable(key, block): Executes a block that can be cancelled by a key.post(block: SignalScope.() -> Signal): Posts a signal to the UI.emit(block: SubscriptionScope.() -> Event): Emits an internal event.
PureAnchor<R, S>
A convenience typealias for Anchor<R, S, Nothing> — use when your anchor has no domain errors.
RememberAnchorScope.create(...)
Creates an Anchor instance.
initialState: Factory for the initial state.effectScope: Factory for effect dependencies.init: Optional initialization block.subscriptions: Optional subscription setup block.onDomainError: Optional callback invoked when a domain error is raised (defaults tonull).defect: Optional callback invoked when an unexpected error occurs (defaults tonull).
Compose API (Android)
RememberAnchor(scope, customKey, content)
Sets up Anchor in a Composable.
scope: Factory block to create the Anchor.customKey: Optional key for state retention.content: Composable block receivingAnchorStateScope.
AnchorStateScope<S>
Scope available inside RememberAnchor.
state: Accesses the current state (triggers recomposition for any change).collectState(selector: (S) -> T): T: Observes a specific part of the state (granular recomposition).
anchor(block)
Helper to create UI action callbacks.
anchor(suspend A.() -> Unit): () -> Unitanchor(suspend A.(I) -> Unit): (I) -> Unitanchor(suspend A.(I, O) -> Unit): (I, O) -> Unit
HandleSignal<T>(block)
Handles one-time signals of type T.
PreviewAnchor(state, content)
A utility for Compose Previews.
Error Handling API
See the Error Handling guide for a full walkthrough with examples. The key types are summarised below.
Raise<Err>
Interface mixed into Anchor<R, S, Err>. Provides:
raise(error: Err): Nothing— short-circuits the current action; control passes toonDomainError.ensure(condition: Boolean, error: () -> Err)— shorthand forif (!condition) raise(error()).Recover<Err, T>.getOrRaise(): T— unwraps aRecover.Okor re-raises itsRecover.Error.
Recover<Err, T>
Sealed return type for the recover { } block:
Recover.Ok(value: T)— the block completed without raising.Recover.Error(error: Err)— the block calledraise().
Helpers: getOrNull(), getErrorOrNull(), getOrElse { }, fold(onError, onOk).
recover { } (standalone)
val result: Recover<MyError, String> = recover {
ensure(input.isNotBlank()) { MyError.Empty }
input.trim()
}
Catches any raise() inside the block and returns a Recover instead of propagating to onDomainError.
ErrorScope<R, S>
Typealias for BaseAnchorScope<R, S>. Used as the receiver in onDomainError and defect handlers. Provides reduce, effect, post, and emit — intentionally omits raise and orDie to prevent re-raising from handlers.
DefectAnchor<Err> / orDie(error)
orDie(error: Err): Nothing— escalates a domain error to thedefecthandler, bypassingonDomainError. Use for programmer errors and broken invariants, not user-facing validation.