use super::component::TxEntry;
use crate::error::Reject;
use crate::pool::TxPool;
pub type Callback = Box<dyn Fn(&mut TxPool, &TxEntry) + Sync + Send>;
pub type ProposedCallback = Box<dyn Fn(&mut TxPool, &TxEntry, bool) + Sync + Send>;
pub type RejectCallback = Box<dyn Fn(&mut TxPool, &TxEntry, Reject) + Sync + Send>;
pub struct Callbacks {
pub(crate) pending: Option<Callback>,
pub(crate) proposed: Option<ProposedCallback>,
pub(crate) committed: Option<Callback>,
pub(crate) reject: Option<RejectCallback>,
}
impl Default for Callbacks {
fn default() -> Self {
Self::new()
}
}
impl Callbacks {
pub fn new() -> Self {
Callbacks {
pending: None,
proposed: None,
committed: None,
reject: None,
}
}
pub fn register_pending(&mut self, callback: Callback) {
self.pending = Some(callback);
}
pub fn register_proposed(&mut self, callback: ProposedCallback) {
self.proposed = Some(callback);
}
pub fn register_committed(&mut self, callback: Callback) {
self.committed = Some(callback);
}
pub fn register_reject(&mut self, callback: RejectCallback) {
self.reject = Some(callback);
}
pub fn call_pending(&self, tx_pool: &mut TxPool, entry: &TxEntry) {
if let Some(call) = &self.pending {
call(tx_pool, entry)
}
}
pub fn call_proposed(&self, tx_pool: &mut TxPool, entry: &TxEntry, new: bool) {
if let Some(call) = &self.proposed {
call(tx_pool, entry, new)
}
}
pub fn call_committed(&self, tx_pool: &mut TxPool, entry: &TxEntry) {
if let Some(call) = &self.committed {
call(tx_pool, entry)
}
}
pub fn call_reject(&self, tx_pool: &mut TxPool, entry: &TxEntry, reject: Reject) {
if let Some(call) = &self.reject {
call(tx_pool, entry, reject)
}
}
}