make fn clearable
This commit is contained in:
@@ -2,7 +2,9 @@ use crate::state::AppState;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio_stream::StreamExt;
|
use tokio_stream::StreamExt;
|
||||||
|
|
||||||
pub fn spawn_download_task(url: &str, state: Arc<AppState>) {
|
pub async fn spawn_download_task(url: &str, state: Arc<AppState>) {
|
||||||
|
state.reset().await;
|
||||||
|
|
||||||
let url = url.to_string();
|
let url = url.to_string();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Err(e) = download_stream(&url, state).await {
|
if let Err(e) = download_stream(&url, state).await {
|
||||||
|
@@ -11,7 +11,7 @@ async fn new(State(state): State<Arc<AppState>>) -> &'static str {
|
|||||||
return "Failed getting latest url";
|
return "Failed getting latest url";
|
||||||
};
|
};
|
||||||
|
|
||||||
downloader::spawn_download_task(&url, state.clone());
|
downloader::spawn_download_task(&url, state.clone()).await;
|
||||||
|
|
||||||
"Download started. Access / to stream the new content."
|
"Download started. Access / to stream the new content."
|
||||||
}
|
}
|
||||||
|
11
src/state.rs
11
src/state.rs
@@ -16,6 +16,17 @@ impl AppState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn reset(&self) {
|
||||||
|
// Clear all chunks
|
||||||
|
self.chunks.write().await.clear();
|
||||||
|
|
||||||
|
// Reset completion status
|
||||||
|
*self.complete.write().await = false;
|
||||||
|
|
||||||
|
// Notify any waiting tasks about the reset
|
||||||
|
self.notify.notify_waiters();
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn add_chunk(&self, chunk: Bytes) {
|
pub async fn add_chunk(&self, chunk: Bytes) {
|
||||||
self.chunks.write().await.push(chunk);
|
self.chunks.write().await.push(chunk);
|
||||||
self.notify.notify_waiters();
|
self.notify.notify_waiters();
|
||||||
|
Reference in New Issue
Block a user