Skip to content

Commit

Permalink
Add CTRL+C handling
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamRagstad committed Jul 29, 2024
1 parent 4b376d5 commit 4a09adf
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 66 deletions.
152 changes: 127 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ repository = "https://github.com/webx-net/webx"
chrono = "0.4.31"
clap = { version = "4.3.21", features = ["color"] }
colored = "2.0.4"
ctrlc = "3.4.4"
deno_core = "0.242.0"
http = "0.2.9"
http-body-util = "0.1.0"
Expand Down
18 changes: 15 additions & 3 deletions src/engine/filewatcher.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use notify::{self, Error, Event, Watcher};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::Sender;
use std::time::{Duration, Instant};
use std::sync::Arc;
use std::time::Instant;

use crate::engine::runtime::WXRuntimeMessage;
use crate::file::parser::parse_webx_file;
use crate::file::webx::WXModulePath;
use crate::reporting::debug::info;
use crate::reporting::warning::warning;
use crate::runner::WXMode;
use crate::timeout_duration;

struct FSWEvent {
pub kind: notify::EventKind,
Expand Down Expand Up @@ -51,7 +54,12 @@ pub struct WXFileWatcher {}

impl WXFileWatcher {
/// Registers the file watcher thread
pub fn run(mode: WXMode, source_root: PathBuf, rt_tx: Sender<WXRuntimeMessage>) {
pub fn run(
mode: WXMode,
source_root: PathBuf,
rt_tx: Sender<WXRuntimeMessage>,
running: Arc<AtomicBool>,
) {
let mut last_event: FSWEvent = FSWEvent::empty();
let mut watcher = notify::recommended_watcher(move |res: Result<Event, Error>| {
match res {
Expand Down Expand Up @@ -106,7 +114,11 @@ impl WXFileWatcher {
.unwrap();
info(mode, "Hot reloading is enabled.");
loop {
std::thread::sleep(Duration::from_millis(1000));
if !running.load(Ordering::SeqCst) {
// println!("Shutting down file watcher...");
break;
}
std::thread::sleep(timeout_duration(mode));
}
}
}
Loading

0 comments on commit 4a09adf

Please sign in to comment.