Abort support

This commit is contained in:
Tomasz Drwięga
2016-08-31 11:38:39 +02:00
parent 0f0af9c1a5
commit 8f13b550d8
5 changed files with 33 additions and 14 deletions

View File

@@ -97,7 +97,7 @@ impl Fetch {
self.abort.load(Ordering::Relaxed)
}
fn mark_aborted(&mut self) -> Next {
self.result = Some(Err(Error::Aborted));
self.result = Some(Err(Error::Aborted.into()));
Next::end()
}
}

View File

@@ -19,7 +19,8 @@
pub mod fetch_file;
use std::env;
use std::sync::mpsc;
use std::sync::{mpsc, Arc};
use std::sync::atomic::AtomicBool;
use std::path::PathBuf;
use hyper;
@@ -62,7 +63,7 @@ impl Client {
self.https_client.close();
}
pub fn request(&mut self, url: String, on_done: Box<Fn() + Send>) -> Result<mpsc::Receiver<FetchResult>, FetchError> {
pub fn request(&mut self, url: String, abort: Arc<AtomicBool>, on_done: Box<Fn() + Send>) -> Result<mpsc::Receiver<FetchResult>, FetchError> {
let is_https = url.starts_with("https://");
let url = try!(url.parse().map_err(|_| FetchError::InvalidUrl));
trace!(target: "dapps", "Fetching from: {:?}", url);
@@ -71,7 +72,7 @@ impl Client {
let (tx, rx) = mpsc::channel();
let temp_path = Self::temp_path();
let res = self.https_client.fetch_to_file(url, temp_path.clone(), move |result| {
let res = self.https_client.fetch_to_file(url, temp_path.clone(), abort, move |result| {
let res = tx.send(
result.map(|_| temp_path).map_err(FetchError::Https)
);
@@ -87,7 +88,7 @@ impl Client {
}
} else {
let (tx, rx) = mpsc::channel();
let res = self.http_client.request(url, Fetch::new(tx, on_done));
let res = self.http_client.request(url, Fetch::new(tx, abort, on_done));
match res {
Ok(_) => Ok(rx),