Add a backend command the page calls
Purpose
- Add one backend command the page calls as
window.playPets.<name>(). - The example is
centerFriend, which moves a friend to the middle of the yard. Name a real command for what it does. - Data flows 1 and 2 in Architecture: PlayPets app show the full path a command takes.
Prerequisites
- You finished Set up a development environment and run the app.
pnpm testandpnpm run test:e2epass.- You read the backend locks standard and the i18n standard.
- Commands run on a blocking thread, not the main thread.
- Each handler takes the app state lock itself, with
lock(state). - A handler does slow work (network, dialogs, the keychain) without holding the lock.
Steps
- Add one row to the
COMMANDStable insrc-tauri/src/commands.rs.- A row gives the command name (the page's method), who may call it, and what it does.
- Who may call it:
main(the yard window),overlay(the desktop windows need it too) ortest_only(the e2e scenario). - The table drives the bridge script, the check of which window may call what, and the dispatch.
- Read arguments with
c.text(i),c.arg(i)andc.float(i);c.parse::<T>(i)reads a structured one and answers a bad request when it does not fit.
rust
static COMMANDS: &[Command] = &[
// …
main("centerFriend", |c| center_friend(c.app, &c.state, c.text(0))),
];- Write the handler in the
commands/file for its area (friends.rs,imports.rs,agents.rs,prefs.rs), or in the module that owns the area.- A handler that changes the state goes through
commit: it takes the lock, runs your change, then saves and notifies every window once. - If the save fails, the change still stands and every window shows it;
committhen returns the error.
- A handler that changes the state goes through
rust
fn center_friend(app: &AppHandle, state: &State<Shared>, id: &str) -> Result<Value, String> {
commit(app, state, |d| {
let pet = d.pets.iter_mut().find(|p| p.id == id).ok_or_else(|| i18n::t("error.friend_missing"))?;
pet.x = 0.5;
pet.y = 0.5;
Ok(Value::Null)
})
}For a new error sentence, add a row at the same position in every table under
src-tauri/src/i18n/(ko.rs,en.rs,ja.rs,zh_hans.rs).- Skip this step if an existing key (
error.friend_missing) works.
- Skip this step if an existing key (
Call it from the page. On failure
callthrows, andguardshows the error sentence as a notice.
js
import { $, call, guard, selected } from '../core/state.js';
$('center-button').onclick = guard(() => call('centerFriend', selected));- Put logic with rules (position math, for example) in pure functions, and test them in
mod testsin the same file. - If people see the flow, add a line that calls it to the scenario in
tests/e2e.mjsand a line that checks it to its expected list.each_command_is_named_once…incommands::testschecks the table for a name listed twice and for what the overlay may ask for.
Verification
bash
pnpm test
pnpm run lint
pnpm run test:e2e- All three pass.
- Without the key from step 3, the Rust test
i18n::tests::every_key_asked_for_is_in_the_tablefails. - Run the app and press the button: the friend moves to the middle of the yard (
changednotified every window). - Restart the app: the friend is still there (
commitsaved it).
Troubleshooting
| Symptom | Cause |
|---|---|
playPets.centerFriend is not a function on the page | The row from step 1 is missing, or it is test_only |
Calling it gives "That can't be done here. Reopen the app." (error.not_allowed) | You called it from the desktop window and the row says main (use overlay) |
| The app freezes | The handler waits on the main thread or on slow work while it holds the lock. Check it against the locks standard |