Start timer on component mount
use std::rc::Rc;
use vertigo::{bind, dom, get_driver, struct_mut::ValueMut, Value};
#[derive(Clone)]
pub struct State {
pub timer_state: Value<u32>,
timer: Rc<ValueMut<bool>>,
}
impl State {
pub fn new() -> State {
State {
timer_state: Value::new(0),
timer: Rc::new(ValueMut::new(false)),
}
}
pub async fn start_timer(self) {
// Ignore if already started
if self.timer.get() {
return;
}
// Mark start of the timer
self.timer.set(true);
// Run into timer loop
for i in 0..50 {
self.timer_state.set(i as u32);
get_driver().sleep(20).await;
}
// Stop the timer
self.timer.set(false);
}
}
let state = State::new();
let _ = bind!(state, async move {
state.start_timer().await;
});
dom! {
<div>{state.timer_state}</div>
}