Class: Wake::Script::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/wake/script.rb

Instance Method Summary collapse

Constructor Details

#initialize(rule) ⇒ Batch

Returns a new instance of Batch.



23
24
25
26
27
# File 'lib/wake/script.rb', line 23

def initialize rule
  @timer = nil
  @rule = rule
  @events = []
end

Instance Method Details

#call(data, event, path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wake/script.rb', line 29

def call data, event, path
  # $stderr.print "batch add #{data} #{event} #{path}\n"
  if @timer
    @timer.cancel
  end
  @timer = EM::Timer.new(0.001) do
    deliver
    Wake.batches.delete self
  end
  Wake.batches[self] = self
  # p data, event, path
  @events << [ data.to_a, event, path ]
  @events.uniq!
  # p @events
end

#deliverObject



45
46
47
48
49
50
51
52
53
# File 'lib/wake/script.rb', line 45

def deliver
  events = @events
  @timer = nil
  @events = []
  @rule.action.call [events]
  events.each do |event|
    Script.learn event[2]
  end
end