Calls that overlap
What happens when a call is sent while an earlier call to the same action still waits, and how to keep the newest answer, send one at a time, or give up after a time limit.
The newest search wins
Each call applies its patches when they arrive, so the answer to an earlier, slower search can overwrite the answer to a later one. Call::latest, fn latest(self) -> Self, aborts the unfinished earlier latest call to the same action, and the patches it has not received are dropped. The calls of every element to one action share it, because the handler decides which signals they patch, and a call made inside a component that is a Part shares it with that part's calls only.
use std::time::Duration;
use hypersig::html::{Event, input};
use hypersig::{Action, Patches, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Search {
query: String,
results: Vec<String>,
searching: bool,
}
hypersig::routes! {
fn pages(Router<()>);
action FIND: Action<Search, String> = get "/find" => find;
}
let query = Search::query();
// A newer search aborts the one still waiting, so its answer is the one shown.
let field = input().bind(query).on_debounced(
Event::Input,
Duration::from_millis(200),
FIND.call_with(query).indicator(Search::searching()).latest(),
);The browser aborts the earlier request, and the server may then stop its handler at an .await, so put latest only on calls that read. A later call never aborts a call without latest. The aborted call runs neither on_error nor on_success, and an indicator stays true until every call that sets it has its answer.
Calls that change data keep their order
A call that changes data goes without latest, and its answers apply in order instead: once a later POST to the action has its complete answer, the patches of an earlier one that are still arriving are dropped. Both requests reach the server and both handlers run, so the data holds both changes, and the page shows the answer to the later one.
One call at a time
Call::exclusive, fn exclusive(self) -> Self, skips a call while another call to the same action awaits its answer, so a second click on an order button sends nothing while the first order is on its way. The skipped call sets no indicator and no error, and runs neither on_error nor on_success. A call made inside a component that is a Part waits only for that part's calls. While a POST waits, the element that sent it carries aria-busy="true", with or without exclusive.
A time limit
Call::timeout, fn timeout(self, limit: Duration) -> Self, fails a call whose answer has not ended limit after it was sent. The browser then aborts the request and drops the patches still to come, the status signal stays 0, and the error signal shows the answer took longer than 10000 ms. Without it, a call waits for as long as the server takes, which an upload of a large file needs. The server may still finish the handler after the browser stopped waiting, so a call that changes data can have taken effect.
A stream stays open until its handler ends it, so a StreamCall has no timeout, and calling it is a compile error, E0599. It has no latest or exclusive either.
use std::time::Duration;
use hypersig::html::div;
use hypersig::{Patches, Signals, StreamAction};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Feed {
items: Vec<String>,
}
hypersig::routes! {
fn pages(Router<()>);
stream UPDATES: StreamAction<Feed> = "/updates" => |(), ()| {
futures::stream::empty::<Patches<Feed>>()
};
}
// error[E0599]: no method named `timeout` found for struct `StreamCall<S>` in the current scope
let feed = div::<Feed>().on_load(UPDATES.call().timeout(Duration::from_secs(5)));What the types leave unchecked
latest compiles on a POST that changes data. When a later call aborts it, the handler may have made part of its change before it stopped at an .await, and the page receives none of its answer. exclusive and latest group calls by action, not by element, so two buttons that call one action with different arguments still skip or abort each other. A call that failed by timeout may have changed the data, so a page that sends it again can repeat the change.
A complete example
This checkout sends one order at a time and gives up on a shipping quote after 10 seconds.
use std::time::Duration;
use hypersig::html::{button, div, p};
use hypersig::{Action, Patches, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Checkout {
ordering: bool,
problem: String,
}
hypersig::routes! {
fn pages(Router<()>);
action ORDER: Action<Checkout> = post "/order" => order;
action QUOTE: Action<Checkout> = post "/quote" => quote;
}
// A second click while the first order is on its way sends nothing.
let order = ORDER.call().indicator(Checkout::ordering()).exclusive();
// A quote that takes more than 10 seconds counts as failed.
let quote = QUOTE
.call()
.error(Checkout::problem())
.timeout(Duration::from_secs(10));
let page = div::<Checkout>()
.child(button().on_click(order).text("Place the order"))
.child(button().on_click(quote).text("Get a shipping quote"))
.child(p().bind_text(Checkout::problem()));Next, Patches covers what the answers carry, and Streams covers an answer that stays open.