Differentiate polling from find_new_files to remove double reads in build mode

This commit is contained in:
Thomas Reynolds 2016-01-20 12:20:27 -08:00
parent ffa662a917
commit 6d1c3562a7
4 changed files with 28 additions and 6 deletions

View file

@ -47,7 +47,7 @@ module Middleman
# @return [void] # @return [void]
Contract Any Contract Any
def before_configuration def before_configuration
@sources.find_new_files! @sources.poll_once!
end end
# After we config, find new files since config can change paths. # After we config, find new files since config can change paths.
@ -60,7 +60,7 @@ module Middleman
end end
@sources.start! @sources.start!
@sources.find_new_files! @sources.poll_once!
end end
protected protected

View file

@ -91,7 +91,7 @@ module Middleman
validator: (block_given? ? block : regex)) validator: (block_given? ? block : regex))
bump_count bump_count
find_new_files! if @running poll_once! if @running
end end
# Whether this path is ignored. # Whether this path is ignored.
@ -219,13 +219,24 @@ module Middleman
.find { |d| d.exists?(path) } .find { |d| d.exists?(path) }
end end
# Manually poll all watchers for new content. # Manually check for new files
# #
# @return [void] # @return [void]
Contract ArrayOf[Pathname] Contract ArrayOf[Pathname]
def find_new_files! def find_new_files!
return [] unless @update_count != @last_update_count return [] unless @update_count != @last_update_count
@last_update_count = @update_count
watchers.reduce([]) { |sum, w| sum + w.find_new_files! }
end
# Manually poll all watchers for new content.
#
# @return [void]
Contract ArrayOf[Pathname]
def poll_once!
return [] unless @update_count != @last_update_count
@last_update_count = @update_count @last_update_count = @update_count
watchers.reduce([]) { |sum, w| sum + w.poll_once! } watchers.reduce([]) { |sum, w| sum + w.poll_once! }
end end

View file

@ -1,6 +1,7 @@
# Watcher Library # Watcher Library
require 'listen' require 'listen'
require 'middleman-core/contracts' require 'middleman-core/contracts'
require 'digest'
# Monkey patch Listen silencer so `only` works on directories too # Monkey patch Listen silencer so `only` works on directories too
module Listen module Listen
@ -177,6 +178,16 @@ module Middleman
@listener = nil @listener = nil
end end
Contract ArrayOf[Pathname]
def find_new_files!
new_files = ::Middleman::Util.all_files_under(@directory.to_s)
.reject { |p| @files.key?(p) }
update(new_files, [])
new_files
end
# Manually trigger update events. # Manually trigger update events.
# #
# @return [void] # @return [void]

View file

@ -31,13 +31,13 @@ end
Then /^the file "([^\"]*)" has the contents$/ do |path, contents| Then /^the file "([^\"]*)" has the contents$/ do |path, contents|
write_file(path, contents) write_file(path, contents)
@server_inst.files.find_new_files! @server_inst.files.poll_once!
end end
Then /^the file "([^\"]*)" is removed$/ do |path| Then /^the file "([^\"]*)" is removed$/ do |path|
step %Q{I remove the file "#{path}"} step %Q{I remove the file "#{path}"}
@server_inst.files.find_new_files! @server_inst.files.poll_once!
end end
Given /^a modification time for a file named "([^\"]*)"$/ do |file| Given /^a modification time for a file named "([^\"]*)"$/ do |file|