Skip to content
FerroUI/ docs

Search guides, controls, Rust APIs, and XAML.

    Keep the interface responsive

    Controls belong to the UI thread. Run expensive computation and blocking I/O away from that thread, then schedule the resulting UI updates through the dispatcher or the reactive UI scheduler.

    After ReactiveUI startup has installed the UI synchronization context, a command can await a task that returns ordinary data:

    use ferroui_system::{threading::tasks, ObservableError};
    async fn calculate_total(values: Vec<u64>) -> Result<u64, ObservableError> {
    tasks::run(move || Ok(values.into_iter().sum::<u64>())).await
    }

    The closure and its data must be Send; do not capture Ref<Control>, an Rc view model, or a UI service provider. Update the view model in the awaiting UI task after completion. This example demonstrates the boundary for native worker tasks; the tiny sum itself would not justify a worker in a real application.

    ferroui_system::task::spawn_local runs a future on the current thread. Similarly, ReactiveCommand::create_run_in_background uses the task-pool scheduler, which defaults to the current thread. Neither name guarantees a worker thread. Blocking work needs an actual worker facility such as tasks::run on supported native targets.

    Expose loading, success, empty, and error states. An empty collection after a successful query is different from a failed query. Preserve the user’s context while refreshing data and offer a retry when failure is recoverable.

    Search-as-you-type creates overlapping operations. Cancel the older request or ignore its result using a request identity; otherwise an old response can overwrite the newest query’s results. Tie cancellation to the view model’s activation or component lifetime.

    ReactiveUI supplies observable composition and scheduler integration. Declarative components supply use_future, suspense, and error boundaries. Choose the mechanism that matches the state model already used by the view.

    use_future(deps, make) runs a future on the UI thread’s dispatcher and restarts it when deps change; suspense shows its pending, ready, and failed states:

    #[component(rerender)]
    fn Search() -> Element {
    let query = use_signal(String::new);
    let results = use_future(query.get(), |query| {
    let query = query.clone();
    async move {
    sleep(Duration::from_millis(300)).await;
    search(&query).await
    }
    });
    rsx! {
    StackPanel {
    TextBox { text: query.two_way() }
    {suspense(
    results,
    || rsx! { ProgressBar { is_indeterminate: true } },
    |found| rsx! { TextBlock { text: format!("{} results", found.len()) } },
    |error| rsx! { TextBlock { text: format!("The search failed: {error}") } },
    )}
    }
    }
    }

    search is the application’s async fn returning Result<_, String>. The framework does the stale-result work of the steps below for you: a future whose dependencies changed, or whose component was removed, is dropped and never writes its state, and the sleep debounces typing. The future runs on the UI thread, so blocking work inside it still freezes the interface; move that work to a worker as described above.

    A browser build cannot treat threads, filesystem access, or blocking waits as if it were a desktop process. The default browser mode executes UI code on the page thread. The optional threaded renderer does not make application code safe to block.

    Exercise delayed completion, cancellation, repeated requests, failure, and navigating away before completion. A responsive spinner does not prove your operation is safe: check that the view releases subscriptions and that stale completion cannot mutate a new view.

    1. Capture the query and a request ID; set loading state and clear the previous error.
    2. Start the operation with cancellation tied to the view’s lifetime.
    3. On completion, compare the request ID with the latest request before changing results.
    4. For success, show either results or an explicit empty message. For failure, preserve the query and offer retry.
    5. Clear loading only if this completion still belongs to the current request.

    Try a slow first query followed by a fast second query. The first completion must not replace the second query’s results. Then navigate away before either completes and check that the old screen receives no update.

    Use Dispatcher::spawn_local on the UI thread when the future must own thread-local UI state. For example, inside a view that already has an asynchronous search service, launch the request and return a handle the view can abort:

    use ferroui_base::threading::Dispatcher;
    let task = Dispatcher::ui_thread().spawn_local(async move {
    let results = service.search(query).await;
    // Check the request identity before applying results to view state.
    results
    });
    let cancel_search = task.abort_handle();
    // Retain cancel_search with the active view. Call abort() on teardown.

    The example assumes an owned service and query and an asynchronous search method. The future runs on the dispatcher, so a synchronous blocking call inside it still freezes the UI. Await the JoinHandle when you need the result and handle its join error separately from a service error. Dropping the join handle lets the task continue; dropping a subscription guard releases its observation. These lifetimes are intentionally different. See subscription lifetimes.