Skip to content

Commit b36b910

Browse files
committed
Make reload debounce delay configurable via CLI flag
1 parent 29dc716 commit b36b910

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

src/main.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ async fn inner_main() -> Result<(), WrappedErr> {
9292
optional -m,--max-wide max_wide: NonZeroUsize
9393
/// Fullscreen the pdf (hide document name, page count, etc)
9494
optional -f,--fullscreen
95+
/// The time to wait for the file to stop changing before reloading, in milliseconds.
96+
/// Defaults to the minimum value of 50ms.
97+
optional --reload-delay reload_delay: u64
9598
/// The number of pages to prerender surrounding the currently-shown page; 0 means no
9699
/// limit. By default, there is no limit.
97100
optional -p,--prerender prerender: usize
@@ -163,7 +166,11 @@ async fn inner_main() -> Result<(), WrappedErr> {
163166
watch_to_render_tx,
164167
path.file_name()
165168
.ok_or(WrappedErr("Path does not have a last component??".into()))?
166-
.to_owned()
169+
.to_owned(),
170+
flags
171+
.reload_delay
172+
.map(Duration::from_millis)
173+
.unwrap_or_default()
167174
))
168175
.map_err(|e| WrappedErr(format!("Couldn't start watching the provided file: {e}").into()))?;
169176

@@ -462,18 +469,19 @@ async fn enter_redraw_loop(
462469
}
463470
}
464471

465-
// Only reload once the file has not changed for 1 second
466-
const DEBOUNCE_DELAY: Duration = Duration::from_millis(1000);
472+
const MIN_DEBOUNCE_DELAY: Duration = Duration::from_millis(50);
467473

468474
fn on_notify_ev(
469475
to_tui_tx: flume::Sender<Result<RenderInfo, RenderError>>,
470476
to_render_tx: flume::Sender<RenderNotif>,
471-
file_name: OsString
477+
file_name: OsString,
478+
debounce_delay: Duration
472479
) -> impl Fn(notify::Result<Event>) {
480+
let debounce_delay = debounce_delay.max(MIN_DEBOUNCE_DELAY);
473481
let last_event: Mutex<Result<(), RenderError>> = Mutex::new(Ok(()));
474482
let last_event = Arc::new(last_event);
475483

476-
let debouncer = EventDebouncer::new(DEBOUNCE_DELAY, {
484+
let debouncer = EventDebouncer::new(debounce_delay, {
477485
let last_event = last_event.clone();
478486
move |()| {
479487
let event = mem::replace(&mut *last_event.lock().unwrap(), Ok(()));

0 commit comments

Comments
 (0)