maciejhirsz / kobold

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

Add the `fence` utility function #41

Closed maciejhirsz closed 1 year ago

maciejhirsz commented 1 year ago

This PR adds the kobold::diff::fence function that guards some inner View against changes, unless it's guard value has changed. Signature:

pub const fn fence<D, V, F>(guard: D, render: F) -> Fence<D, F>
where
    D: Diff,
    V: View,
    F: FnOnce() -> V;

Example:

use kobold::prelude::*;
use kobold::diff::fence;

struct User {
    id: usize,
    name: String,
    email: String,
}

#[component]
fn UserRow(user: &User) -> impl View + '_ {
    fence(user.id, || view! {
        // This row is only re-rendered if `user.id` has changed
        <tr>
            <td>{ user.id }</td>

            // Assuming that `name` never changes for a `User`
            // we can disable diffing here with the `use` keyword.
            <td>{ use &user.name }</td>
            <td>{ use &user.email }</td>
        </tr>
    })
}