Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: on_init_sync and on_post_upgrade_sync #1282

Merged
merged 9 commits into from
Feb 27, 2025
Merged
Prev Previous commit
Next Next commit
feat: sync hook no result
  • Loading branch information
peterpeterparker committed Feb 27, 2025
commit 5f579812fdb85fa873cc834f1f60acbea67eb09a
4 changes: 4 additions & 0 deletions src/libs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ pub fn on_init(attr: TokenStream, item: TokenStream) -> TokenStream {
/// - If the upgrade fails, the satellite might become unresponsive and lose its data.
/// - **Developers should prefer `on_post_upgrade` unless they explicitly need synchronous behavior.**
///
/// Furthermore, note that the random number generator or other capabilities might not have been initialized at this point.
///
/// # Example (Restricted Usage)
///
/// ```rust
Expand Down Expand Up @@ -490,6 +492,8 @@ pub fn on_post_upgrade_sync(attr: TokenStream, item: TokenStream) -> TokenStream
/// - It **bypasses deferred execution**, meaning long-running operations **may slow down startup**.
/// - Regular users **should use `on_init` instead**, unless there's a strict requirement for synchronous behavior.
///
/// Furthermore, note that the random number generator or other capabilities might not have been initialized at this point.
///
/// # Example (Restricted Usage)
///
/// ```rust
Expand Down
34 changes: 33 additions & 1 deletion src/libs/macros/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ fn parse_hook(hook: &Hook, attr: TokenStream, item: TokenStream) -> Result<Token
let hook_fn = Ident::new(&map_hook_name(hook.clone()), proc_macro2::Span::call_site());

match hook {
Hook::OnPostUpgrade | Hook::OnInit | Hook::OnPostUpgradeSync | Hook::OnInitSync => {
Hook::OnPostUpgrade | Hook::OnInit => {
parse_lifecycle_hook(&ast, signature, &hook_fn)
}
Hook::OnPostUpgradeSync | Hook::OnInitSync => {
parse_lifecycle_sync_hook(&ast, signature, &hook_fn)
}
_ => parse_doc_hook(&ast, signature, &hook_fn, hook, attr),
}
}
Expand Down Expand Up @@ -272,3 +275,32 @@ fn parse_lifecycle_hook_body(signature: &Signature, hook_fn: &Ident) -> proc_mac
}
}
}

fn parse_lifecycle_sync_hook(
ast: &ItemFn,
signature: &Signature,
hook_fn: &Ident,
) -> Result<TokenStream, String> {
let hook_body = parse_lifecycle_hook_sync_body(signature, hook_fn);

let result = quote! {
#ast

#hook_body
};

Ok(result.into())
}

fn parse_lifecycle_hook_sync_body(signature: &Signature, hook_fn: &Ident) -> proc_macro2::TokenStream {
let func_name = &signature.ident;

let function_call = quote! { #func_name() };

quote! {
#[no_mangle]
pub extern "Rust" fn #hook_fn() {
#function_call;
}
}
}
14 changes: 5 additions & 9 deletions src/tests/fixtures/test_satellite/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use junobuild_macros::{on_delete_doc, on_set_doc, on_init_sync, on_post_upgrade_sync};
use junobuild_satellite::{error, include_satellite, info, warn, OnDeleteDocContext, OnSetDocContext};
use junobuild_satellite::{error, include_satellite, info, set_doc_store, OnDeleteDocContext, OnSetDocContext};

#[on_set_doc]
fn on_set_doc(_context: OnSetDocContext) -> Result<(), String> {
Expand All @@ -16,17 +16,13 @@ fn on_delete_doc(_context: OnDeleteDocContext) -> Result<(), String> {
}

#[on_init_sync]
fn on_init_sync() -> Result<(), String> {
warn("On init sync was executed".to_string())?;

Ok(())
fn on_init_sync() {
ic_cdk::print("On init sync was executed");
}

#[on_post_upgrade_sync]
fn on_post_upgrade_sync() -> Result<(), String> {
warn("On post upgrade sync was executed".to_string())?;

Ok(())
fn on_post_upgrade_sync() {
ic_cdk::print("On post upgrade sync was executed");
}

include_satellite!();
Loading