Skip to content

Commit 1318ffb

Browse files
authored
refactor(core): remove async from app hooks, add InvokeMessage type (#1392)
1 parent c3e06ee commit 1318ffb

File tree

24 files changed

+409
-366
lines changed

24 files changed

+409
-366
lines changed

.changes/plugin-mutable.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"tauri": minor
33
---
44

5-
The plugin instance is now mutable and must be `Send + Sync`.
5+
The plugin instance is now mutable and must be `Send`.

examples/api/src-tauri/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct Reply {
1414

1515
fn main() {
1616
tauri::AppBuilder::default()
17-
.setup(|webview_manager| async move {
17+
.setup(move |webview_manager| {
1818
let dispatcher = webview_manager.current_webview().unwrap();
1919
let dispatcher_ = dispatcher.clone();
2020
dispatcher.listen("js-event", move |event| {

examples/multiwindow/src-tauri/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use tauri::WebviewBuilderExt;
77

88
fn main() {
99
tauri::AppBuilder::default()
10-
.setup(|webview_manager| async move {
10+
.setup(move |webview_manager| {
1111
if webview_manager.current_window_label() == "Main" {
1212
webview_manager.listen("clicked", move |_| {
1313
println!("got 'clicked' event on global channel");

tauri-api/src/cli.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::config::{CliArg, CliConfig, Config};
1+
use crate::config::{CliArg, CliConfig};
22

33
use clap::{App, Arg, ArgMatches, ErrorKind};
44
use serde::Serialize;
@@ -53,13 +53,7 @@ impl Matches {
5353
}
5454

5555
/// Gets the arg matches of the CLI definition.
56-
pub fn get_matches(config: &Config) -> crate::Result<Matches> {
57-
let cli = config
58-
.tauri
59-
.cli
60-
.as_ref()
61-
.ok_or(crate::Error::CliNotConfigured)?;
62-
56+
pub fn get_matches(cli: &CliConfig) -> crate::Result<Matches> {
6357
let about = cli
6458
.description()
6559
.unwrap_or(&crate_description!().to_string())

tauri-api/src/error.rs

-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ pub enum Error {
1616
/// The dialog operation was cancelled by the user.
1717
#[error("user cancelled the dialog")]
1818
DialogCancelled,
19-
/// CLI config not set.
20-
#[error("CLI configuration not set on tauri.conf.json")]
21-
CliNotConfigured,
2219
/// The network error.
2320
#[error("Network Error: {0}")]
2421
Network(#[from] reqwest::Error),

tauri-macros/src/command.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,10 @@ pub fn generate_command(attrs: Vec<NestedMeta>, function: ItemFn) -> TokenStream
6262
// If function doesn't take the webview manager, wrapper just takes webview manager generically and ignores it
6363
// Otherwise the wrapper uses the specific type from the original function declaration
6464
let mut manager_arg_type = quote!(::tauri::WebviewManager<A>);
65-
let mut application_ext_generic = quote!(<A: ::tauri::ApplicationExt>);
6665
let manager_arg_maybe = match types.first() {
6766
Some(first_type) if uses_manager => {
6867
// Give wrapper specific type
6968
manager_arg_type = quote!(#first_type);
70-
// Generic is no longer needed
71-
application_ext_generic = quote!();
7269
// Remove webview manager arg from list so it isn't expected as arg from JS
7370
types.drain(0..1);
7471
names.drain(0..1);
@@ -91,24 +88,28 @@ pub fn generate_command(attrs: Vec<NestedMeta>, function: ItemFn) -> TokenStream
9188
let return_value = if returns_result {
9289
quote! {
9390
match #fn_name(#manager_arg_maybe #(parsed_args.#names),*)#await_maybe {
94-
Ok(value) => ::core::result::Result::Ok(value.into()),
95-
Err(e) => ::core::result::Result::Err(tauri::Error::Command(::serde_json::to_value(e)?)),
91+
Ok(value) => ::core::result::Result::Ok(value),
92+
Err(e) => ::core::result::Result::Err(e),
9693
}
9794
}
9895
} else {
99-
quote! { ::core::result::Result::Ok(#fn_name(#manager_arg_maybe #(parsed_args.#names),*)#await_maybe.into()) }
96+
quote! { ::core::result::Result::<_, ()>::Ok(#fn_name(#manager_arg_maybe #(parsed_args.#names),*)#await_maybe) }
10097
};
10198

10299
quote! {
103100
#function
104-
pub async fn #fn_wrapper #application_ext_generic(_manager: #manager_arg_type, arg: ::serde_json::Value) -> ::tauri::Result<::tauri::InvokeResponse> {
101+
pub fn #fn_wrapper<A: ::tauri::ApplicationExt + 'static>(_manager: #manager_arg_type, message: ::tauri::InvokeMessage<A>) {
105102
#[derive(::serde::Deserialize)]
106103
#[serde(rename_all = "camelCase")]
107104
struct ParsedArgs {
108105
#(#names: #types),*
109106
}
110-
let parsed_args: ParsedArgs = ::serde_json::from_value(arg).map_err(|e| ::tauri::Error::InvalidArgs(#fn_name_str, e))?;
111-
#return_value
107+
match ::serde_json::from_value::<ParsedArgs>(message.payload()) {
108+
Ok(parsed_args) => message.respond_async(async move {
109+
#return_value
110+
}),
111+
Err(e) => message.reject(::core::result::Result::<(), String>::Err(::tauri::Error::InvalidArgs(#fn_name_str, e).to_string())),
112+
}
112113
}
113114
}
114115
}
@@ -133,10 +134,10 @@ pub fn generate_handler(item: proc_macro::TokenStream) -> TokenStream {
133134
});
134135

135136
quote! {
136-
|webview_manager, command, arg| async move {
137-
match command.as_str() {
138-
#(stringify!(#fn_names) => #fn_wrappers(webview_manager, arg).await,)*
139-
_ => Err(tauri::Error::UnknownApi(None)),
137+
move |webview_manager, message| {
138+
match message.command() {
139+
#(stringify!(#fn_names) => #fn_wrappers(webview_manager, message),)*
140+
_ => {},
140141
}
141142
}
142143
}

tauri-utils/src/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl Default for WindowConfig {
156156
}
157157

158158
/// A CLI argument definition
159-
#[derive(PartialEq, Deserialize, Debug, Default)]
159+
#[derive(PartialEq, Deserialize, Debug, Default, Clone)]
160160
#[serde(rename_all = "camelCase")]
161161
pub struct CliArg {
162162
/// The short version of the argument, without the preceding -.
@@ -243,7 +243,7 @@ pub struct CliArg {
243243
}
244244

245245
/// The CLI root command definition.
246-
#[derive(PartialEq, Deserialize, Debug)]
246+
#[derive(PartialEq, Deserialize, Debug, Clone)]
247247
#[serde(tag = "cli", rename_all = "camelCase")]
248248
#[allow(missing_docs)] // TODO
249249
pub struct CliConfig {

0 commit comments

Comments
 (0)