Allow an outcome to be determined by the action handler without &mut!
This commit is contained in:
parent
2a0661b122
commit
350b85e0c7
|
@ -2081,6 +2081,7 @@ dependencies = [
|
||||||
"futures",
|
"futures",
|
||||||
"miette",
|
"miette",
|
||||||
"notify 5.0.0-pre.12",
|
"notify 5.0.0-pre.12",
|
||||||
|
"once_cell",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
|
|
|
@ -23,6 +23,7 @@ dunce = "1.0.2"
|
||||||
futures = "0.3.16"
|
futures = "0.3.16"
|
||||||
derive_builder = "0.10.2"
|
derive_builder = "0.10.2"
|
||||||
atomic-take = "1.0.0"
|
atomic-take = "1.0.0"
|
||||||
|
once_cell = "1.8.0"
|
||||||
|
|
||||||
[dependencies.command-group]
|
[dependencies.command-group]
|
||||||
version = "1.0.5"
|
version = "1.0.5"
|
||||||
|
|
|
@ -7,6 +7,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use atomic_take::AtomicTake;
|
use atomic_take::AtomicTake;
|
||||||
|
use once_cell::sync::OnceCell;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
sync::{mpsc, watch},
|
sync::{mpsc, watch},
|
||||||
time::timeout,
|
time::timeout,
|
||||||
|
@ -43,9 +44,38 @@ impl Default for WorkingData {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Action {
|
pub struct Action {
|
||||||
pub events: Vec<Event>,
|
pub events: Vec<Event>,
|
||||||
|
outcome: Arc<OnceCell<Outcome>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
pub enum Outcome {
|
||||||
|
DoNothing, // TODO more
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Outcome {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::DoNothing // TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Action {
|
||||||
|
fn new(events: Vec<Event>) -> Self {
|
||||||
|
Self {
|
||||||
|
events,
|
||||||
|
..Self::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the action's outcome.
|
||||||
|
///
|
||||||
|
/// This takes `self` and `Action` is not `Clone`, so it's only possible to call it once.
|
||||||
|
/// Regardless, if you _do_ manage to call it twice, it will do nothing beyond the first call.
|
||||||
|
pub fn outcome(self, outcome: Outcome) {
|
||||||
|
self.outcome.set(outcome).ok();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn worker(
|
pub async fn worker(
|
||||||
|
@ -74,9 +104,7 @@ pub async fn worker(
|
||||||
|
|
||||||
last = Instant::now();
|
last = Instant::now();
|
||||||
|
|
||||||
let action = Action {
|
let action = Action::new(set.drain(..).collect());
|
||||||
events: set.drain(..).collect(),
|
|
||||||
};
|
|
||||||
debug!(?action, "action constructed");
|
debug!(?action, "action constructed");
|
||||||
|
|
||||||
if let Some(h) = working.borrow().action_handler.take() {
|
if let Some(h) = working.borrow().action_handler.take() {
|
||||||
|
@ -84,10 +112,14 @@ pub async fn worker(
|
||||||
handler = h;
|
handler = h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let outcome = action.outcome.clone();
|
||||||
let err = handler.handle(action).map_err(|e| rte("action worker", e));
|
let err = handler.handle(action).map_err(|e| rte("action worker", e));
|
||||||
if let Err(err) = err {
|
if let Err(err) = err {
|
||||||
errors.send(err).await?;
|
errors.send(err).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let outcome = outcome.get().cloned().unwrap_or_default();
|
||||||
|
debug!(?outcome, "handler finished");
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue