Skip to content

Commit 0a786e1

Browse files
committed
add section explaining the _usage macros
1 parent dbced71 commit 0a786e1

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
![License](https://img.shields.io/crates/l/tauri-interop.svg)
66

77
What this crate tries to achieve:
8-
- generate a equal wasm-function for your defined `tauri::command`
8+
- generate an equal wasm-function for your defined `tauri::command`
99
- collecting all defined `tauri::command`s without adding them manually
10-
- a way to sending events from tauri and receiving them in the frontend
10+
- a convenient way to send events from tauri and receiving them in the frontend
1111

1212

1313
## Basic usage:
@@ -17,6 +17,52 @@ What this crate tries to achieve:
1717
> Some examples in this documentation can't be executed with doctests due to
1818
> required wasm target and tauri modified environment (see [withGlobalTauri](https://tauri.app/v1/api/config/#buildconfig.withglobaltauri))
1919
20+
### QOL macros
21+
22+
This crate also adds some quality-of-life macros. These are intended to ease the drawbacks of compiling to
23+
multiple architectures.
24+
25+
#### Conditional `use`
26+
Because most crates are not intended to be compiled to wasm and most wasm crates are not intended to be compiled to
27+
the host-triplet they have to be excluded in each others compile process. The usual process to exclude uses for a certain
28+
architecture would look something like this:
29+
30+
```rust
31+
#[cfg(not(target_family = "wasm"))]
32+
use tauri::AppHandle;
33+
34+
#[tauri_interop::command]
35+
pub fn empty_invoke(_handle: AppHandle) {}
36+
```
37+
38+
**General usage:**
39+
40+
With the help of `tauri_interop::host_usage!()` and `tauri_interop::wasm_usage!()` we don't need to remember which
41+
attribute we have to add and can just convert the above to the following:
42+
43+
```rust
44+
tauri_interop::host_usage! {
45+
use tauri::AppHandle;
46+
}
47+
48+
#[tauri_interop::command]
49+
pub fn empty_invoke(_handle: AppHandle) {}
50+
```
51+
52+
**Multiple `use` usage:**
53+
54+
When multiple `use` should be excluded, they need to be separated by a single pipe (`|`). For example:
55+
56+
```rust
57+
tauri_interop::host_usage! {
58+
use tauri::State;
59+
| use std::sync::RwLock;
60+
}
61+
62+
#[tauri_interop::command]
63+
pub fn empty_invoke(_state: State<RwLock<String>>) {}
64+
```
65+
2066
### Command (Frontend => Backend Communication)
2167
> For more examples see [cmd.rs](./test-project/api/src/cmd.rs) in test-project
2268

0 commit comments

Comments
 (0)