Skip to content

Commit ec246ba

Browse files
committed
refactor(fs_watcher): unify the way of constructing watcher (#8571)
1 parent cb29a71 commit ec246ba

File tree

4 files changed

+57
-45
lines changed

4 files changed

+57
-45
lines changed

crates/rolldown_dev/src/dev_engine.rs

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -74,47 +74,16 @@ impl DevEngine {
7474
debounce_delay: ctx.options.debounce_duration,
7575
compare_contents_for_polling: ctx.options.compare_contents_for_polling,
7676
debounce_tick_rate: ctx.options.debounce_tick_rate,
77+
use_polling: ctx.options.use_polling,
78+
use_debounce: ctx.options.use_debounce,
7779
};
7880

7981
let event_handler = BundleCoordinator::create_watcher_event_handler(coordinator_tx.clone());
8082

81-
let watcher = {
82-
if ctx.options.disable_watcher {
83-
NoopFsWatcher::with_config(event_handler, watcher_config)?.into_dyn_fs_watcher()
84-
} else {
85-
#[cfg(not(target_family = "wasm"))]
86-
{
87-
use rolldown_fs_watcher::{
88-
DebouncedPollFsWatcher, DebouncedRecommendedFsWatcher, PollFsWatcher,
89-
RecommendedFsWatcher,
90-
};
91-
92-
match (ctx.options.use_polling, ctx.options.use_debounce) {
93-
// Polling + no debounce = PollFsWatcher
94-
(true, false) => {
95-
PollFsWatcher::with_config(event_handler, watcher_config)?.into_dyn_fs_watcher()
96-
}
97-
// Polling + debounce = DebouncedPollFsWatcher
98-
(true, true) => DebouncedPollFsWatcher::with_config(event_handler, watcher_config)?
99-
.into_dyn_fs_watcher(),
100-
// No polling + no debounce = RecommendedFsWatcher
101-
(false, false) => RecommendedFsWatcher::with_config(event_handler, watcher_config)?
102-
.into_dyn_fs_watcher(),
103-
// No polling + debounce = DebouncedRecommendedFsWatcher
104-
(false, true) => {
105-
DebouncedRecommendedFsWatcher::with_config(event_handler, watcher_config)?
106-
.into_dyn_fs_watcher()
107-
}
108-
}
109-
}
110-
#[cfg(target_family = "wasm")]
111-
{
112-
use rolldown_fs_watcher::RecommendedFsWatcher;
113-
// For WASM, always use NotifyWatcher (which is PollWatcher in WASM)
114-
// Use the FsWatcher trait implementation
115-
RecommendedFsWatcher::with_config(event_handler, watcher_config)?.into_dyn_fs_watcher()
116-
}
117-
}
83+
let watcher = if ctx.options.disable_watcher {
84+
NoopFsWatcher::with_config(event_handler, watcher_config)?.into_dyn_fs_watcher()
85+
} else {
86+
rolldown_fs_watcher::create_fs_watcher(event_handler, watcher_config)?
11887
};
11988

12089
let coordinator =

crates/rolldown_fs_watcher/src/fs_watcher_config.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ pub struct FsWatcherConfig {
2727
///
2828
/// ⚠️Only take effect for debounced watchers.
2929
pub debounce_tick_rate: Option<u64>,
30+
31+
/// Whether to use polling-based file watching instead of native OS events.
32+
/// Default: false (use native OS events).
33+
///
34+
/// ⚠️Only used by `create_fs_watcher` for watcher selection, not by concrete watcher implementations.
35+
pub use_polling: bool,
36+
37+
/// Whether to use debounced event delivery.
38+
/// Default: false.
39+
///
40+
/// ⚠️Only used by `create_fs_watcher` for watcher selection, not by concrete watcher implementations.
41+
pub use_debounce: bool,
3042
}
3143

3244
impl Default for FsWatcherConfig {
@@ -37,6 +49,8 @@ impl Default for FsWatcherConfig {
3749
poll_interval: 100,
3850
compare_contents_for_polling: false,
3951
debounce_tick_rate: None,
52+
use_polling: false,
53+
use_debounce: false,
4054
}
4155
}
4256
}

crates/rolldown_fs_watcher/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,32 @@ pub use paths_mut::PathsMut;
3939
pub use recommended_fs_watcher::RecommendedFsWatcher;
4040

4141
pub type DynFsWatcher = Box<dyn FsWatcher + Send + 'static>;
42+
43+
/// Create a filesystem watcher based on the `use_polling` and `use_debounce` fields in the config.
44+
///
45+
/// This is the canonical factory for non-noop watchers. Consumers that need a `NoopFsWatcher`
46+
/// (e.g. when watching is disabled) should construct it directly.
47+
pub fn create_fs_watcher<F: FsEventHandler>(
48+
event_handler: F,
49+
config: FsWatcherConfig,
50+
) -> rolldown_error::BuildResult<DynFsWatcher> {
51+
#[cfg(not(target_family = "wasm"))]
52+
{
53+
match (config.use_polling, config.use_debounce) {
54+
(true, false) => Ok(PollFsWatcher::with_config(event_handler, config)?.into_dyn_fs_watcher()),
55+
(true, true) => {
56+
Ok(DebouncedPollFsWatcher::with_config(event_handler, config)?.into_dyn_fs_watcher())
57+
}
58+
(false, false) => {
59+
Ok(RecommendedFsWatcher::with_config(event_handler, config)?.into_dyn_fs_watcher())
60+
}
61+
(false, true) => {
62+
Ok(DebouncedRecommendedFsWatcher::with_config(event_handler, config)?.into_dyn_fs_watcher())
63+
}
64+
}
65+
}
66+
#[cfg(target_family = "wasm")]
67+
{
68+
Ok(RecommendedFsWatcher::with_config(event_handler, config)?.into_dyn_fs_watcher())
69+
}
70+
}

crates/rolldown_watcher/src/watcher.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use futures::future::Shared;
99
use oxc_index::IndexVec;
1010
use rolldown::BundlerConfig;
1111
use rolldown_error::BuildResult;
12-
use rolldown_fs_watcher::{FsWatcher, FsWatcherConfig};
13-
#[cfg(not(target_family = "wasm"))]
14-
use rolldown_fs_watcher::{PollFsWatcher, RecommendedFsWatcher};
12+
#[cfg(target_family = "wasm")]
13+
use rolldown_fs_watcher::FsWatcher;
14+
use rolldown_fs_watcher::FsWatcherConfig;
1515
use std::future::Future;
1616
use std::pin::Pin;
1717
use tokio::sync::mpsc;
@@ -44,6 +44,9 @@ impl WatcherConfig {
4444
config.poll_interval = poll_interval;
4545
}
4646
config.compare_contents_for_polling = self.compare_contents_for_polling;
47+
config.use_polling = self.use_polling;
48+
// rolldown_watcher doesn't support debounce currently
49+
config.use_debounce = false;
4750
config
4851
}
4952
}
@@ -126,11 +129,8 @@ impl Watcher {
126129
let task_index = WatchTaskIdx::from_usize(index);
127130
let fs_handler = TaskFsEventHandler { task_index, tx: tx.clone() };
128131
#[cfg(not(target_family = "wasm"))]
129-
let fs_watcher: Box<dyn FsWatcher + Send + 'static> = if watcher_config.use_polling {
130-
Box::new(PollFsWatcher::with_config(fs_handler, fs_watcher_config.clone())?)
131-
} else {
132-
Box::new(RecommendedFsWatcher::with_config(fs_handler, fs_watcher_config.clone())?)
133-
};
132+
let fs_watcher =
133+
rolldown_fs_watcher::create_fs_watcher(fs_handler, fs_watcher_config.clone())?;
134134
#[cfg(target_family = "wasm")]
135135
let fs_watcher: Box<dyn FsWatcher + Send + 'static> = Box::new(
136136
rolldown_fs_watcher::NoopFsWatcher::with_config(fs_handler, fs_watcher_config.clone())?,

0 commit comments

Comments
 (0)