Statements
A statement changes the signals in the browser, without a request. Statements run in order and branch on expressions.
A statement runs in the browser
A Stmt<S> is a change to the signals of the page whose struct is S. An event method such as on_click attaches it to an element, and the browser runs it on each event without a request. Events lists the events.
Assigning a value
Sig::set, fn set(self, value: impl IntoExpr<S, T>) -> Stmt<S>, assigns an expression of the signal's own type, and Sig::toggle flips a bool. The value is evaluated when the statement runs, so count.set(count + 1) reads the count at that moment.
A constant of a type other than the literal types goes in by reference, as &Column::Done, because IntoExpr is implemented for &T where T: Serialize. Passing the enum by value is a compile error:
use hypersig::Signals;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
enum Column {
Todo,
Done,
}
#[derive(Signals, Serialize, Deserialize)]
struct Board {
column: Column,
}
// error[E0277]: `Column` is not an expression of type `Column` on the page whose signals are `Board`
let done = Board::column().set(Column::Done);One statement after another
Stmt::then, fn then(self, next: impl IntoStmt<S>) -> Stmt<S>, runs next after self. Each statement reads the values the ones before it wrote, including computed signals recomputed from them:
use hypersig::html::button;
use hypersig::Signals;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Order {
quantity: i32,
summary: String,
}
let quantity = Order::quantity();
let summary = Order::summary();
// The second statement reads the quantity the first one wrote.
let add = quantity
.set(quantity + 1)
.then(summary.set(quantity.to_text().concat(" in the cart")));
let plus = button::<Order>().on_click(add).text("+1");Branching
Stmt::when(cond, then) runs then only while cond is true, and Stmt::if_else(cond, then, otherwise) picks one of two statements. Stmt::none does nothing, for a branch that has nothing to do. The condition is evaluated each time the statement runs, so one statement takes either branch on different clicks.
Keeping a value for later statements
Sig::let_in_stmt and Expr::let_in_stmt evaluate a value once, when the statement runs, and hand the closure an expression that reads that value. Statements that run later inside the closure, such as a call's on_error, still read it after the signals it came from have changed:
use hypersig::{Action, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Notes {
next: i32,
failed: Vec<i32>,
}
hypersig::routes! {
fn actions(Router<()>);
action SAVE: Action<Notes, i32> = post "/save" => save;
}
let next = Notes::next();
// `sent` is the number `next` held when the statement ran. `on_error` runs
// after the answer arrives, when `next` has moved on, and still reads it.
let save = next.let_in_stmt(|sent| {
SAVE.call_with(&sent)
.on_error(|_| Notes::failed().push(sent))
.then(next.set(next + 1))
});Other statements
An event method takes any IntoStmt<S>, and then joins any of them:
| Statement | Covered in |
|---|---|
push, retain, sort_by, and the other list statements | Lists |
An action's call(), a Call | Actions |
navigate, push_url, and replace_url on a link | Links and redirects |
focus and the other methods of a NodeRef | Node refs |
Stmt::view_transition | Styling |
What the types leave unchecked
setexists only on a signal the browser may write. A computed signal and a read-only view have none, so writing one is a compile error, which Computed signals shows.- A statement that fails in the browser writes the error to the console with the prefix
hypersig:, and the statements after it in the same chain do not run.
A game turn
One click adds a bonus unless the game is over, then takes a life and ends the game when the last one is gone:
use hypersig::html::button;
use hypersig::{Signals, Stmt};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Player {
score: i32,
lives: i32,
over: bool,
}
let score = Player::score();
let lives = Player::lives();
let over = Player::over();
let hit = lives
.set(lives - 1)
.then(Stmt::when(lives.le(0), over.set(true)));
let bonus = Stmt::if_else(over, Stmt::none(), score.set(score + 100));
let play = button::<Player>().on_click(bonus.then(hit)).text("Play");Next
Events covers the events a statement runs on. Binding form controls writes signals from form controls without a statement.