improve getter on main thread

This commit is contained in:
Lucas Nogueira
2025-11-03 00:48:15 -03:00
parent 090edf0ba3
commit bfb417c8ce

View File

@@ -45,7 +45,7 @@ mod cef_impl;
#[macro_export]
macro_rules! getter {
($self: ident, $rx: expr, $message: expr) => {{
$self.context.post_message($message)?;
$crate::send_user_message(&$self.context, $message)?;
$rx
.recv()
.map_err(|_| tauri_runtime::Error::FailedToReceiveMessage)
@@ -372,6 +372,25 @@ impl<T: UserEvent> RuntimeContext<T> {
}
}
// Mirrors tauri-runtime-wry's send_user_message behavior: if we're already on the main
// thread, handle the message immediately; otherwise, post it to the main thread.
pub(crate) fn send_user_message<T: UserEvent>(
context: &RuntimeContext<T>,
message: Message<T>,
) -> Result<()> {
if thread::current().id() == context.main_thread_id {
cef_impl::handle_message(&context.cef_context, message);
} else {
context
.main_thread_task_runner
.post_task(Some(&mut cef_impl::SendMessageTask::new(
context.cef_context.clone(),
Arc::new(RefCell::new(message)),
)));
}
Ok(())
}
impl<T: UserEvent> fmt::Debug for RuntimeContext<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RuntimeContext").finish()