Skip to content

Commit 392796b

Browse files
committed
Sleep callback for all_events
1 parent e063dd2 commit 392796b

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

lib/fssm/path.rb

Lines changed: 9 additions & 1 deletion
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -1,7 +1,9 @@
1
class FSSM::Path
1
class FSSM::Path
2-
def initialize(path=nil, glob=nil, &block)
2+
attr_reader :collect
3+
def initialize(path=nil, glob=nil, collect=false, &block)
3
set_path(path || '.')
4
set_path(path || '.')
4
set_glob(glob || '**/*')
5
set_glob(glob || '**/*')
6+
@collect = collect
5
init_callbacks
7
init_callbacks
6

8

7
if block_given?
9
if block_given?
@@ -38,6 +40,12 @@ def delete(callback_or_path=nil, &block)
38
callback_action(:delete, (block_given? ? block : callback_or_path))
40
callback_action(:delete, (block_given? ? block : callback_or_path))
39
end
41
end
40

42

43+
def sleep(all_events=nil, &block)
44+
@callbacks[:sleep] = block if block.is_a?(Proc)
45+
# avoid returning nil when skip_callbacks
46+
@callbacks[:sleep].call(all_events) unless all_events == nil
47+
end
48+
41
private
49
private
42

50

43
def init_callbacks
51
def init_callbacks

lib/fssm/state.rb

Lines changed: 35 additions & 12 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -3,32 +3,55 @@ class FSSM::State
3
def initialize(path)
3
def initialize(path)
4
@path = path
4
@path = path
5
@cache = FSSM::Tree::Cache.new
5
@cache = FSSM::Tree::Cache.new
6+
@created = []
7+
@modified = []
8+
@deleted = []
6
end
9
end
7-
10+
8
def refresh(base=nil, skip_callbacks=false)
11
def refresh(base=nil, skip_callbacks=false)
9
previous, current = recache(base || @path.to_pathname)
12
previous, current = recache(base || @path.to_pathname)
13+
refresh_keys(previous, current)
10

14

11-
unless skip_callbacks
15+
if @path.collect
12-
deleted(previous, current)
16+
@path.sleep(all_events) unless skip_callbacks
13-
created(previous, current)
17+
else
14-
modified(previous, current)
18+
unless skip_callbacks
19+
created(previous, current)
20+
modified(previous, current)
21+
deleted(previous, current)
22+
end
15
end
23
end
16
end
24
end
17

25

18
private
26
private
19

27

20-
def created(previous, current)
28+
def refresh_keys(previous, current)
21-
(current.keys - previous.keys).each {|created| @path.create(created)}
29+
@created = current.keys - previous.keys
30+
@modified.clear
31+
(current.keys & previous.keys).each do |file|
32+
@modified.push(file) if (current[file] <=> previous[file]) != 0
33+
end
34+
@deleted = previous.keys - current.keys
22
end
35
end
23

36

24-
def deleted(previous, current)
37+
def all_events
25-
(previous.keys - current.keys).each {|deleted| @path.delete(deleted)}
38+
{
39+
:created => @created,
40+
:modified => @modified,
41+
:deleted => @deleted
42+
}
43+
end
44+
45+
def created(previous, current)
46+
@created.each {|created| @path.create(created)}
26
end
47
end
27

48

28
def modified(previous, current)
49
def modified(previous, current)
29-
(current.keys & previous.keys).each do |file|
50+
@modified.each { |modified| @path.update(modified) }
30-
@path.update(file) if (current[file] <=> previous[file]) != 0
51+
end
31-
end
52+
53+
def deleted(previous, current)
54+
@deleted.each {|deleted| @path.delete(deleted)}
32
end
55
end
33

56

34
def recache(base)
57
def recache(base)

0 commit comments

Comments
 (0)