Skip to content

Commit

Permalink
spawn server and watcher seperately
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnynotsolucky committed Aug 17, 2023
1 parent 1490423 commit 274170f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
21 changes: 10 additions & 11 deletions sluggy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ impl Command {

let mut watcher = Watch::new(
[
&generate_config.content_dir,
&generate_config.assets_dir,
&generate_config.template_dir,
&generate_config.css_dir,
&generate_config.out_dir,
generate_config.content_dir.clone(),
generate_config.assets_dir.clone(),
generate_config.template_dir.clone(),
generate_config.css_dir.clone(),
generate_config.out_dir.clone(),
]
.into_iter(),
Duration::from_millis(250),
Expand Down Expand Up @@ -132,13 +132,12 @@ impl Command {
},
);

let serve_handle = tokio::spawn(serve(server_config));
let watch_handle = tokio::spawn(watcher.watch());

select! {
res = serve(server_config) => {
res?
},
res = watcher.watch() => {
res?
},
_ = serve_handle => {},
_ = watch_handle => {},
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions sluggy/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@ use futures::{
use notify::{RecommendedWatcher, RecursiveMode};
use sluggy_core::error::Result;
use std::{future::Future, path::Path, time::Duration};
use std::path::PathBuf;
use tokio::{runtime::Handle, task::block_in_place};

pub struct Watch<P, H, I>
pub struct Watch<H, I>
where
P: AsRef<Path>,
H: WatchHandler,
I: Iterator<Item = P>,
I: Iterator<Item = PathBuf>,
{
paths: I,
timeout: Duration,
handler: H,
}

impl<P, H, I> Watch<P, H, I>
impl<H, I> Watch<H, I>
where
P: AsRef<Path>,
H: WatchHandler,
I: Iterator<Item = P>,
I: Iterator<Item = PathBuf>,
{
pub fn new(paths: I, timeout: Duration, handler: H) -> Self {
Self {
Expand All @@ -33,7 +32,7 @@ where
}
}

pub async fn watch(&mut self) -> Result<()> {
pub async fn watch(mut self) -> Result<()> {
let (mut debouncer, mut rx) = create_debounced_watcher(self.timeout)?;

for path in self.paths.by_ref() {
Expand Down

0 comments on commit 274170f

Please sign in to comment.