Class: DelayExecutioner
- Inherits:
-
Object
- Object
- DelayExecutioner
- Defined in:
- lib/vimamsa/util.rb
Overview
Execute proc after wait_time seconds after last .run call. Used for image scaling after window resize
Class Method Summary collapse
-
.exec(id:, wait:, callable:) ⇒ Object
Run ‘callable.call’ if ‘wait’ time elapsed from last exec call for this id.
Instance Method Summary collapse
-
#initialize(wait_time, _proc) ⇒ DelayExecutioner
constructor
A new instance of DelayExecutioner.
- #run ⇒ Object
- #start_thread ⇒ Object
Constructor Details
#initialize(wait_time, _proc) ⇒ DelayExecutioner
Returns a new instance of DelayExecutioner.
214 215 216 217 218 219 |
# File 'lib/vimamsa/util.rb', line 214 def initialize(wait_time, _proc) @wait_time = wait_time @proc = _proc @lastt = Time.now @thread_running = false end |
Class Method Details
.exec(id:, wait:, callable:) ⇒ Object
Run ‘callable.call’ if ‘wait’ time elapsed from last exec call for this id
205 206 207 208 209 210 211 212 |
# File 'lib/vimamsa/util.rb', line 205 def self.exec(id:, wait:, callable:) @@h ||= {} h = @@h if h[id].nil? h[id] = DelayExecutioner.new(wait, callable) end h[id].run end |
Instance Method Details
#run ⇒ Object
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/vimamsa/util.rb', line 234 def run() # Reset @lastt to further delay execution until @wait_time from now @lastt = Time.now # If already executed after last call to run() if @thread_running == false @thread_running = true start_thread end end |
#start_thread ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/vimamsa/util.rb', line 221 def start_thread Thread.new { while true sleep 0.1 if Time.now - @lastt > @wait_time @proc.call @thread_running = false break end end } end |