feat: improve DX of events (#12)

* feat: improve DX of events

* Update global_shortcut.rs

* Update event.rs

* deploy docs to gh pages

* Delete rustdoc.yml

* add tests for global shortcut

* improve logs produced by tauri_log

* wip docs

* update docs

* move error to separate module

* feat: simplify functions returning array backed iterators

* rebase and cleanup

* fixes
This commit is contained in:
Jonas Kruckenberg 2022-11-19 20:33:06 +01:00 committed by GitHub
parent 300fe18d22
commit e28a0bb749
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 655 additions and 289 deletions

View file

@ -4,7 +4,7 @@
)]
use std::sync::atomic::{AtomicBool, Ordering};
use tauri::{Manager, State};
use tauri::{Manager, Runtime, State, Window};
use tauri_plugin_log::{LogTarget, LoggerBuilder};
struct Received(AtomicBool);
@ -14,6 +14,22 @@ fn verify_receive(emitted: State<Received>) -> bool {
emitted.0.load(Ordering::Relaxed)
}
#[tauri::command]
async fn emit_event<R: Runtime>(win: Window<R>) -> Result<(), ()> {
let _ = win.emit("rust-event-once", "Hello World from Rust!");
Ok(())
}
#[tauri::command]
async fn emit_event_5_times<R: Runtime>(win: Window<R>) -> Result<(), ()> {
for i in 0..5 {
let _ = win.emit("rust-event-listen", i);
}
Ok(())
}
#[tauri::command]
fn exit_with_error(e: &str) -> bool {
eprintln!("{}", e);
@ -35,12 +51,12 @@ fn main() {
tauri::Builder::default()
.plugin(log_plugin)
.invoke_handler(tauri::generate_handler![verify_receive, exit_with_error])
.invoke_handler(tauri::generate_handler![verify_receive, emit_event, emit_event_5_times, exit_with_error])
.setup(|app| {
app.manage(Received(AtomicBool::new(false)));
let app_handle = app.handle();
app.listen_global("foo", move |_| {
app.listen_global("javascript-event", move |_| {
app_handle
.state::<Received>()
.0