Skip to content

Commit

Permalink
Replace beforeunload with pagehide
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jun 5, 2023
1 parent 12fb37d commit ab46aa5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- On Web, use the correct canvas size when calculating the new size during scale factor change,
instead of using the output bitmap size.
- On Web, scale factor and dark mode detection are now more robust.
- On Web, fix the bfcache by not using the `beforeunload` event.

# 0.28.6

Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ version = "0.3.22"
features = [
'console',
'CssStyleDeclaration',
'BeforeUnloadEvent',
'Document',
'DomRect',
'Element',
Expand Down
15 changes: 5 additions & 10 deletions src/platform_impl/web/web_sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::dpi::{LogicalSize, Size};
use crate::platform::web::WindowExtWebSys;
use crate::window::Window;
use wasm_bindgen::closure::Closure;
use web_sys::{BeforeUnloadEvent, Element, HtmlCanvasElement};
use web_sys::{Element, HtmlCanvasElement};

pub fn throw(msg: &str) {
wasm_bindgen::throw_str(msg);
Expand All @@ -28,18 +28,13 @@ pub fn exit_fullscreen(window: &web_sys::Window) {
}

pub struct UnloadEventHandle {
_listener: event_handle::EventListenerHandle<dyn FnMut(BeforeUnloadEvent)>,
_listener: event_handle::EventListenerHandle<dyn FnMut()>,
}

pub fn on_unload(
window: &web_sys::Window,
mut handler: impl FnMut() + 'static,
) -> UnloadEventHandle {
let closure = Closure::wrap(
Box::new(move |_: BeforeUnloadEvent| handler()) as Box<dyn FnMut(BeforeUnloadEvent)>
);
pub fn on_unload(window: &web_sys::Window, handler: impl FnMut() + 'static) -> UnloadEventHandle {
let closure = Closure::new(handler);

let listener = event_handle::EventListenerHandle::new(window, "beforeunload", closure);
let listener = event_handle::EventListenerHandle::new(window, "pagehide", closure);
UnloadEventHandle {
_listener: listener,
}
Expand Down

0 comments on commit ab46aa5

Please sign in to comment.