refactor(download manager): Removed Arc requirement for DownloadableMetadata

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2025-01-02 12:24:22 +11:00
parent cac612b176
commit 8be1dd435c
9 changed files with 40 additions and 40 deletions

View File

@ -220,9 +220,9 @@ pub fn fetch_download_dir_stats() -> Result<Vec<String>, String> {
Ok(directories)
}
pub fn set_game_status<F: FnOnce(&mut RwLockWriteGuard<'_, Database>, &Arc<DownloadableMetadata>)>(
pub fn set_game_status<F: FnOnce(&mut RwLockWriteGuard<'_, Database>, &DownloadableMetadata)>(
app_handle: &AppHandle,
id: Arc<DownloadableMetadata>,
id: DownloadableMetadata,
setter: F,
) {
let mut db_handle = DB.borrow_data_mut().unwrap();

View File

@ -21,7 +21,7 @@ pub enum DownloadManagerSignal {
/// Pauses the DownloadManager
Stop,
/// Called when a DownloadAgent has fully completed a download.
Completed(Arc<DownloadableMetadata>),
Completed(DownloadableMetadata),
/// Generates and appends a DownloadAgent
/// to the registry and queue
Queue(DownloadAgent),
@ -30,9 +30,9 @@ pub enum DownloadManagerSignal {
/// then exit
Finish,
/// Stops, removes, and tells a download to cleanup
Cancel(Arc<DownloadableMetadata>),
Cancel(DownloadableMetadata),
/// Removes a given application
Remove(Arc<DownloadableMetadata>),
Remove(DownloadableMetadata),
/// Any error which occurs in the agent
Error(ApplicationDownloadError),
/// Pushes UI update
@ -40,7 +40,7 @@ pub enum DownloadManagerSignal {
UpdateUIStats(usize, usize), //kb/s and seconds
/// Uninstall download
/// Takes download ID
Uninstall(Arc<DownloadableMetadata>),
Uninstall(DownloadableMetadata),
}
#[derive(Debug, Clone)]
@ -109,17 +109,17 @@ impl DownloadManager {
self.command_sender.send(DownloadManagerSignal::Queue(download))?;
self.command_sender.send(DownloadManagerSignal::Go)
}
pub fn edit(&self) -> MutexGuard<'_, VecDeque<Arc<DownloadableMetadata>>> {
pub fn edit(&self) -> MutexGuard<'_, VecDeque<DownloadableMetadata>> {
self.download_queue.edit()
}
pub fn read_queue(&self) -> VecDeque<Arc<DownloadableMetadata>> {
pub fn read_queue(&self) -> VecDeque<DownloadableMetadata> {
self.download_queue.read()
}
pub fn get_current_download_progress(&self) -> Option<f64> {
let progress_object = (*self.progress.lock().unwrap()).clone()?;
Some(progress_object.get_progress())
}
pub fn rearrange_string(&self, meta: &Arc<DownloadableMetadata>, new_index: usize) {
pub fn rearrange_string(&self, meta: &DownloadableMetadata, new_index: usize) {
let mut queue = self.edit();
let current_index = get_index_from_id(&mut queue, meta).unwrap();
let to_move = queue.remove(current_index).unwrap();
@ -128,7 +128,7 @@ impl DownloadManager {
.send(DownloadManagerSignal::UpdateUIQueue)
.unwrap();
}
pub fn cancel(&self, meta: Arc<DownloadableMetadata>) {
pub fn cancel(&self, meta: DownloadableMetadata) {
self.command_sender
.send(DownloadManagerSignal::Remove(meta))
.unwrap();
@ -174,7 +174,7 @@ impl DownloadManager {
.unwrap();
self.terminator.join()
}
pub fn uninstall_application(&self, meta: Arc<DownloadableMetadata>) {
pub fn uninstall_application(&self, meta: DownloadableMetadata) {
self.command_sender
.send(DownloadManagerSignal::Uninstall(meta))
.unwrap();
@ -187,8 +187,8 @@ impl DownloadManager {
/// Takes in the locked value from .edit() and attempts to
/// get the index of whatever id is passed in
fn get_index_from_id(
queue: &mut MutexGuard<'_, VecDeque<Arc<DownloadableMetadata>>>,
meta: &Arc<DownloadableMetadata>,
queue: &mut MutexGuard<'_, VecDeque<DownloadableMetadata>>,
meta: &DownloadableMetadata,
) -> Option<usize> {
queue
.iter()

View File

@ -58,7 +58,7 @@ Behold, my madness - quexeky
*/
pub struct DownloadManagerBuilder {
download_agent_registry: HashMap<Arc<DownloadableMetadata>, DownloadAgent>,
download_agent_registry: HashMap<DownloadableMetadata, DownloadAgent>,
download_queue: Queue,
command_receiver: Receiver<DownloadManagerSignal>,
sender: Sender<DownloadManagerSignal>,
@ -100,7 +100,7 @@ impl DownloadManagerBuilder {
*self.status.lock().unwrap() = status;
}
fn remove_and_cleanup_front_download(&mut self, meta: &Arc<DownloadableMetadata>) -> DownloadAgent {
fn remove_and_cleanup_front_download(&mut self, meta: &DownloadableMetadata) -> DownloadAgent {
self.download_queue.pop_front();
let download_agent = self.download_agent_registry.remove(meta).unwrap();
self.cleanup_current_download();
@ -249,7 +249,7 @@ impl DownloadManagerBuilder {
active_control_flag.set(DownloadThreadControlFlag::Stop);
}
}
fn manage_completed_signal(&mut self, meta: Arc<DownloadableMetadata>) {
fn manage_completed_signal(&mut self, meta: DownloadableMetadata) {
info!("Got signal Completed");
if let Some(interface) = &self.current_download_agent {
if interface.metadata() == meta {
@ -270,7 +270,7 @@ impl DownloadManagerBuilder {
self.set_status(DownloadManagerStatus::Error(error));
}
fn manage_cancel_signal(&mut self, meta: &Arc<DownloadableMetadata>) {
fn manage_cancel_signal(&mut self, meta: &DownloadableMetadata) {
info!("Got signal Cancel");
if let Some(current_download) = &self.current_download_agent {
@ -293,7 +293,7 @@ impl DownloadManagerBuilder {
}
}
}
fn uninstall_application(&mut self, meta: &Arc<DownloadableMetadata>) {
fn uninstall_application(&mut self, meta: &DownloadableMetadata) {
let download_agent = match self.download_agent_registry.get(meta) {
Some(download_agent) => download_agent.clone(),
None => return,

View File

@ -11,7 +11,7 @@ pub trait Downloadable: Send + Sync {
fn progress(&self) -> Arc<ProgressObject>;
fn control_flag(&self) -> DownloadThreadControl;
fn status(&self) -> DownloadStatus;
fn metadata(&self) -> Arc<DownloadableMetadata>;
fn metadata(&self) -> DownloadableMetadata;
fn on_initialised(&self, app_handle: &AppHandle);
fn on_error(&self, app_handle: &AppHandle, error: ApplicationDownloadError);
fn on_complete(&self, app_handle: &AppHandle);

View File

@ -2,6 +2,6 @@ use std::sync::Arc;
use super::{download_manager_builder::DownloadAgent, downloadable_metadata::DownloadableMetadata};
pub fn generate_downloadable(meta: Arc<DownloadableMetadata>) -> DownloadAgent {
pub fn generate_downloadable(meta: DownloadableMetadata) -> DownloadAgent {
todo!()
}

View File

@ -7,7 +7,7 @@ use super::downloadable_metadata::DownloadableMetadata;
#[derive(Clone)]
pub struct Queue {
inner: Arc<Mutex<VecDeque<Arc<DownloadableMetadata>>>>,
inner: Arc<Mutex<VecDeque<DownloadableMetadata>>>,
}
#[allow(dead_code)]
@ -17,37 +17,37 @@ impl Queue {
inner: Arc::new(Mutex::new(VecDeque::new())),
}
}
pub fn read(&self) -> VecDeque<Arc<DownloadableMetadata>> {
pub fn read(&self) -> VecDeque<DownloadableMetadata> {
self.inner.lock().unwrap().clone()
}
pub fn edit(&self) -> MutexGuard<'_, VecDeque<Arc<DownloadableMetadata>>> {
pub fn edit(&self) -> MutexGuard<'_, VecDeque<DownloadableMetadata>> {
self.inner.lock().unwrap()
}
pub fn pop_front(&self) -> Option<Arc<DownloadableMetadata>> {
pub fn pop_front(&self) -> Option<DownloadableMetadata> {
self.edit().pop_front()
}
pub fn empty(&self) -> bool {
self.inner.lock().unwrap().len() == 0
}
pub fn exists(&self, meta: Arc<DownloadableMetadata>) -> bool {
pub fn exists(&self, meta: DownloadableMetadata) -> bool {
self.read().contains(&meta)
}
/// Either inserts `interface` at the specified index, or appends to
/// the back of the deque if index is greater than the length of the deque
pub fn insert(&self, interface: Arc<DownloadableMetadata>, index: usize) {
pub fn insert(&self, interface: DownloadableMetadata, index: usize) {
if self.read().len() > index {
self.append(interface);
} else {
self.edit().insert(index, interface);
}
}
pub fn append(&self, interface: Arc<DownloadableMetadata>) {
pub fn append(&self, interface: DownloadableMetadata) {
self.edit().push_back(interface);
}
pub fn pop_front_if_equal(
&self,
meta: &Arc<DownloadableMetadata>,
) -> Option<Arc<DownloadableMetadata>> {
meta: &DownloadableMetadata,
) -> Option<DownloadableMetadata> {
let mut queue = self.edit();
let front = match queue.front() {
Some(front) => front,
@ -58,10 +58,10 @@ impl Queue {
}
None
}
pub fn get_by_meta(&self, meta: &Arc<DownloadableMetadata>) -> Option<usize> {
pub fn get_by_meta(&self, meta: &DownloadableMetadata) -> Option<usize> {
self.read().iter().position(|data| data == meta)
}
pub fn move_to_index_by_meta(&self, meta: &Arc<DownloadableMetadata>, new_index: usize) -> Result<(), ()> {
pub fn move_to_index_by_meta(&self, meta: &DownloadableMetadata, new_index: usize) -> Result<(), ()> {
let index = match self.get_by_meta(meta) {
Some(index) => index,
None => return Err(()),

View File

@ -1,7 +1,7 @@
use crate::auth::generate_authorization_header;
use crate::db::{set_game_status, DatabaseImpls};
use crate::download_manager::application_download_error::ApplicationDownloadError;
use crate::download_manager::download_manager::DownloadManagerSignal;
use crate::download_manager::download_manager::{DownloadManagerSignal, DownloadStatus};
use crate::download_manager::download_thread_control_flag::{DownloadThreadControl, DownloadThreadControlFlag};
use crate::download_manager::downloadable::Downloadable;
use crate::download_manager::downloadable_metadata::DownloadableMetadata;
@ -317,7 +317,7 @@ impl Downloadable for GameDownloadAgent {
self.control_flag.clone()
}
fn metadata(&self) -> Arc<DownloadableMetadata> {
fn metadata(&self) -> DownloadableMetadata {
todo!()
}
@ -354,7 +354,7 @@ impl Downloadable for GameDownloadAgent {
todo!()
}
fn status(&self) -> crate::download_manager::download_manager::DownloadStatus {
fn status(&self) -> DownloadStatus {
todo!()
}
}

View File

@ -191,7 +191,7 @@ pub fn fetch_game(id: DownloadableMetadata, app: tauri::AppHandle) -> Result<Fet
}
#[tauri::command]
pub fn fetch_game_status(meta: Arc<DownloadableMetadata>) -> Result<ApplicationStatusWithTransient, String> {
pub fn fetch_game_status(meta: DownloadableMetadata) -> Result<ApplicationStatusWithTransient, String> {
let status = DownloadStatusManager::fetch_state(&meta);
Ok(status)
@ -243,7 +243,7 @@ pub fn fetch_game_verion_options<'a>(
#[tauri::command]
pub fn uninstall_game(
game_id: Arc<DownloadableMetadata>,
game_id: DownloadableMetadata,
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<(), String> {
let state_lock = state.lock().unwrap();
@ -266,7 +266,7 @@ pub fn push_game_update(app_handle: &AppHandle, meta: DownloadableMetadata, stat
}
pub fn on_game_complete(
meta: Arc<DownloadableMetadata>,
meta: DownloadableMetadata,
install_dir: String,
app_handle: &AppHandle,
) -> Result<(), RemoteAccessError> {
@ -303,7 +303,7 @@ pub fn on_game_complete(
handle
.applications
.versions
.entry(*meta.clone())
.entry(meta.clone())
.or_default()
.insert(meta.version.clone(), data.clone());
drop(handle);
@ -325,7 +325,7 @@ pub fn on_game_complete(
db_handle
.applications
.statuses
.insert(*meta.clone(), status.clone());
.insert(meta.clone(), status.clone());
drop(db_handle);
DB.save().unwrap();
app_handle

View File

@ -26,7 +26,7 @@ impl Downloadable for ToolDownloadAgent {
todo!()
}
fn metadata(&self) -> Arc<DownloadableMetadata> {
fn metadata(&self) -> DownloadableMetadata {
todo!()
}