Class: EaseEngine::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/ease_engine/timer.rb

Defined Under Namespace

Classes: Info, Record

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Timer

Returns a new instance of Timer.



25
26
27
28
29
30
31
32
33
# File 'lib/ease_engine/timer.rb', line 25

def initialize( options = {} )
  options[ :update_time_usec ] = 100000 if ! options.key?( :update_time_usec )
  
  @update_time_usec = options[ :update_time_usec ]
  @check_time_usec = 0
  
  @timer = Record.new( 1, 0xFFFFFFFF )
  @group = EaseEngine::Data::Group.new
end

Instance Attribute Details

#update_time_usecObject

Returns the value of attribute update_time_usec.



23
24
25
# File 'lib/ease_engine/timer.rb', line 23

def update_time_usec
  @update_time_usec
end

Instance Method Details

#add(group_id, timeout_usec, arg, callback) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/ease_engine/timer.rb', line 35

def add( group_id, timeout_usec, arg, callback )
  timer_id = @timer.add( Info.new( EaseEngine::Time.new.to_unix_epoch_time_usec + timeout_usec, arg, callback ) )
  return 0 if 0 == timer_id
  
  @group.add( group_id, timer_id ) if 0 < group_id
  
  timer_id
end

#remove(group_id, timer_id = 0) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/ease_engine/timer.rb', line 44

def remove( group_id, timer_id = 0 )
  @group.group( group_id ).each{|timer_id, group_id|
    @timer.remove( timer_id )
  }
  @group.remove( group_id, timer_id )
  @timer.remove( timer_id )
end

#updateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ease_engine/timer.rb', line 52

def update
  return if 0 == @timer.size
  
  usec = EaseEngine::Time.new.to_unix_epoch_time_usec
  return if usec < @check_time_usec
  
  @timer.each{|id, index, info|
    if info.end_time_usec < usec
      remove( 0, id )
      
      info.callback.call( info.arg )
    else
      # 終了時間の早い順に昇順ソートしているので、後のデータをチェックする必要なし
      break
    end
  }
  
  @check_time_usec = usec + @update_time_usec
end