Skip to content

Commit

Permalink
wasm graceful exit
Browse files Browse the repository at this point in the history
  • Loading branch information
enomado committed Jul 29, 2022
1 parent 468eb84 commit 3814ad1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 13 additions & 7 deletions eframe/src/web/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,21 @@ impl AppRunner {
}

pub fn destroy(&mut self) -> Result<(), JsValue> {
tracing::debug!("destroying...");
let is_destroyed_already = self.is_destroyed.fetch();

for x in self.events_to_unsubscribe.drain(..) {
x.unsubscribe()?;
}
if is_destroyed_already {
tracing::warn!("App was destroyed already");
Ok(())
} else {
tracing::debug!("Destroying");
for x in self.events_to_unsubscribe.drain(..) {
x.unsubscribe()?;
}

self.painter.destroy();
self.is_destroyed.set_true();
Ok(())
self.painter.destroy();
self.is_destroyed.set_true();
Ok(())
}
}

/// Returns how long to wait until the next repaint.
Expand Down
8 changes: 5 additions & 3 deletions eframe/src/web/events.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use super::*;
use std::sync::atomic::{AtomicBool, Ordering};

struct IsDestroyed(pub bool);

pub fn paint_and_schedule(
runner_ref: &AppRunnerRef,
panicked: Arc<AtomicBool>,
) -> Result<(), JsValue> {
fn paint_if_needed(runner_ref: &AppRunnerRef) -> Result<bool, JsValue> {
fn paint_if_needed(runner_ref: &AppRunnerRef) -> Result<IsDestroyed, JsValue> {
let mut runner_lock = runner_ref.lock();
let is_destroyed = runner_lock.is_destroyed.fetch();

Expand All @@ -20,7 +22,7 @@ pub fn paint_and_schedule(
runner_lock.auto_save();
}

Ok(is_destroyed)
Ok(IsDestroyed(is_destroyed))
}

fn request_animation_frame(
Expand All @@ -38,7 +40,7 @@ pub fn paint_and_schedule(
// Only paint and schedule if there has been no panic
if !panicked.load(Ordering::SeqCst) {
let is_destroyed = paint_if_needed(runner_ref)?;
if !is_destroyed {
if !is_destroyed.0 {
request_animation_frame(runner_ref.clone(), panicked)?;
}
}
Expand Down

0 comments on commit 3814ad1

Please sign in to comment.