Extended control flow
AppContext has functions that help with application control flow.
-
add_timer()
: Sets a timer event. This returns a TimerHandle to identify a specific timer. -
queue()
andqueue_err()
: These functions add additional items to the list that will be processed after the event handler returns. The result of the event handler will be added at the end of this list too. -
spawn()
: Run a closure as a background task. Such a closure gets a cancel-token and a back-channel to report its findings.#![allow(unused)] fn main() { let cancel = ctx.spawn(move |cancel, send| { let mut data = Data::new(config); loop { // ... long task ... // report partial results send.send(Ok(Control::Message(AppMsg::Partial))); if cancel.is_canceled() { break; } } Ok(Control::Message(AppMsg::Final)) }); }
Spawns a background task. This is a move closure to own the parameters for the 'static closure. It returns a clone of the cancel token to interrupt the task if necessary.
#![allow(unused)] fn main() { let cancel = ctx.spawn(move |cancel, send| { }
Captures its parameters.
#![allow(unused)] fn main() { let mut data = Data::new(config); }
Goes into the extended calculation. This uses
send
to report a partial result as a message. At a point where canceling is sensible it checks the cancel state.#![allow(unused)] fn main() { loop { // ... long task ... // report partial results send.send(Ok(Control::Message(AppMsg::Partial))); if cancel.is_canceled() { break; } } }
Finishes with some result.
#![allow(unused)] fn main() { Ok(Control::Message(AppMsg::Final)) }