日本語

Signals inside signals

A field can hold a struct, an Option, or the state of a component, and at reaches the fields inside it.

Fields inside fields

A field may itself be a Signals struct. Sig::at, fn at<U>(self, field: Sig<T, U>) -> Sig<S, U, R>, reaches into it, and the result is a Sig like any other. Its path() joins the field names with dots.

use hypersig::html::span;
use hypersig::Signals;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Address {
    city: String,
}

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

// `at` reaches a field of the struct a signal holds.
let city = Account::address().at(Address::city());
assert_eq!(city.path(), "address.city");

let line = span::<Account>().bind_text(city);

at takes a Sig<T, U>, where T is the type the outer signal holds, so a field of another struct is a compile error:

use hypersig::Signals;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Address {
    city: String,
}

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

// error[E0308]: mismatched types: expected `Sig<Address, _>`, found `Sig<Account, String>`
let name = Account::address().at(Account::name());

A path is at most MAX_DEPTH, 8, fields deep. The types do not count the depth, so the at call that would make a ninth field panics with a signal path is at most 8 fields deep.

A whole struct as one value

A signal that holds a struct is a value of its own. set copies another struct of the same type into it, and with(field, value) makes a copy with one field replaced. On an expression that evaluates to a struct, at(field) reads one field, as Sig::at does on a signal.

use hypersig::html::{button, span};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Address {
    city: String,
    zip: String,
}

#[derive(Signals, Serialize, Deserialize)]
struct Account {
    home: Address,
    shipping: Address,
}

let home = Account::home();
let shipping = Account::shipping();

// One statement copies the whole struct.
let same_as_home = button::<Account>().on_click(shipping.set(home)).text("Same as home");
// `with` copies it with one field replaced.
let moved = button::<Account>()
    .on_click(home.set(home.with(Address::city(), "Osaka")))
    .text("Moved to Osaka");
let city = span::<Account>().bind_text(shipping.at(Address::city()));

Optional fields

The signal of an Option field has is_some and is_none, which give a bool, and unwrap_or(default), which gives the held value or default. some() wraps any value in Some, and &None as a literal sets the field back to None.

use hypersig::html::{button, div, span};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Profile {
    name: String,
    nickname: Option<String>,
}

let name = Profile::name();
let nickname = Profile::nickname();

let card = div::<Profile>()
    .child(span().bind_text(nickname.unwrap_or(name)))
    .child(span().show(nickname.is_none()).text(" (no nickname)"))
    // `&None` sets the field back to `None`.
    .child(button().on_click(nickname.set(&None)).text("Clear"))
    .child(button().on_click(nickname.set(name.some())).text("Use my name"));

The state of components

A component of hypersig_ui that keeps state of its own, such as Combobox or DataTable, takes it as a Sig of its state struct, so that state is a field of the page's struct too. Gather those fields into one struct that derives Default, and the page's struct gains one field, set with Default::default(). Adding a component then adds a field to that struct and leaves the page's initializer as it is. ComboboxState and TableState, the state structs of hypersig_ui, implement Default, which gives an empty, closed combobox and an unfiltered, unsorted table on its first page.

use hypersig::html::div;
use hypersig::{NodeRef, Page, Signals};
use hypersig_ui::{Combobox, ComboboxState, ComboboxText, TableState};
use serde::{Deserialize, Serialize};

// The state of every component on the page, in one field of the page's struct.
#[derive(Signals, Serialize, Deserialize, Default)]
struct Controls {
    country: ComboboxState,
    orders: TableState,
}

#[derive(Signals, Serialize, Deserialize)]
struct Orders {
    customer: String,
    controls: Controls,
}

hypersig::node_ref!(const COUNTRY_FIELD: NodeRef<Orders>);
hypersig::node_ref!(const COUNTRY_OPTIONS: NodeRef<Orders>);
const COUNTRY: Combobox<Orders> = Combobox::new(COUNTRY_FIELD, COUNTRY_OPTIONS);

let controls = Orders::controls();
let page = Page::new(
    "Orders",
    Orders {
        customer: String::new(),
        controls: Controls::default(),
    },
)
.child(div().child(COUNTRY.render(
    "Country",
    controls.at(Controls::country()),
    ["France", "Japan", "Kenya"],
    &ComboboxText::default(),
)));

Other places a nested struct appears

  • A Vec of values or of structs is a list, which Lists covers.
  • A field marked #[signals(computed)] inside a nested struct is reached with at_computed instead of at, as Computed signals shows.
  • A nested struct that implements Part renders its own piece of page with its paths under the field, which Components covers.

Next

Expressions computes values from these fields, and Statements changes them.