Class: Hoodie::Timers::Group

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/hoodie/timers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGroup

Returns a new instance of Group.



161
162
163
164
165
166
167
# File 'lib/hoodie/timers.rb', line 161

def initialize
  @events = Events.new
  @timers = Set.new
  @paused_timers = Set.new
  @interval = Hitimes::Interval.new
  @interval.start
end

Instance Attribute Details

#eventsObject (readonly)

Scheduled events:



170
171
172
# File 'lib/hoodie/timers.rb', line 170

def events
  @events
end

#paused_timersObject (readonly)

Paused timers:



176
177
178
# File 'lib/hoodie/timers.rb', line 176

def paused_timers
  @paused_timers
end

#timersObject (readonly)

Active timers:



173
174
175
# File 'lib/hoodie/timers.rb', line 173

def timers
  @timers
end

Instance Method Details

#after(interval, &block) ⇒ Object

Call the given block after the given interval. The first argument will be the time at which the group was asked to fire timers for.



180
181
182
# File 'lib/hoodie/timers.rb', line 180

def after(interval, &block)
  Timer.new(self, interval, false, &block)
end

#cancelObject

Cancel all timers.



244
245
246
# File 'lib/hoodie/timers.rb', line 244

def cancel
  @timers.dup.each(&:cancel)
end

#current_offsetObject

The group’s current time.



249
250
251
# File 'lib/hoodie/timers.rb', line 249

def current_offset
  @interval.to_f
end

#delay(seconds) ⇒ Object

Delay all timers.



237
238
239
240
241
# File 'lib/hoodie/timers.rb', line 237

def delay(seconds)
  @timers.each do |timer|
    timer.delay(seconds)
  end
end

#every(interval, recur = true, &block) ⇒ Object

Call the given block periodically at the given interval. The first argument will be the time at which the group was asked to fire timers for.



186
187
188
# File 'lib/hoodie/timers.rb', line 186

def every(interval, recur = true, &block)
  Timer.new(self, interval, recur, &block)
end

#fire(offset = current_offset) ⇒ Object

Fire all timers that are ready.



221
222
223
# File 'lib/hoodie/timers.rb', line 221

def fire(offset = current_offset)
  @events.fire(offset)
end

#pauseObject

Pause all timers.



226
227
228
# File 'lib/hoodie/timers.rb', line 226

def pause
  @timers.dup.each(&:pause)
end

#resumeObject Also known as: continue

Resume all timers.



231
232
233
# File 'lib/hoodie/timers.rb', line 231

def resume
  @paused_timers.dup.each(&:resume)
end

#wait(&_block) ⇒ Object

Wait for the next timer and fire it. Can take a block, which should behave like sleep(n), except that n may be nil (sleep forever) or a negative number (fire immediately after return).



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/hoodie/timers.rb', line 193

def wait(&_block)
  if block_given?
    yield wait_interval

    while interval = wait_interval and interval > 0
      yield interval
    end
  else
    while interval = wait_interval and interval > 0
      # We cannot assume that sleep will wait for the specified time, it might be +/- a bit.
      sleep interval
    end
  end
  fire
end

#wait_interval(offset = current_offset) ⇒ Object

Interval to wait until when the next timer will fire.

  • nil: no timers

  • -ve: timers expired already

  • 0: timers ready to fire

  • +ve: timers waiting to fire



214
215
216
217
218
# File 'lib/hoodie/timers.rb', line 214

def wait_interval(offset = current_offset)
  if handle = @events.first
    return handle.time - Float(offset)
  end
end