Skip to content

Commit 309a643

Browse files
authored
Queue messages in the frontend when they can't be processed (#3005)
1 parent 23eb599 commit 309a643

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

frontend/wasm/src/editor_api.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// on the dispatcher messaging system and more complex Rust data types.
66
//
77
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};
99
use editor::application::Editor;
1010
use editor::consts::FILE_SAVE_SUFFIX;
1111
use editor::messages::input_mapper::utility_types::input_keyboard::ModifierKeys;
@@ -157,12 +157,23 @@ impl EditorHandle {
157157
#[cfg(not(feature = "native"))]
158158
fn dispatch<T: Into<Message>>(&self, message: T) {
159159
// Process no further messages after a crash to avoid spamming the console
160+
161+
use crate::MESSAGE_BUFFER;
160162
if EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
161163
return;
162164
}
163165

164166
// 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+
});
166177

167178
// Send each `FrontendMessage` to the JavaScript frontend
168179
for message in frontend_messages.into_iter() {
@@ -240,6 +251,13 @@ impl EditorHandle {
240251

241252
if !EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
242253
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+
243261
handle.dispatch(InputPreprocessorMessage::CurrentTime {
244262
timestamp: js_sys::Date::now() as u64,
245263
});
@@ -985,7 +1003,7 @@ fn auto_save_all_documents() {
9851003
return;
9861004
}
9871005

988-
editor_and_handle(|_, handle| {
1006+
handle(|handle| {
9891007
handle.dispatch(PortfolioMessage::AutoSaveAllDocuments);
9901008
});
9911009
}

frontend/wasm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub static LOGGER: WasmLog = WasmLog;
2121

2222
thread_local! {
2323
pub static EDITOR: Mutex<Option<editor::application::Editor>> = const { Mutex::new(None) };
24+
pub static MESSAGE_BUFFER: std::cell::RefCell<Vec<Message>> = const { std::cell::RefCell::new(Vec::new()) };
2425
pub static EDITOR_HANDLE: Mutex<Option<editor_api::EditorHandle>> = const { Mutex::new(None) };
2526
}
2627

0 commit comments

Comments
 (0)