Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Sleep callback for all_events

  • Loading branch information...
commit 392796b6b80b7699498ecbd876b00a3490a293df 1 parent e063dd2
@consiliens consiliens authored
Showing with 44 additions and 13 deletions.
  1. +9 −1 lib/fssm/path.rb
  2. +35 −12 lib/fssm/state.rb
View
10 lib/fssm/path.rb
@@ -1,7 +1,9 @@
class FSSM::Path
- def initialize(path=nil, glob=nil, &block)
+ attr_reader :collect
+ def initialize(path=nil, glob=nil, collect=false, &block)
set_path(path || '.')
set_glob(glob || '**/*')
+ @collect = collect
init_callbacks
if block_given?
@@ -38,6 +40,12 @@ def delete(callback_or_path=nil, &block)
callback_action(:delete, (block_given? ? block : callback_or_path))
end
+ def sleep(all_events=nil, &block)
+ @callbacks[:sleep] = block if block.is_a?(Proc)
+ # avoid returning nil when skip_callbacks
+ @callbacks[:sleep].call(all_events) unless all_events == nil
+ end
+
private
def init_callbacks
View
47 lib/fssm/state.rb
@@ -3,32 +3,55 @@ class FSSM::State
def initialize(path)
@path = path
@cache = FSSM::Tree::Cache.new
+ @created = []
+ @modified = []
+ @deleted = []
end
-
+
def refresh(base=nil, skip_callbacks=false)
previous, current = recache(base || @path.to_pathname)
+ refresh_keys(previous, current)
- unless skip_callbacks
- deleted(previous, current)
- created(previous, current)
- modified(previous, current)
+ if @path.collect
+ @path.sleep(all_events) unless skip_callbacks
+ else
+ unless skip_callbacks
+ created(previous, current)
+ modified(previous, current)
+ deleted(previous, current)
+ end
end
end
private
- def created(previous, current)
- (current.keys - previous.keys).each {|created| @path.create(created)}
+ def refresh_keys(previous, current)
+ @created = current.keys - previous.keys
+ @modified.clear
+ (current.keys & previous.keys).each do |file|
+ @modified.push(file) if (current[file] <=> previous[file]) != 0
+ end
+ @deleted = previous.keys - current.keys
end
- def deleted(previous, current)
- (previous.keys - current.keys).each {|deleted| @path.delete(deleted)}
+ def all_events
+ {
+ :created => @created,
+ :modified => @modified,
+ :deleted => @deleted
+ }
+ end
+
+ def created(previous, current)
+ @created.each {|created| @path.create(created)}
end
def modified(previous, current)
- (current.keys & previous.keys).each do |file|
- @path.update(file) if (current[file] <=> previous[file]) != 0
- end
+ @modified.each { |modified| @path.update(modified) }
+ end
+
+ def deleted(previous, current)
+ @deleted.each {|deleted| @path.delete(deleted)}
end
def recache(base)
Please sign in to comment.
Something went wrong with that request. Please try again.