blib/lib/IO/Async/Timer.pm

filter: Statementstmt 93.1% 41 / 44 Branchbran 70.0% 14 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 88.4% 69 / 78 CC 11 SCAR 11.7
CC
11
Cov
86%
stop
5.0
new
4.5
start
4.0
CRAP 3.2
« blib/lib/IO/Async/Protocol/Stream.pm ↑ Summary blib/lib/IO/Async/PID.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2009-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Timer 0.805;
7  
8 15 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use base qw( IO::Async::Notifier );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 C<IO::Async::Timer> - base class for Notifiers that use timed delays
17  
18 =head1 DESCRIPTION
19  
20 =for highlighter language=perl
21  
22 This module provides a subclass of L<IO::Async::Notifier> for implementing
23 notifiers that use timed delays. For specific implementations, see one of the
24 subclasses:
25  
26 =over 8
27  
28 =item *
29  
30 L<IO::Async::Timer::Absolute> - event callback at a fixed future time
31  
32 =item *
33  
34 L<IO::Async::Timer::Countdown> - event callback after a fixed delay
35  
36 =item *
37  
38 L<IO::Async::Timer::Periodic> - event callback at regular intervals
39  
40 =back
41  
42 =cut
43  
44 =head1 CONSTRUCTOR
45  
46 =cut
47  
48 =head2 new
49  
50 $timer = IO::Async::Timer->new( %args );
51  
52 Constructs a particular subclass of C<IO::Async::Timer> object, and returns
53 it. This constructor is provided for backward compatibility to older code
54 which doesn't use the subclasses. New code should directly construct a
55 subclass instead.
56  
57 =over 8
58  
59 =item mode => STRING
60  
61 The type of timer to create. Currently the only allowed mode is C<countdown>
62 but more types may be added in the future.
63  
64 =back
65  
66 Once constructed, the C<Timer> will need to be added to the C<Loop> before it
67 will work. It will also need to be started by the C<start> method.
68  
69 =cut
70  
71 sub new
72 {
73 34 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=15.0
Pod documented
74 34 my %args = @_;
75  
76 34 ▶ if( my $mode = delete $args{mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mode = delete $args{"mode"}) 0 34 34
77 # Might define some other modes later
78 0 ▶ $mode eq "countdown" or croak "Expected 'mode' to be 'countdown'";
Branch: 1 branch
BranchTrueFalseTotal
$mode eq "countdown" or croak("Expected 'mode' to be 'countdown'") 0 0 0
79  
80 0 require IO::Async::Timer::Countdown;
81 0 return IO::Async::Timer::Countdown->new( %args );
82 }
83  
84 34 return $class->SUPER::new( %args );
85 }
86  
87 sub _add_to_loop
88 {
89 36 ▶ my $self = shift;
Subroutine _add_to_loop: called CC=2 SCAR=6.9
90 36 ▶ $self->start if delete $self->{pending};
Branch: 1 branch
BranchTrueFalseTotal
if delete $self->{"pending"} 16 20 36
91 }
92  
93 sub _remove_from_loop
94 {
95 30 ▶ my $self = shift;
Subroutine _remove_from_loop: called CC=1 SCAR=0.0
96 30 $self->stop;
97 }
98  
99 =head1 METHODS
100  
101 =cut
102  
103 =head2 is_running
104  
105 $running = $timer->is_running;
106  
107 Returns true if the Timer has been started, and has not yet expired, or been
108 stopped.
109  
110 =cut
111  
112 sub is_running
113 {
114 162 ▶ my $self = shift;
Subroutine is_running: called CC=1 SCAR=0.0
Pod documented
115  
116 162 defined $self->{id};
117 }
118  
119 =head2 start
120  
121 $timer->start;
122  
123 Starts the Timer. Throws an error if it was already running.
124  
125 If the Timer is not yet in a Loop, the actual start will be deferred until it
126 is added. Once added, it will be running, and will expire at the given
127 duration after the time it was added.
128  
129 As a convenience, C<$timer> is returned. This may be useful for starting
130 timers at construction time:
131  
132 $loop->add( IO::Async::Timer->new( ... )->start );
133  
134 =cut
135  
136 sub start
137 {
138 110 ▶ my $self = shift;
Subroutine start: called CC=4 SCAR=13.9
Pod documented
139  
140 110 my $loop = $self->loop;
141 110 ▶ if( !defined $loop ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $loop) 31 79 110
142 31 $self->{pending} = 1;
143 31 return $self;
144 }
145  
146 79 ▶ defined $self->{id} and croak "Cannot start a Timer that is already running";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"id"} and croak("Cannot start a Timer that is already running") 0 79 79
147  
148 79 ▶ if( !$self->{cb} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"cb"}) 30 49 79
149 30 $self->{cb} = $self->_make_cb;
150 }
151  
152 $self->{id} = $loop->watch_time(
153 $self->_make_enqueueargs,
154 code => $self->{cb},
155 79 );
156  
157 79 return $self;
158 }
159  
160 =head2 stop
161  
162 $timer->stop;
163  
164 Stops the Timer if it is running. If it has not yet been added to the C<Loop>
165 but there is a start pending, this will cancel it.
166  
167 =cut
168  
169 sub stop
170 {
171 61 ▶ my $self = shift;
Subroutine stop: called CC=5 SCAR=16.2
Pod documented
172  
173 61 ▶ if( $self->{pending} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pending"}) 1 60 61
174 1 delete $self->{pending};
175 1 return;
176 }
177  
178 60 ▶ return if !$self->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_running 26 34 60
179  
180 34 ▶ my $loop = $self->loop or croak "Cannot stop a Timer that is not in a Loop";
Branch: 1 branch
BranchTrueFalseTotal
my $loop = $self->loop or croak("Cannot stop a Timer that is not in a Loop") 0 34 34
181  
182 34 ▶ defined $self->{id} or return; # nothing to do but no error
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"id"} or return 0 34 34
183  
184 34 $loop->unwatch_time( $self->{id} );
185  
186 34 undef $self->{id};
187 }
188  
189 =head1 AUTHOR
190  
191 Paul Evans <leonerd@leonerd.org.uk>
192  
193 =cut
194  
195 15 0x55AA;
« blib/lib/IO/Async/Protocol/Stream.pm ↑ Summary blib/lib/IO/Async/PID.pm »