use std::sync::Arc;
use crate::TopicKey;
use super::local_event_bus_inner::LocalEventBusInner;
pub(crate) struct ProcessingTask {
bus: Arc<LocalEventBusInner>,
topic_key: TopicKey,
task: Option<Box<dyn FnOnce() + Send + 'static>>,
finished: bool,
}
impl ProcessingTask {
pub(crate) fn new<F>(bus: Arc<LocalEventBusInner>, topic_key: TopicKey, task: F) -> Self
where
F: FnOnce() + Send + 'static,
{
Self {
bus,
topic_key,
task: Some(Box::new(task)),
finished: false,
}
}
pub(crate) fn run(mut self) {
if let Some(task) = self.task.take() {
task();
}
self.finish();
}
fn finish(&mut self) {
if self.finished {
return;
}
self.finished = true;
self.bus.finish_processing(&self.topic_key);
}
}
impl Drop for ProcessingTask {
fn drop(&mut self) {
self.finish();
}
}