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:
parent
300fe18d22
commit
e28a0bb749
18 changed files with 655 additions and 289 deletions
|
@ -53,7 +53,7 @@ pub async fn pick_files() -> anyhow::Result<()> {
|
|||
.await?;
|
||||
|
||||
ensure!(file.is_some());
|
||||
ensure!(file.unwrap().len() > 1);
|
||||
ensure!(file.unwrap().count() > 1);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ pub async fn pick_folders() -> anyhow::Result<()> {
|
|||
.await?;
|
||||
|
||||
ensure!(file.is_some());
|
||||
ensure!(file.unwrap().len() > 1);
|
||||
ensure!(file.unwrap().count() > 1);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,10 +1,38 @@
|
|||
use anyhow::ensure;
|
||||
use futures::StreamExt;
|
||||
use tauri_sys::{event, tauri};
|
||||
|
||||
pub async fn emit() -> anyhow::Result<()> {
|
||||
event::emit("foo", &"bar").await?;
|
||||
event::emit("javascript-event", &"bar").await?;
|
||||
|
||||
ensure!(tauri::invoke::<_, bool>("verify_receive", &()).await?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn listen() -> anyhow::Result<()> {
|
||||
let events = event::listen::<u32>("rust-event-listen").await?;
|
||||
tauri::invoke::<_, ()>("emit_event_5_times", &()).await?;
|
||||
|
||||
let events: Vec<u32> = events
|
||||
.take(5)
|
||||
.map(|e| e.payload)
|
||||
.collect()
|
||||
.await;
|
||||
|
||||
ensure!(events == vec![0, 1, 2, 3, 4]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn once() -> anyhow::Result<()> {
|
||||
// this causes enough delay for `once` to register it's event listener before the event gets triggered
|
||||
wasm_bindgen_futures::spawn_local(async {
|
||||
tauri::invoke::<_, ()>("emit_event", &()).await.unwrap();
|
||||
});
|
||||
let event = event::once::<String>("rust-event-once").await?;
|
||||
|
||||
ensure!(event.payload == "Hello World from Rust!");
|
||||
|
||||
Ok(())
|
||||
}
|
31
examples/test/src/global_shortcut.rs
Normal file
31
examples/test/src/global_shortcut.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use futures::StreamExt;
|
||||
use tauri_sys::global_shortcut;
|
||||
|
||||
pub async fn register_all() -> anyhow::Result<()> {
|
||||
let task = async {
|
||||
let shortcuts = ["CommandOrControl+Shift+C", "Ctrl+Alt+F12"];
|
||||
|
||||
let streams = futures::future::try_join_all(shortcuts.map(|s| async move {
|
||||
let stream = global_shortcut::register(s).await?;
|
||||
|
||||
anyhow::Ok(stream.map(move |_| s))
|
||||
}))
|
||||
.await?;
|
||||
|
||||
let mut events = futures::stream::select_all(streams);
|
||||
|
||||
while let Some(shortcut) = events.next().await {
|
||||
log::debug!("Shortcut {} triggered", shortcut)
|
||||
}
|
||||
|
||||
anyhow::Ok(())
|
||||
};
|
||||
|
||||
let timeout = gloo_timers::future::sleep(Duration::from_secs(20));
|
||||
|
||||
futures::future::select(Box::pin(task), timeout).await;
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -6,6 +6,7 @@ mod notification;
|
|||
mod os;
|
||||
mod tauri_log;
|
||||
mod window;
|
||||
mod global_shortcut;
|
||||
|
||||
extern crate console_error_panic_hook;
|
||||
use log::LevelFilter;
|
||||
|
@ -39,7 +40,7 @@ where
|
|||
}
|
||||
|
||||
#[component]
|
||||
pub async fn Test<'a, G: Html, F>(cx: Scope<'a>, props: TestProps<'a, F>) -> View<G>
|
||||
pub async fn TestInner<'a, G: Html, F>(cx: Scope<'a>, props: TestProps<'a, F>) -> View<G>
|
||||
where
|
||||
F: Future<Output = anyhow::Result<()>> + 'a,
|
||||
{
|
||||
|
@ -64,9 +65,30 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn Test<'a, G: Html, F>(cx: Scope<'a>, props: TestProps<'a, F>) -> View<G>
|
||||
where
|
||||
F: Future<Output = anyhow::Result<()>> + 'a,
|
||||
{
|
||||
let fallback = view! { cx,
|
||||
tr {
|
||||
td { code { (props.name.to_string()) } }
|
||||
td {
|
||||
span(class="loader") { "⏳" }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
view! { cx,
|
||||
Suspense(fallback=fallback) {
|
||||
TestInner(name=props.name, test=props.test)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ci"))]
|
||||
#[component]
|
||||
pub async fn InteractiveTest<'a, G: Html, F>(cx: Scope<'a>, props: TestProps<'a, F>) -> View<G>
|
||||
pub fn InteractiveTest<'a, G: Html, F>(cx: Scope<'a>, props: TestProps<'a, F>) -> View<G>
|
||||
where
|
||||
F: Future<Output = anyhow::Result<()>> + 'a,
|
||||
{
|
||||
|
@ -81,19 +103,8 @@ where
|
|||
(if *render_test.get() {
|
||||
let test = test.take().unwrap();
|
||||
|
||||
let fallback = view! { cx,
|
||||
tr {
|
||||
td { code { (props.name.to_string()) } }
|
||||
td {
|
||||
"Running Test..."
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
view! { cx,
|
||||
Suspense(fallback=fallback) {
|
||||
Test(name=props.name, test=test)
|
||||
}
|
||||
Test(name=props.name, test=test)
|
||||
}
|
||||
} else {
|
||||
view! { cx,
|
||||
|
@ -148,12 +159,14 @@ fn main() {
|
|||
view! { cx,
|
||||
table {
|
||||
tbody {
|
||||
Suspense(fallback=view!{ cx, "Running Tests..." }) {
|
||||
// Suspense(fallback=view!{ cx, "Running Tests..." }) {
|
||||
Test(name="app::get_name",test=app::get_name())
|
||||
Test(name="app::get_version",test=app::get_version())
|
||||
Test(name="app::get_tauri_version",test=app::get_tauri_version())
|
||||
Test(name="clipboard::read_text | clipboard::write_text",test=clipboard::test())
|
||||
Test(name="event::emit",test=event::emit())
|
||||
Test(name="event::listen",test=event::listen())
|
||||
Test(name="event::once",test=event::once())
|
||||
InteractiveTest(name="dialog::message",test=dialog::message())
|
||||
InteractiveTest(name="dialog::ask",test=dialog::ask())
|
||||
InteractiveTest(name="dialog::confirm",test=dialog::confirm())
|
||||
|
@ -170,11 +183,12 @@ fn main() {
|
|||
Test(name="notification::is_permission_granted",test=notification::is_permission_granted())
|
||||
Test(name="notification::request_permission",test=notification::request_permission())
|
||||
InteractiveTest(name="notification::show_notification",test=notification::show_notification())
|
||||
InteractiveTest(name="global_shortcut::register_all",test=global_shortcut::register_all())
|
||||
|
||||
// Test(name="window::WebviewWindow::new",test=window::create_window())
|
||||
|
||||
Terminate
|
||||
}
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ use tauri_sys::tauri;
|
|||
struct LogArgs {
|
||||
level: Level,
|
||||
message: String,
|
||||
location: String,
|
||||
file: Option<String>,
|
||||
line: Option<u32>,
|
||||
}
|
||||
|
@ -56,6 +57,7 @@ impl log::Log for TauriLogger {
|
|||
if self.enabled(record.metadata()) {
|
||||
let args = LogArgs {
|
||||
level: record.level().into(),
|
||||
location: record.target().to_string(),
|
||||
message: format!("{}", record.args()),
|
||||
file: record.file().map(ToString::to_string),
|
||||
line: record.line(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue