Skip to content

Commit

Permalink
update readme and doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
photovoltex committed Nov 26, 2023
1 parent 42af1d4 commit 3de55a2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
8 changes: 8 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -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'
68 changes: 50 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 will fail due to requiring wasm as target -->
```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,
}
```

Expand All @@ -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 will fail due to requiring wasm as target -->
```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 will fail due to requiring wasm as target -->
```compile_fail
// Test::listen_to_<field>
```rust , ignore
// this code is ignore due to required target wasm
let listen_handle = Test::listen_to_foo(|foo| { /* use received foo here */ })
```

Expand Down

0 comments on commit 3de55a2

Please sign in to comment.