class Dizby::TimedCollection

Public Class Methods

new(timeout, step = timeout) click to toggle source
# File lib/dizby/utility/timed_collection.rb, line 14
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

Public Instance Methods

add(id) click to toggle source
# File lib/dizby/utility/timed_collection.rb, line 27
def add(id)
  @states.synchronize { @states[id] = TimedState.new(@timeout) }
end
revive(id) click to toggle source
# File lib/dizby/utility/timed_collection.rb, line 21
def revive(id)
  @states.synchronize { @states.fetch(id).revive }
rescue KeyError
  raise InvalidIdentifier, 'identifier timed out or did not exist'
end

Private Instance Methods

update() click to toggle source
# File lib/dizby/utility/timed_collection.rb, line 33
def update
  loop do
    sleep(@step)

    @states.synchronize do
      @states.each_value(&:update)
      @states.keep_if { |_, state| state.alive? }
    end
  end
end