From dd8cd4a2fa78fd9bc143b590dae915f9fa9cae72 Mon Sep 17 00:00:00 2001 From: Keavon Chambers Date: Tue, 21 Dec 2021 18:43:02 -0800 Subject: [PATCH] Skip the web page close confirmation when crashed or in debug mode --- frontend/src/lifetime/input.ts | 6 ++++++ frontend/wasm/src/api.rs | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/frontend/src/lifetime/input.ts b/frontend/src/lifetime/input.ts index a84cf3ad..f21c74aa 100644 --- a/frontend/src/lifetime/input.ts +++ b/frontend/src/lifetime/input.ts @@ -160,6 +160,12 @@ export function createInputManager(editor: EditorState, container: HTMLElement, }; const onBeforeUnload = (e: BeforeUnloadEvent) => { + // Skip the message if the editor crashed, since work is already lost + if (editor.instance.has_crashed()) return; + + // Skip the message during development, since it's annoying when testing + if (process.env.NODE_ENV === "development") return; + const allDocumentsSaved = document.state.documents.reduce((acc, doc) => acc && doc.isSaved, true); if (!allDocumentsSaved) { e.returnValue = "Unsaved work will be lost if the web browser tab is closed. Close anyway?"; diff --git a/frontend/wasm/src/api.rs b/frontend/wasm/src/api.rs index d4dd5242..10b4f049 100644 --- a/frontend/wasm/src/api.rs +++ b/frontend/wasm/src/api.rs @@ -44,8 +44,8 @@ impl JsEditorHandle { // Sends a message to the dispatcher in the Editor Backend fn dispatch>(&self, message: T) { // Process no further messages after a crash to avoid spamming the console - let has_crashed = EDITOR_HAS_CRASHED.with(|crash_state| crash_state.borrow().clone()); - if let Some(message) = has_crashed { + let possible_crash_message = EDITOR_HAS_CRASHED.with(|crash_state| crash_state.borrow().clone()); + if let Some(message) = possible_crash_message { if !self.instance_received_crashed.get() { self.handle_response(message); self.instance_received_crashed.set(true); @@ -82,6 +82,15 @@ impl JsEditorHandle { } } + // ======================================================================== + // Create JS -> Rust wrapper functions below + // ======================================================================== + + pub fn has_crashed(&self) -> JsValue { + let has_crashed = EDITOR_HAS_CRASHED.with(|crash_state| crash_state.borrow().is_some()); + has_crashed.into() + } + /// Modify the currently selected tool in the document state store pub fn select_tool(&self, tool: String) -> Result<(), JsValue> { match translate_tool_type(&tool) {