Skip to content

Commit

Permalink
feat(core): support async functions in mobile_entry_point macro (#1…
Browse files Browse the repository at this point in the history
…1162)

closes #11158
  • Loading branch information
amrbashir authored Sep 27, 2024
1 parent d9d2502 commit 1d8b67b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changes/mobile-async.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": "patch:feat"
"tauri-macros": "patch:feat"
---

Support async functions for `mobile_entry_point` macro
27 changes: 24 additions & 3 deletions crates/tauri-macros/src/mobile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,33 @@ fn get_env_var(name: &str, error: &mut Option<TokenStream2>, function: &ItemFn)

pub fn entry_point(_attributes: TokenStream, item: TokenStream) -> TokenStream {
let function = parse_macro_input!(item as ItemFn);
let function_name = &function.sig.ident;
let function_name = function.sig.ident.clone();

let mut error = None;
let domain = get_env_var("TAURI_ANDROID_PACKAGE_NAME_PREFIX", &mut error, &function);
let app_name = get_env_var("TAURI_ANDROID_PACKAGE_NAME_APP_NAME", &mut error, &function);

let (wrapper, wrapper_name) = if function.sig.asyncness.is_some() {
let wrapper_name = syn::Ident::new(&format!("{function_name}_wrapper"), function_name.span());
(
quote! {
#function

fn #wrapper_name() {
::tauri::async_runtime::block_on(#function_name());
}
},
wrapper_name,
)
} else {
(
quote! {
#function
},
function_name,
)
};

if let Some(e) = error {
quote!(#e).into()
} else {
Expand All @@ -49,7 +70,7 @@ pub fn entry_point(_attributes: TokenStream, item: TokenStream) -> TokenStream {
}
}

#function
#wrapper

fn _start_app() {
#[cfg(target_os = "ios")]
Expand All @@ -58,7 +79,7 @@ pub fn entry_point(_attributes: TokenStream, item: TokenStream) -> TokenStream {
{
::tauri::android_binding!(#domain, #app_name, _start_app, ::tauri::wry);
}
stop_unwind(#function_name);
stop_unwind(#wrapper_name);
}

// be careful when renaming this, the `start_app` symbol is checked by the CLI
Expand Down

0 comments on commit 1d8b67b

Please sign in to comment.