Async, Resources & Held Transitions
First-class async state with resource(), Loading and Errored boundaries, two-view held transitions, and atomic optimistic actions.
Crow treats asynchronous data as a first-class graph primitive rather than a pile of ad-hoc Loading | Success | Error sealed classes in a ViewModel.
Resources & Boundaries
Declare an async computation tied to the active Owner with resource:
@Ui
fun UserProfile(userId: String) {
val profile = resource(key = { userId }) { id ->
api.fetchUser(id)
}
Errored(fallback = { error, retry ->
ErrorCard(message = error.message ?: "Failed", onRetry = { profile.reload(); retry() })
}) {
Loading(fallback = { ProfileSkeleton() }) {
// Reading `profile.value` in a live prop suspends Loading until the first value arrives
ProfileContent(profile.value, refreshing = profile.isPending)
}
}
}How Loading & Errored Work
- Cold load: When
profile()is read before its first value resolves,NotReadymarks the enclosingLoadingboundary. The content tree mounts into a hidden region while the fallback displays, then reveals atomically in a single frame once ready. - Stale-run cancellation: If
userIdchanges while a fetch is in flight, superseded runs are cancelled and ignored automatically. - Error recovery: Exceptions inside the resource or during build route to the nearest
Erroredboundary with aretry()callback.
Held Transitions (Two Consistent Views)
Once a resource has a value, changing its key does not flash back to the Loading skeleton!
Instead, Crow enters a held transition:
- The write that triggered the reload is held in a provisional view alongside the pending resource.
- Any UI binding depending on the held state keeps displaying the previous committed output — zero half-updated frames, zero spinner flicker.
profile.isPendingis aLane.Immediatesignal, so you can show a subtle top progress bar or dim indicator immediately.- Direct-manipulation inputs (
Lane.Immediate, like typing in a search box) update right away without waiting on the network. - When the resource (or a whole chain of dependent resources) settles, the entire graph commits in one atomic flush.
Actions & Optimistic UI
Mutations run inside action { ... }, which stages all signal writes transactionally until the coroutine completes:
val displayedLikes = optimistic(likedIds)
val toggleLike = action { postId: String ->
// Visible everywhere immediately while the request is in flight
displayedLikes.set(likedIds.value + postId)
val updated = api.likePost(postId)
// Staged write: applied atomically when the action completes,
// removing the optimistic overlay in the same batch (zero flicker).
likedIds.value = updated
}- Automatic rollback: If the network request throws or is cancelled, all staged writes and
optimisticoverlays vanish automatically. until { condition }: Suspends on the authoritative view (ignoring optimistic overlays and staged writes), so an optimistic update never falsely acknowledges itself.- Verified: Crow's async transition and action engine is verified against a naive reference model by the E3 fuzzer across 50,000+ randomized interleaving seeds.
For full caching, deduplication, pagination, and background revalidation, see crow-query.
How is this guide?
Inside the K2 Compiler
How crow-compiler lowers @Ui functions into LocalGraphs, fused modifier appliers, direct bindings, thunk tables, and zero-key saved() slots.
Saved State & @CrowApp
Zero-key process-death persistence with saved(), saved back stacks, and zero-boilerplate Android entry points with @CrowApp and ProcessHost.