use crate::components::button::ComponentButton; use std::{cell::RefCell, collections::VecDeque, rc::Rc}; pub struct Tasks(Rc>>); impl Clone for Tasks { fn clone(&self) -> Self { Self(self.0.clone()) } } impl Tasks { pub fn new() -> Self { Self(Rc::new(RefCell::new(VecDeque::new()))) } pub fn push(&self, task: TaskType) { self.0.borrow_mut().push_back(task); } pub fn drain(&mut self) -> VecDeque { let mut tasks = self.0.borrow_mut(); std::mem::take(&mut *tasks) } } impl Default for Tasks { fn default() -> Self { Self::new() } } impl Tasks { pub fn handle_button(&self, button: &Rc, task: TaskType) { button.on_click({ let this = self.clone(); Box::new(move |_, _| { this.push(task.clone()); Ok(()) }) }); } pub fn make_callback(&self, task: TaskType) -> Rc { let this = self.clone(); Rc::new(move || { this.push(task.clone()); }) } }