blib/lib/IO/Async/Timer/Absolute.pm

filter: Statementstmt 97.0% 33 / 34 Branchbran 71.4% 10 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 91.2% 52 / 57 CC 8 SCAR 13.9
CC
8
Cov
90%
configure
7.2
__ANON__
2.0
_make_cb
1.0
CRAP 4.0
« blib/lib/IO/Async/Internals/FunctionWorker.pm ↑ Summary blib/lib/IO/Async/OS/MSWin32.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, 2010-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Timer::Absolute 0.805;
7  
8 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use base qw( IO::Async::Timer );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 C<IO::Async::Timer::Absolute> - event callback at a fixed future time
17  
18 =head1 SYNOPSIS
19  
20 =for highlighter language=perl
21  
22 use IO::Async::Timer::Absolute;
23  
24 use POSIX qw( mktime );
25  
26 use IO::Async::Loop;
27 my $loop = IO::Async::Loop->new;
28  
29 my @time = gmtime;
30  
31 my $timer = IO::Async::Timer::Absolute->new(
32 time => mktime( 0, 0, 0, $time[3]+1, $time[4], $time[5] ),
33  
34 on_expire => sub {
35 print "It's midnight\n";
36 $loop->stop;
37 },
38 );
39  
40 $loop->add( $timer );
41  
42 $loop->run;
43  
44 =head1 DESCRIPTION
45  
46 This subclass of L<IO::Async::Timer> implements one-shot events at a fixed
47 time in the future. The object waits for a given timestamp, and invokes its
48 callback at that point in the future.
49  
50 For a C<Timer> object that waits for a delay relative to the time it is
51 started, see instead L<IO::Async::Timer::Countdown>.
52  
53 =cut
54  
55 =head1 EVENTS
56  
57 The following events are invoked, either using subclass methods or CODE
58 references in parameters:
59  
60 =head2 on_expire
61  
62 Invoked when the timer expires.
63  
64 =cut
65  
66 =head1 PARAMETERS
67  
68 The following named parameters may be passed to C<new> or C<configure>:
69  
70 =head2 on_expire => CODE
71  
72 CODE reference for the C<on_expire> event.
73  
74 =head2 time => NUM
75  
76 The epoch time at which the timer will expire.
77  
78 Once constructed, the timer object will need to be added to the C<Loop> before
79 it will work.
80  
81 Unlike other timers, it does not make sense to C<start> this object, because
82 its expiry time is absolute, and not relative to the time it is started.
83  
84 =cut
85  
86 sub configure
87 {
88 7 ▶ my $self = shift;
Subroutine configure: called CC=7 SCAR=19.7
Pod documented
89 7 my %params = @_;
90  
91 7 ▶ if( exists $params{on_expire} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"on_expire"}) 5 2 7
92 5 my $on_expire = delete $params{on_expire};
93 5 ▶ ref $on_expire or croak "Expected 'on_expire' as a reference";
Branch: 1 branch
BranchTrueFalseTotal
ref $on_expire or croak("Expected 'on_expire' as a reference") 0 5 5
94  
95 5 $self->{on_expire} = $on_expire;
96 5 undef $self->{cb}; # Will be lazily constructed when needed
97 }
98  
99 7 ▶ if( exists $params{time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"time"}) 6 1 7
100 6 my $time = delete $params{time};
101  
102 6 ▶ $self->stop if $self->is_running;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_running 1 5 6
103  
104 6 $self->{time} = $time;
105  
106 6 ▶ $self->start if !$self->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_running 6 0 6
107 }
108  
109 7 ▶ unless( $self->can_event( 'on_expire' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can_event("on_expire")) 0 7 7
110 0 croak 'Expected either a on_expire callback or an ->on_expire method';
111 }
112  
113 7 $self->SUPER::configure( %params );
114 }
115  
116 sub _make_cb
117 {
118 5 ▶ my $self = shift;
Subroutine _make_cb: called CC=1 SCAR=0.0
119  
120 return $self->_capture_weakself( sub {
121 4 ▶ my $self = shift or return;
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 4 4
122  
123 4 undef $self->{id};
124  
125 4 $self->invoke_event( "on_expire" );
126 5 } );
127 }
128  
129 sub _make_enqueueargs
130 {
131 6 ▶ my $self = shift;
Subroutine _make_enqueueargs: called CC=1 SCAR=0.0
132  
133 6 return at => $self->{time};
134 }
135  
136 =head1 AUTHOR
137  
138 Paul Evans <leonerd@leonerd.org.uk>
139  
140 =cut
141  
142 2 0x55AA;
« blib/lib/IO/Async/Internals/FunctionWorker.pm ↑ Summary blib/lib/IO/Async/OS/MSWin32.pm »