summaryrefslogtreecommitdiff
path: root/runtime/worker.rs
diff options
context:
space:
mode:
authorColin Ihrig <cjihrig@gmail.com>2022-06-28 10:49:30 -0400
committerGitHub <noreply@github.com>2022-06-28 10:49:30 -0400
commit0f6a5c5fc24e8dc9125c5c536c8547a86ca87b15 (patch)
tree41538ee1df06dc80d60e49ed50177f99ba8dc297 /runtime/worker.rs
parentab11b45d1d2678cfea2217ac72fc24317eef777d (diff)
feat(web): add beforeunload event (#14830)
This commit adds the 'beforeunload' event. Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'runtime/worker.rs')
-rw-r--r--runtime/worker.rs18
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)]