Why this event does'nt work? #10033
-
I'm writing an rust ide on android. Since tauri(2.0-beta) doesn't yet support mobile to use sidecar/resources, I use Then I need to extract some resources(like proot, rootfs, prebuilted rust-analyzer, etc.) when the program starts and put them in a place like I want users to see the progress of resource extraction, So I do this(vue3):
import { ref, onMounted, onUnmounted } from "vue";
import { useRouter } from "vue-router";
import { VList, VListItem, VIcon } from "vuetify/components";
import { listen, UnlistenFn } from "@tauri-apps/api/event";
import { invoke } from "@tauri-apps/api/core";
interface Payload {
message: string;
}
let unlisten: UnlistenFn | null = null;
const router = useRouter();
const setupProcess = ref("Waiting until setup process complete...");
const setupCompleted = ref(false);
onMounted(async () => {
invoke("init_resources").then(() => {
setupCompleted.value = true;
});
unlisten = await listen<Payload>("setup-process", (event) => {
const { message } = event.payload;
setupProcess.value += `\n${message}`;
});
});
onUnmounted(() => {
unlisten?.();
});
function routeToProjectView() {
router.push("/project");
}
#[derive(Clone, serde::Serialize)]
pub struct Payload {
message: String,
}
impl Payload {
pub fn new(message: String) -> Self {
Self { message }
}
}
#[tauri::command]
fn init_resources(app: AppHandle) {
app.emit("setup-process", Payload::new("Checking resources".into()))
.unwrap();
async_runtime::block_on(Resources::auto_update()).unwrap();
app.emit(
"setup-process",
Payload::new("Checking proot rootfs".into()),
)
.unwrap();
setup_rootfs().unwrap();
app.emit("setup-process", Payload::new("All done.".into()))
.unwrap();
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![init_resources])
.run(tauri::generate_context!())
.expect("error while running tauri application");
} However, when I was testing it, I found that no events were received, and if I tried to await listen, it just kept blocking current thread. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Did you try changing this onMounted(async () => {
invoke("init_resources").then(() => {
setupCompleted.value = true;
});
unlisten = await listen<Payload>("setup-process", (event) => {
const { message } = event.payload;
setupProcess.value += `\n${message}`;
});
}); to this? onMounted(async () => {
unlisten = await listen<Payload>("setup-process", (event) => {
const { message } = event.payload;
setupProcess.value += `\n${message}`;
});
invoke("init_resources").then(() => {
setupCompleted.value = true;
});
}); If the command executes too fast it will be finished before you have a chance to register the events, so just make sure the listeners are registered beforehand and it should work. |
Beta Was this translation helpful? Give feedback.
-
So this is my solution:
{
"$schema": "../gen/schemas/mobile-schema.json",
"identifier": "main-capability",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": ["event:default"] // add any other permissions you need!
} and event should work fine |
Beta Was this translation helpful? Give feedback.
SO THAT'S IT. There is a permission error. That's strange, I think it should be allowed by default