日本語

Optimistic updates

A statement before a call changes the page at once, and the server's answer then sets the value it stored.

A statement before the call

A statement runs in the browser at once, and a call is a statement too, so a statement placed before the call changes the page before the request goes out: count.set(count + 1).then(LIKE.call()). Stmt::then, fn then(self, next: impl IntoStmt<S>) -> Self, runs self and then next. The count shows the new value while the request is on its way, and the patch the handler answers with then sets the count the server stored, which may differ when other visitors liked the post meanwhile.

A statement after the call is sent

Call::then, fn then(self, next: impl IntoStmt<S>) -> Stmt<S>, sends the call and runs next without waiting for the answer. The call evaluates its arguments when it is sent, so next can change what they read: COMMENT.call_with(draft).then(draft.set("")) sends the comment and empties the field at once. The patches the server answers with apply whenever they arrive. To run a statement after the answer, use on_success instead.

Undoing the change when the call fails

When the call fails, the change stays in the browser: nothing rolls the value back by itself. error can show the failure, and on_error can undo the change with a statement of its own, such as likes.set(likes - 1). The closure passed to on_error receives the error text as an Expr<S, String>, so setting the count to it is a compile error, E0277.

use hypersig::html::button;
use hypersig::{Action, Patches, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Post {
    likes: i32,
}

hypersig::routes! {
    fn pages(Router<()>);
    action LIKE: Action<Post> = post "/like" => like;
}

let likes = Post::likes();
// error[E0277]: `Expr<Post, String>` is not an expression of type `i32` on the page whose signals are `Post`
let like = likes
    .set(likes + 1)
    .then(LIKE.call().on_error(|message| likes.set(message)));

What the types leave unchecked

Nothing compares the change made in the browser with the one the handler makes. A statement that adds 1 while the handler adds 2 shows one value until the answer arrives and another after it. Each answer sets the value the server held when it answered, so two quick clicks can show 2, then 1 when the first answer arrives, then 2 when the second does. A statement in on_error that undoes the change runs once per failed call, so the same two clicks that both fail take the count back down twice.

A complete example

This post counts a like at once and takes it back if the call fails. Its comment form adds the comment to the list, sends it, and empties the field, all before the server answers.

use hypersig::html::{button, each, form, input, li, span, ul};
use hypersig::{Action, Patches, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Post {
    likes: i32,
    comments: Vec<String>,
    draft: String,
    problem: String,
}

hypersig::routes! {
    fn pages(Router<()>);
    action LIKE: Action<Post> = post "/like" => like;
    action COMMENT: Action<Post, String> = post "/comment" => comment;
}

let likes = Post::likes();
// The count goes up at once, and back down if the call fails.
let like = likes.set(likes + 1).then(
    LIKE.call()
        .error(Post::problem())
        .on_error(|_| likes.set(likes - 1)),
);

let draft = Post::draft();
// The call reads `draft` when it is sent, so emptying it afterwards does not
// change what the call sends.
let send = Post::comments()
    .push(draft)
    .then(COMMENT.call_with(draft).error(Post::problem()))
    .then(draft.set(""));

let page = ul::<Post>()
    .child(
        li().child(button().on_click(like).text("Like "))
            .child(span().bind_text(likes.to_text())),
    )
    .child(li().child(form().on_submit(send).child(input().bind(draft))))
    .child(each(Post::comments(), |text| li().bind_text(text)));

Next, Calls that overlap covers a second click while the first call waits, and Patches covers what the handler sends back.