Skip to content

Commit

Permalink
Fix ScheduleRunnerPlugin (bevyengine#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab authored and mrk-its committed Oct 6, 2020
1 parent 0abdf99 commit c22fbc1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,5 @@ impl App {
}

/// An event that indicates the app should exit. This will fully exit the app process.
#[derive(Clone)]
pub struct AppExit;
30 changes: 18 additions & 12 deletions crates/bevy_app/src/schedule_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ impl Plugin for ScheduleRunnerPlugin {
RunMode::Loop { wait } => {
let mut tick = move |app: &mut App,
wait: Option<Duration>|
-> Option<Duration> {
-> Result<Option<Duration>, AppExit> {
let start_time = Instant::now();

if let Some(app_exit_events) = app.resources.get_mut::<Events<AppExit>>() {
if app_exit_event_reader.latest(&app_exit_events).is_some() {
return None;
if let Some(exit) = app_exit_event_reader.latest(&app_exit_events) {
return Err(exit.clone());
}
}

app.update();

if let Some(app_exit_events) = app.resources.get_mut::<Events<AppExit>>() {
if app_exit_event_reader.latest(&app_exit_events).is_some() {
return None;
if let Some(exit) = app_exit_event_reader.latest(&app_exit_events) {
return Err(exit.clone());
}
}

Expand All @@ -85,17 +85,19 @@ impl Plugin for ScheduleRunnerPlugin {
if let Some(wait) = wait {
let exe_time = end_time - start_time;
if exe_time < wait {
return Some(wait - exe_time);
return Ok(Some(wait - exe_time));
}
}

None
Ok(None)
};

#[cfg(not(target_arch = "wasm32"))]
{
while let Some(delay) = tick(&mut app, wait) {
thread::sleep(delay);
while let Ok(delay) = tick(&mut app, wait) {
if let Some(delay) = delay {
thread::sleep(delay);
}
}
}

Expand All @@ -118,10 +120,14 @@ impl Plugin for ScheduleRunnerPlugin {

let c = move || {
let mut app = Rc::get_mut(&mut rc).unwrap();
let delay = tick(&mut app, wait).unwrap_or(asap);
set_timeout(f.borrow().as_ref().unwrap(), delay);
let delay = tick(&mut app, wait);
match delay {
Ok(delay) => {
set_timeout(f.borrow().as_ref().unwrap(), delay.unwrap_or(asap))
}
Err(_) => {}
}
};

*g.borrow_mut() = Some(Closure::wrap(Box::new(c) as Box<dyn FnMut()>));
set_timeout(g.borrow().as_ref().unwrap(), asap);
};
Expand Down

0 comments on commit c22fbc1

Please sign in to comment.