hn

Macro hn 

Source
macro_rules! hn {
    ($($clmv:ident,)* |_| $body:expr) => { ... };
    ($($clmv:ident,)* |$args:ident| $body:expr) => { ... };
    ($($clmv:ident,)* |$args:ident  : & $Args:ty| $body:expr) => { ... };
}
Expand description

Declare a mutable clone-move 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 on_click property.

on_click = hn!(|_| {
    println!("Clicked {}!", args.click_count);
});

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

let foo = var(0);

// ..

on_click = hn!(foo, |args| {
    foo.set(args.click_count);
});

// can still use after:
let bar = foo.map(|c| formatx!("click_count: {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.

§App Scope

When used in app scopes the APP_HANDLER contextual service can be used to unsubscribe from inside the handler.

The example declares an event handler for the CLICK_EVENT. Unlike in an widget this handler will run in the app scope, in this case the APP_HANDLER is available during handler calls, in the example the subscription handle is marked perm, but the event still unsubscribes from the inside.

CLICK_EVENT
    .on_event(hn!(|args| {
        println!("Clicked Somewhere!");
        if args.target == "something" {
            APP_HANDLER.unsubscribe();
        }
    }))
    .perm();