Class: Libuv::Timer

Inherits:
Handle show all
Defined in:
lib/libuv/timer.rb

Constant Summary

Constants included from Assertions

Assertions::MSG_NO_PROC

Constants inherited from Q::Promise

Q::Promise::MAKE_PROMISE

Instance Attribute Summary

Attributes inherited from Handle

#closed, #loop, #storage

Instance Method Summary collapse

Methods inherited from Handle

#active?, #close, #closing?, #ref, #unref

Methods included from Assertions

#assert_block, #assert_boolean, #assert_type

Methods included from Resource

#check_result, #check_result!, #resolve, #to_ptr

Methods inherited from Q::DeferredPromise

#resolved?, #then

Methods inherited from Q::Promise

#catch, #finally

Constructor Details

#initialize(loop, callback = nil) ⇒ Timer

Returns a new instance of Timer.

Parameters:

  • loop (::Libuv::Loop)

    loop this timer will be associated

  • callback (Proc) (defaults to: nil)

    callback to be called when the timer is triggered



10
11
12
13
14
15
16
17
18
# File 'lib/libuv/timer.rb', line 10

def initialize(loop, callback = nil)
    @loop, @callback = loop, callback
    
    timer_ptr = ::Libuv::Ext.allocate_handle_timer
    error = check_result(::Libuv::Ext.timer_init(loop.handle, timer_ptr))
    @stopped = true

    super(timer_ptr, error)
end

Instance Method Details

#againObject

Resets the current repeat



48
49
50
51
52
# File 'lib/libuv/timer.rb', line 48

def again
    return if @closed
    error = check_result ::Libuv::Ext.timer_again(handle)
    reject(error) if error
end

#progress(callback = nil, &blk) ⇒ Object

Used to update the callback to be triggered by the timer

Parameters:

  • callback (Proc) (defaults to: nil)

    the callback to be called by the timer



71
72
73
# File 'lib/libuv/timer.rb', line 71

def progress(callback = nil, &blk)
    @callback = callback || blk
end

#repeatObject

Returns the current repeat timeout



63
64
65
66
# File 'lib/libuv/timer.rb', line 63

def repeat
    return if @closed
    ::Libuv::Ext.timer_get_repeat(handle)
end

#repeat=(repeat) ⇒ Object

Updates the repeat timeout



55
56
57
58
59
60
# File 'lib/libuv/timer.rb', line 55

def repeat=(repeat)
    return if @closed
    repeat = repeat.to_i
    check_result ::Libuv::Ext.timer_set_repeat(handle, repeat)
    reject(error) if error
end

#start(timeout, repeat = 0) ⇒ Object

Enables the timer. A repeat of 0 means no repeat

Parameters:

  • timeout (Fixnum)

    time in milliseconds before the timer callback is triggered the first time

  • repeat (Fixnum) (defaults to: 0)

    time in milliseconds between repeated callbacks after the first



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/libuv/timer.rb', line 24

def start(timeout, repeat = 0)
    return if @closed
    @stopped = false

    # prevent timeouts less than 0 (very long time otherwise as cast to an unsigned)
    # and you probably don't want to wait a few lifetimes
    timeout = 0 if timeout < 0

    timeout = timeout.to_i
    repeat = repeat.to_i

    error = check_result ::Libuv::Ext.timer_start(handle, callback(:on_timer), timeout, repeat)
    reject(error) if error
end

#stopObject

Disables the timer.



40
41
42
43
44
45
# File 'lib/libuv/timer.rb', line 40

def stop
    return if @stopped || @closed
    @stopped = true
    error = check_result ::Libuv::Ext.timer_stop(handle)
    reject(error) if error
end