1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
use std::cell::RefCell;

thread_local!(static TASKS: RefCell<Vec<Box<dyn FnOnce()>>> = RefCell::new(vec![]));

pub fn dispatch() {
    let tasks: Vec<_> = TASKS.with(|tasks| tasks.borrow_mut().drain(..).collect());
    for task in tasks {
        task();
    }
}

pub fn add(task: impl FnOnce() + 'static) {
    TASKS.with(|tasks| {
        tasks.borrow_mut().push(Box::new(task));
    });
}