Created
August 1, 2022 17:20
-
-
Save akiross/fbfdaa14facc5e0f0074a09f81656272 to your computer and use it in GitHub Desktop.
Tauri async sleep event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "app" | |
version = "0.1.0" | |
description = "A Tauri App" | |
authors = ["you"] | |
license = "" | |
repository = "" | |
default-run = "app" | |
edition = "2021" | |
rust-version = "1.57" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[build-dependencies] | |
tauri-build = { version = "1.0.4", features = [] } | |
[dependencies] | |
serde_json = "1.0" | |
serde = { version = "1.0", features = ["derive"] } | |
tauri = { version = "1.0.5", features = ["api-all"] } | |
tokio = { version = "1.20.1", features = ["time"] } | |
[features] | |
# by default Tauri runs in production mode | |
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL | |
default = [ "custom-protocol" ] | |
# this feature is used used for production builds where `devPath` points to the filesystem | |
# DO NOT remove this | |
custom-protocol = [ "tauri/custom-protocol" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Document</title> | |
</head> | |
<body> | |
<h1>Welcome from Tauri!</h1> | |
<div id="message"></div> | |
<script> | |
const invoke = window.__TAURI__.invoke; | |
const listen = window.__TAURI__.event.listen; | |
invoke("greet", { name: "Patrick" }).then( | |
(response) => (document.getElementById("message").innerText = response) | |
); | |
listen("timer", (event) => | |
document.getElementById("message").innerText = "GoT tImEr EvEnt!"; | |
}).then((response) => console.log("Done listen")); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![cfg_attr( | |
all(not(debug_assertions), target_os = "windows"), | |
windows_subsystem = "windows" | |
)] | |
use tauri::{App, Manager}; | |
use core::time::Duration; | |
#[tauri::command] | |
fn greet(name: &str) -> String { | |
format!("Hello, {}!", name) | |
} | |
#[derive(Clone, serde::Serialize)] | |
struct Payload { | |
time: i32, | |
} | |
fn main() { | |
tauri::Builder::default() | |
.invoke_handler(tauri::generate_handler![greet]) | |
.setup(|app| { | |
let ah = app.handle(); | |
tauri::async_runtime::spawn(async move { | |
let ah = ah; | |
tokio::time::sleep(Duration::from_secs(10)).await; | |
println!("Ciaone!"); | |
ah.emit_all("timer", Payload{ time: 1 }); | |
}); | |
Ok(()) | |
}) | |
.run(tauri::generate_context!()) | |
.expect("error while running tauri application"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment