Macro zng_app::handler::app_hn_once

source ·
macro_rules! app_hn_once {
    ($($tt:tt)+) => { ... };
}
Expand description

Declare a clone-move app event handler that is only called once.

The macro input is a closure with optional clone-move variables, internally it uses clmv! so the input is the same syntax.

§Examples

The example captures data by move and then destroys it in the first call, this cannot be done using app_hn! because the data needs to be available for all event calls. In this case the closure is only called once, subsequent events are ignored by the handler and it automatically requests unsubscribe.

let data = vec![1, 2, 3];

CLICK_EVENT.on_event(app_hn_once!(|_| {
    for i in data {
        print!("{i}, ");
    }
})).perm();

Other then declaring a FnOnce this macro behaves like app_hn!, so the same considerations apply. You can clone-move variables, the type of the input is the event arguments and must be annotated.

let data = vec![1, 2, 3];

CLICK_EVENT.on_event(app_hn_once!(data, |args: &ClickArgs| {
    drop(data);
})).perm();

println!("{data:?}");