Add a bit of laziness

This commit is contained in:
Thomas Reynolds 2014-10-15 14:25:06 -05:00
parent f73e89370d
commit 445443cffc
3 changed files with 23 additions and 13 deletions

View file

@ -1,4 +1,5 @@
require 'middleman-core/contracts' require 'middleman-core/contracts'
require 'backports/2.0.0/enumerable/lazy'
module Middleman module Middleman
# The standard "record" that contains information about a file on disk. # The standard "record" that contains information about a file on disk.
@ -168,6 +169,7 @@ module Middleman
Contract Symbol, String, Maybe[Bool] => Maybe[SourceFile] Contract Symbol, String, Maybe[Bool] => Maybe[SourceFile]
def find(type, path, glob=false) def find(type, path, glob=false)
watchers watchers
.lazy
.select { |d| d.type == type } .select { |d| d.type == type }
.map { |d| d.find(path, glob) } .map { |d| d.find(path, glob) }
.reject { |d| d.nil? } .reject { |d| d.nil? }
@ -182,6 +184,7 @@ module Middleman
Contract Symbol, String => Bool Contract Symbol, String => Bool
def exists?(type, path) def exists?(type, path)
watchers watchers
.lazy
.select { |d| d.type == type } .select { |d| d.type == type }
.any? { |d| d.exists?(path) } .any? { |d| d.exists?(path) }
end end

View file

@ -1,7 +1,7 @@
# Watcher Library # Watcher Library
require 'listen' require 'listen'
require 'middleman-core/contracts' require 'middleman-core/contracts'
require 'backports/2.0.0/enumerable/lazy'
module Middleman module Middleman
# The default source watcher implementation. Watches a directory on disk # The default source watcher implementation. Watches a directory on disk
@ -211,25 +211,31 @@ module Middleman
Contract ArrayOf[Pathname], ArrayOf[Pathname] => Any Contract ArrayOf[Pathname], ArrayOf[Pathname] => Any
def update(updated_paths, removed_paths) def update(updated_paths, removed_paths)
valid_updates = updated_paths valid_updates = updated_paths
.lazy
.map(&method(:path_to_source_file)) .map(&method(:path_to_source_file))
.select(&method(:valid?)) .select(&method(:valid?))
.to_a
valid_updates.each do |f| .each do |f|
add_file_to_cache(f) add_file_to_cache(f)
logger.debug "== Change (#{f[:type]}): #{f[:relative_path]}" logger.debug "== Change (#{f[:type]}): #{f[:relative_path]}"
end end
valid_removes = removed_paths valid_removes = removed_paths
.lazy
.select(&@files.method(:key?)) .select(&@files.method(:key?))
.map(&@files.method(:[])) .map(&@files.method(:[]))
.select(&method(:valid?)) .select(&method(:valid?))
.to_a
valid_removes.each do |f| .each do |f|
remove_file_from_cache(f) remove_file_from_cache(f)
logger.debug "== Deletion (#{f[:type]}): #{f[:relative_path]}" logger.debug "== Deletion (#{f[:type]}): #{f[:relative_path]}"
end end
run_callbacks(@on_change_callbacks, valid_updates, valid_removes) unless valid_updates.empty? && valid_removes.empty? run_callbacks(
@on_change_callbacks,
valid_updates,
valid_removes
) unless valid_updates.empty? && valid_removes.empty?
end end
def add_file_to_cache(f) def add_file_to_cache(f)

View file

@ -20,6 +20,7 @@ Gem::Specification.new do |s|
# Core # Core
s.add_dependency('bundler', ['~> 1.1']) s.add_dependency('bundler', ['~> 1.1'])
s.add_dependency('backports', ['~> 3.6'])
s.add_dependency('rack', ['>= 1.4.5', '< 2.0']) s.add_dependency('rack', ['>= 1.4.5', '< 2.0'])
s.add_dependency('tilt', ['~> 1.4.1']) s.add_dependency('tilt', ['~> 1.4.1'])
s.add_dependency('erubis') s.add_dependency('erubis')