Class: Wires::TimeSchedulerItem
- Inherits:
-
Object
- Object
- Wires::TimeSchedulerItem
- Defined in:
- lib/wires/time.rb
Instance Attribute Summary collapse
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
- #active? ⇒ Boolean
- #cancel ⇒ Object
-
#count ⇒ Object
Get/set @count (and apply constraints on set).
-
#count=(x) ⇒ Object
TODO: handle explicit cancel?.
- #count_dec(x = 1) ⇒ Object
-
#count_inc(x = 1) ⇒ Object
Inc/dec @count.
-
#fire(*args) ⇒ Object
Fire the event now, regardless of time or active status.
-
#fire_if_ready(*args) ⇒ Object
Fire the event only if it is ready.
-
#fire_when_ready(*args) ⇒ Object
Block until event is ready, then fire and block until it is done.
- #inactive? ⇒ Boolean
-
#initialize(time, event, channel = '*', interval: 0.seconds, count: 1, ignore_past: false, cancel: false) ⇒ TimeSchedulerItem
constructor
A new instance of TimeSchedulerItem.
- #ready? ⇒ Boolean
- #time_until ⇒ Object
-
#wait_until_ready ⇒ Object
Block until event is ready.
Constructor Details
#initialize(time, event, channel = '*', interval: 0.seconds, count: 1, ignore_past: false, cancel: false) ⇒ TimeSchedulerItem
Returns a new instance of TimeSchedulerItem.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/wires/time.rb', line 12 def initialize(time, event, channel='*', interval:0.seconds, count:1, ignore_past:false, cancel:false) expect_type time, Time @active = (not cancel) tempcount = count while (time < Time.now) and (tempcount > 0) time += interval tempcount -= 1 end if not ignore_past time -= interval self.count = count else self.count = tempcount end @time = time @event = Event.new_from(event) @channel = channel @interval = interval end |
Instance Attribute Details
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
10 11 12 |
# File 'lib/wires/time.rb', line 10 def channel @channel end |
#event ⇒ Object (readonly)
Returns the value of attribute event.
10 11 12 |
# File 'lib/wires/time.rb', line 10 def event @event end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
10 11 12 |
# File 'lib/wires/time.rb', line 10 def interval @interval end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
10 11 12 |
# File 'lib/wires/time.rb', line 10 def time @time end |
Instance Method Details
#active? ⇒ Boolean
39 |
# File 'lib/wires/time.rb', line 39 def active?; @active end |
#cancel ⇒ Object
44 |
# File 'lib/wires/time.rb', line 44 def cancel; @active = false ;nil end |
#count ⇒ Object
Get/set @count (and apply constraints on set)
47 |
# File 'lib/wires/time.rb', line 47 def count; @count end |
#count=(x) ⇒ Object
TODO: handle explicit cancel?
49 |
# File 'lib/wires/time.rb', line 49 def count=(x); @count=[x,0].max; @active&&=(count>0) ;nil end |
#count_dec(x = 1) ⇒ Object
53 |
# File 'lib/wires/time.rb', line 53 def count_dec(x=1); self.count=(@count-x) end |
#count_inc(x = 1) ⇒ Object
Inc/dec @count. Necessary because += and -= outside of lock are not atomic!
52 |
# File 'lib/wires/time.rb', line 52 def count_inc(x=1); self.count=(@count+x) end |
#fire(*args) ⇒ Object
Fire the event now, regardless of time or active status
56 57 58 59 60 |
# File 'lib/wires/time.rb', line 56 def fire(*args) Channel.new(@channel).fire(@event, *args) count_dec @time += @interval if @active nil end |
#fire_if_ready(*args) ⇒ Object
Fire the event only if it is ready
63 |
# File 'lib/wires/time.rb', line 63 def fire_if_ready(*args); self.fire(*args) if ready? end |
#fire_when_ready(*args) ⇒ Object
Block until event is ready, then fire and block until it is done
69 70 71 72 |
# File 'lib/wires/time.rb', line 69 def fire_when_ready(*args); wait_until_ready fire_if_ready(*args) end |
#inactive? ⇒ Boolean
40 |
# File 'lib/wires/time.rb', line 40 def inactive?; not @active end |
#ready? ⇒ Boolean
41 |
# File 'lib/wires/time.rb', line 41 def ready?; @active and (Time.now >= @time) end |
#time_until ⇒ Object
42 |
# File 'lib/wires/time.rb', line 42 def time_until; (@active ? [(Time.now - @time), 0].max : nil) end |
#wait_until_ready ⇒ Object
Block until event is ready
66 |
# File 'lib/wires/time.rb', line 66 def wait_until_ready; sleep 0 until ready? end |