Busy and failed calls
A call can set a signal while it waits, write the error text when it fails, and run a statement on failure or success.
While a call waits
Call::indicator, fn indicator(self, flag: Sig<S, bool>) -> Self, sets flag to true when the call is sent and to false once its answer has arrived. Several calls may set one flag, which then turns false once every one of them has its answer, so one saving signal can cover every save button on a page. Bind the flag like any other signal, for example with attr_when(attr::DISABLED, flag) on the button or show(flag) on a spinner.
While a POST waits, the element whose handler sent it carries aria-busy="true", with or without an indicator, unless the page gave that element an aria-busy of its own. A screen reader announces the element as busy, and a stylesheet can select [aria-busy="true"]. A GET sets no aria-busy.
The flag must be a bool signal. A signal of another type is a compile error, E0308.
use hypersig::html::button;
use hypersig::{Action, Patches, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Notes {
saving: bool,
problem: String,
}
hypersig::routes! {
fn pages(Router<()>);
action SAVE: Action<Notes> = post "/save" => save;
}
// error[E0308]: mismatched types: expected `Sig<Notes, bool>`, found `Sig<Notes, String>`
let save = button::<Notes>().on_click(SAVE.call().indicator(Notes::problem()));When a call fails
Call::error, fn error(self, message: Sig<S, String>) -> Self, sets message to "" each time the call is sent and to the error text when it fails, so the same failure twice changes the signal twice. The text depends on what came back:
| The answer | The text |
|---|---|
A status outside 2xx with a text/plain body, which axum sends for a String or a &str | The body, so the handler writes the sentence the page shows |
| A status outside 2xx with an empty body or one of another type, such as a proxy's HTML error page | The status and its reason, such as 502 Bad Gateway |
| No answer: the connection failed or the browser is offline | The browser's own message for the failed fetch |
| A 2xx answer that was redirected | the answer was redirected to and the URL |
A 2xx answer that is not text/event-stream | the answer is text/html, not an event stream, with the type it had |
| An answer that closes before the handler has finished, such as one a host cuts at its time limit | the answer closed before the server ended it |
No complete answer within the timeout | the answer took longer than 10000 ms, with the limit it had |
Call::status, fn status(self, status: Sig<S, i32>) -> Self, sets status to 0 when the call is sent and to the HTTP status of a failed answer. It stays 0 in the last five rows of the table, where no status the page can act on arrived. Compare it with a status written from StatusCode, such as i32::from(StatusCode::UNAUTHORIZED.as_u16()), to show a sign-in link for a 401 and the text for anything else, without reading the text.
Every failure is also written to the browser's console, as hypersig: POST /save failed with 409: Someone else saved these notes first.. This happens with or without error.
A statement for failure or success
Call::on_error, fn on_error<B: IntoStmt<S>>(self, handle: impl FnOnce(Expr<S, String>) -> B) -> Self, runs the statement handle builds when the call fails, and handle receives the same text error writes. Call::on_success, fn on_success(self, next: impl IntoStmt<S>) -> Self, runs next once the answer has arrived and its patches are applied. Both run where the call was made: inside a list item they change that item, so a cart that orders several lines at once marks only the line whose order failed. Neither runs for a latest call that a later one aborted, or once the page navigates away.
use hypersig::html::{button, each_keyed, li, span, ul};
use hypersig::{Action, Patches, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Line {
id: i32,
name: String,
problem: String,
ordered: bool,
}
#[derive(Signals, Serialize, Deserialize)]
struct Cart {
lines: Vec<Line>,
}
hypersig::routes! {
fn pages(Router<()>);
action ORDER: Action<Cart, i32> = post "/order" => order;
}
// Each statement changes the line whose button sent the call.
let lines = ul::<Cart>().child(each_keyed(
Cart::lines(),
|line| line.at(Line::id()),
|line| {
let order = ORDER
.call_with(line.at(Line::id()))
.on_error(|message| line.at(Line::problem()).set(message))
.on_success(line.at(Line::ordered()).set(true));
li().child(span().bind_text(line.at(Line::name())))
.child(button().on_click(order).text("Order"))
.child(span().bind_text(line.at(Line::problem())))
},
));What the types leave unchecked
Nothing requires a call to report its failure. A call without error, status, or on_error that fails changes nothing on the page, and only the console line shows it. Nothing checks that the page binds the error signal either, so a signal that no element shows leaves the visitor without a message. The text of a text/plain error body reaches the visitor as it was sent, so a handler should answer with a sentence meant for them and log the details on the server.
A complete example
This save button is disabled while its call waits, shows the error text in an alert, and offers a sign-in link when the server answers 401.
use axum::http::StatusCode;
use hypersig::html::{Role, a, attr, button, div, p};
use hypersig::{Action, Patches, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Notes {
saving: bool,
problem: String,
failure: i32,
}
hypersig::routes! {
fn pages(Router<()>);
action SAVE: Action<Notes> = post "/save" => save;
}
let save = SAVE
.call()
.indicator(Notes::saving())
.error(Notes::problem())
.status(Notes::failure());
let signed_out = Notes::failure().equals(i32::from(StatusCode::UNAUTHORIZED.as_u16()));
let page = div::<Notes>()
.child(
button()
.attr_when(attr::DISABLED, Notes::saving())
.on_click(save)
.text("Save"),
)
.child(p().role(Role::Alert).bind_text(Notes::problem()))
.child(a().show(signed_out).text("Sign in again"));Next, Optimistic updates changes the page before the answer arrives, and Calls that overlap covers a second call sent while the first one waits.