From 3de55a2590f8d7305156fd8459f115870214e9e8 Mon Sep 17 00:00:00 2001 From: photovoltex Date: Sun, 26 Nov 2023 13:44:59 +0100 Subject: [PATCH] update readme and doc tests --- .cargo/config.toml | 8 ++++++ README.md | 68 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 83a15ff..8ca4920 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,11 @@ [build] +# this feature would allow to ignore certain doctest on certain targets +# due to dev-dependencies are included in both targets we can't use this yet +rustdocflags = ["-Z", "unstable-options", "--enable-per-target-ignores"] # uncommented when checking wasm code on correctnes and reload rust-analyzer # target = "wasm32-unknown-unknown" + +# only works if cargo install wasm-bindgen-cli --vers "X.Y.Z" +# where "X.Y.Z" => currently used (see Cargo.toml) wasm_bindgen version +[target.wasm32-unknown-unknown] +runner = 'wasm-bindgen-test-runner' diff --git a/README.md b/README.md index 4a83e43..a6b2f11 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,20 @@ What this crate tries to achieve: ### Command (Frontend => Backend Communication) Definition for both tauri supported triplet and wasm: -```rust +```rust , ignore-wasm32-unknown-unknown #[tauri_interop::command] fn greet(name: &str) -> String { format!("Hello, {}! You've been greeted from Rust!", name) } + +// generated the `get_handlers()` function +tauri_interop::collect_commands!(); + +fn main() { + tauri::Builder::default() + // This is where you pass in the generated handler collector + .invoke_handler(get_handlers()); +} ``` Using `tauri_interop::command` does two things: @@ -30,18 +39,19 @@ Using `tauri_interop::command` does two things: The defined command above can then be used in wasm as below. Due to receiving data from tauri via a promise, the command response has to be awaited. - -```compile_fail +```rust , ignore +// this code is ignore due to required target wasm let greetings = greet("frontend").await; ``` ### Event (Backend => Frontend Communication) Definition for both tauri supported triplet and wasm: -```rust +```rust , ignore-wasm32-unknown-unknown +#[derive(Default)] #[tauri_interop::emit_or_listen] pub struct Test { - pub foo: String, - pub bar: i32, + foo: String, + pub bar: bool, } ``` @@ -51,21 +61,43 @@ which are used depending on the `target_family` - `tauri_interop::emit` is used otherwise To emit a variable from the above struct (which is mostly intended to be used as state) in the host triplet - -```compile_fail -let test = Test { - foo: "foo".into(), - bar: 69 -}; - -// where `handle` is of type `tauri::AppHandle` -test.emit(&handle, TestEmit::Foo); +```rust , ignore-wasm32-unknown-unknown +#[derive(Default)] +#[tauri_interop::emit_or_listen] +pub struct Test { + foo: String, + pub bar: bool, +} + +// one context where `tauri::AppHandle` can be obtained +#[tauri_interop::command] +fn emit_bar(handle: tauri::AppHandle) { + let mut test = Test::default(); + + test.emit(&handle, TestEmit::Bar); // emits `false` + test.bar = true; + test.emit(&handle, TestEmit::Bar); // emits updated value `true` +} + +// a different context where `tauri::AppHandle` can be obtained +fn main() { + tauri::Builder::default() + .setup(|app| { + let handle: tauri::AppHandle = app.handle(); + + let mut test = Test::default(); + + // to emit and update an field an update function for each field is generated + test.update_foo(&handle, "Bar".into()); // emits '"Bar"' + + Ok(()) + }); +} ``` the above emitted value can then be received in wasm as: - -```compile_fail -// Test::listen_to_ +```rust , ignore +// this code is ignore due to required target wasm let listen_handle = Test::listen_to_foo(|foo| { /* use received foo here */ }) ```