How-to

Website

Web app

Update at once, and roll back on failure

A like that waits for the server feels slow. To show the change at once, update the state before the request, keep the old value, and put it back if the request fails:

Needs: a server that answers POST /api/like/<id>. It claims /: paste it into a new project, or replace the starter's src/pages/Home.wf with it.

wf

type Item { id: String, liked: Bool } page Likes(path: "/", title: "Likes", description: "A like that shows at once.") { state items: [Item] = [Item(id: "a", liked: false)] state error = "" action like(id: String) { error = "" let before = items items = items.map(i => if i.id == id { Item(id: i.id, liked: !i.liked) } else { i }) match await http.post("/api/like/{id}") { .ok { } .err(_) { items = before error = "Could not save. Try again." } } } Heading("Likes").h1 for i in items by i.id { Button(if i.liked { "♥" } else { "♡" }, aria-label: "Like").sm { on click { like(i.id) } } } if error != "" { Toast(error).danger } }

How it works

  • let before = items keeps the list as it was. A let is local to the action.
  • The next line gives items a new list with the one item flipped. The button repaints at once, before any request leaves.
  • The request is awaited and its result matched. If it failed, the .err arm puts before back and sets error, and the Toast tells the reader.
  • The action clears error first. A Toast shows once, when its branch is drawn, so without the reset a second failure would leave error as it was and show nothing.

Variations

Let the action roll itself back

optimistic(holder, change) does the same with less code inside an action. It applies the change at once, and if anything later in the action fails — a try passing its error up, or a throw — the change is taken back. The action declares -> !, so try may pass the failure up (Fetching data):

wf

action like(id: String) -> ! { optimistic(items, list => list.map(i => if i.id == id { Item(id: i.id, liked: !i.liked) } else { i })) try await http.post("/api/like/{id}") }

Change what a read shows

When the list comes from an endpoint of an api, change the read itself: .update(change) shows the change in every resource over that endpoint, and returns what takes it back. This store adds a todo at once, and takes it out again if the server refuses it:

wf

type Todo { id: String, title: String } type NewTodo { title: String } api Backend(base: "/api") { } api Backend.todos { get list() at "todos" -> [Todo]! post create(body: NewTodo) at "todos" -> Todo! invalidates [list] } store Todos { action add(title: String) { let undo = Backend.todos.list.update(list => list.concat([Todo(id: "new", title: title)])) if let .err(_) = await Backend.todos.create(body: NewTodo(title: title)) { undo() } } } page List(path: "/todos", title: "Todos", description: "Todos that show at once.") { use Todos resource todos = Backend.todos.list() Heading("Todos").h1 Button("Add bread") { on click { Todos.add("Bread") } } for todo in todos.value ?? [] by todo.id { Text(todo.title) } }

When the create succeeds, invalidates [list] asks for the list again, and the server's copy, with the todo's real id, takes the place of the one shown (Refresh a list after a write).