Skip to content

Commit

Permalink
refactor(core): remove async from app hooks, add InvokeMessage type (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Mar 26, 2021
1 parent c3e06ee commit 1318ffb
Show file tree
Hide file tree
Showing 24 changed files with 409 additions and 366 deletions.
2 changes: 1 addition & 1 deletion .changes/plugin-mutable.md
Expand Up @@ -2,4 +2,4 @@
"tauri": minor
---

The plugin instance is now mutable and must be `Send + Sync`.
The plugin instance is now mutable and must be `Send`.
2 changes: 1 addition & 1 deletion examples/api/src-tauri/src/main.rs
Expand Up @@ -14,7 +14,7 @@ struct Reply {

fn main() {
tauri::AppBuilder::default()
.setup(|webview_manager| async move {
.setup(move |webview_manager| {
let dispatcher = webview_manager.current_webview().unwrap();
let dispatcher_ = dispatcher.clone();
dispatcher.listen("js-event", move |event| {
Expand Down
2 changes: 1 addition & 1 deletion examples/multiwindow/src-tauri/src/main.rs
Expand Up @@ -7,7 +7,7 @@ use tauri::WebviewBuilderExt;

fn main() {
tauri::AppBuilder::default()
.setup(|webview_manager| async move {
.setup(move |webview_manager| {
if webview_manager.current_window_label() == "Main" {
webview_manager.listen("clicked", move |_| {
println!("got 'clicked' event on global channel");
Expand Down
10 changes: 2 additions & 8 deletions tauri-api/src/cli.rs
@@ -1,4 +1,4 @@
use crate::config::{CliArg, CliConfig, Config};
use crate::config::{CliArg, CliConfig};

use clap::{App, Arg, ArgMatches, ErrorKind};
use serde::Serialize;
Expand Down Expand Up @@ -53,13 +53,7 @@ impl Matches {
}

/// Gets the arg matches of the CLI definition.
pub fn get_matches(config: &Config) -> crate::Result<Matches> {
let cli = config
.tauri
.cli
.as_ref()
.ok_or(crate::Error::CliNotConfigured)?;

pub fn get_matches(cli: &CliConfig) -> crate::Result<Matches> {
let about = cli
.description()
.unwrap_or(&crate_description!().to_string())
Expand Down
3 changes: 0 additions & 3 deletions tauri-api/src/error.rs
Expand Up @@ -16,9 +16,6 @@ pub enum Error {
/// The dialog operation was cancelled by the user.
#[error("user cancelled the dialog")]
DialogCancelled,
/// CLI config not set.
#[error("CLI configuration not set on tauri.conf.json")]
CliNotConfigured,
/// The network error.
#[error("Network Error: {0}")]
Network(#[from] reqwest::Error),
Expand Down
27 changes: 14 additions & 13 deletions tauri-macros/src/command.rs
Expand Up @@ -62,13 +62,10 @@ pub fn generate_command(attrs: Vec<NestedMeta>, function: ItemFn) -> TokenStream
// If function doesn't take the webview manager, wrapper just takes webview manager generically and ignores it
// Otherwise the wrapper uses the specific type from the original function declaration
let mut manager_arg_type = quote!(::tauri::WebviewManager<A>);
let mut application_ext_generic = quote!(<A: ::tauri::ApplicationExt>);
let manager_arg_maybe = match types.first() {
Some(first_type) if uses_manager => {
// Give wrapper specific type
manager_arg_type = quote!(#first_type);
// Generic is no longer needed
application_ext_generic = quote!();
// Remove webview manager arg from list so it isn't expected as arg from JS
types.drain(0..1);
names.drain(0..1);
Expand All @@ -91,24 +88,28 @@ pub fn generate_command(attrs: Vec<NestedMeta>, function: ItemFn) -> TokenStream
let return_value = if returns_result {
quote! {
match #fn_name(#manager_arg_maybe #(parsed_args.#names),*)#await_maybe {
Ok(value) => ::core::result::Result::Ok(value.into()),
Err(e) => ::core::result::Result::Err(tauri::Error::Command(::serde_json::to_value(e)?)),
Ok(value) => ::core::result::Result::Ok(value),
Err(e) => ::core::result::Result::Err(e),
}
}
} else {
quote! { ::core::result::Result::Ok(#fn_name(#manager_arg_maybe #(parsed_args.#names),*)#await_maybe.into()) }
quote! { ::core::result::Result::<_, ()>::Ok(#fn_name(#manager_arg_maybe #(parsed_args.#names),*)#await_maybe) }
};

quote! {
#function
pub async fn #fn_wrapper #application_ext_generic(_manager: #manager_arg_type, arg: ::serde_json::Value) -> ::tauri::Result<::tauri::InvokeResponse> {
pub fn #fn_wrapper<A: ::tauri::ApplicationExt + 'static>(_manager: #manager_arg_type, message: ::tauri::InvokeMessage<A>) {
#[derive(::serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct ParsedArgs {
#(#names: #types),*
}
let parsed_args: ParsedArgs = ::serde_json::from_value(arg).map_err(|e| ::tauri::Error::InvalidArgs(#fn_name_str, e))?;
#return_value
match ::serde_json::from_value::<ParsedArgs>(message.payload()) {
Ok(parsed_args) => message.respond_async(async move {
#return_value
}),
Err(e) => message.reject(::core::result::Result::<(), String>::Err(::tauri::Error::InvalidArgs(#fn_name_str, e).to_string())),
}
}
}
}
Expand All @@ -133,10 +134,10 @@ pub fn generate_handler(item: proc_macro::TokenStream) -> TokenStream {
});

quote! {
|webview_manager, command, arg| async move {
match command.as_str() {
#(stringify!(#fn_names) => #fn_wrappers(webview_manager, arg).await,)*
_ => Err(tauri::Error::UnknownApi(None)),
move |webview_manager, message| {
match message.command() {
#(stringify!(#fn_names) => #fn_wrappers(webview_manager, message),)*
_ => {},
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tauri-utils/src/config.rs
Expand Up @@ -156,7 +156,7 @@ impl Default for WindowConfig {
}

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

/// The CLI root command definition.
#[derive(PartialEq, Deserialize, Debug)]
#[derive(PartialEq, Deserialize, Debug, Clone)]
#[serde(tag = "cli", rename_all = "camelCase")]
#[allow(missing_docs)] // TODO
pub struct CliConfig {
Expand Down

0 comments on commit 1318ffb

Please sign in to comment.