Add a bit of laziness
This commit is contained in:
parent
f73e89370d
commit
445443cffc
|
@ -1,4 +1,5 @@
|
|||
require 'middleman-core/contracts'
|
||||
require 'backports/2.0.0/enumerable/lazy'
|
||||
|
||||
module Middleman
|
||||
# The standard "record" that contains information about a file on disk.
|
||||
|
@ -168,6 +169,7 @@ module Middleman
|
|||
Contract Symbol, String, Maybe[Bool] => Maybe[SourceFile]
|
||||
def find(type, path, glob=false)
|
||||
watchers
|
||||
.lazy
|
||||
.select { |d| d.type == type }
|
||||
.map { |d| d.find(path, glob) }
|
||||
.reject { |d| d.nil? }
|
||||
|
@ -182,6 +184,7 @@ module Middleman
|
|||
Contract Symbol, String => Bool
|
||||
def exists?(type, path)
|
||||
watchers
|
||||
.lazy
|
||||
.select { |d| d.type == type }
|
||||
.any? { |d| d.exists?(path) }
|
||||
end
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# Watcher Library
|
||||
require 'listen'
|
||||
|
||||
require 'middleman-core/contracts'
|
||||
require 'backports/2.0.0/enumerable/lazy'
|
||||
|
||||
module Middleman
|
||||
# The default source watcher implementation. Watches a directory on disk
|
||||
|
@ -211,25 +211,31 @@ module Middleman
|
|||
Contract ArrayOf[Pathname], ArrayOf[Pathname] => Any
|
||||
def update(updated_paths, removed_paths)
|
||||
valid_updates = updated_paths
|
||||
.lazy
|
||||
.map(&method(:path_to_source_file))
|
||||
.select(&method(:valid?))
|
||||
|
||||
valid_updates.each do |f|
|
||||
.to_a
|
||||
.each do |f|
|
||||
add_file_to_cache(f)
|
||||
logger.debug "== Change (#{f[:type]}): #{f[:relative_path]}"
|
||||
end
|
||||
|
||||
valid_removes = removed_paths
|
||||
.lazy
|
||||
.select(&@files.method(:key?))
|
||||
.map(&@files.method(:[]))
|
||||
.select(&method(:valid?))
|
||||
|
||||
valid_removes.each do |f|
|
||||
.to_a
|
||||
.each do |f|
|
||||
remove_file_from_cache(f)
|
||||
logger.debug "== Deletion (#{f[:type]}): #{f[:relative_path]}"
|
||||
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
|
||||
|
||||
def add_file_to_cache(f)
|
||||
|
|
|
@ -20,6 +20,7 @@ Gem::Specification.new do |s|
|
|||
|
||||
# Core
|
||||
s.add_dependency('bundler', ['~> 1.1'])
|
||||
s.add_dependency('backports', ['~> 3.6'])
|
||||
s.add_dependency('rack', ['>= 1.4.5', '< 2.0'])
|
||||
s.add_dependency('tilt', ['~> 1.4.1'])
|
||||
s.add_dependency('erubis')
|
||||
|
|
Loading…
Reference in a new issue