Class: UV::ScheduledEvent

Inherits:
Libuv::Q::DeferredPromise
  • Object
show all
Includes:
Comparable
Defined in:
lib/uv-rays/scheduler.rb

Direct Known Subclasses

OneShot, Repeat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scheduler) ⇒ ScheduledEvent

Returns a new instance of ScheduledEvent.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/uv-rays/scheduler.rb', line 20

def initialize(scheduler)
    # Create a dummy deferrable
    reactor = scheduler.reactor
    defer = reactor.defer

    # Record a backtrace of where the schedule was created
    @trace = caller

    # Setup common event variables
    @scheduler = scheduler
    @created = reactor.now
    @last_scheduled = @created
    @trigger_count = 0

    # init the promise
    super(reactor, defer)
end

Instance Attribute Details

#createdObject (readonly)

Returns the value of attribute created.



15
16
17
# File 'lib/uv-rays/scheduler.rb', line 15

def created
  @created
end

#last_scheduledObject (readonly)

Returns the value of attribute last_scheduled.



16
17
18
# File 'lib/uv-rays/scheduler.rb', line 16

def last_scheduled
  @last_scheduled
end

#next_scheduledObject (readonly)

Returns the value of attribute next_scheduled.



17
18
19
# File 'lib/uv-rays/scheduler.rb', line 17

def next_scheduled
  @next_scheduled
end

#trigger_countObject (readonly)

Returns the value of attribute trigger_count.



18
19
20
# File 'lib/uv-rays/scheduler.rb', line 18

def trigger_count
  @trigger_count
end

Instance Method Details

#<=>(anOther) ⇒ Object

required for comparable



57
58
59
# File 'lib/uv-rays/scheduler.rb', line 57

def <=>(anOther)
    @next_scheduled <=> anOther.next_scheduled
end

#cancelObject

reject the promise



62
63
64
# File 'lib/uv-rays/scheduler.rb', line 62

def cancel
    @defer.reject(:cancelled)
end

#inspectObject Also known as: to_s

Provide relevant inspect information



39
40
41
42
43
44
45
46
# File 'lib/uv-rays/scheduler.rb', line 39

def inspect
    insp = String.new("#<#{self.class}:#{"0x00%x" % (self.__id__ << 1)} ")
    insp << "trigger_count=#{@trigger_count} "
    insp << "config=#{info} " if self.respond_to?(:info, true)
    insp << "next_scheduled=#{to_time(@next_scheduled)} "
    insp << "last_scheduled=#{to_time(@last_scheduled)} created=#{to_time(@created)}>"
    insp
end

#to_time(internal_time) ⇒ Object



49
50
51
52
53
# File 'lib/uv-rays/scheduler.rb', line 49

def to_time(internal_time)
    if internal_time
        ((internal_time + @scheduler.time_diff) / 1000).to_i
    end
end

#triggerObject

notify listeners of the event



67
68
69
70
# File 'lib/uv-rays/scheduler.rb', line 67

def trigger
    @trigger_count += 1
    @defer.notify(@reactor.now, self)
end