Fix ScheduleRunnerPlugin (#610)

Fixes #609
This commit is contained in:
Daniel McNab 2020-10-01 18:52:29 +01:00 committed by GitHub
parent 408114269b
commit cd9e502b12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 12 deletions

View File

@ -80,4 +80,5 @@ impl App {
} }
/// An event that indicates the app should exit. This will fully exit the app process. /// An event that indicates the app should exit. This will fully exit the app process.
#[derive(Clone)]
pub struct AppExit; pub struct AppExit;

View File

@ -63,20 +63,20 @@ impl Plugin for ScheduleRunnerPlugin {
RunMode::Loop { wait } => { RunMode::Loop { wait } => {
let mut tick = move |app: &mut App, let mut tick = move |app: &mut App,
wait: Option<Duration>| wait: Option<Duration>|
-> Option<Duration> { -> Result<Option<Duration>, AppExit> {
let start_time = Instant::now(); let start_time = Instant::now();
if let Some(app_exit_events) = app.resources.get_mut::<Events<AppExit>>() { if let Some(app_exit_events) = app.resources.get_mut::<Events<AppExit>>() {
if app_exit_event_reader.latest(&app_exit_events).is_some() { if let Some(exit) = app_exit_event_reader.latest(&app_exit_events) {
return None; return Err(exit.clone());
} }
} }
app.update(); app.update();
if let Some(app_exit_events) = app.resources.get_mut::<Events<AppExit>>() { if let Some(app_exit_events) = app.resources.get_mut::<Events<AppExit>>() {
if app_exit_event_reader.latest(&app_exit_events).is_some() { if let Some(exit) = app_exit_event_reader.latest(&app_exit_events) {
return None; return Err(exit.clone());
} }
} }
@ -85,17 +85,19 @@ impl Plugin for ScheduleRunnerPlugin {
if let Some(wait) = wait { if let Some(wait) = wait {
let exe_time = end_time - start_time; let exe_time = end_time - start_time;
if exe_time < wait { if exe_time < wait {
return Some(wait - exe_time); return Ok(Some(wait - exe_time));
} }
} }
None Ok(None)
}; };
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
{ {
while let Some(delay) = tick(&mut app, wait) { while let Ok(delay) = tick(&mut app, wait) {
thread::sleep(delay); if let Some(delay) = delay {
thread::sleep(delay);
}
} }
} }
@ -118,10 +120,14 @@ impl Plugin for ScheduleRunnerPlugin {
let c = move || { let c = move || {
let mut app = Rc::get_mut(&mut rc).unwrap(); let mut app = Rc::get_mut(&mut rc).unwrap();
let delay = tick(&mut app, wait).unwrap_or(asap); let delay = tick(&mut app, wait);
set_timeout(f.borrow().as_ref().unwrap(), delay); 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()>)); *g.borrow_mut() = Some(Closure::wrap(Box::new(c) as Box<dyn FnMut()>));
set_timeout(g.borrow().as_ref().unwrap(), asap); set_timeout(g.borrow().as_ref().unwrap(), asap);
}; };