|
5 | 5 | // on the dispatcher messaging system and more complex Rust data types.
|
6 | 6 | //
|
7 | 7 | use crate::helpers::translate_key;
|
8 |
| -use crate::{EDITOR, EDITOR_HANDLE, EDITOR_HAS_CRASHED, Error}; |
| 8 | +use crate::{EDITOR, EDITOR_HANDLE, EDITOR_HAS_CRASHED, Error, MESSAGE_BUFFER}; |
9 | 9 | use editor::application::Editor;
|
10 | 10 | use editor::consts::FILE_SAVE_SUFFIX;
|
11 | 11 | use editor::messages::input_mapper::utility_types::input_keyboard::ModifierKeys;
|
@@ -157,12 +157,23 @@ impl EditorHandle {
|
157 | 157 | #[cfg(not(feature = "native"))]
|
158 | 158 | fn dispatch<T: Into<Message>>(&self, message: T) {
|
159 | 159 | // Process no further messages after a crash to avoid spamming the console
|
| 160 | + |
| 161 | + use crate::MESSAGE_BUFFER; |
160 | 162 | if EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
|
161 | 163 | return;
|
162 | 164 | }
|
163 | 165 |
|
164 | 166 | // Get the editor, dispatch the message, and store the `FrontendMessage` queue response
|
165 |
| - let frontend_messages = editor(|editor| editor.handle_message(message.into())); |
| 167 | + let frontend_messages = EDITOR.with(|editor| { |
| 168 | + let mut guard = editor.try_lock(); |
| 169 | + let Ok(Some(editor)) = guard.as_deref_mut() else { |
| 170 | + // Enqueue messages which can't be procssed currently |
| 171 | + MESSAGE_BUFFER.with_borrow_mut(|buffer| buffer.push(message.into())); |
| 172 | + return vec![]; |
| 173 | + }; |
| 174 | + |
| 175 | + editor.handle_message(message) |
| 176 | + }); |
166 | 177 |
|
167 | 178 | // Send each `FrontendMessage` to the JavaScript frontend
|
168 | 179 | for message in frontend_messages.into_iter() {
|
@@ -240,6 +251,13 @@ impl EditorHandle {
|
240 | 251 |
|
241 | 252 | if !EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
|
242 | 253 | handle(|handle| {
|
| 254 | + // Process all messages that have been queued up |
| 255 | + let messages = MESSAGE_BUFFER.take(); |
| 256 | + |
| 257 | + for message in messages { |
| 258 | + handle.dispatch(message); |
| 259 | + } |
| 260 | + |
243 | 261 | handle.dispatch(InputPreprocessorMessage::CurrentTime {
|
244 | 262 | timestamp: js_sys::Date::now() as u64,
|
245 | 263 | });
|
@@ -985,7 +1003,7 @@ fn auto_save_all_documents() {
|
985 | 1003 | return;
|
986 | 1004 | }
|
987 | 1005 |
|
988 |
| - editor_and_handle(|_, handle| { |
| 1006 | + handle(|handle| { |
989 | 1007 | handle.dispatch(PortfolioMessage::AutoSaveAllDocuments);
|
990 | 1008 | });
|
991 | 1009 | }
|
0 commit comments