Skip to content

Commit

Permalink
fix imports and create pollwatcher on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
jackTabsCode committed Nov 21, 2023
1 parent 11018cd commit 187fa29
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions crates/memofs/src/std_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ use std::io;
use std::path::Path;

use crossbeam_channel::Receiver;

use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};

#[cfg(target_os = "macos")]
use notify::{Config, PollWatcher};

#[cfg(target_os = "macos")]
use std::time::Duration;

use crate::{DirEntry, Metadata, ReadDir, VfsBackend, VfsEvent};

/// `VfsBackend` that uses `std::fs` and the `notify` crate.
pub struct StdBackend {
#[cfg(target_os = "macos")]
watcher: PollWatcher,

#[cfg(not(target_os = "macos"))]
watcher: RecommendedWatcher,

Expand All @@ -20,7 +28,7 @@ impl StdBackend {
pub fn new() -> StdBackend {
let (tx, rx) = crossbeam_channel::unbounded();

let watcher = notify::recommended_watcher(move |res: Result<Event, _>| match res {
let event_handler = move |res: Result<Event, _>| match res {
Ok(event) => match event.kind {
EventKind::Create(_) => {
for path in event.paths {
Expand All @@ -40,7 +48,16 @@ impl StdBackend {
_ => {}
},
Err(e) => println!("watch error: {:?}", e),
})
};

#[cfg(not(target_os = "macos"))]
let watcher = notify::recommended_watcher(event_handler).unwrap();

#[cfg(target_os = "macos")]
let watcher = PollWatcher::new(
event_handler,
Config::default().with_poll_interval(Duration::from_millis(200)),
)
.unwrap();

Self {
Expand Down

0 comments on commit 187fa29

Please sign in to comment.