fix: restore legacy progress after owned tasks finish
This commit is contained in:
1 parent
cf9007fa06
commit
615d3fb8fc
2 files changed
+55
-23
No files matched your search
+33
-23
@@ -12,7 +12,7 @@ pub struct ProgressState {
|
|||||||
pub progress: Option<(u64, u64)>,
|
pub progress: Option<(u64, u64)>,
|
||||||
pub sub_action: Option<String>,
|
pub sub_action: Option<String>,
|
||||||
tasks: BTreeMap<u64, TaskState>,
|
tasks: BTreeMap<u64, TaskState>,
|
||||||
legacy_active: bool,
|
legacy: Option<TaskState>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -28,7 +28,6 @@ static NEXT_TASK_ID: AtomicU64 = AtomicU64::new(1);
|
|||||||
|
|
||||||
pub struct ProgressTask {
|
pub struct ProgressTask {
|
||||||
id: u64,
|
id: u64,
|
||||||
finished: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -53,10 +52,7 @@ impl ProgressTask {
|
|||||||
}
|
}
|
||||||
tracing::info!("{}", action);
|
tracing::info!("{}", action);
|
||||||
super::request_redraw();
|
super::request_redraw();
|
||||||
Self {
|
Self { id }
|
||||||
id,
|
|
||||||
finished: false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle(&self) -> ProgressTaskHandle {
|
pub fn handle(&self) -> ProgressTaskHandle {
|
||||||
@@ -75,15 +71,13 @@ impl ProgressTask {
|
|||||||
update_task_progress(self.id, current, total);
|
update_task_progress(self.id, current, total);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn finish(mut self) {
|
pub fn finish(self) {
|
||||||
self.remove();
|
drop(self);
|
||||||
self.finished = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fail(mut self, error: impl std::fmt::Display) {
|
pub fn fail(self, error: impl std::fmt::Display) {
|
||||||
tracing::debug!("Progress task failed: {error}");
|
tracing::debug!("Progress task failed: {error}");
|
||||||
self.remove();
|
drop(self);
|
||||||
self.finished = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(&self) {
|
fn remove(&self) {
|
||||||
@@ -138,18 +132,21 @@ fn update_task_progress(id: u64, current: u64, total: u64) {
|
|||||||
|
|
||||||
impl Drop for ProgressTask {
|
impl Drop for ProgressTask {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if !self.finished {
|
self.remove();
|
||||||
self.remove();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn refresh_visible(state: &mut ProgressState) {
|
fn refresh_visible(state: &mut ProgressState) {
|
||||||
if let Some((_, task)) = state.tasks.last_key_value() {
|
if let Some(task) = state
|
||||||
|
.tasks
|
||||||
|
.last_key_value()
|
||||||
|
.map(|(_, task)| task)
|
||||||
|
.or(state.legacy.as_ref())
|
||||||
|
{
|
||||||
state.current_action = Some(task.action.clone());
|
state.current_action = Some(task.action.clone());
|
||||||
state.sub_action.clone_from(&task.sub_action);
|
state.sub_action.clone_from(&task.sub_action);
|
||||||
state.progress = task.progress;
|
state.progress = task.progress;
|
||||||
} else if !state.legacy_active {
|
} else {
|
||||||
state.current_action = None;
|
state.current_action = None;
|
||||||
state.sub_action = None;
|
state.sub_action = None;
|
||||||
state.progress = None;
|
state.progress = None;
|
||||||
@@ -160,9 +157,16 @@ pub fn set_action(text: impl Into<String>) {
|
|||||||
let text = text.into();
|
let text = text.into();
|
||||||
match PROGRESS.lock() {
|
match PROGRESS.lock() {
|
||||||
Ok(mut state) => {
|
Ok(mut state) => {
|
||||||
state.legacy_active = true;
|
let sub_action = state
|
||||||
state.current_action = Some(text.clone());
|
.legacy
|
||||||
state.progress = None;
|
.as_ref()
|
||||||
|
.and_then(|task| task.sub_action.clone());
|
||||||
|
state.legacy = Some(TaskState {
|
||||||
|
action: text.clone(),
|
||||||
|
sub_action,
|
||||||
|
progress: None,
|
||||||
|
});
|
||||||
|
refresh_visible(&mut state);
|
||||||
super::request_redraw();
|
super::request_redraw();
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -175,7 +179,10 @@ pub fn set_action(text: impl Into<String>) {
|
|||||||
pub fn set_progress(current: u64, total: u64) {
|
pub fn set_progress(current: u64, total: u64) {
|
||||||
match PROGRESS.lock() {
|
match PROGRESS.lock() {
|
||||||
Ok(mut state) => {
|
Ok(mut state) => {
|
||||||
state.progress = Some((current, total));
|
if let Some(task) = state.legacy.as_mut() {
|
||||||
|
task.progress = Some((current, total));
|
||||||
|
}
|
||||||
|
refresh_visible(&mut state);
|
||||||
super::request_redraw();
|
super::request_redraw();
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -188,7 +195,10 @@ pub fn set_sub_action(text: impl Into<String>) {
|
|||||||
let text = text.into();
|
let text = text.into();
|
||||||
match PROGRESS.lock() {
|
match PROGRESS.lock() {
|
||||||
Ok(mut state) => {
|
Ok(mut state) => {
|
||||||
state.sub_action = Some(text.clone());
|
if let Some(task) = state.legacy.as_mut() {
|
||||||
|
task.sub_action = Some(text.clone());
|
||||||
|
}
|
||||||
|
refresh_visible(&mut state);
|
||||||
super::request_redraw();
|
super::request_redraw();
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -201,7 +211,7 @@ pub fn set_sub_action(text: impl Into<String>) {
|
|||||||
pub fn clear() {
|
pub fn clear() {
|
||||||
match PROGRESS.lock() {
|
match PROGRESS.lock() {
|
||||||
Ok(mut state) => {
|
Ok(mut state) => {
|
||||||
state.legacy_active = false;
|
state.legacy = None;
|
||||||
refresh_visible(&mut state);
|
refresh_visible(&mut state);
|
||||||
super::request_redraw();
|
super::request_redraw();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,28 @@ fn clearing_legacy_progress_keeps_owned_tasks() {
|
|||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn owned_tasks_restore_legacy_progress_and_legacy_updates_do_not_cover_them() {
|
||||||
|
let _guard = TEST_LOCK.lock().unwrap();
|
||||||
|
clear();
|
||||||
|
set_action("legacy");
|
||||||
|
set_sub_action("download");
|
||||||
|
set_progress(1, 3);
|
||||||
|
let task = ProgressTask::start("owned");
|
||||||
|
set_progress(2, 3);
|
||||||
|
assert_eq!(
|
||||||
|
PROGRESS.lock().unwrap().current_action.as_deref(),
|
||||||
|
Some("owned")
|
||||||
|
);
|
||||||
|
task.finish();
|
||||||
|
let state = PROGRESS.lock().unwrap();
|
||||||
|
assert_eq!(state.current_action.as_deref(), Some("legacy"));
|
||||||
|
assert_eq!(state.sub_action.as_deref(), Some("download"));
|
||||||
|
assert_eq!(state.progress, Some((2, 3)));
|
||||||
|
drop(state);
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_new_legacy_action_does_not_reuse_the_previous_phase_progress() {
|
fn a_new_legacy_action_does_not_reuse_the_previous_phase_progress() {
|
||||||
let _guard = TEST_LOCK.lock().unwrap();
|
let _guard = TEST_LOCK.lock().unwrap();
|
||||||
|
|||||||
Reference in new issue
Block a user