stem_splitter_core/io/
progress.rs

1// src/core/progress.rs
2use std::sync::{Mutex, OnceLock};
3
4static DOWNLOAD_PROGRESS_CB: OnceLock<Mutex<Option<Box<dyn Fn(u64, u64) + Send + 'static>>>> =
5    OnceLock::new();
6static SPLIT_PROGRESS_CB: OnceLock<Mutex<Option<Box<dyn Fn(SplitProgress) + Send + 'static>>>> =
7    OnceLock::new();
8
9#[derive(Debug, Clone, serde::Serialize)]
10pub enum SplitProgress {
11    Stage(&'static str),
12    Chunks {
13        done: usize,
14        total: usize,
15        percent: f32,
16    },
17    Writing {
18        stem: String,
19        done: usize,
20        total: usize,
21        percent: f32,
22    },
23    Finished,
24}
25
26pub fn set_download_progress_callback(cb: impl Fn(u64, u64) + Send + 'static) {
27    let _ = DOWNLOAD_PROGRESS_CB.set(Mutex::new(Some(Box::new(cb))));
28}
29
30pub fn emit_download_progress(done: u64, total: u64) {
31    if let Some(m) = DOWNLOAD_PROGRESS_CB.get() {
32        if let Ok(g) = m.lock() {
33            if let Some(cb) = &*g {
34                cb(done, total);
35            }
36        }
37    }
38}
39
40pub fn set_split_progress_callback(cb: impl Fn(SplitProgress) + Send + 'static) {
41    let _ = SPLIT_PROGRESS_CB.set(Mutex::new(Some(Box::new(cb))));
42}
43
44pub fn emit_split_progress(p: SplitProgress) {
45    if let Some(m) = SPLIT_PROGRESS_CB.get() {
46        if let Ok(g) = m.lock() {
47            if let Some(cb) = &*g {
48                cb(p);
49            }
50        }
51    }
52}