Skip to content

Commit 1d6f418

Browse files
authored
refactor(core): merge invoke items into single struct, allow ? (#1683)
1 parent 152c755 commit 1d6f418

File tree

14 files changed

+314
-455
lines changed

14 files changed

+314
-455
lines changed

.changes/plugin-refactor.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
---
44

55
Refactored the `Plugin` trait `initialize` and `extend_api` signatures.
6-
`initialize` now takes the `App` as first argument, and `extend_api` takes a `InvokeResolver` as last argument.
6+
`initialize` now takes the `App` as first argument, and `extend_api` takes an `Invoke` instead of `InvokeMessage`.
77
This adds support to managed state on plugins.

.changes/remove-with-window.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"tauri": patch
33
---
44

5-
Removes the `with_window` attribute on the `command` macro. Tauri now infers it using the `FromCommand` trait.
5+
Removes the `with_window` attribute on the `command` macro. Tauri now infers it using the `CommandArg` trait.

core/tauri-macros/src/command.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn generate_command(function: ItemFn) -> TokenStream {
6363
}
6464
}
6565

66-
let arg_name_ = arg_name.clone().unwrap();
66+
let arg_name_ = arg_name.unwrap();
6767
let arg_name_s = arg_name_.to_string();
6868

6969
let arg_type = match arg_type {
@@ -76,13 +76,14 @@ pub fn generate_command(function: ItemFn) -> TokenStream {
7676
}
7777
};
7878

79-
invoke_args.append_all(quote! {
80-
let #arg_name_ = match <#arg_type>::from_command(#fn_name_str, #arg_name_s, &message) {
81-
Ok(value) => value,
82-
Err(e) => return tauri::InvokeResponse::error(::tauri::Error::InvalidArgs(#fn_name_str, e).to_string())
83-
};
79+
let item = quote!(::tauri::command::CommandItem {
80+
name: #fn_name_str,
81+
key: #arg_name_s,
82+
message: &__message,
8483
});
85-
invoke_arg_names.push(arg_name_.clone());
84+
85+
invoke_args.append_all(quote!(let #arg_name_ = <#arg_type>::from_command(#item)?;));
86+
invoke_arg_names.push(arg_name_);
8687
invoke_arg_types.push(arg_type);
8788
}
8889

@@ -97,21 +98,19 @@ pub fn generate_command(function: ItemFn) -> TokenStream {
9798
// otherwise we wrap it with an `Ok()`, converting the return value to tauri::InvokeResponse
9899
// note that all types must implement `serde::Serialize`.
99100
let return_value = if returns_result {
100-
quote! {
101-
match #fn_name(#(#invoke_arg_names),*)#await_maybe {
102-
Ok(value) => ::core::result::Result::<_, ()>::Ok(value).into(),
103-
Err(e) => ::core::result::Result::<(), _>::Err(e).into(),
104-
}
105-
}
101+
quote!(::core::result::Result::Ok(#fn_name(#(#invoke_arg_names),*)#await_maybe?))
106102
} else {
107-
quote! { ::core::result::Result::<_, ()>::Ok(#fn_name(#(#invoke_arg_names),*)#await_maybe).into() }
103+
quote! { ::core::result::Result::<_, ::tauri::InvokeError>::Ok(#fn_name(#(#invoke_arg_names),*)#await_maybe) }
108104
};
109105

106+
// double underscore prefix temporary until underlying scoping issue is fixed (planned)
110107
quote! {
111108
#function
112-
#vis fn #fn_wrapper<P: ::tauri::Params>(message: ::tauri::InvokeMessage<P>, resolver: ::tauri::InvokeResolver<P>) {
113-
use ::tauri::command::FromCommand;
114-
resolver.respond_async(async move {
109+
110+
#vis fn #fn_wrapper<P: ::tauri::Params>(invoke: ::tauri::Invoke<P>) {
111+
use ::tauri::command::CommandArg;
112+
let ::tauri::Invoke { message: __message, resolver: __resolver } = invoke;
113+
__resolver.respond_async(async move {
115114
#invoke_args
116115
#return_value
117116
})
@@ -139,12 +138,12 @@ pub fn generate_handler(item: proc_macro::TokenStream) -> TokenStream {
139138
});
140139

141140
quote! {
142-
move |message, resolver| {
143-
let cmd = message.command().to_string();
144-
match cmd.as_str() {
145-
#(stringify!(#fn_names) => #fn_wrappers(message, resolver),)*
141+
move |invoke| {
142+
let cmd = invoke.message.command();
143+
match cmd {
144+
#(stringify!(#fn_names) => #fn_wrappers(invoke),)*
146145
_ => {
147-
resolver.reject(format!("command {} not found", cmd))
146+
invoke.resolver.reject(format!("command {} not found", cmd))
148147
},
149148
}
150149
}

0 commit comments

Comments
 (0)