Mason Only Add While Active (#496)

It's jarring for the example to automatically add to the button count,
so this makes it so it only increments while active is true.

---------

Co-authored-by: Daniel McNab <36049421+DJMcNab@users.noreply.github.com>
This commit is contained in:
Jared O'Connell 2024-08-09 11:50:35 -04:00 committed by GitHub
parent cfb0ef231e
commit 2dc7b80153
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 15 deletions

View File

@ -92,18 +92,24 @@ fn app_logic(data: &mut AppData) -> impl WidgetView<AppData> {
button("Reset", |data: &mut AppData| data.count = 0), button("Reset", |data: &mut AppData| data.count = 0),
flex((fizz_buzz_flex_sequence, flex_sequence)).direction(axis), flex((fizz_buzz_flex_sequence, flex_sequence)).direction(axis),
)), )),
async_repeat( // The following `async_repeat` view only exists whilst the example is in the "active" state, so
|proxy| async move { // the updates it performs will only be running whilst we are in that state.
let mut interval = time::interval(Duration::from_secs(1)); data.active.then(|| {
loop { async_repeat(
interval.tick().await; |proxy| async move {
let Ok(()) = proxy.message(()) else { let mut interval = time::interval(Duration::from_secs(1));
break; loop {
}; interval.tick().await;
} let Ok(()) = proxy.message(()) else {
}, break;
|data: &mut AppData, ()| data.count += 1, };
), }
},
|data: &mut AppData, ()| {
data.count += 1;
},
)
}),
) )
} }

View File

@ -115,9 +115,7 @@ struct NoElements;
impl ElementSplice<NoElement> for NoElements { impl ElementSplice<NoElement> for NoElements {
fn with_scratch<R>(&mut self, f: impl FnOnce(&mut AppendVec<NoElement>) -> R) -> R { fn with_scratch<R>(&mut self, f: impl FnOnce(&mut AppendVec<NoElement>) -> R) -> R {
let mut append_vec = AppendVec::default(); let mut append_vec = AppendVec::default();
let ret = f(&mut append_vec); f(&mut append_vec)
debug_assert!(append_vec.into_inner().is_empty());
ret
} }
fn insert(&mut self, _: NoElement) {} fn insert(&mut self, _: NoElement) {}