From 8258a02c82278fe199e02b807bf3cb2280f63b52 Mon Sep 17 00:00:00 2001 From: Thomas Sawyer Date: Thu, 26 Nov 2026 01:29:56 -0500 Subject: [PATCH] Take the baseline when the reloader is made The first call to update only recorded the files' times, so a change made between making a reloader and its first update was missed. Co-Authored-By: Claude Fable 5.2 --- HISTORY.md | 3 ++- lib/autoreload/reloader.rb | 6 ++++-- test/case_autoreload.rb | 11 +++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index f5ffb30..5ae002f 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -7,7 +7,8 @@ A maintenance release, for Ruby 3.1 and up. Changes: * `Reloader#update` is public, to check for changes at once without - the thread. + the thread. A reloader notes its files when it is made, so the first + `update` already reloads what has changed since. * The interval can be a fraction of a second. Before, it was rounded down, and an interval under one second made the thread spin. * The list of watched files in verbose mode goes to standard error, diff --git a/lib/autoreload/reloader.rb b/lib/autoreload/reloader.rb index d702832..11edd75 100644 --- a/lib/autoreload/reloader.rb +++ b/lib/autoreload/reloader.rb @@ -44,11 +44,12 @@ def initialize(options={}, &block) else @files = [] end + + update # note the files as they are now end # Public: Start the reload thread. def start - update # prime the path loads @thread = Thread.new do loop do begin @@ -105,7 +106,8 @@ def libraries end end - # Check every watched file and reload those that have changed. + # Check every watched file and reload those that have changed since + # the reloader was made, or since they were last reloaded. def update libraries.each{ |lib| check(lib) } end diff --git a/test/case_autoreload.rb b/test/case_autoreload.rb index 9a15153..63fa7e3 100644 --- a/test/case_autoreload.rb +++ b/test/case_autoreload.rb @@ -21,6 +21,17 @@ end end + test "a change made before the first update is seen" do + library_dir do + write_file('ar_first.rb', 'def ar_first; 1; end') + reloader = AutoReload::Reloader.new{ require 'ar_first' } + + write_file('ar_first.rb', 'def ar_first; 2; end', 10) + reloader.update + ar_first.assert == 2 + end + end + test "a file that has not changed is not loaded again" do library_dir do write_file('ar_same.rb', '$ar_same = ($ar_same || 0) + 1')