diff --git a/hyperprocess_macro/src/lib.rs b/hyperprocess_macro/src/lib.rs index 5a679bc..63ec9bc 100644 --- a/hyperprocess_macro/src/lib.rs +++ b/hyperprocess_macro/src/lib.rs @@ -1252,10 +1252,6 @@ fn generate_message_handlers( format!("Handler {} requires a request body", stringify!(#fn_name)).into_bytes() ); } - - hyperware_app_common::APP_HELPERS.with(|ctx| { - ctx.borrow_mut().current_path = None; - }); return; } } @@ -1309,18 +1305,8 @@ fn generate_message_handlers( let handler_body = if handler.is_async { quote! { // Capture context values before async execution - let current_path = hyperware_app_common::get_path(); - let current_method = hyperware_app_common::get_http_method(); - let state_ptr: *mut #self_ty = state; hyperware_app_common::hyper! { - // Restore context in the async task - hyperware_app_common::APP_HELPERS.with(|ctx| { - let mut ctx_mut = ctx.borrow_mut(); - ctx_mut.current_path = current_path; - ctx_mut.current_http_method = current_method; - }); - let result = unsafe { (*state_ptr).#fn_name().await }; #response_handling } @@ -1340,9 +1326,6 @@ fn generate_message_handlers( if #path_check && #method_check { hyperware_process_lib::logging::debug!("Matched parameter-less handler {} for {} {}", stringify!(#fn_name), http_method, current_path); #handler_body - hyperware_app_common::APP_HELPERS.with(|ctx| { - ctx.borrow_mut().current_path = None; - }); return; } } @@ -1418,9 +1401,6 @@ fn generate_message_handlers( #http_request_match_arms hyperware_app_common::maybe_save_state(&mut *state); } - hyperware_app_common::APP_HELPERS.with(|ctx| { - ctx.borrow_mut().current_path = None; - }); return; }, Err(e) => { @@ -1446,9 +1426,6 @@ fn generate_message_handlers( None, error_details.into_bytes() ); - hyperware_app_common::APP_HELPERS.with(|ctx| { - ctx.borrow_mut().current_path = None; - }); return; } } @@ -1466,9 +1443,6 @@ fn generate_message_handlers( None, format!("No handler found for {} {}", http_method, current_path).into_bytes(), ); - hyperware_app_common::APP_HELPERS.with(|ctx| { - ctx.borrow_mut().current_path = None; - }); }, hyperware_process_lib::http::server::HttpServerRequest::WebSocketPush { channel_id, message_type } => { hyperware_process_lib::logging::debug!("Received WebSocket message on channel {}, type: {:?}", channel_id, message_type); diff --git a/hyperware_app_common/src/lib.rs b/hyperware_app_common/src/lib.rs index a9b894f..4f81abf 100644 --- a/hyperware_app_common/src/lib.rs +++ b/hyperware_app_common/src/lib.rs @@ -9,7 +9,7 @@ use futures_util::task::noop_waker_ref; use hyperware_process_lib::http::server::HttpServer; use hyperware_process_lib::logging::info; use hyperware_process_lib::{ - get_state, http, kiprintln, set_state, BuildError, LazyLoadBlob, Message, Request, SendError, + get_state, http, kiprintln, set_state, timer, BuildError, LazyLoadBlob, Message, Request, SendError, }; use serde::Deserialize; use serde::Serialize; @@ -186,6 +186,21 @@ pub enum AppSendError { BuildError(BuildError), } +pub async fn sleep(sleep_ms: u64) -> Result<(), AppSendError> { + let request = Request::to(("our", "timer", "distro", "sys")) + .body(timer::TimerAction::SetTimer(sleep_ms)) + .expects_response((sleep_ms / 1_000) + 1); + + let correlation_id = Uuid::new_v4().to_string(); + if let Err(e) = request.context(correlation_id.as_bytes().to_vec()).send() { + return Err(AppSendError::BuildError(e)); + } + + let _ = ResponseFuture::new(correlation_id).await; + + return Ok(()); +} + pub async fn send(request: Request) -> Result where R: serde::de::DeserializeOwned,