Skip to content
This repository has been archived by the owner on Dec 14, 2019. It is now read-only.

Commit

Permalink
Add pollInterval option to watcher, which starts subprocess in poll mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Sobo committed May 8, 2019
1 parent d37a69f commit 8b9240a
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 136 deletions.
127 changes: 127 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ module.exports = class Watcher {
this.onError = params.onError;
}

const args = [];
if (params.pollInterval) args.push("--poll-interval", params.pollInterval);
this.childProcess = spawn(
`${__dirname}/../notify-subprocess-${process.platform}`,
args,
{
stdio: ["pipe", "pipe", "pipe"]
}
Expand Down
1 change: 1 addition & 0 deletions subprocess/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dunce = "1.0"
notify = "4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
structopt = "0.2"
51 changes: 37 additions & 14 deletions subprocess/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dunce;
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
use notify::{DebouncedEvent, PollWatcher, RecommendedWatcher, RecursiveMode, Watcher};
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fs;
Expand All @@ -8,12 +8,21 @@ use std::path::{Path, PathBuf};
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
use std::time::Duration;
use structopt::StructOpt;

type RequestId = usize;
type WatchId = usize;

struct Supervisor {
watcher: RecommendedWatcher,
#[derive(StructOpt, Debug)]
#[structopt(name = "subprocess")]
struct Opt {
/// Enable polling mode with the specified interval in milliseconds
#[structopt(long = "poll-interval")]
poll_interval: Option<u64>,
}

struct Supervisor<W> {
watcher: W,
watches: Arc<Mutex<Vec<Watch>>>,
}

Expand Down Expand Up @@ -88,11 +97,11 @@ enum Event {
},
}

impl Supervisor {
fn new() -> Result<Self, notify::Error> {
impl<W: Watcher> Supervisor<W> {
fn new(delay: Duration) -> Result<Self, notify::Error> {
let (tx, rx) = mpsc::channel();

let watcher = notify::watcher(tx, Duration::from_millis(300))?;
let watcher = W::new(tx, delay)?;
let watches = Arc::new(Mutex::new(Vec::new()));

let watches_2 = watches.clone();
Expand Down Expand Up @@ -348,13 +357,27 @@ fn emit_json(message: Outgoing) {
}

fn main() {
match Supervisor::new() {
Ok(mut supervisor) => supervisor.handle_requests(),
Err(error) => {
emit_json(Outgoing::WatcherError {
description: String::from(error.description()),
});
eprintln!("Error creating notify watcher: {:?}", error);
let opt = Opt::from_args();

if let Some(poll_interval) = opt.poll_interval {
match Supervisor::<PollWatcher>::new(Duration::from_millis(poll_interval)) {
Ok(mut supervisor) => supervisor.handle_requests(),
Err(error) => {
emit_json(Outgoing::WatcherError {
description: String::from(error.description()),
});
eprintln!("Error creating notify watcher: {:?}", error);
}
}
}
} else {
match Supervisor::<RecommendedWatcher>::new(Duration::from_millis(100)) {
Ok(mut supervisor) => supervisor.handle_requests(),
Err(error) => {
emit_json(Outgoing::WatcherError {
description: String::from(error.description()),
});
eprintln!("Error creating notify watcher: {:?}", error);
}
}
};
}
Loading

0 comments on commit 8b9240a

Please sign in to comment.