//! use crate::{ asyncjob::{AsyncJob, RunParams}, error::Result, sync::remotes::fetch_all, sync::{cred::BasicAuthCredential, RepoPath}, AsyncGitNotification, ProgressPercent, }; use std::sync::{Arc, Mutex}; enum JobState { Request(Option), Response(Result<()>), } /// #[derive(Clone)] pub struct AsyncFetchJob { state: Arc>>, repo: RepoPath, } /// impl AsyncFetchJob { /// pub fn new( repo: RepoPath, basic_credential: Option, ) -> Self { Self { repo, state: Arc::new(Mutex::new(Some(JobState::Request( basic_credential, )))), } } /// pub fn result(&self) -> Option> { if let Ok(mut state) = self.state.lock() { if let Some(state) = state.take() { return match state { JobState::Request(_) => None, JobState::Response(result) => Some(result), }; } } None } } impl AsyncJob for AsyncFetchJob { type Notification = AsyncGitNotification; type Progress = ProgressPercent; fn run( &mut self, _params: RunParams, ) -> Result { if let Ok(mut state) = self.state.lock() { *state = state.take().map(|state| match state { JobState::Request(basic_credentials) => { //TODO: support progress let result = fetch_all( &self.repo, &basic_credentials, &None, ); JobState::Response(result) } JobState::Response(result) => { JobState::Response(result) } }); } Ok(AsyncGitNotification::Fetch) } }