Class: Sysdo::EventSource::PolledCollectionChange

Inherits:
Sysdo::EventSource show all
Defined in:
lib/sysdo/event_source/polled_collection_change.rb

Instance Attribute Summary

Attributes inherited from Sysdo::EventSource

#on_fire

Instance Method Summary collapse

Methods inherited from Sysdo::EventSource

#fire, #listen

Constructor Details

#initialize(change, poll_rate = 1, &collection) ⇒ PolledCollectionChange

Returns a new instance of PolledCollectionChange.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sysdo/event_source/polled_collection_change.rb', line 4

def initialize(change, poll_rate = 1, &collection)
  raise "change parameter must be :add or :remove" \
    unless change == :add || change == :remove
  
  super() do |s|
    last = collection.()

    loop do
      now = collection.()
      if last != now
        if change == :add
          relevant_items = now - last
        elsif change == :remove
          relevant_items = last - now
        end

        relevant_items.each do |new_item|
          s.fire(new_item)
        end unless relevant_items.empty?
      end
      last = now

      sleep poll_rate
    end
  end
end