Last active
March 18, 2019 00:45
-
-
Save christiankakesa/82750976453857576ef0daa1025d83de to your computer and use it in GitHub Desktop.
Example of complete ticker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Ticker | |
getter tick, done | |
@tick = Channel(Bool).new | |
@done = Channel(Bool).new | |
def initialize(tick_s : Int32) | |
spawn do | |
loop do | |
sleep tick_s | |
@tick.send true | |
end | |
end | |
end | |
def stop! | |
@done.send true | |
end | |
end | |
ticker = Ticker.new(2) # 2 seconds | |
spawn do | |
loop do | |
select | |
when ticker.tick.receive | |
puts "#{Time.now}" | |
when ticker.done.receive | |
puts "break!!!" | |
break | |
end | |
end | |
end | |
sleep 7 | |
ticker.stop! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment