Class: InternetHakai::TimerFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/internethakai/hakairev/timer_factory.rb

Constant Summary collapse

@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout, concurrency, loop) ⇒ TimerFactory

Returns a new instance of TimerFactory.



7
8
9
10
11
12
13
14
# File 'lib/internethakai/hakairev/timer_factory.rb', line 7

def initialize timeout, concurrency, loop
    @timeout = timeout
    @concurrency = concurrency
    @loop = loop
    @stack = []
    @@instance = self
    prepare
end

Class Method Details

.instanceObject



4
5
6
# File 'lib/internethakai/hakairev/timer_factory.rb', line 4

def self::instance
    @@instance
end

Instance Method Details

#collect(timer) ⇒ Object



26
27
28
29
30
# File 'lib/internethakai/hakairev/timer_factory.rb', line 26

def collect timer
    return if timer.nil?
    timer.disable if timer.enabled?
    @stack.push(timer)
end

#createObject



20
21
22
23
24
25
# File 'lib/internethakai/hakairev/timer_factory.rb', line 20

def create
    timer = Rev::TimerWatcher::new(@timeout, true)
    timer.attach(@loop)
    timer.disable
    return timer
end

#getObject



31
32
33
34
35
36
# File 'lib/internethakai/hakairev/timer_factory.rb', line 31

def get
    timer = @stack.shift || create
    timer.reset
    timer.enable unless timer.enabled?
    return timer
end

#prepareObject



15
16
17
18
19
# File 'lib/internethakai/hakairev/timer_factory.rb', line 15

def prepare
    1.upto(@concurrency) do
        @stack << create
    end
end