Skip to content

Commit

Permalink
update readme, changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
MatsDK committed Jun 17, 2023
1 parent 5006960 commit 60deedf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-peas-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"taurpc": minor
---

use state/window/app_handle in commands
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,53 @@ trait Api {
}
```

# Accessing managed state

You can use Tauri's managed state within your commands, along the `state` argument, you can also use the `window` and `app_handle` arguments. [Tauri docs](https://tauri.app/v1/guides/features/command/#accessing-the-window-in-commands)

If you want your state to be mutable, you need to use a container that enables interior mutability, like a [Mutex](https://doc.rust-lang.org/std/sync/struct.Mutex.html).

```rust
// src-tauri/src/main.rs

use std::sync::Mutex;
use tauri::{Manager, Runtime, State, Window};

type MyState = Mutex<String>;

#[taurpc::procedures]
trait Api {
fn method_with_state(state: State<MyState>);

fn method_with_window<R: Runtime>(window: Window<R>);
}

#[derive(Clone)]
struct ApiImpl;
impl Api for ApiImpl {
fn with_state(self, state: State<MyState>) {
// ...
}

fn with_window<R: Runtime>(self, window: Window<R>) {
// ...
}
}

fn main() {
tauri::Builder::default()
.invoke_handler(taurpc::create_rpc_handler(ApiImpl.into_handler()))
.manage(Mutex::new("some state value".to_string()))
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
```

# Features

- [x] Basic inputs
- [x] Struct inputs
- [ ] Sharing state
- [x] Sharing state
- [ ] Renaming methods
- [ ] Merging routers
- [ ] Custom error handling
Expand Down
13 changes: 5 additions & 8 deletions example/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use std::sync::{Arc, Mutex};

use std::sync::Mutex;
use tauri::{Manager, Runtime};

#[taurpc::rpc_struct]
Expand Down Expand Up @@ -29,9 +28,8 @@ struct ApiImpl;
impl Api for ApiImpl {
fn test_state(self, input: String, state: tauri::State<GlobalState>) {
let mut data = state.lock().unwrap();
println!("{:?}", data);
*data = input;
println!("called `test`");
println!("{:?}", data);
}

fn test_window<R: Runtime>(self, window: tauri::Window<R>) {
Expand All @@ -43,8 +41,7 @@ impl Api for ApiImpl {
println!("{:?}, {:?}", app_dir, app_handle.package_info());
}

fn test_event(self, input1: String, user: u8) -> Option<User> {
println!("called `test_event` {}, {}", input1, user);
fn test_event(self, input1: String, _user: u8) -> Option<User> {
Some(User {
first_name: input1.clone(),
last_name: input1,
Expand All @@ -54,7 +51,7 @@ impl Api for ApiImpl {
}
}

type GlobalState = Arc<Mutex<String>>;
type GlobalState = Mutex<String>;

fn main() {
tauri::Builder::default()
Expand All @@ -64,7 +61,7 @@ fn main() {
app.get_window("main").unwrap().open_devtools();
Ok(())
})
.manage(Arc::new(Mutex::new(String::from("default value"))))
.manage(Mutex::new("some state value".to_string()))
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
7 changes: 4 additions & 3 deletions taurpc/taurpc-macros/src/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use syn::{
parenthesized,
parse::{self, Parse, ParseStream},
spanned::Spanned,
FnArg, Generics, Ident, Pat, PatType, ReturnType, Token, Type, Visibility,
FnArg, Generics, Ident, Pat, PatType, ReturnType, Token, Visibility,
};

use crate::{parse_arg_key, parse_args};
Expand Down Expand Up @@ -83,6 +83,7 @@ impl Parse for RpcMethod {
let mut args = Vec::new();
for arg in content.parse_terminated(FnArg::parse, Token![,])? {
match arg {
// TODO: allow other Pat variants
FnArg::Typed(pat_ty) if matches!(*pat_ty.pat, Pat::Ident(_)) => {
args.push(pat_ty);
}
Expand Down Expand Up @@ -176,8 +177,6 @@ impl<'a> ProceduresGenerator<'a> {
})
.collect::<Vec<_>>();

println!("{:?}", types);

quote! {
#ident(( #( #types ),* ))
}
Expand All @@ -186,6 +185,7 @@ impl<'a> ProceduresGenerator<'a> {
quote! {
#[derive(taurpc::TS, taurpc::Serialize)]
#[serde(tag = "proc_name", content = "input_type")]
#[allow(non_camel_case_types)]
#vis enum #inputs_ident {
#( #inputs ),*
}
Expand Down Expand Up @@ -215,6 +215,7 @@ impl<'a> ProceduresGenerator<'a> {
quote! {
#[derive(taurpc::TS, taurpc::Serialize)]
#[serde(tag = "proc_name", content = "output_type")]
#[allow(non_camel_case_types)]
#vis enum #outputs_ident {
#( #outputs ),*
}
Expand Down

0 comments on commit 60deedf

Please sign in to comment.