diff --git a/frontend/wasm/src/editor_api.rs b/frontend/wasm/src/editor_api.rs index 617231b74e..60702dce6c 100644 --- a/frontend/wasm/src/editor_api.rs +++ b/frontend/wasm/src/editor_api.rs @@ -5,7 +5,7 @@ // on the dispatcher messaging system and more complex Rust data types. // use crate::helpers::translate_key; -use crate::{EDITOR, EDITOR_HANDLE, EDITOR_HAS_CRASHED, Error}; +use crate::{EDITOR, EDITOR_HANDLE, EDITOR_HAS_CRASHED, Error, MESSAGE_BUFFER}; use editor::application::Editor; use editor::consts::FILE_SAVE_SUFFIX; use editor::messages::input_mapper::utility_types::input_keyboard::ModifierKeys; @@ -157,12 +157,23 @@ impl EditorHandle { #[cfg(not(feature = "native"))] fn dispatch>(&self, message: T) { // Process no further messages after a crash to avoid spamming the console + + use crate::MESSAGE_BUFFER; if EDITOR_HAS_CRASHED.load(Ordering::SeqCst) { return; } // Get the editor, dispatch the message, and store the `FrontendMessage` queue response - let frontend_messages = editor(|editor| editor.handle_message(message.into())); + let frontend_messages = EDITOR.with(|editor| { + let mut guard = editor.try_lock(); + let Ok(Some(editor)) = guard.as_deref_mut() else { + // Enqueue messages which can't be procssed currently + MESSAGE_BUFFER.with_borrow_mut(|buffer| buffer.push(message.into())); + return vec![]; + }; + + editor.handle_message(message) + }); // Send each `FrontendMessage` to the JavaScript frontend for message in frontend_messages.into_iter() { @@ -240,6 +251,13 @@ impl EditorHandle { if !EDITOR_HAS_CRASHED.load(Ordering::SeqCst) { handle(|handle| { + // Process all messages that have been queued up + let messages = MESSAGE_BUFFER.take(); + + for message in messages { + handle.dispatch(message); + } + handle.dispatch(InputPreprocessorMessage::CurrentTime { timestamp: js_sys::Date::now() as u64, }); @@ -985,7 +1003,7 @@ fn auto_save_all_documents() { return; } - editor_and_handle(|_, handle| { + handle(|handle| { handle.dispatch(PortfolioMessage::AutoSaveAllDocuments); }); } diff --git a/frontend/wasm/src/lib.rs b/frontend/wasm/src/lib.rs index 01415fc29f..8ed0d9abe5 100644 --- a/frontend/wasm/src/lib.rs +++ b/frontend/wasm/src/lib.rs @@ -21,6 +21,7 @@ pub static LOGGER: WasmLog = WasmLog; thread_local! { pub static EDITOR: Mutex> = const { Mutex::new(None) }; + pub static MESSAGE_BUFFER: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; pub static EDITOR_HANDLE: Mutex> = const { Mutex::new(None) }; }