Class: MIDIator::Timer
- Inherits:
-
Object
- Object
- MIDIator::Timer
- Defined in:
- lib/midiator/timer.rb
Overview
A timer that allows you to schedule notes to be played in the future.
Authors
-
Topher Cyll
-
Ben Bleything <[email protected]>
Copyright
Copyright © 2008 Ben Bleything
This code released under the terms of the MIT license.
Instance Attribute Summary collapse
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#resolution ⇒ Object
readonly
Returns the value of attribute resolution.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Instance Method Summary collapse
-
#at(time, &block) ⇒ Object
Add a new job to be performed at
time
. -
#flush ⇒ Object
Empty the queue.
-
#initialize(resolution) ⇒ Timer
constructor
Create a new Timer object that ticks every
resolution
seconds.
Constructor Details
#initialize(resolution) ⇒ Timer
Create a new Timer object that ticks every resolution
seconds.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/midiator/timer.rb', line 21 def initialize( resolution ) @resolution = resolution @queue = [] @thread = Thread.new do loop do dispatch sleep @resolution end end end |
Instance Attribute Details
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
18 19 20 |
# File 'lib/midiator/timer.rb', line 18 def queue @queue end |
#resolution ⇒ Object (readonly)
Returns the value of attribute resolution.
18 19 20 |
# File 'lib/midiator/timer.rb', line 18 def resolution @resolution end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
18 19 20 |
# File 'lib/midiator/timer.rb', line 18 def thread @thread end |
Instance Method Details
#at(time, &block) ⇒ Object
Add a new job to be performed at time
.
41 42 43 44 |
# File 'lib/midiator/timer.rb', line 41 def at( time, &block ) time = time.to_f if time.kind_of? Time @queue.push [ time, block ] end |
#flush ⇒ Object
Empty the queue
35 36 37 |
# File 'lib/midiator/timer.rb', line 35 def flush @queue.clear end |