Class: Libuv::Timer

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

Constant Summary collapse

TIMEOUT_ERROR =
"timeout must be an Integer".freeze
REPEAT_ERROR =
"repeat must be an Integer".freeze

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, #storage

Instance Method Summary collapse

Methods included from Assertions

#assert_block, #assert_boolean, #assert_type

Methods inherited from Handle

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

Methods included from Resource

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

Methods inherited from Q::DeferredPromise

#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



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

def initialize(loop, callback = nil)
    @loop, @callback = loop, callback
    
    timer_ptr = ::Libuv::Ext.create_handle(:uv_timer)
    error = check_result(::Libuv::Ext.timer_init(loop.handle, timer_ptr))

    super(timer_ptr, error)
end

Instance Method Details

#again ⇒ Object

Resets the current repeat



43
44
45
46
47
# File 'lib/libuv/timer.rb', line 43

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



66
67
68
# File 'lib/libuv/timer.rb', line 66

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

#repeat ⇒ Object

Returns the current repeat timeout



58
59
60
61
# File 'lib/libuv/timer.rb', line 58

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

#repeat=(repeat) ⇒ Object

Updates the repeat timeout



50
51
52
53
54
55
# File 'lib/libuv/timer.rb', line 50

def repeat=(repeat)
    return if @closed
    assert_type(Integer, repeat, REPEAT_ERROR)
    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



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

def start(timeout, repeat = 0)
    return if @closed
    
    assert_type(Integer, timeout, TIMEOUT_ERROR)
    assert_type(Integer, repeat, REPEAT_ERROR)

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

#stop ⇒ Object

Disables the timer.



36
37
38
39
40
# File 'lib/libuv/timer.rb', line 36

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