Fix panic on pausing dead tasks

This commit is contained in:
Jonathan Kelley 2024-02-02 14:33:02 -08:00
parent ee19df55c7
commit 3295935de7
No known key found for this signature in database
GPG key ID: 1FBB50F7EB0A08BE
2 changed files with 33 additions and 20 deletions

View file

@ -12,16 +12,21 @@ fn app() -> Element {
use_future(move || async move { use_future(move || async move {
let mut focused = 0; let mut focused = 0;
if running() {
loop { loop {
tokio::time::sleep(std::time::Duration::from_millis(10)).await; tokio::time::sleep(std::time::Duration::from_millis(10)).await;
if let Some(element) = elements.with(|f| f.get(focused).cloned()) {
_ = element.set_focus(true).await; if !running() {
} else { continue;
focused = 0;
}
focused += 1;
} }
if let Some(element) = elements.with(|f| f.get(focused).cloned()) {
_ = element.set_focus(true).await;
} else {
focused = 0;
}
focused += 1;
} }
}); });

View file

@ -37,12 +37,24 @@ impl Task {
/// Pause the task. /// Pause the task.
pub fn pause(&self) { pub fn pause(&self) {
Runtime::with(|rt| rt.tasks.borrow()[self.0].active.set(false)); self.set_active(false);
}
/// Resume the task.
pub fn resume(&self) {
self.set_active(true);
} }
/// Check if the task is paused. /// Check if the task is paused.
pub fn paused(&self) -> bool { pub fn paused(&self) -> bool {
Runtime::with(|rt| !rt.tasks.borrow()[self.0].active.get()).unwrap_or_default() Runtime::with(|rt| {
if let Some(task) = rt.tasks.borrow().get(self.0) {
!task.active.get()
} else {
false
}
})
.unwrap_or_default()
} }
/// Wake the task. /// Wake the task.
@ -57,16 +69,12 @@ impl Task {
/// Set the task as active or paused. /// Set the task as active or paused.
pub fn set_active(&self, active: bool) { pub fn set_active(&self, active: bool) {
Runtime::with(|rt| rt.tasks.borrow()[self.0].active.set(active));
}
/// Resume the task.
pub fn resume(&self) {
Runtime::with(|rt| { Runtime::with(|rt| {
// set the active flag, and then ping the scheduler to ensure the task gets queued if let Some(task) = rt.tasks.borrow().get(self.0) {
let was_active = rt.tasks.borrow()[self.0].active.replace(true); let was_active = task.active.replace(active);
if !was_active { if !was_active && active {
_ = rt.sender.unbounded_send(SchedulerMsg::TaskNotified(*self)); _ = rt.sender.unbounded_send(SchedulerMsg::TaskNotified(*self));
}
} }
}); });
} }