Skip to content

Commit

Permalink
generate wrapped updater functions
Browse files Browse the repository at this point in the history
  • Loading branch information
photovoltex committed Nov 26, 2023
1 parent 183ff7a commit 013b26d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
17 changes: 14 additions & 3 deletions tauri-interop-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,23 @@ pub fn emit(_: TokenStream, stream: TokenStream) -> TokenStream {
let field_ident = field.ident.as_ref().expect("handled before");
let variation = field_ident.to_string().to_case(Case::Pascal);

(format_ident!("{field_ident}"), format_ident!("{variation}"))
(format_ident!("{field_ident}"), format_ident!("{variation}"), &field.ty)
})
.collect::<Vec<_>>();

let struct_ident = &stream_struct.ident;
let mut updaters = Vec::new();
let mapped_variants = variants
.iter()
.map(|(field_ident, variant_ident)| {
.map(|(field_ident, variant_ident, ty)| {
let update = format_ident!("update_{}", field_ident);
updaters.push(quote!{
pub fn #update(&mut self, handle: &tauri::AppHandle, #field_ident: #ty) -> Result<(), tauri::Error> {
self.#field_ident = #field_ident;
self.emit(handle, #name::#variant_ident)
}
});

quote! {
#name::#variant_ident => {
log::trace!(
Expand All @@ -80,7 +89,7 @@ pub fn emit(_: TokenStream, stream: TokenStream) -> TokenStream {

let variants = variants
.into_iter()
.map(|(_, variation)| variation)
.map(|(_, variation, _)| variation)
.collect::<Vec<_>>();

let stream = quote! {
Expand All @@ -92,6 +101,8 @@ pub fn emit(_: TokenStream, stream: TokenStream) -> TokenStream {
#stream_struct

impl #struct_ident {
#( #updaters )*

#[must_use]
pub fn emit(&self, handle: &::tauri::AppHandle, field: #name) -> Result<(), tauri::Error> {
use tauri::Manager;
Expand Down
13 changes: 5 additions & 8 deletions test-project/api/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@ pub fn result_test() -> Result<i32, String> {

#[tauri_interop::command]
pub fn emit(handle: tauri::AppHandle) {
log::info!("echo cmd received");
log::info!("emit cmd received");

let test_state = TestState {
echo: String::from("value"),
foo: 420,
bar: false,
};
let mut test_state = TestState::default();
test_state.update_foo(&handle, "update from backend".into()).unwrap();

test_state.emit(&handle, TestStateEmit::Echo).unwrap();
test_state.emit(&handle, TestStateEmit::Foo).unwrap();
test_state.emit(&handle, TestStateEmit::Bar).unwrap();
test_state.bar = true;
test_state.emit(&handle, TestStateEmit::Bar).unwrap();
}

Expand Down
2 changes: 2 additions & 0 deletions test-project/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::disallowed_names)]

#![feature(iter_intersperse)]

pub mod cmd;
Expand Down
7 changes: 5 additions & 2 deletions test-project/api/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#[allow(dead_code)]
#[derive(Default)]
#[tauri_interop::emit_or_listen]
pub struct TestState {
pub echo: String,
pub foo: i32,
foo: String,
pub bar: bool,
}

// /// not allowed
// #[tauri_interop::emit_or_listen]
// pub struct StructTupleState(String);

// /// not allowed
// #[tauri_interop::emit_or_listen]
// pub struct PanicState {}
7 changes: 1 addition & 6 deletions test-project/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use api::model::TestState;
use gloo_timers::callback::Timeout;

fn main() {
console_log::init_with_level(log::Level::Debug).expect("no errors during logger init");
console_log::init_with_level(log::Level::Trace).expect("no errors during logger init");
console_error_panic_hook::set_once();

api::cmd::empty_invoke();
Expand All @@ -12,10 +12,6 @@ fn main() {
});

wasm_bindgen_futures::spawn_local(async move {
let handle_echo = TestState::listen_to_echo(|echo| log::info!("echo: {echo}"))
.await
.unwrap();

let handle_foo = TestState::listen_to_foo(|echo| log::info!("foo: {echo}"))
.await
.unwrap();
Expand All @@ -31,7 +27,6 @@ fn main() {
// it can be fixed with `handle.closure.take().unwrap().forget()`
// see the `Closure::forget` docs, why this isn't the recommended way
Timeout::new(2000, move || {
handle_echo.detach_listen();
handle_foo.detach_listen();
handle_bar.detach_listen();
})
Expand Down

0 comments on commit 013b26d

Please sign in to comment.