@@ -12,10 +12,7 @@ module Marmot
1212 @canceled = false
1313 @callback : Callback = - > (t : Task ) {}
1414
15- getter tick = Channel (Task ).new
16-
17- # :nodoc:
18- abstract def wait_next_tick : Nil
15+ protected getter tick = Channel (Task ).new
1916
2017 # Cancels the task.
2118 #
@@ -29,14 +26,12 @@ module Marmot
2926 @canceled
3027 end
3128
32- # :nodoc:
33- def run : Nil
29+ protected def run : Nil
3430 @callback .call(self )
3531 rescue
3632 end
3733
38- # :nodoc:
39- def start : Nil
34+ protected def start : Nil
4035 spawn do
4136 while ! canceled?
4237 wait_next_tick
@@ -50,13 +45,15 @@ module Marmot
5045 @tick .close
5146 end
5247 end
48+
49+ private abstract def wait_next_tick : Nil
5350 end
5451
5552 class CronTask < Task
5653 def initialize (@hour : Int32 , @minute : Int32 , @second : Int32 , @callback : Callback )
5754 end
5855
59- def wait_next_tick : Nil
56+ protected def wait_next_tick : Nil
6057 sleep span
6158 end
6259
@@ -87,21 +84,29 @@ module Marmot
8784 end
8885
8986 class OnChannelTask (T ) < Task
87+ # Gets the value received on the channel.
88+ #
89+ # If the channel is closed while waiting, a `nil` value will saved here, and
90+ # the task will run one last time.
9091 getter value : T ? = nil
9192
9293 def initialize (@channel : Channel (T ), @callback : Callback )
9394 end
9495
95- def wait_next_tick : Nil
96- @value = @channel .receive?
96+ protected def wait_next_tick : Nil
97+ if @channel .closed?
98+ cancel
99+ else
100+ @value = @channel .receive?
101+ end
97102 end
98103 end
99104
100105 class RepeatTask < Task
101106 def initialize (@span : Time ::Span , @first_run : Bool , @callback : Callback )
102107 end
103108
104- def wait_next_tick : Nil
109+ protected def wait_next_tick : Nil
105110 if @first_run
106111 @first_run = false
107112 else
0 commit comments