日本語

The state of a list item

A field of a list item is a signal whose path starts at the item. The server cannot patch it, and state the browser keeps per item belongs at the page's root.

The fields of one item

Sig has a third type parameter that says where its path starts. It defaults to Root, the page's signal struct, so Sig<S, T> is a field reached from the page. Item::at, fn at<U>(self, field: Sig<T, U>) -> Sig<S, U, Local>, gives a field of an item, whose path starts at the item and is resolved per item in the browser. Local is writable, so set, toggle, bind, and the list statements work on an item's field as on a page field:

use hypersig::html::{attr, button, each_keyed, input, li, span, ul};
use hypersig::{Local, Sig, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Task {
    id: i32,
    title: String,
    done: bool,
    votes: i32,
}

#[derive(Signals, Serialize, Deserialize)]
struct Board {
    tasks: Vec<Task>,
}

let list = ul::<Board>().child(each_keyed(
    Board::tasks(),
    |task| task.at(Task::id()),
    |task| {
        // A path from whichever item the browser is rendering.
        let votes: Sig<Board, i32, Local> = task.at(Task::votes());
        li().child(input().attr(attr::TYPE, "checkbox").bind(task.at(Task::done())))
            .child(input().bind(task.at(Task::title())))
            .child(span().bind_text(votes.to_text()))
            .child(button().on_click(votes.set(votes + 1)).text("+1"))
            .child(button().on_click(task.remove()).text("Remove"))
    },
));

The server cannot patch one item's field

A Sig<S, T, Local> has no patch method, so the server cannot address one item's field by position, which would land on another item once the list changed:

use hypersig::html::{each, li, ul};
use hypersig::{Patches, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Todo {
    done: bool,
}

#[derive(Signals, Serialize, Deserialize)]
struct Todos {
    items: Vec<Todo>,
}

let list = ul::<Todos>().child(each(Todos::items(), |todo| {
    // error[E0599]: no method named `patch` found for struct `Sig<Todos, bool, hypersig::Local>`
    let _: Patches<Todos> = todo.at(Todo::done()).patch(true).into();
    li()
}));

The server instead replaces the whole list with a patch of the list field, changes the items it names by key with a list patch such as patch_append or patch_remove, or re-renders a part of the page as a fragment. List patches covers list patches, and Fragments covers fragments.

Choosing per item

The closure passed to each runs once on the server to build a template, so it cannot branch on an item's value in Rust: an if there would decide for every item at once. An expression decides per item in the browser, through show, if_else, or a binding:

use hypersig::html::{each, li, span, ul};
use hypersig::{Signals, if_else};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Task {
    title: String,
    done: bool,
    urgent: bool,
}

#[derive(Signals, Serialize, Deserialize)]
struct Board {
    tasks: Vec<Task>,
}

// The closure runs once, so a Rust `if` here would choose for every item
// at once. Expressions choose per item in the browser.
let list = ul::<Board>().child(each(Board::tasks(), |task| {
    let done = task.at(Task::done());
    li().child(span().bind_text(task.at(Task::title())))
        .child(span().bind_text(if_else(done, " (done)", "")))
        .child(span().show(task.at(Task::urgent()).and(!done)).text(" Urgent"))
}));

Lists inside list items

A field of an item can be a list too, and each takes it like any list signal. The inner closure can read both its own item and the item of the list around it:

use hypersig::html::{each, li, p, ul};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Tag {
    name: String,
}

#[derive(Signals, Serialize, Deserialize)]
struct Post {
    title: String,
    tags: Vec<Tag>,
}

#[derive(Signals, Serialize, Deserialize)]
struct Blog {
    posts: Vec<Post>,
}

let posts = ul::<Blog>().child(each(Blog::posts(), |post| {
    li().child(p().bind_text(post.at(Post::title())))
        // The inner closure reads its own item and the post around it.
        .child(ul().child(each(post.at(Post::tags()), |tag| {
            li().bind_text(tag.at(Tag::name()).concat(" in ").concat(post.at(Post::title())))
        })))
}));

Browser-only state of an item

A patch from the server writes each item it carries whole. patch_upsert puts the server's item in place of the one with the same key, and patch replaces the whole list. A field held on the item only for the browser, such as whether a card is open for editing, takes the value the server sent, so a card someone is editing closes when another user's change to it arrives. Keep such state at the page's root as the key of the item it applies to, and compare it with each item's key:

use hypersig::Signals;
use hypersig::html::{button, each_keyed, input, li, span, ul};

#[derive(Signals, Serialize, Deserialize)]
struct Card {
    id: i64,
    title: String,
}

#[derive(Signals, Serialize, Deserialize)]
struct Board {
    cards: Vec<Card>,
    // At the root, where a patch of a card does not reach it.
    editing_id: Option<i64>,
}

let editing = Board::editing_id();
let board = ul::<Board>().child(each_keyed(
    Board::cards(),
    |card| card.at(Card::id()),
    |card| {
        let this = card.at(Card::id()).some();
        li().child(
            span()
                .show(editing.not_equals(this.clone()))
                .bind_text(card.at(Card::title())),
        )
        .child(input().show(editing.equals(this.clone())).bind(card.at(Card::title())))
        .child(button().on_click(editing.set(this)).text("Edit"))
    },
));

What the types leave unchecked

  • An Item and the signals built from it belong to the closure they were passed to. The types do not stop one from being moved out, and reading it after the closure returns, or rendering what was built from it outside its list, panics.
  • Item::at refuses a field marked #[signals(computed)], and html::part takes only a field from the page root, so neither a computed signal nor a Part can sit at an item's field.

Next

Actions calls the server from a statement, and List patches answers with changes to a list.