Class: Spade::Runtime::Reactor::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/spade/runtime/reactor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reactor) ⇒ Timer

Returns a new instance of Timer.



124
125
126
127
128
129
130
# File 'lib/spade/runtime/reactor.rb', line 124

def initialize(reactor)
  @interval = 0
  @periodic = false
  @callback = nil
  @reactor = reactor
    @running = false
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



121
122
123
# File 'lib/spade/runtime/reactor.rb', line 121

def callback
  @callback
end

#intervalObject

Returns the value of attribute interval.



121
122
123
# File 'lib/spade/runtime/reactor.rb', line 121

def interval
  @interval
end

#periodicObject

Returns the value of attribute periodic.



121
122
123
# File 'lib/spade/runtime/reactor.rb', line 121

def periodic
  @periodic
end

#runningObject (readonly)

Returns the value of attribute running.



122
123
124
# File 'lib/spade/runtime/reactor.rb', line 122

def running
  @running
end

Instance Method Details

#cancelObject



147
148
149
150
151
# File 'lib/spade/runtime/reactor.rb', line 147

def cancel
  EventMachine.cancel_timer(@timer) if @timer
  @timer = nil
  @reactor.remove_timer(self)
end

#startObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/spade/runtime/reactor.rb', line 132

def start
  @running = true
  if @periodic
    @timer = EventMachine.add_periodic_timer(@interval.to_f / 1000) do
      @callback.call
    end
  else
    @timer = EventMachine.add_timer(@interval.to_f / 1000) do
      @callback.call
      @timer = nil
      @reactor.remove_timer(self)
    end
  end
end