File Coverage

blib/lib/IO/Async/Future.pm
Criterion Covered Total %
statement 39 39 100.0
branch 3 4 75.0
condition n/a
subroutine 12 12 100.0
pod 5 5 100.0
total 59 60 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-2022 -- leonerd@leonerd.org.uk
5              
6             package IO::Async::Future;
7              
8 50     50   10280 use strict;
  50         91  
  50         1575  
9 50     50   254 use warnings;
  50         79  
  50         2364  
10              
11             our $VERSION = '0.802';
12              
13 50     50   321 use base qw( Future );
  50         117  
  50         13091  
14             Future->VERSION( '0.05' ); # to respect subclassing
15              
16             # Newer versions of Future have a proper subclassing-data API; for older
17             # versions we just treat it as a hashref
18 50     50   126705 use constant FUTURE_HAS_UDATA => defined Future->can( "udata" );
  50         189  
  50         3168  
19              
20 50     50   259 use Carp;
  50         141  
  50         16767  
21              
22             =head1 NAME
23              
24             C - use L with L
25              
26             =head1 SYNOPSIS
27              
28             use IO::Async::Loop;
29              
30             my $loop = IO::Async::Loop->new;
31              
32             my $future = $loop->new_future;
33              
34             $loop->watch_time( after => 3, code => sub { $future->done( "Done" ) } );
35              
36             print $future->get, "\n";
37              
38             =head1 DESCRIPTION
39              
40             This subclass of L stores a reference to the L
41             instance that created it, allowing the C method to block until the
42             Future is ready. These objects should not be constructed directly; instead
43             the C method on the containing Loop should be used.
44              
45             For a full description on how to use Futures, see the L documentation.
46              
47             =cut
48              
49             =head1 CONSTRUCTORS
50              
51             New C objects should be constructed by using the following
52             methods on the C. For more detail see the L
53             documentation.
54              
55             $future = $loop->new_future
56              
57             Returns a new pending Future.
58              
59             $future = $loop->delay_future( %args )
60              
61             Returns a new Future that will become done at a given time.
62              
63             $future = $loop->timeout_future( %args )
64              
65             Returns a new Future that will become failed at a given time.
66              
67             =cut
68              
69             sub new
70             {
71 1589     1589 1 32765 my $proto = shift;
72 1589         8190 my $self = $proto->SUPER::new;
73              
74 1589         14646 my $loop;
75 1589 100       3302 if( ref $proto ) {
76 509         1962 $loop = $proto->loop;
77             }
78             else {
79 1080         1657 $loop = shift;
80             }
81              
82 1589         1965 if( FUTURE_HAS_UDATA ) {
83             $self->set_udata( loop => $loop );
84             }
85             else {
86 1589         3897 $self->{loop} = $loop;
87             }
88              
89 1589         4300 return $self;
90             }
91              
92             =head1 METHODS
93              
94             =cut
95              
96             =head2 loop
97              
98             $loop = $future->loop
99              
100             Returns the underlying L object.
101              
102             =cut
103              
104             sub loop
105             {
106 593     593 1 929 my $self = shift;
107 593         1357 return FUTURE_HAS_UDATA ? $self->udata( "loop" ) : $self->{loop};
108             }
109              
110             sub await
111             {
112 76     76 1 3760 my $self = shift;
113 76         348 $self->loop->await( $self );
114             }
115              
116             =head2 done_later
117              
118             $future->done_later( @result )
119              
120             A shortcut to calling the C method in a C idle watch on the
121             underlying Loop object. Ensures that a returned Future object is not ready
122             immediately, but will wait for the next IO round.
123              
124             Like C, returns C<$future> itself to allow easy chaining.
125              
126             =cut
127              
128             sub done_later
129             {
130 1     1 1 4 my $self = shift;
131 1         33 my @result = @_;
132              
133 1     1   5 $self->loop->later( sub { $self->done( @result ) } );
  1         4  
134              
135 1         4 return $self;
136             }
137              
138             =head2 fail_later
139              
140             $future->fail_later( $exception, @details )
141              
142             A shortcut to calling the C method in a C idle watch on the
143             underlying Loop object. Ensures that a returned Future object is not ready
144             immediately, but will wait for the next IO round.
145              
146             Like C, returns C<$future> itself to allow easy chaining.
147              
148             =cut
149              
150             sub fail_later
151             {
152 1     1 1 5 my $self = shift;
153 1         3 my ( $exception, @details ) = @_;
154              
155 1 50       3 $exception or croak "Expected a true exception";
156              
157 1     1   2 $self->loop->later( sub { $self->fail( $exception, @details ) } );
  1         11  
158              
159 1         3 return $self;
160             }
161              
162             =head1 AUTHOR
163              
164             Paul Evans
165              
166             =cut
167              
168             0x55AA;