Files
openidea/src-tauri/tests/blocking_commands.rs
T
rohitkumardandaandrohitdanda 034b2b9f65 fix(app): keep the window responsive while local history, shelves and run files load (#2789)
Tauri runs commands declared without `async` on the main thread, so Local History, shelf, change-list and run-configuration reads and writes, `.env` loading and the line diff of two arbitrary texts blocked input, painting and every other IPC call while they ran. They are now async and run on the blocking pool; IPC signatures are unchanged. `vcs_changelists_save` stays synchronous so its fire-and-forget saves land in order. A guard test keeps these commands async.

Co-authored-by: rohitdanda <10144914+rohitdanda@users.noreply.github.com>
2026-11-12 02:06:22 +08:00

47 lines
1.6 KiB
Rust

//! Tauri runs a command declared without `async` on the main thread, so disk I/O or a
//! large diff there freezes the window (input, painting, every other IPC call).
/// Commands that read or write project files, Local History or `.env` files, or diff two
/// arbitrary texts: they must be `async` and do that work on the blocking pool.
const OFF_MAIN_THREAD: &[(&str, &[&str])] = &[
("doc.rs", &["history_list", "history_content"]),
(
"vcs.rs",
&[
"vcs_diff_hunks",
"vcs_shelves",
"vcs_shelf_delete",
"vcs_shelf_rename",
"vcs_shelf_patch",
"vcs_changelists_load",
],
),
(
"run.rs",
&[
"run_save_configs",
"run_templates",
"run_save_templates",
"run_import_sources",
"run_import_configs",
"run_env_files",
],
),
];
#[test]
fn disk_and_diff_commands_stay_off_the_main_thread() {
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/commands");
let mut on_main = Vec::new();
for (file, commands) in OFF_MAIN_THREAD {
let source = std::fs::read_to_string(dir.join(file)).unwrap();
for command in *commands {
assert!(source.contains(&format!("fn {command}(")), "{file} has no command {command}");
if !source.contains(&format!("pub async fn {command}(")) {
on_main.push(*command);
}
}
}
assert!(on_main.is_empty(), "synchronous commands run on the main thread: {on_main:?}");
}