maciejhirsz / kobold

Easy declarative web interfaces.
https://docs.rs/kobold/
Mozilla Public License 2.0
385 stars 7 forks source link

New `event!` macro deprecating `bind!` #92

Closed maciejhirsz closed 3 months ago

maciejhirsz commented 3 months ago

New abstraction over Hook::bind, all of the following lines are equivalent:

// no sugar
let onclick = counter.bind(|counter, _| *counter += 1);

// old bind! macro
bind! {
    counter:

    let onclick = move |_| *counter += 1;
}

// new event! macro long form
let onclick = event!(|counter, _| *counter += 1);

// new event! macro skipping event in closure
let onclick = event!(|counter| *counter += 1);

// new event! macro shorthand
let onclick = event!(*counter += 1);

The shorthand variant works only if the expression begins either with the same identifier as the &Hook to the state, or deref asterisk followed by the same identifier like in the example alone. Aside from assignment like above, this makes the following scenarios very ergonomic:

/// assigning a field on a struct
let onclick = event!(state.some_field = value);

/// calling a method
let onclick = event!(state.some_method());