Class: Cosmos::Sleeper
Overview
Allows for a breakable sleep implementation using the self-pipe trick See www.sitepoint.com/the-self-pipe-trick-explained/
Instance Method Summary collapse
-
#cancel ⇒ Object
Break sleeping - Once canceled a sleeper cannot be used again.
-
#initialize ⇒ Sleeper
constructor
A new instance of Sleeper.
-
#sleep(seconds) ⇒ Object
Breakable version of sleep.
Constructor Details
#initialize ⇒ Sleeper
Returns a new instance of Sleeper.
16 17 18 19 20 |
# File 'lib/cosmos/utilities/sleeper.rb', line 16 def initialize @pipe_reader, @pipe_writer = IO.pipe @readers = [@pipe_reader] @canceled = false end |
Instance Method Details
#cancel ⇒ Object
Break sleeping - Once canceled a sleeper cannot be used again
36 37 38 39 40 41 |
# File 'lib/cosmos/utilities/sleeper.rb', line 36 def cancel if !@canceled @canceled = true @pipe_writer.write('.') end end |
#sleep(seconds) ⇒ Object
Breakable version of sleep
26 27 28 29 30 31 32 33 |
# File 'lib/cosmos/utilities/sleeper.rb', line 26 def sleep(seconds) read_ready, _ = IO.select(@readers, nil, nil, seconds) if read_ready && read_ready.include?(@pipe_reader) return true else return false end end |