Skip to content

Commit 74e3e36

Browse files
committed
Use a mutex for storing the frontend state instead of a RefCell
1 parent 9bdcbb6 commit 74e3e36

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

Diff for: frontend/wasm/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ debug = true
5757

5858
[package.metadata.wasm-pack.profile.dev]
5959
wasm-opt = false
60-
panic = "unwind"
6160

6261
[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
6362
debug-js-glue = true

Diff for: frontend/wasm/src/editor_api.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ impl EditorHandle {
7171
pub fn new(frontend_message_handler_callback: js_sys::Function) -> Self {
7272
let editor = Editor::new();
7373
let editor_handle = EditorHandle { frontend_message_handler_callback };
74-
if EDITOR.with(|editor_cell| editor_cell.set(RefCell::new(editor))).is_err() {
74+
if EDITOR.with(|handle| handle.lock().ok().map(|mut guard| *guard = Some(editor))).is_none() {
7575
log::error!("Attempted to initialize the editor more than once");
7676
}
77-
if EDITOR_HANDLE.with(|handle_cell| handle_cell.set(RefCell::new(editor_handle.clone()))).is_err() {
77+
if EDITOR_HANDLE.with(|handle| handle.lock().ok().map(|mut guard| *guard = Some(editor_handle.clone()))).is_none() {
7878
log::error!("Attempted to initialize the editor handle more than once");
7979
}
8080
editor_handle
@@ -888,29 +888,29 @@ fn set_timeout(f: &Closure<dyn FnMut()>, delay: Duration) {
888888
}
889889

890890
/// Provides access to the `Editor` by calling the given closure with it as an argument.
891-
fn editor<T: Default>(callback: impl FnOnce(&mut editor::application::Editor) -> T) -> T {
891+
fn editor<T>(callback: impl FnOnce(&mut editor::application::Editor) -> T) -> T {
892892
EDITOR.with(|editor| {
893-
let Some(Ok(mut editor)) = editor.get().map(RefCell::try_borrow_mut) else {
894-
// TODO: Investigate if this should just panic instead, and if not doing so right now may be the cause of silent crashes that don't inform the user that the app has panicked
895-
log::error!("Failed to borrow the editor");
896-
return T::default();
893+
let mut guard = editor.lock();
894+
let Ok(Some(ref mut editor)) = guard.as_deref_mut() else {
895+
panic!("Failed to borrow the editor");
897896
};
898897

899-
callback(&mut editor)
898+
callback(editor)
900899
})
901900
}
902901

903902
/// Provides access to the `Editor` and its `EditorHandle` by calling the given closure with them as arguments.
904903
pub(crate) fn editor_and_handle(mut callback: impl FnMut(&mut Editor, &mut EditorHandle)) {
905-
editor(|editor| {
906-
EDITOR_HANDLE.with(|editor_handle| {
907-
let Some(Ok(mut handle)) = editor_handle.get().map(RefCell::try_borrow_mut) else {
904+
EDITOR_HANDLE.with(|editor_handle| {
905+
editor(|editor| {
906+
let mut guard = editor_handle.lock();
907+
let Ok(Some(ref mut editor_handle)) = guard.as_deref_mut() else {
908908
log::error!("Failed to borrow editor handle");
909909
return;
910910
};
911911

912912
// Call the closure with the editor and its handle
913-
callback(editor, &mut handle);
913+
callback(editor, editor_handle);
914914
})
915915
});
916916
}

Diff for: frontend/wasm/src/lib.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ pub mod helpers;
99

1010
use editor::messages::prelude::*;
1111

12-
use std::cell::{OnceCell, RefCell};
1312
use std::panic;
1413
use std::sync::atomic::{AtomicBool, Ordering};
14+
use std::sync::Mutex;
1515
use wasm_bindgen::prelude::*;
1616

1717
// Set up the persistent editor backend state
1818
pub static EDITOR_HAS_CRASHED: AtomicBool = AtomicBool::new(false);
1919
pub static NODE_GRAPH_ERROR_DISPLAYED: AtomicBool = AtomicBool::new(false);
2020
pub static LOGGER: WasmLog = WasmLog;
21+
2122
thread_local! {
22-
pub static EDITOR: OnceCell<RefCell<editor::application::Editor>> = const { OnceCell::new() };
23-
pub static EDITOR_HANDLE: OnceCell<RefCell<editor_api::EditorHandle>> = const { OnceCell::new() };
23+
pub static EDITOR: Mutex<Option<editor::application::Editor>> = const { Mutex::new(None) };
24+
pub static EDITOR_HANDLE: Mutex<Option<editor_api::EditorHandle>> = const { Mutex::new(None) };
2425
}
2526

2627
/// Initialize the backend
@@ -73,11 +74,10 @@ pub fn panic_hook(info: &panic::PanicInfo) {
7374
error!("{info}");
7475

7576
EDITOR_HANDLE.with(|editor_handle| {
76-
editor_handle.get().map(|handle| {
77-
handle
78-
.borrow_mut()
79-
.send_frontend_message_to_js_rust_proxy(FrontendMessage::DisplayDialogPanic { panic_info: info.to_string() })
80-
})
77+
let mut guard = editor_handle.lock();
78+
if let Ok(Some(ref mut handle)) = guard.as_deref_mut() {
79+
handle.send_frontend_message_to_js_rust_proxy(FrontendMessage::DisplayDialogPanic { panic_info: info.to_string() });
80+
}
8181
});
8282
}
8383

0 commit comments

Comments
 (0)