File Coverage

blib/lib/POE/Future.pm
Criterion Covered Total %
statement 34 37 91.8
branch n/a
condition n/a
subroutine 14 16 87.5
pod 3 3 100.0
total 51 56 91.0


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, 2014-2026 -- leonerd@leonerd.org.uk
5              
6             package POE::Future 0.06;
7              
8 4     4   1437074 use v5.14;
  4         17  
9 4     4   31 use warnings;
  4         7  
  4         247  
10              
11 4     4   21 use Carp;
  4         31  
  4         286  
12              
13 4     4   43 use base qw( Future );
  4         8  
  4         2945  
14             Future->VERSION( '0.49' ); # udata
15              
16 4     4   59151 use POE;
  4         52433  
  4         30  
17              
18             =head1 NAME
19              
20             C - use L with L
21              
22             =head1 SYNOPSIS
23              
24             =for highlighter language=perl
25              
26             use POE::Future;
27              
28             my $future = POE::Future->new_delay( 10 )
29             ->then_done( "Hello, world!" );
30              
31             say $future->get;
32              
33             =head1 DESCRIPTION
34              
35             This subclass of L integrates with L, allowing the C
36             method to block until the future is ready. It allows C-using code to be
37             written that returns C instances, so that it can make full use of
38             C's abilities, including L, and also that modules using
39             it can provide a C-based asynchronous interface of their own.
40              
41             For a full description on how to use Futures, see the L documentation.
42              
43             =cut
44              
45             =head1 CONSTRUCTORS
46              
47             =cut
48              
49             =head2 new
50              
51             $f = POE::Future->new;
52              
53             Returns a new leaf future instance, which will allow waiting for its result to
54             be made available, using the C method.
55              
56             =cut
57              
58             =head2 new_delay
59              
60             $f = POE::Future->new_delay( $after );
61              
62             Returns a new leaf future instance which will become ready (with an empty
63             result) after the specified delay time.
64              
65             =cut
66              
67             sub new_delay
68             {
69 3     3 1 169526 my $self = shift->new;
70 3         33 my ( $after ) = @_;
71              
72             $self->set_udata( session => my $session = POE::Session->create(
73             inline_states => {
74 3     3   2305 _start => sub { $_[KERNEL]->delay( done => $after ) },
75 1     1   234 cancel => sub { $_[KERNEL]->delay( done => ) },
76 2     2   1300935 done => sub { $self->done },
77             },
78 3         48 ) );
79              
80             $self->on_cancel( sub {
81 1     1   65 my ( $self ) = @_;
82 1         12 POE::Kernel->post( $session, cancel => );
83 3         1043 });
84              
85 3         124 return $self;
86             }
87              
88             =head2 new_alarm
89              
90             $f = POE::Future->new_alarm( $at );
91              
92             Returns a new leaf future instance which will become ready (with an empty
93             result) at the specified alarm time.
94              
95             =cut
96              
97             sub new_alarm
98             {
99 1     1 1 483 my $self = shift->new;
100 1         40 my ( $at ) = @_;
101              
102             $self->set_udata( session => my $session = POE::Session->create(
103             inline_states => {
104 1     1   304 _start => sub { $_[KERNEL]->alarm( done => $at ) },
105 0     0   0 cancel => sub { $_[KERNEL]->alarm( done => ) },
106 1     1   337608 done => sub { $self->done },
107             },
108 1         17 ) );
109              
110             $self->on_cancel( sub {
111 0     0   0 my ( $self ) = @_;
112 0         0 POE::Kernel->post( $session, cancel => );
113 1         324 });
114              
115 1         29 return $self;
116             }
117              
118             =pod
119              
120             To create a delay or alarm timer that will fail instead of succeed, use the
121             C method:
122              
123             my $f = POE::Future->new_delay( 20 )
124             ->then_fail( "Timeout" );
125              
126             =cut
127              
128             sub await
129             {
130 4     4 1 290156 my $self = shift;
131 4         28 POE::Kernel::run_one_timeslice until $self->is_ready;
132 4         2164 return $self;
133             }
134              
135             =head1 AUTHOR
136              
137             Paul Evans
138              
139             =cut
140              
141             0x55AA;