Refactor watchexec to only have &self methods and default to wrap it in Arc
This commit is contained in:
parent
249c581dc9
commit
6a46c2bff3
|
@ -91,6 +91,12 @@ dependencies = [
|
|||
"syn 1.0.73",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atomic-take"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9f65e4fb35ff6a80b3298d1f028649f3a23da141fa3951e9b24dde1d515b67e"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.1"
|
||||
|
@ -2067,6 +2073,7 @@ checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
|
|||
name = "watchexec"
|
||||
version = "1.17.1"
|
||||
dependencies = [
|
||||
"atomic-take",
|
||||
"color-eyre",
|
||||
"command-group",
|
||||
"derive_builder",
|
||||
|
|
|
@ -22,6 +22,7 @@ tracing = "0.1.26"
|
|||
dunce = "1.0.2"
|
||||
futures = "0.3.16"
|
||||
derive_builder = "0.10.2"
|
||||
atomic-take = "1.0.0"
|
||||
|
||||
[dependencies.command-group]
|
||||
version = "1.0.5"
|
||||
|
@ -34,4 +35,3 @@ features = ["full"]
|
|||
[dev-dependencies]
|
||||
color-eyre = "0.5.11"
|
||||
tracing-subscriber = "0.2.19"
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use std::{mem::take, sync::Arc};
|
||||
|
||||
use atomic_take::AtomicTake;
|
||||
use futures::FutureExt;
|
||||
use tokio::{
|
||||
spawn,
|
||||
|
@ -16,13 +17,17 @@ use crate::{
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Watchexec {
|
||||
handle: JoinHandle<Result<(), CriticalError>>,
|
||||
handle: Arc<AtomicTake<JoinHandle<Result<(), CriticalError>>>>,
|
||||
start_lock: Arc<Notify>,
|
||||
fs_watch: watch::Sender<fs::WorkingData>,
|
||||
}
|
||||
|
||||
impl Watchexec {
|
||||
pub fn new(mut config: Config) -> Result<Self, CriticalError> {
|
||||
/// TODO
|
||||
///
|
||||
/// Returns an [`Arc`] for convenience; use [`try_unwrap`][Arc::try_unwrap()] to get the value
|
||||
/// directly if needed.
|
||||
pub fn new(mut config: Config) -> Result<Arc<Self>, CriticalError> {
|
||||
let (fs_s, fs_r) = watch::channel(take(&mut config.fs));
|
||||
|
||||
let notify = Arc::new(Notify::new());
|
||||
|
@ -45,11 +50,11 @@ impl Watchexec {
|
|||
try_join!(fs, signal).map(drop)
|
||||
});
|
||||
|
||||
Ok(Self {
|
||||
handle,
|
||||
Ok(Arc::new(Self {
|
||||
handle: Arc::new(AtomicTake::new(handle)),
|
||||
start_lock,
|
||||
fs_watch: fs_s,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn reconfig(&self, config: Config) -> Result<(), ReconfigError> {
|
||||
|
@ -57,11 +62,17 @@ impl Watchexec {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn run(&mut self) -> Result<(), CriticalError> {
|
||||
/// Start watchexec and obtain the handle to its main task.
|
||||
///
|
||||
/// This must only be called once.
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if called twice.
|
||||
pub fn main(&self) -> JoinHandle<Result<(), CriticalError>> {
|
||||
self.start_lock.notify_one();
|
||||
(&mut self.handle)
|
||||
.await
|
||||
.map_err(CriticalError::MainTaskJoin)?
|
||||
self.handle
|
||||
.take()
|
||||
.expect("Watchexec::main was called twice")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue