File Coverage

blib/lib/IO/Async/Future.pm
Criterion Covered Total %
statement 38 38 100.0
branch 3 4 75.0
condition n/a
subroutine 12 12 100.0
pod 5 5 100.0
total 58 59 98.3


line stmt bran cond sub pod time code
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 47     47   13164 use v5.14;
  47         193  
9 47     47   270 use warnings;
  47         98  
  47         3459  
10              
11 47     47   501 use base qw( Future );
  47         106  
  47         15819  
12             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 47     47   208848 use constant FUTURE_HAS_UDATA => defined Future->can( "udata" );
  47         136  
  47         4727  
17              
18 47     47   327 use Carp;
  47         140  
  47         21629  
19              
20             =head1 NAME
21              
22             C - use L with L
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 stores a reference to the L
42             instance that created it, allowing the C method to block until the
43             Future is ready. These objects should not be constructed directly; instead
44             the C method on the containing Loop should be used.
45              
46             For a full description on how to use Futures, see the L documentation.
47              
48             =cut
49              
50             =head1 CONSTRUCTORS
51              
52             New C objects should be constructed by using the following
53             methods on the C. For more detail see the L
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 1491     1491 1 49606 my $proto = shift;
73 1491         18828 my $self = $proto->SUPER::new;
74              
75 1491         26338 my $loop;
76 1491 100       3937 if( ref $proto ) {
77 470         1386 $loop = $proto->loop;
78             }
79             else {
80 1021         1940 $loop = shift;
81             }
82              
83 1491         9329 if( FUTURE_HAS_UDATA ) {
84 1491         11706 $self->set_udata( loop => $loop );
85             }
86             else {
87             $self->{loop} = $loop;
88             }
89              
90 1491         21599 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 object.
102              
103             =cut
104              
105             sub loop
106             {
107 515     515 1 939 my $self = shift;
108 515         2446 return FUTURE_HAS_UDATA ? $self->udata( "loop" ) : $self->{loop};
109             }
110              
111             sub await
112             {
113 40     40 1 1217 my $self = shift;
114 40         283 $self->loop->await( $self );
115             }
116              
117             =head2 done_later
118              
119             $future->done_later( @result );
120              
121             A shortcut to calling the C method in a C 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, returns C<$future> itself to allow easy chaining.
126              
127             =cut
128              
129             sub done_later
130             {
131 1     1 1 7 my $self = shift;
132 1         5 my @result = @_;
133              
134 1     1   4 $self->loop->later( sub { $self->done( @result ) } );
  1         6  
135              
136 1         8 return $self;
137             }
138              
139             =head2 fail_later
140              
141             $future->fail_later( $exception, @details );
142              
143             A shortcut to calling the C method in a C 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, returns C<$future> itself to allow easy chaining.
148              
149             =cut
150              
151             sub fail_later
152             {
153 1     1 1 11 my $self = shift;
154 1         4 my ( $exception, @details ) = @_;
155              
156 1 50       6 $exception or croak "Expected a true exception";
157              
158 1     1   5 $self->loop->later( sub { $self->fail( $exception, @details ) } );
  1         14  
159              
160 1         7 return $self;
161             }
162              
163             =head1 AUTHOR
164              
165             Paul Evans
166              
167             =cut
168              
169             0x55AA;