use super::{Scheduler, Timer};
use std::cell::{Cell, RefCell};
use std::collections::VecDeque;
use std::rc::{Rc, Weak};
type Task = Box<dyn FnOnce()>;
pub(crate) struct EventLoop {
me: Weak<EventLoop>,
queue: RefCell<VecDeque<Task>>,
after_task: RefCell<Vec<Task>>,
depth: Cell<u32>,
checkpoint_requested: Cell<bool>,
stopped: Cell<bool>,
draining: Cell<bool>,
scheduler: Rc<dyn Scheduler>,
}
impl EventLoop {
pub(crate) fn new(scheduler: Rc<dyn Scheduler>) -> Rc<Self> {
Rc::new_cyclic(|me| EventLoop {
me: me.clone(),
queue: RefCell::default(),
after_task: RefCell::default(),
depth: Cell::new(0),
checkpoint_requested: Cell::new(false),
stopped: Cell::new(false),
draining: Cell::new(false),
scheduler,
})
}
pub(crate) fn microtask(&self, task: impl FnOnce() + 'static) {
if self.stopped.get() {
return;
}
self.queue.borrow_mut().push_back(Box::new(task));
if self.depth.get() == 0 {
self.request_checkpoint();
}
}
pub(crate) fn after_current_task(&self, work: impl FnOnce() + 'static) {
if self.stopped.get() {
return;
}
if self.depth.get() == 0 || self.draining.get() {
return self.microtask(work);
}
self.after_task.borrow_mut().push(Box::new(work));
}
fn run_after_task(&self) {
loop {
let next = {
let mut a = self.after_task.borrow_mut();
if a.is_empty() {
break;
}
a.remove(0)
};
next();
}
}
pub(crate) fn dispatch<T>(&self, body: impl FnOnce() -> T) -> T {
if self.depth.get() > 0 {
return body();
}
self.drain();
self.depth.set(1);
let result = body();
self.run_after_task();
self.drain();
self.depth.set(0);
result
}
pub(crate) fn call<T>(&self, body: impl FnOnce() -> T) -> T {
if self.depth.get() > 0 {
return body();
}
self.depth.set(1);
let result = body();
self.run_after_task();
self.depth.set(0);
if !self.queue.borrow().is_empty() {
self.request_checkpoint();
}
result
}
pub(crate) fn stop(&self) {
self.stopped.set(true);
self.queue.borrow_mut().clear();
self.after_task.borrow_mut().clear();
}
fn request_checkpoint(&self) {
if self.checkpoint_requested.replace(true) {
return;
}
let me = self.me.clone();
self.scheduler.checkpoint(Box::new(move || {
if let Some(me) = me.upgrade() {
me.checkpoint_requested.set(false);
me.dispatch(|| {});
}
}));
}
fn drain(&self) {
let was = self.draining.replace(true);
loop {
let next = self.queue.borrow_mut().pop_front();
let Some(task) = next else { break };
task();
}
self.draining.set(was);
}
}
pub(crate) struct LoopScheduler {
pub(crate) base: Rc<dyn Scheduler>,
pub(crate) event_loop: Weak<EventLoop>,
}
impl Scheduler for LoopScheduler {
fn now(&self) -> f64 {
self.base.now()
}
fn monotonic_now(&self) -> f64 {
self.base.monotonic_now()
}
fn schedule(&self, ms: f64, action: Box<dyn FnOnce()>) -> Box<dyn Timer> {
let event_loop = self.event_loop.clone();
self.base.schedule(
ms,
Box::new(move || {
if let Some(l) = event_loop.upgrade() {
l.dispatch(action);
}
}),
)
}
fn schedule_repeating(&self, ms: f64, mut action: Box<dyn FnMut()>) -> Box<dyn Timer> {
let event_loop = self.event_loop.clone();
self.base.schedule_repeating(
ms,
Box::new(move || {
if let Some(l) = event_loop.upgrade() {
l.dispatch(&mut action);
}
}),
)
}
fn checkpoint(&self, action: Box<dyn FnOnce()>) {
self.base.checkpoint(action)
}
}