use std::sync::Arc;
use near_time::Duration;
use super::data::TestLoopData;
type TestLoopCallback = Box<dyn FnOnce(&mut TestLoopData) + Send>;
#[derive(Clone)]
pub struct RawPendingEventsSender(Arc<dyn Fn(CallbackEvent) + Send + Sync>);
impl RawPendingEventsSender {
pub(crate) fn new(f: impl Fn(CallbackEvent) + Send + Sync + 'static) -> Self {
Self(Arc::new(f))
}
pub(crate) fn for_identifier(&self, identifier: &str) -> PendingEventsSender {
PendingEventsSender { identifier: identifier.to_string(), sender: self.clone() }
}
}
#[derive(Clone)]
pub struct PendingEventsSender {
identifier: String,
sender: RawPendingEventsSender,
}
impl PendingEventsSender {
pub fn send(&self, description: String, callback: TestLoopCallback) {
self.send_with_delay(description, callback, Duration::ZERO);
}
pub fn send_with_delay(
&self,
description: String,
callback: TestLoopCallback,
delay: Duration,
) {
let identifier = self.identifier.clone();
(self.sender.0)(CallbackEvent { identifier, description, callback, delay });
}
}
pub(crate) struct CallbackEvent {
pub(crate) callback: TestLoopCallback,
pub(crate) delay: Duration,
pub(crate) identifier: String,
pub(crate) description: String,
}