#![allow(dead_code)]
use std::sync::Mutex;
#[derive(Clone, Debug)]
pub enum CommandLifecycleState {
Started,
Completed,
}
pub type CommandLifecycleListener =
Option<Box<dyn Fn(String, CommandLifecycleState) + Send + Sync>>;
static LISTENER: Mutex<CommandLifecycleListener> = Mutex::new(None);
pub fn set_command_lifecycle_listener(cb: CommandLifecycleListener) {
if let Ok(mut listener) = LISTENER.lock() {
*listener = cb;
}
}
pub fn notify_command_lifecycle(uuid: String, state: CommandLifecycleState) {
if let Ok(listener) = LISTENER.lock() {
if let Some(ref cb) = *listener {
cb(uuid, state);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_set_and_notify() {
let called = std::sync::Mutex::new(false);
let cb = Box::new(move |_uuid: String, _state: CommandLifecycleState| {
let _ = called.lock().map(|mut c| *c = true);
});
set_command_lifecycle_listener(Some(cb));
notify_command_lifecycle("test-uuid".to_string(), CommandLifecycleState::Started);
}
}