diff options
Diffstat (limited to 'runtime/worker.rs')
-rw-r--r-- | runtime/worker.rs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/runtime/worker.rs b/runtime/worker.rs index acb50dc30..fd937559f 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -371,6 +371,24 @@ impl MainWorker { "dispatchEvent(new Event('unload'))", ) } + + /// Dispatches "beforeunload" event to the JavaScript runtime. Returns a boolean + /// indicating if the event was prevented and thus event loop should continue + /// running. + pub fn dispatch_beforeunload_event( + &mut self, + script_name: &str, + ) -> Result<bool, AnyError> { + let value = self.js_runtime.execute_script( + script_name, + // NOTE(@bartlomieju): not using `globalThis` here, because user might delete + // it. Instead we're using global `dispatchEvent` function which will + // used a saved reference to global scope. + "dispatchEvent(new Event('beforeunload', { cancelable: true }));", + )?; + let local_value = value.open(&mut self.js_runtime.handle_scope()); + Ok(local_value.is_false()) + } } #[cfg(test)] |