Implement editor level future message handler (#4182)

* Implement proper editor level async message handler

* Take application_io and wake callback as Editor::new arguments

* Rename AsyncMessage -> FutureMessage

* add DesktopWrapperMessage::Wake

---------

Co-authored-by: Timon <me@timon.zip>
This commit is contained in:
Dennis Kobert
2026-05-31 23:26:29 +02:00
committed by GitHub
parent 51427bef5f
commit 78679a5ba2
22 changed files with 261 additions and 82 deletions

View File

@@ -93,8 +93,9 @@ impl EditorWrapper {
}
};
let mut editor = Editor::new(Environment { platform: Platform::Web, host }, uuid_random_seed, storage);
editor.replace_application_io(PlatformApplicationIo::new().await);
let application_io = PlatformApplicationIo::new().await;
let wake = crate::helpers::async_wake_callback();
let editor = Editor::new(Environment { platform: Platform::Web, host }, uuid_random_seed, storage, application_io, wake);
if EDITOR.with(|slot| slot.lock().ok().map(|mut guard| *guard = Some(editor))).is_none() {
log::error!("Attempted to initialize the editor more than once");
@@ -145,14 +146,6 @@ impl EditorWrapper {
// Sends a FrontendMessage to JavaScript
pub(crate) fn send_frontend_message_to_js(&self, message: FrontendMessage) {
if let FrontendMessage::Await { future } = message {
let wrapper = self.clone();
wasm_bindgen_futures::spawn_local(async move {
wrapper.send_frontend_message_to_js(future.await);
});
return;
}
if let FrontendMessage::UpdateImageData { ref image_data } = message {
let new_hash = calculate_hash(&CacheHashWrapper(image_data));
let prev_hash = IMAGE_DATA_HASH.load(Ordering::Relaxed);

View File

@@ -108,6 +108,17 @@ pub(crate) async fn poll_node_graph_evaluation() {
});
}
/// Web wake callback: queues a microtask that dispatches [`FutureMessage::Wake`].
#[cfg(all(not(feature = "native"), target_family = "wasm"))]
pub(crate) fn async_wake_callback() -> Wake {
use std::sync::Arc;
Arc::new(|| {
wasm_bindgen_futures::spawn_local(async {
wrapper(|wrapper| wrapper.dispatch(FutureMessage::Wake));
});
})
}
pub(crate) fn auto_save_all_documents() {
// Process no further messages after a crash to avoid spamming the console
if EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {