use crate::tree::Tree;
pub(crate) const ASSET_BASE: u32 = 1 << 20;
type Deliver = Box<dyn FnOnce(&mut Tree, Vec<u8>)>;
#[derive(Default)]
pub struct Assets {
next: u32,
pending: Vec<(u32, String)>,
waiting: Vec<(u32, Deliver)>,
}
impl Assets {
pub(crate) fn fetch(&mut self, url: &str, deliver: impl FnOnce(&mut Tree, Vec<u8>) + 'static) -> u32 {
let id = ASSET_BASE + self.next;
self.next += 1;
self.pending.push((id, url.to_owned()));
self.waiting.push((id, Box::new(deliver)));
id
}
pub(crate) fn take_requests(&mut self) -> Vec<(u32, String)> {
std::mem::take(&mut self.pending)
}
pub(crate) fn take(&mut self, id: u32) -> Option<Deliver> {
let at = self.waiting.iter().position(|(waiting, _)| *waiting == id)?;
Some(self.waiting.swap_remove(at).1)
}
}