Class: Dizby::TimedCollection
- Inherits:
-
Object
- Object
- Dizby::TimedCollection
- Defined in:
- lib/dizby/utility/timed_collection.rb
Instance Method Summary collapse
- #add(id)
-
#initialize(timeout, step = timeout) ⇒ TimedCollection
constructor
A new instance of TimedCollection.
- #revive(id)
- #update private
Constructor Details
#initialize(timeout, step = timeout) ⇒ TimedCollection
Returns a new instance of TimedCollection.
15 16 17 18 19 20 |
# File 'lib/dizby/utility/timed_collection.rb', line 15 def initialize(timeout, step = timeout) @timeout = timeout @step = [timeout, step].min # don't allow a larger step than timeout @states = Dizby.monitor({}) @updater = Thread.start { update } end |
Instance Method Details
#add(id)
28 29 30 |
# File 'lib/dizby/utility/timed_collection.rb', line 28 def add(id) @states.synchronize { @states[id] = TimedState.new(@timeout) } end |
#revive(id)
22 23 24 25 26 |
# File 'lib/dizby/utility/timed_collection.rb', line 22 def revive(id) @states.synchronize { @states.fetch(id).revive } rescue KeyError raise InvalidIdentifier, 'identifier timed out or did not exist' end |
#update (private)
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/dizby/utility/timed_collection.rb', line 34 def update loop do sleep(@step) @states.synchronize do @states.each_value(&:update) @states.keep_if { |_, state| state.alive? } end end end |