Class: Zoidberg::Timer
- Inherits:
-
Object
- Object
- Zoidberg::Timer
- Includes:
- SoftShell
- Defined in:
- lib/zoidberg/timer.rb
Overview
Simple timer class
Defined Under Namespace
Instance Attribute Summary collapse
-
#paused ⇒ TrueClass, FalseClass
readonly
Timer is paused.
-
#to_run ⇒ Array<Action>
readonly
Items to run.
Instance Method Summary collapse
-
#after(interval) { ... } ⇒ Action
Run action after given interval.
-
#cancel ⇒ self
Remove all actions from the timer.
-
#every(interval) { ... } ⇒ Action
Run recurring action at given interval.
-
#initialize ⇒ self
constructor
Create a new timer.
-
#next_interval ⇒ Numeric, NilClass
Interval to next action.
-
#pause ⇒ self
Pause the timer to prevent any actions from being run.
-
#reset(wakeup = true) ⇒ self
Reset the timer.
-
#resume ⇒ self
Resume a paused timer.
-
#run_ready ⇒ self
Run any actions that are ready.
Methods included from SoftShell
#_zoidberg_thread, #async, #defer, included, #sleep
Constructor Details
#initialize ⇒ self
Create a new timer
77 78 79 80 81 |
# File 'lib/zoidberg/timer.rb', line 77 def initialize @to_run = [] @paused = false @thread = Thread.new{ run! } end |
Instance Attribute Details
#paused ⇒ TrueClass, FalseClass (readonly)
72 73 74 |
# File 'lib/zoidberg/timer.rb', line 72 def paused @paused end |
#to_run ⇒ Array<Action> (readonly)
70 71 72 |
# File 'lib/zoidberg/timer.rb', line 70 def to_run @to_run end |
Instance Method Details
#after(interval) { ... } ⇒ Action
Run action after given interval
105 106 107 108 109 110 111 112 113 |
# File 'lib/zoidberg/timer.rb', line 105 def after(interval, &block) action = Action.new( {:interval => interval}, &block ) to_run.push(action) reset action end |
#cancel ⇒ self
Remove all actions from the timer
140 141 142 143 144 |
# File 'lib/zoidberg/timer.rb', line 140 def cancel to_run.clear reset current_self end |
#every(interval) { ... } ⇒ Action
Run recurring action at given interval
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/zoidberg/timer.rb', line 88 def every(interval, &block) action = Action.new({ :interval => interval, :recur => true }, &block ) to_run.push(action) reset action end |
#next_interval ⇒ Numeric, NilClass
161 162 163 164 165 166 167 |
# File 'lib/zoidberg/timer.rb', line 161 def next_interval unless(to_run.empty? || paused) item = to_run.first result = (item.last_run + item.interval) - Time.now.to_f result < 0 ? 0 : result end end |
#pause ⇒ self
Pause the timer to prevent any actions from being run
118 119 120 121 122 123 124 |
# File 'lib/zoidberg/timer.rb', line 118 def pause unless(@paused) @paused = true reset end current_self end |
#reset(wakeup = true) ⇒ self
Reset the timer
150 151 152 153 154 155 156 157 158 |
# File 'lib/zoidberg/timer.rb', line 150 def reset(wakeup=true) to_run.sort_by! do |item| (item.interval + item.last_run) - Time.now.to_f end if(wakeup) waker.write '-' end current_self end |
#resume ⇒ self
Resume a paused timer
129 130 131 132 133 134 135 |
# File 'lib/zoidberg/timer.rb', line 129 def resume if(@paused) @paused = false reset end current_self end |
#run_ready ⇒ self
Run any actions that are ready
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/zoidberg/timer.rb', line 172 def run_ready items = to_run.find_all do |item| ((item.interval + item.last_run) - Time.now.to_f) <= 0 end to_run.delete_if do |item| items.include?(item) end items.map do |item| begin item.run! unless item.cancelled? rescue DeadException item.cancel rescue => e Zoidberg.logger.error "<#{self}> Timed action generated an error: #{e.class.name} - #{e}" end item if item.recur end.compact.each do |item| to_run << item end current_self end |