use crate::agent::State;
#[derive(Default)]
pub struct Handler {
on_state_change: Option<Box<dyn FnMut(State) + Send + 'static>>,
on_candidate: Option<Box<dyn FnMut(String) + Send + 'static>>,
on_gathering_done: Option<Box<dyn FnMut() + Send + 'static>>,
on_recv: Option<Box<dyn FnMut(&[u8]) + Send + 'static>>,
}
impl Handler {
pub fn state_handler<F>(mut self, f: F) -> Self
where
F: FnMut(State),
F: Send + Sync + 'static,
{
self.on_state_change = Some(Box::new(f));
self
}
pub fn candidate_handler<F>(mut self, f: F) -> Self
where
F: FnMut(String),
F: Send + 'static,
{
self.on_candidate = Some(Box::new(f));
self
}
pub fn gathering_done_handler<F>(mut self, f: F) -> Self
where
F: FnMut(),
F: Send + 'static,
{
self.on_gathering_done = Some(Box::new(f));
self
}
pub fn recv_handler<F>(mut self, f: F) -> Self
where
F: FnMut(&[u8]),
F: Send + 'static,
{
self.on_recv = Some(Box::new(f));
self
}
pub(crate) fn on_state_changed(&mut self, state: State) {
if let Some(f) = &mut self.on_state_change {
f(state)
}
}
pub(crate) fn on_candidate(&mut self, candidate: String) {
if let Some(f) = &mut self.on_candidate {
f(candidate)
}
}
pub(crate) fn on_gathering_done(&mut self) {
if let Some(f) = &mut self.on_gathering_done {
f()
}
}
pub(crate) fn on_recv(&mut self, packet: &[u8]) {
if let Some(f) = &mut self.on_recv {
f(packet)
}
}
}