Skip to content

Commit 1d8b67b

Browse files
authored
feat(core): support async functions in mobile_entry_point macro (#11162)
closes #11158
1 parent d9d2502 commit 1d8b67b

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

.changes/mobile-async.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": "patch:feat"
3+
"tauri-macros": "patch:feat"
4+
---
5+
6+
Support async functions for `mobile_entry_point` macro

crates/tauri-macros/src/mobile.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,33 @@ fn get_env_var(name: &str, error: &mut Option<TokenStream2>, function: &ItemFn)
2929

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

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

38+
let (wrapper, wrapper_name) = if function.sig.asyncness.is_some() {
39+
let wrapper_name = syn::Ident::new(&format!("{function_name}_wrapper"), function_name.span());
40+
(
41+
quote! {
42+
#function
43+
44+
fn #wrapper_name() {
45+
::tauri::async_runtime::block_on(#function_name());
46+
}
47+
},
48+
wrapper_name,
49+
)
50+
} else {
51+
(
52+
quote! {
53+
#function
54+
},
55+
function_name,
56+
)
57+
};
58+
3859
if let Some(e) = error {
3960
quote!(#e).into()
4061
} else {
@@ -49,7 +70,7 @@ pub fn entry_point(_attributes: TokenStream, item: TokenStream) -> TokenStream {
4970
}
5071
}
5172

52-
#function
73+
#wrapper
5374

5475
fn _start_app() {
5576
#[cfg(target_os = "ios")]
@@ -58,7 +79,7 @@ pub fn entry_point(_attributes: TokenStream, item: TokenStream) -> TokenStream {
5879
{
5980
::tauri::android_binding!(#domain, #app_name, _start_app, ::tauri::wry);
6081
}
61-
stop_unwind(#function_name);
82+
stop_unwind(#wrapper_name);
6283
}
6384

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

0 commit comments

Comments
 (0)