zng::handler

Macro app_hn

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

Declare a mutable clone-move app event handler.

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 declares an event handler for the CLICK_EVENT.

CLICK_EVENT.on_event(app_hn!(|_, _| {
    println!("Clicked Somewhere!");
})).perm();

The closure input is &A, &dyn AppWeakHandle with &A equaling &ClickArgs for this event. Note that if you want to use the event args you must annotate the input type, the context and handle type is inferred.

The handle can be used to unsubscribe the event handler, if unsubscribe is called the handler will be dropped some time before the next event update.

CLICK_EVENT.on_event(app_hn!(|args: &ClickArgs, handle| {
    println!("Clicked {}!", args.target);
    handle.unsubscribe();
})).perm();

Internally the clmv! macro is used so you can clone-move variables into the handler.

let foo = var("".to_txt());

CLICK_EVENT.on_event(app_hn!(foo, |args: &ClickArgs, _| {
    foo.set(args.target.to_txt());
})).perm();

// can still use after:
let bar = foo.map(|c| formatx!("last click: {c}"));

In the example above only a clone of foo is moved into the handler. Note that handlers always capture by move, if foo was not listed in the clone-move section it would not be available after the handler is created. See clmv! for details.