Class: ModSpox::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/mod_spox/Timer.rb

Instance Method Summary collapse

Constructor Details

#initialize(timer, pipeline) ⇒ Timer

timer

ActionTimer to use

pipeline

Message pipeline

Creates a timer handler for interactions with the ActionTimer



12
13
14
15
16
17
18
19
20
# File 'lib/mod_spox/Timer.rb', line 12

def initialize(timer, pipeline)
    @pipeline = pipeline
    @timer = timer
    {:Internal_TimerAdd => :add_message,
     :Internal_TimerRemove => :remove_message,
     :Internal_TimerClear => :clear}.each_pair do |type,method|
        @pipeline.hook(self, method, type)
    end
end

Instance Method Details

#add_message(message) ⇒ Object

message

TimerAdd message

Add a recurring code block



24
25
26
27
28
29
30
31
32
33
# File 'lib/mod_spox/Timer.rb', line 24

def add_message(message)
    Logger.info("New block is being added to the timer")
    begin
        action = @timer.add(message.period, message.once, message.data, message.requester, &message.block)
        @pipeline << Messages::Internal::TimerResponse.new(message.requester, action, true, message.id)
    rescue Object => boom
        Logger.error("Failed to add new block to timer: #{boom}")
        @pipeline << Messages::Internal::TimerResponse.new(message.requester, nil, false, message.id)
    end
end

#clear(message = nil) ⇒ Object

Clears all actions in the timer’s queue



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

def clear(message=nil)
    if(message.nil? || message.plugin.nil?)
        @timer.clear
    else
        @timer.clear(message.plugin)
    end
end

#remove_message(message) ⇒ Object

message

TimerRemove message

Remove an action from the timer



37
38
39
40
41
# File 'lib/mod_spox/Timer.rb', line 37

def remove_message(message)
    @timer.remove(message.action)
    Logger.info("Action has been removed from the Timer")
    @pipeline << Messages::Internal::TimerResponse.new(nil, message.action, false, message.id)
end

#stopObject

stop the timer



53
54
55
# File 'lib/mod_spox/Timer.rb', line 53

def stop
    @timer.stop
end