Skip to Content
CrowCrow

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, NotReady marks the enclosing Loading boundary. 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 userId changes 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 Errored boundary with a retry() 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:

  1. The write that triggered the reload is held in a provisional view alongside the pending resource.
  2. Any UI binding depending on the held state keeps displaying the previous committed output — zero half-updated frames, zero spinner flicker.
  3. profile.isPending is a Lane.Immediate signal, so you can show a subtle top progress bar or dim indicator immediately.
  4. Direct-manipulation inputs (Lane.Immediate, like typing in a search box) update right away without waiting on the network.
  5. 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 optimistic overlays 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?

On this page