use crate::state::AppState;
use super::super::persist::{
maybe_flush_announcement_read, maybe_flush_cache, maybe_flush_deps_cache,
maybe_flush_files_cache, maybe_flush_install, maybe_flush_news_bookmarks,
maybe_flush_news_content_cache, maybe_flush_news_read, maybe_flush_news_read_ids,
maybe_flush_news_recent, maybe_flush_news_seen_aur_comments, maybe_flush_news_seen_versions,
maybe_flush_pkgbuild_parse_cache, maybe_flush_recent, maybe_flush_sandbox_cache,
maybe_flush_services_cache,
};
use super::background::Channels;
pub fn cleanup_on_exit(app: &mut AppState, channels: &Channels) {
tracing::debug!("[Runtime] Main loop exited, resetting resolution flags");
app.deps_resolving = false;
app.files_resolving = false;
app.services_resolving = false;
app.sandbox_resolving = false;
tracing::debug!("[Runtime] Cancelling preflight operations and resetting flags");
app.preflight_cancelled
.store(true, std::sync::atomic::Ordering::Relaxed);
app.preflight_summary_resolving = false;
app.preflight_deps_resolving = false;
app.preflight_files_resolving = false;
app.preflight_services_resolving = false;
app.preflight_sandbox_resolving = false;
app.preflight_summary_items = None;
app.preflight_deps_items = None;
app.preflight_files_items = None;
app.preflight_services_items = None;
app.preflight_sandbox_items = None;
channels
.event_thread_cancelled
.store(true, std::sync::atomic::Ordering::Relaxed);
maybe_flush_cache(app);
maybe_flush_recent(app);
maybe_flush_news_recent(app);
maybe_flush_news_bookmarks(app);
maybe_flush_news_content_cache(app);
maybe_flush_news_read(app);
maybe_flush_news_read_ids(app);
maybe_flush_news_seen_versions(app);
maybe_flush_news_seen_aur_comments(app);
maybe_flush_announcement_read(app);
maybe_flush_install(app);
maybe_flush_deps_cache(app);
maybe_flush_files_cache(app);
maybe_flush_services_cache(app);
maybe_flush_sandbox_cache(app);
maybe_flush_pkgbuild_parse_cache();
}
#[cfg(test)]
mod tests {
use super::*;
use crate::state::{PackageItem, Source, modal::PreflightAction};
#[tokio::test]
async fn cleanup_on_exit_cancels_preflight_operations() {
let mut app = AppState::default();
let channels = Channels::new(std::path::PathBuf::from("/tmp/test"));
let test_package = PackageItem {
name: "test-package".to_string(),
version: "1.0.0".to_string(),
description: "Test package".to_string(),
source: Source::Official {
repo: "core".to_string(),
arch: "x86_64".to_string(),
},
popularity: None,
out_of_date: None,
orphaned: false,
};
app.install_list.push(test_package.clone());
app.preflight_summary_resolving = true;
app.preflight_deps_resolving = true;
app.preflight_files_resolving = true;
app.preflight_services_resolving = true;
app.preflight_sandbox_resolving = true;
app.preflight_summary_items = Some((vec![test_package.clone()], PreflightAction::Install));
app.preflight_deps_items = Some((vec![test_package.clone()], PreflightAction::Install));
app.preflight_files_items = Some(vec![test_package.clone()]);
app.preflight_services_items = Some(vec![test_package.clone()]);
app.preflight_sandbox_items = Some(vec![test_package]);
assert!(app.preflight_summary_resolving);
assert!(app.preflight_deps_resolving);
assert!(app.preflight_files_resolving);
assert!(app.preflight_services_resolving);
assert!(app.preflight_sandbox_resolving);
assert!(
!app.preflight_cancelled
.load(std::sync::atomic::Ordering::Relaxed)
);
assert!(app.preflight_summary_items.is_some());
assert!(app.preflight_deps_items.is_some());
assert!(app.preflight_files_items.is_some());
assert!(app.preflight_services_items.is_some());
assert!(app.preflight_sandbox_items.is_some());
cleanup_on_exit(&mut app, &channels);
assert!(!app.preflight_summary_resolving);
assert!(!app.preflight_deps_resolving);
assert!(!app.preflight_files_resolving);
assert!(!app.preflight_services_resolving);
assert!(!app.preflight_sandbox_resolving);
assert!(
app.preflight_cancelled
.load(std::sync::atomic::Ordering::Relaxed)
);
assert!(app.preflight_summary_items.is_none());
assert!(app.preflight_deps_items.is_none());
assert!(app.preflight_files_items.is_none());
assert!(app.preflight_services_items.is_none());
assert!(app.preflight_sandbox_items.is_none());
}
}