blib/lib/IO/Async/Future.pm

filter: Statementstmt 100% 40 / 40 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 98.3% 60 / 61 CC 3 SCAR 2.5
CC
3
Cov
98%
fail_later
2.0
new
2.0
BEGIN
1.0
CRAP 1.3
« blib/lib/IO/Async/OS/cygwin.pm ↑ Summary blib/lib/IO/Async/Metrics.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, 2013-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Future 0.805;
7  
8 46 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 46 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 46 ▶use base qw( Future );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 46 Future->VERSION( '0.05' ); # to respect subclassing
13  
14 # Newer versions of Future have a proper subclassing-data API; for older
15 # versions we just treat it as a hashref
16 46 ▶use constant FUTURE_HAS_UDATA => defined Future->can( "udata" );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 46 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 C<IO::Async::Future> - use L<Future> with L<IO::Async>
23  
24 =head1 SYNOPSIS
25  
26 =for highlighter language=perl
27  
28 use Future::AsyncAwait;
29 use IO::Async::Loop;
30  
31 my $loop = IO::Async::Loop->new;
32  
33 my $future = $loop->new_future;
34  
35 $loop->watch_time( after => 3, code => sub { $future->done( "Done" ) } );
36  
37 print await( $future ), "\n";
38  
39 =head1 DESCRIPTION
40  
41 This subclass of L<Future> stores a reference to the L<IO::Async::Loop>
42 instance that created it, allowing the C<await> method to block until the
43 Future is ready. These objects should not be constructed directly; instead
44 the C<new_future> method on the containing Loop should be used.
45  
46 For a full description on how to use Futures, see the L<Future> documentation.
47  
48 =cut
49  
50 =head1 CONSTRUCTORS
51  
52 New C<IO::Async::Future> objects should be constructed by using the following
53 methods on the C<Loop>. For more detail see the L<IO::Async::Loop>
54 documentation.
55  
56 $future = $loop->new_future;
57  
58 Returns a new pending Future.
59  
60 $future = $loop->delay_future( %args );
61  
62 Returns a new Future that will become done at a given time.
63  
64 $future = $loop->timeout_future( %args );
65  
66 Returns a new Future that will become failed at a given time.
67  
68 =cut
69  
70 sub new
71 {
72 1485 ▶ my $proto = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
73 1485 my $self = $proto->SUPER::new;
74  
75 1485 my $loop;
76 1485 ▶ if( ref $proto ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) { } 469 1016 1485
77 469 $loop = $proto->loop;
78 }
79 else {
80 1016 $loop = shift;
81 }
82  
83 1485 if( FUTURE_HAS_UDATA ) {
84 1485 $self->set_udata( loop => $loop );
85 }
86 else {
87 $self->{loop} = $loop;
88 }
89  
90 1485 return $self;
91 }
92  
93 =head1 METHODS
94  
95 =cut
96  
97 =head2 loop
98  
99 $loop = $future->loop;
100  
101 Returns the underlying L<IO::Async::Loop> object.
102  
103 =cut
104  
105 sub loop
106 {
107 514 ▶ my $self = shift;
Subroutine loop: called CC=1 SCAR=0.0
Pod documented
108 514 return FUTURE_HAS_UDATA ? $self->udata( "loop" ) : $self->{loop};
109 }
110  
111 sub await
112 {
113 40 ▶ my $self = shift;
Subroutine await: called CC=1 SCAR=0.0
Pod documented
114 40 $self->loop->await( $self );
115 }
116  
117 =head2 done_later
118  
119 $future->done_later( @result );
120  
121 A shortcut to calling the C<done> method in a C<later> idle watch on the
122 underlying Loop object. Ensures that a returned Future object is not ready
123 immediately, but will wait for the next IO round.
124  
125 Like C<done>, returns C<$future> itself to allow easy chaining.
126  
127 =cut
128  
129 sub done_later
130 {
131 1 ▶ my $self = shift;
Subroutine done_later: called CC=1 SCAR=0.0
Pod documented
132 1 my @result = @_;
133  
134 1 ▶ $self->loop->later( sub { $self->done( @result ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
135  
136 1 return $self;
137 }
138  
139 =head2 fail_later
140  
141 $future->fail_later( $exception, @details );
142  
143 A shortcut to calling the C<fail> method in a C<later> idle watch on the
144 underlying Loop object. Ensures that a returned Future object is not ready
145 immediately, but will wait for the next IO round.
146  
147 Like C<fail>, returns C<$future> itself to allow easy chaining.
148  
149 =cut
150  
151 sub fail_later
152 {
153 1 ▶ my $self = shift;
Subroutine fail_later: called CC=2 SCAR=7.0
Pod documented
154 1 my ( $exception, @details ) = @_;
155  
156 1 ▶ $exception or croak "Expected a true exception";
Branch: 1 branch
BranchTrueFalseTotal
$exception or croak("Expected a true exception") 0 1 1
157  
158 1 ▶ $self->loop->later( sub { $self->fail( $exception, @details ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
159  
160 1 return $self;
161 }
162  
163 =head1 AUTHOR
164  
165 Paul Evans <leonerd@leonerd.org.uk>
166  
167 =cut
168  
169 46 0x55AA;
« blib/lib/IO/Async/OS/cygwin.pm ↑ Summary blib/lib/IO/Async/Metrics.pm »