# Showing, failing and loading
Three building blocks change what a part of a window shows, without a rebuild of the rest:
- [`show`](crate::show): content that is there while a condition holds;
- [`catch_errors`](crate::catch_errors): an error boundary, which shows a fallback when
the content inside it fails;
- [`suspense`](crate::suspense): a loading boundary, which shows a placeholder while
data read inside it is still arriving, fetched by a [`Resource`](crate::reactive::Resource).
Each builds its content in a reactive scope of its own. When the content goes (the
condition turns false, a retry, a new key), everything it made goes with it: its elements,
signals, effects and the fetches its resources started, which are cancelled.
## Showing content while a condition holds
`show(ui, when, build)` builds what `build` makes while `when` is true. `when` is a signal, a
closure or a constant. The content is built each time it appears and disposed when it goes;
[`Show::fallback`](crate::Show::fallback) builds what shows otherwise, and
[`Show::enter`](crate::Show::enter) and [`Show::exit`](crate::Show::exit) play
transitions ([`crate::guide::motion`]).
```
use remedy_ui::reactive::Signal;
use remedy_ui::testing::TestApp;
use remedy_ui::show;
use remedy_ui::widgets as w;
let error = Signal::new(None::<String>);
app.frames(1);
assert_eq!(names(&ui), ["The disk is full"]);
```
A `show` with nothing to show (the condition is false and there is no fallback, and no exit
transition is still playing) takes no room at all: no size, and no gap in the row or column
around it, as content that is not there would not. Screens that take turns can each
`grow` in the same column, and only the one shown fills it.
```
use remedy_ui::reactive::Signal;
use remedy_ui::testing::TestApp;
use remedy_ui::show;
use remedy_ui::widgets as w;
let open = Signal::new(false);
app.frames(1);
assert_eq!(top("Below").map(|y| y - first), Some(80.0));
```
For content that stays built and only hides (a panel whose state should survive), use
[`El::hidden`](crate::El::hidden) instead; `show` is for content that should not exist while
it is not shown.
## Error boundaries
A bug in one panel should not take the window down. `catch_errors(ui, fallback, child)` shows
what `child` builds. When building it, or any handler, binding, effect or painter inside it,
panics, the boundary unmounts that content and shows what `fallback` builds from the
[`CaughtError`](crate::CaughtError) and a [`Retry`](crate::Retry). The rest of the
window carries on. `child` is a builder, a closure that makes the content, not an element
made beforehand: the boundary calls it inside its own scope (so a panic while building is
caught too) and again on retry, for a fresh copy. `fallback` is a builder too.
The error says what failed, where in the tree (`element_path`), where in the source
(`location`) and while doing what ([`FailurePhase`](crate::view::FailurePhase)). It also goes
to the window's failure list ([`Ui::failures`](crate::Ui::failures)) and error hook
([`Ui::set_error_hook`](crate::Ui::set_error_hook)), where an app can log or report it. The
inspector's Failures page lists them, with Retry for boundaries that are still failed.
```
use remedy_ui::a11y::Role;
use remedy_ui::reactive::Signal;
use remedy_ui::testing::TestApp;
use remedy_ui::catch_errors;
use remedy_ui::view::FailurePhase;
use remedy_ui::widgets as w;
// Contact lines the app reads from a file; the second has no phone number.
let lines = Signal::new(vec!["Ada;ada@example.com;555-0100".to_string(), "Grace;grace@example.com".to_string()]);
lines.set(vec!["Grace;grace@example.com;555-0199".to_string()]);
assert!(app.press(Role::Button, "Try again"));
assert!(ui.reader_view().iter().any(|n| n.name.as_deref() == Some("Grace 555-0199")));
```
The boundary takes a builder, not an element, so Retry can build the content again. A panic
with no boundary above it is logged and contained where it happened, and the window goes on.
## Data that arrives later
A [`Resource`](crate::reactive::Resource) fetches a value off the UI thread for a key: when
the key changes it cancels the fetch under way and starts another, and when its owner goes
it cancels for good. The fetch gets a [`Cancel`](crate::reactive::Cancel) to check, and
returns a `Result`; [`Resource::get`](crate::reactive::Resource::get),
[`loading`](crate::reactive::Resource::loading) and [`error`](crate::reactive::Resource::error)
are tracked, so bindings follow them. Fetches run on the shared
[`WorkerPool`](crate::reactive::WorkerPool), or on an [`Executor`](crate::reactive::Executor)
provided as context, which is how an app with its own async runtime plugs it in. RemedyUI
depends on no async runtime.
`suspense(ui, placeholder, child)` builds the content at once, hidden, so its resources start
fetching, and shows it when every resource read inside it has loaded. The placeholder appears
only after a short delay ([`SUSPENSE_DELAY`](crate::view::SUSPENSE_DELAY)) and then stays at
least [`SUSPENSE_MIN_SHOWN`](crate::view::SUSPENSE_MIN_SHOWN), so quick loads show nothing and
slow ones never flash.
```
use remedy_ui::reactive::{Resource, Signal};
use remedy_ui::testing::TestApp;
use remedy_ui::suspense;
use remedy_ui::widgets as w;
let user = Signal::new(7u32);
user.set(8);
for _ in 0..1000 {
if shows("User 8") {
break;
}
std::thread::yield_now();
app.frames(1);
}
assert!(shows("User 8"));
```
Put a `catch_errors` around a `suspense` to show a failed fetch with a way to retry: a
resource's error is a value ([`FetchError`](crate::reactive::FetchError)), so the content
decides how to show it, and a panic in the content reaches the boundary.