Class: Ruck::EventClock

Inherits:
Object
  • Object
show all
Defined in:
lib/ruck/event_clock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEventClock

Returns a new instance of EventClock.



6
7
8
9
10
# File 'lib/ruck/event_clock.rb', line 6

def initialize
  @now = 0
  @waiting = Hash.new { |hash, event| hash[event] = [] }
  @raised = []
end

Instance Attribute Details

#nowObject (readonly)

Returns the value of attribute now.



4
5
6
# File 'lib/ruck/event_clock.rb', line 4

def now
  @now
end

Instance Method Details

#fast_forward(dt) ⇒ Object

fast-forward this clock by the given time delta



13
14
15
# File 'lib/ruck/event_clock.rb', line 13

def fast_forward(dt)
  @now += dt
end

#nextObject



26
27
28
# File 'lib/ruck/event_clock.rb', line 26

def next
  [@raised.first, 0] if @raised.length > 0
end

#raise_all(event) ⇒ Object



34
35
36
37
# File 'lib/ruck/event_clock.rb', line 34

def raise_all(event)
  @raised += @waiting[event]
  @waiting[event].clear
end

#schedule(obj, event = nil) ⇒ Object



17
18
19
# File 'lib/ruck/event_clock.rb', line 17

def schedule(obj, event = nil)
  @waiting[event] << obj
end

#unschedule(obj) ⇒ Object



21
22
23
24
# File 'lib/ruck/event_clock.rb', line 21

def unschedule(obj)
  @waiting.each { |event, objs| objs.delete(obj) }
  @raised.delete(obj)
end

#unschedule_nextObject



30
31
32
# File 'lib/ruck/event_clock.rb', line 30

def unschedule_next
  [@raised.shift, 0] if @raised.length > 0
end