Skip to content

Commit

Permalink
change handler to optional
Browse files Browse the repository at this point in the history
  • Loading branch information
NotThatRqd committed Nov 19, 2023
1 parent bc027e3 commit e4a15c1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ let buttons = vec![count_button, plus_button, end_button];

let (tx, rx) = oneshot::channel();

let shutdown_config = ShutdownConfig::new(Some(rx), Box::new(server_end));
let shutdown_config = ShutdownConfig::new(Some(rx), Some(Box::new(server_end)));

bind_server(&"0.0.0.0:3000".parse().unwrap(), buttons, Counter::new(tx), None);
// uncomment to actually run the server:
Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
//!
//! let (tx, rx) = oneshot::channel();
//!
//! let shutdown_config = ShutdownConfig::new(Some(rx), Box::new(server_end));
//! let shutdown_config = ShutdownConfig::new(Some(rx), Some(Box::new(server_end)));
//!
//! bind_server(&"0.0.0.0:3000".parse().unwrap(), buttons, Counter::new(tx), None);
//! // uncomment to actually run the server:
Expand Down Expand Up @@ -144,13 +144,13 @@ pub use tokio::sync::oneshot;
/// Also see: [tokio::sync::oneshot]
pub struct ShutdownConfig<S: Send + Sync + 'static> {
pub shutdown_rx: Option<oneshot::Receiver<()>>,
pub handler: Box<dyn FnOnce(&S)>,
pub handler: Option<Box<dyn FnOnce(&S)>>,
}

impl<S: Send + Sync + 'static> ShutdownConfig<S> {
pub fn new(
shutdown_rx: Option<oneshot::Receiver<()>>,
handler: Box<dyn FnOnce(&S)>,
handler: Option<Box<dyn FnOnce(&S)>>,
) -> ShutdownConfig<S> {
ShutdownConfig {
shutdown_rx,
Expand Down Expand Up @@ -204,7 +204,9 @@ async fn shutdown_handler<S: Send + Sync + 'static>(
_ = ctrl_c_signal() => {},
_ = shutdown_rx => {},
}
(config.handler)(&state.user_state);
if let Some(handler) = config.handler {
handler(&state.user_state);
}
return;
}
}
Expand Down

0 comments on commit e4a15c1

Please sign in to comment.