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 2 3 66.6
total 50 56 89.2


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-2019 -- leonerd@leonerd.org.uk
5              
6             package POE::Future;
7              
8 4     4   619103 use strict;
  4         33  
  4         120  
9 4     4   22 use warnings;
  4         6  
  4         179  
10              
11             our $VERSION = '0.04';
12              
13 4     4   22 use Carp;
  4         7  
  4         245  
14              
15 4     4   37 use base qw( Future );
  4         8  
  4         2800  
16             Future->VERSION( '0.05' ); # to respect subclassing
17              
18 4     4   41063 use POE;
  4         30331  
  4         28  
19              
20             =head1 NAME
21              
22             C - use L with L
23              
24             =head1 SYNOPSIS
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 4477 my $self = shift->new;
70 3         29 my ( $after ) = @_;
71              
72             $self->{session} = POE::Session->create(
73             inline_states => {
74 3     3   1388 _start => sub { $_[KERNEL]->delay( done => $after ) },
75 1     1   190 cancel => sub { $_[KERNEL]->delay( done => ) },
76 2     2   1301256 done => sub { $self->done },
77             },
78 3         38 );
79              
80             $self->on_cancel( sub {
81 1     1   64 my ( $self ) = @_;
82 1         11 POE::Kernel->post( $self->{session}, cancel => );
83 3         848 });
84              
85 3         88 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 629 my $self = shift->new;
100 1         11 my ( $at ) = @_;
101              
102             $self->{session} = POE::Session->create(
103             inline_states => {
104 1     1   241 _start => sub { $_[KERNEL]->alarm( done => $at ) },
105 0     0   0 cancel => sub { $_[KERNEL]->alarm( done => ) },
106 1     1   660666 done => sub { $self->done },
107             },
108 1         20 );
109              
110             $self->on_cancel( sub {
111 0     0   0 my ( $self ) = @_;
112 0         0 POE::Kernel->post( $self->{session}, cancel => );
113 1         341 });
114              
115 1         34 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 0 3808 my $self = shift;
131 4         29 POE::Kernel::run_one_timeslice until $self->is_ready;
132             }
133              
134             =head1 AUTHOR
135              
136             Paul Evans
137              
138             =cut
139              
140             0x55AA;