Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions hyperprocess_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ fn generate_message_handlers(
}
hyperware_app_common::APP_CONTEXT.with(|ctx| {
ctx.borrow_mut().current_path = None;
ctx.borrow_mut().current_message = None;
});
},
hyperware_process_lib::http::server::HttpServerRequest::WebSocketPush { channel_id, message_type } => {
Expand Down Expand Up @@ -991,6 +992,10 @@ fn generate_message_handlers(
hyperware_process_lib::logging::warn!("Failed to parse HTTP server request: {}", e);
}
}
// Clear current message after handling
hyperware_app_common::APP_CONTEXT.with(|ctx| {
ctx.borrow_mut().current_message = None;
});
}

/// Handle local messages
Expand All @@ -1011,6 +1016,10 @@ fn generate_message_handlers(
hyperware_process_lib::logging::warn!("Failed to deserialize local request into HPMRequest enum: {}", e);
}
}
// Clear current message after handling
hyperware_app_common::APP_CONTEXT.with(|ctx| {
ctx.borrow_mut().current_message = None;
});
}

/// Handle remote messages
Expand All @@ -1030,6 +1039,10 @@ fn generate_message_handlers(
hyperware_process_lib::logging::warn!("Failed to deserialize remote request into HPMRequest enum: {}\nRaw request value: {:?}", e, message.body());
}
}
// Clear current message after handling
hyperware_app_common::APP_CONTEXT.with(|ctx| {
ctx.borrow_mut().current_message = None;
});
}
}
}
Expand Down Expand Up @@ -1106,21 +1119,6 @@ fn generate_component_impl(
};

quote! {
thread_local! {
static CURRENT_MESSAGE: std::cell::RefCell<Option<hyperware_process_lib::Message>> =
std::cell::RefCell::new(None);
}

fn source() -> hyperware_process_lib::Address {
CURRENT_MESSAGE.with(|cell| {
cell.borrow()
.as_ref()
.expect("No message in current context")
.source()
.clone()
})
}

wit_bindgen::generate!({
path: "../target/wit",
world: #wit_world,
Expand Down Expand Up @@ -1180,8 +1178,8 @@ fn generate_component_impl(

match hyperware_process_lib::await_message() {
Ok(message) => {
CURRENT_MESSAGE.with(|cell| {
*cell.borrow_mut() = Some(message.clone());
hyperware_app_common::APP_CONTEXT.with(|ctx| {
ctx.borrow_mut().current_message = Some(message.clone());
});
match message {
hyperware_process_lib::Message::Response { body, context, .. } => {
Expand Down
14 changes: 14 additions & 0 deletions hyperware_app_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ thread_local! {
executor: Executor::new(),
current_path: None,
current_server: None,
current_message: None,
});

pub static RESPONSE_REGISTRY: RefCell<HashMap<String, Vec<u8>>> = RefCell::new(HashMap::new());
Expand All @@ -38,6 +39,7 @@ pub struct AppContext {
pub executor: Executor,
pub current_path: Option<String>,
pub current_server: Option<*mut HttpServer>,
pub current_message: Option<Message>,
}

// Access function for the current path
Expand All @@ -50,6 +52,18 @@ pub fn get_server() -> Option<&'static mut HttpServer> {
APP_CONTEXT.with(|ctx| ctx.borrow().current_server.map(|ptr| unsafe { &mut *ptr }))
}

// Access function for the source address of the current message
pub fn source() -> hyperware_process_lib::Address {
APP_CONTEXT.with(|ctx| {
ctx.borrow()
.current_message
.as_ref()
.expect("No message in current context")
.source()
.clone()
})
}

pub struct Executor {
tasks: Vec<Pin<Box<dyn Future<Output = ()>>>>,
}
Expand Down