Skip to content

Commit

Permalink
Update v2 docs
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Apr 30, 2024
1 parent 4e7f9d8 commit cb33951
Showing 1 changed file with 69 additions and 13 deletions.
82 changes: 69 additions & 13 deletions docs/v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,20 @@ use tauri_specta::{ts, js};
// this example exports your types on startup when in debug mode. You can do whatever.

fn main() {
let specta_builder = {
let invoke_handler = {
// You can use `tauri_specta::js::builder` for exporting JS Doc instead of Typescript!`
let specta_builder = tauri_specta::ts::builder()
.commands(tauri_specta::collect_commands![greet, greet2, greet3 ]); // <- Each of your comments
let builder = tauri_specta::ts::builder()
.commands(tauri_specta::collect_commands![greet, greet2, greet3 ]); // <- Each of your commands


#[cfg(debug_assertions)] // <- Only export on non-release builds
let specta_builder = specta_builder.path("../src/bindings.ts");
let builder = builder.path("../src/bindings.ts");

specta_builder.into_plugin()
builder.build().unwrap()
};

tauri::Builder::default()
.plugin(specta_builder)
.invoke_handler(invoke_handler)
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Expand All @@ -98,24 +98,50 @@ await commands.greet("Brendan");

## Events

Firstly you have to define your event types. You can add as many of these as you want.
You must reconfigure your builder to support events:

```diff
fn main() {
- let invoke_handler = {
+ let (invoke_handler, register_events) = {
// You can use `tauri_specta::js::builder` for exporting JS Doc instead of Typescript!`
let builder = tauri_specta::ts::builder()
.commands(tauri_specta::collect_commands![greet, greet2, greet3 ]) // <- Each of your commands
+ .events(tauri_specta::collect_events![]); // This should contain all your events.

#[cfg(debug_assertions)] // <- Only export on non-release builds
let builder = builder.path("../src/bindings.ts");

builder.build().unwrap()
};

tauri::Builder::default()
.invoke_handler(invoke_handler)
+ .setup(|app| {
+ register_events(app);
+ })
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
```

Now you can add your first event. You can add as many of these as your want.

```rust
#[derive(Debug, Clone, Serialize, Deserialize, specta::Type, tauri_specta::Event)]
pub struct DemoEvent(String);
```

Next you must add it to the builder like the following:

and make sure you register the event with Tauri Specta by adding it to the `collect_events` macro like the following:
```rust
let specta_builder = ts::builder()
.events(tauri_specta::collect_events![DemoEvent]); // This should contain all your events.
.events(tauri_specta::collect_events![DemoEvent]); // This should contain all your events, comma separated.
```

Then it can be used in Rust like the following:
Finally, you can setup a listener or emit a message from Rust like the following:

```rust
tauri::Builder::default()
.invoke_handler(invoke_handler)
.setup(|app| {
let handle = app.handle();

Expand All @@ -124,9 +150,19 @@ tauri::Builder::default()
});

DemoEvent("Test".to_string()).emit_all(&handle).unwrap();
});
})
...
```

Most methods take a `handle` which can be any of the following types:
- [`App`](https://docs.rs/tauri/2.0.0-beta.16/tauri/struct.App.html)
- [`AppHandle`](https://docs.rs/tauri/2.0.0-beta.16/tauri/struct.AppHandle.html)
- [`Webview`](https://docs.rs/tauri/2.0.0-beta.16/tauri/webview/struct.Webview.html)
- [`WebviewWindow`](https://docs.rs/tauri/2.0.0-beta.16/tauri/webview/struct.WebviewWindow.html)
- [`Window`](https://docs.rs/tauri/2.0.0-beta.16/tauri/window/struct.Window.html)

The [`Event`](https://docs.rs/tauri-specta/latest/tauri_specta/trait.Event.html) trait defines all methods that can be used to emit or listen so refer to it.

and it can be used in TS like the following:

```ts
Expand All @@ -145,3 +181,23 @@ await events.demoEvent.emit("Test")
// Emit to a window
await events.demoEvent(appWindow).emit("Test")
```

## Extra Types

Sometimes you might want to export a type to Typescript but it doesn't actually show up in any of our commands.

You can do that like the following:

```rust
let builder = ts::builder()
// < your commands and events are probally here
.types(TypeCollection::default().register::<Custom>()); // < call `register` as much as you want.
```

`register` only supports [named types](https://docs.rs/specta/2.0.0-rc.11/specta/type/trait.NamedType.html) as otherwise you would run into the following problem:
```rust
// vvv - What would this be without a name?
export ... = {};
```

Any type implemented using the [`Type`](https://docs.rs/specta/latest/specta/derive.Type.html) derive macro will meet this requirement.

0 comments on commit cb33951

Please sign in to comment.