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

Attributes inherited from Q::Promise

#trace

Instance Method Summary collapse

Methods inherited from Handle

#active?, #close, #closed?, #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, #ruby_catch, #value

Constructor Details

#initialize(reactor, callback = nil, &blk) ⇒ Timer

Returns a new instance of Timer.

Parameters:

  • reactor (::Libuv::Reactor)

    reactor 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
20
21
# File 'lib/libuv/timer.rb', line 12

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

    super(timer_ptr, error)
end

Instance Method Details

#againObject

Resets the current repeat



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

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

    self
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



89
90
91
92
93
# File 'lib/libuv/timer.rb', line 89

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

    self
end

#repeat(time = nil) ⇒ Object

Set or gets the current repeat timeout Repeat is the time in milliseconds between repeated callbacks after the initial timeout fires

Parameters:

  • times (Integer)

    time in milliseconds between repeated callbacks after the first



76
77
78
79
80
81
82
83
84
# File 'lib/libuv/timer.rb', line 76

def repeat(time = nil)
    return if @closed
    if time.nil?
        ::Libuv::Ext.timer_get_repeat(handle)
    else
        self.repeat = time
        self
    end
end

#repeat=(time) ⇒ Object

Set the current repeat timeout Repeat is the time in milliseconds between repeated callbacks after the initial timeout fires

Parameters:

  • time (Integer)

    time in milliseconds between repeated callbacks after the first



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

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

#start(timeout, repeat = 0) ⇒ Object

Enables the timer. A repeat of 0 means no repeat

Parameters:

  • timeout (Integer)

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

  • repeat (Integer) (defaults to: 0)

    time in milliseconds between repeated callbacks after the first



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/libuv/timer.rb', line 27

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 = timeout.to_i
    timeout = 0 if timeout < 0

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

    self
end

#stopObject

Disables the timer.



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

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

    self
end