Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions lib/autoreload/reloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/case_autoreload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading