blib/lib/AnyEvent/Promise.pm

filter: Statementstmt 97.4% 75 / 77 Branchbran 83.3% 5 / 6 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 95.8% 23 / 24 Podpod 100% 7 / 7 totaltotal 94.9% 113 / 119 CC 5 SCAR 3.8
CC
5
Cov
95%
__ANON__
3.0
then
3.0
__ANON__
2.0
CRAP 1.5
↑ Summary
1 package AnyEvent::Promise;
2  
3 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Try::Tiny qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 AnyEvent::Promise - Evented promises
14  
15 =head1 VERSION
16  
17 Version 0.01
18  
19 =cut
20  
21 4 our $VERSION = '0.02';
22  
23 =head1 SYNOPSIS
24  
25 Avoid the evented pyramid of doom!
26  
27 use AnyEvent::Promise;
28 use AnyEvent::Redis;
29  
30 my $redis = AnyEvent::Redis->new();
31  
32 my $p = promise(sub {
33 $redis->get('test');
34 })->then(sub {
35 $redis->set('test', shift);
36 })->then(sub {
37 $redis->get('test');
38 })->then(sub {
39 say shift;
40 })->catch(sub {
41 say 'I failed!';
42 say @_;
43 })->fulfill;
44  
45 =head1 DESCRIPTION
46  
47 L<AnyEvent::Promise> allows evented interfaces to be chained, taking away some
48 of the redundancy of layering L<AnyEvent> condition variable callbacks.
49  
50 A promise is created using L<AnyEvent::Promise::new|/new> or the exported
51 L</promise> helper function. These will both return a promise instance and add
52 the callback function as the start of the promise chain. Each call to L</then>
53 on the promise instance will add another subroutine which returns a condition
54 variable to the chain.
55  
56 The promise callback chain won't start until L</condvar> or L</fulfill> is
57 called on the instance. Calling L</condvar> or L</cv> will start the callback
58 chain and return the promise guarding condvar, which is fulfilled after the last
59 callback on the chain returns. Similarily, L</fulfill> will start the chain, but
60 will block until the guarding condvar is fulfilled.
61  
62 Errors in the callbacks can be caught by setting an exception handler via the
63 L</catch> method on the promise instance. This method will catch exceptions
64 raised from L<AnyEvent> objects and exceptions raised in blocks provided to
65 L</then>. If an error is encountered in the chain, an exception will be thrown
66 and the rest of the chain will be skipped, jumping straight to the catch
67 callback.
68  
69 =head1 EXPORT
70  
71 =head2 promise($cb)
72  
73 Start promise chain with callback C<$cb>. This function is a shortcut to
74 L<AnyEvent::Promise::new|/new>, and returns a promise object with the callback
75 attached.
76  
77 =cut
78 1 ▶sub promise { AnyEvent::Promise->new(@_) }
Subroutine promise: called CC=1 SCAR=0.0
Pod documented
79  
80 5 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
81 4 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
82 5 *{caller() . '::promise'} = \&promise;
83 }
84  
85 =head1 METHODS
86  
87 =head2 new($cb)
88  
89 Create an instance of a promise, start the chain off with callback C<$cb>. See
90 L</then> for information on passing in a callback and condvar.
91  
92 =cut
93 11 ▶sub new {
Subroutine new: called CC=1 SCAR=0.1
Pod documented
94 11 my ($class, $cb) = @_;
95  
96 11 my $self = bless {
97 guard => undef,
98 initial => undef,
99 fulfill => undef,
100 reject => undef,
101 rejected => 0
102 }, $class;
103  
104 11 $self->{guard} = AnyEvent->condvar;
105 11 $self->{initial} = AnyEvent->condvar;
106  
107 11 my $reject = AnyEvent->condvar;
108 0 ▶ $reject->cb(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 carp shift->recv;
110 0 $self->{guard}->send;
111 11 });
112 11 $self->{reject} = $reject;
113  
114 11 $self->then($cb);
115  
116 11 return $self;
117 }
118  
119 =head2 then($cb)
120  
121 Add callback C<$cb> on to the promise chain.
122  
123 This callback will receive the return of the previous callback -- i.e. the
124 callback will receive the value sent by the previous condvar directly. In order
125 to continue the promise chain, the callback should return a condvar.
126  
127 Instead of:
128  
129 my $cv = $redis->get('test');
130 $cv->cb(sub {
131 my $ret = shift->recv;
132 my $cv2 = $redis->set('test', $ret);
133 $cv2->cb(sub {
134 my $cv3 = $redis->get('test');
135 $cv3->cb(sub {
136 my $ret3 = shift->recv;
137 printf("Got a value: %s\n", $ret3);
138 });
139 });
140 });
141 $cv->recv;
142  
143 .. a promise chain can be used, by chaining calls to the L</then> method:
144  
145 my $promise = AnyEvent::Promise->new(sub {
146 $redis->get('test');
147 })->then(sub {
148 my $val = shift;
149 $redis->set('test', $val);
150 })->then(sub {
151 $redis->get('test');
152 })->then(sub {
153 my $val = shift;
154 printf("Got a value: %s\n", $val)
155 })->fulfill;
156  
157 =cut
158 23 ▶sub then {
Subroutine then: called CC=3 SCAR=11.0
Pod documented
159 23 my ($self, $fn) = @_;
160  
161 return $self
162 23 ▶ if ($self->{rejected});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rejected"} 0 23 23
163  
164 23 $self->{guard}->begin;
165  
166 23 my $cvin = $self->{fulfill};
167 23 ▶ if (!defined $cvin) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cvin) 11 12 23
168 11 $cvin = $self->{initial};
169 }
170  
171 23 my $cvout = AnyEvent->condvar;
172 15 ▶ $cvin->cb(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
173 15 my $thenret = shift;
174 15 ▶ Try::Tiny::try {
Subroutine __ANON__: called CC=3 SCAR=11.0
175 15 my $ret = $thenret->recv;
176 15 my $cvret = $fn->($ret);
177 13 ▶ if ($cvret and ref $cvret eq 'AnyEvent::CondVar') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cvret and ref $cvret eq 'AnyEvent::CondVar') { } 10 3 13
Condition66%2/3
$cvret and ref $cvret eq "AnyEvent::CondVar"
!ll&&!rl&&r
0310
MC/DC50%1/2
$cvret and ref $cvret eq "AnyEvent::CondVar"
$cvret ref $cvret eq "AnyEvent::CondVar"
Truth table66%2/3
A and B
A= $cvretB= ref $cvret eq "AnyEvent::CondVar"
ABresult
0X0
100
111
178 10 ▶ $cvret->cb(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
179 10 my $ret_inner = shift;
180 10 ▶ Try::Tiny::try {
Subroutine __ANON__: called CC=1 SCAR=0.0
181 10 $cvout->send($ret_inner->recv);
182 9 $self->{guard}->end;
183 }
184 1 ▶ Try::Tiny::catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
185 1 $self->{rejected} = 1;
186 1 $self->{reject}->send(@_);
187 }
188 10 });
189 }
190 else {
191 3 $cvout->send($cvret);
192 3 $self->{guard}->end;
193 }
194 }
195 2 ▶ Try::Tiny::catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
196 2 $self->{rejected} = 1;
197 2 $self->{reject}->send(@_);
198 }
199 23 });
200 23 $self->{fulfill} = $cvout;
201  
202 23 return $self;
203 }
204  
205 =head2 catch($cb)
206  
207 Catch raised errors in the callback chain. Exceptions in the promise chain will
208 jump up to this catch callback, bypassing any other callbacks in the promise
209 chain. The error caught by L<Try::Tiny> will be sent as arguments to the
210 callback C<$cb>.
211  
212 =cut
213 3 ▶sub catch {
Subroutine catch: called CC=1 SCAR=0.0
Pod documented
214 3 my ($self, $fn) = @_;
215  
216 3 ▶ $self->{reject}->cb(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
217 3 my @err = shift->recv;
218 3 $fn->(@err);
219 3 $self->{guard}->send;
220 3 });
221  
222 3 return $self;
223 }
224  
225 =head2 condvar(...)
226  
227 Trigger the start of the promise chain and return the guard condvar from the
228 promise. The guard condvar is fulfilled either after the last callback returns
229 or an exception is encountered somewhere in the chain.
230  
231 All arguments passed into L</condvar> are sent to the first condvar in the
232 promise chain.
233  
234 =cut
235 7 ▶sub condvar {
Subroutine condvar: called CC=1 SCAR=0.0
Pod documented
236 7 my $self = shift;
237 7 $self->{initial}->send(@_);
238 4 ▶ $self->{fulfill}->cb(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
239 4 $self->{guard}->send;
240 7 });
241 7 return $self->{guard};
242 };
243  
244 =head2 cv(...)
245  
246 Alias of L</condvar>
247  
248 =cut
249 1 ▶sub cv { condvar(@_) }
Subroutine cv: called CC=1 SCAR=0.0
Pod documented
250  
251 =head2 fulfill(...)
252  
253 Similar to L</condvar>, trigger the start of the promise chain, but C<recv> on
254 the returned condvar as well.
255  
256 =cut
257 6 ▶sub fulfill {
Subroutine fulfill: called CC=1 SCAR=0.0
Pod documented
258 6 my $self = shift;
259 6 my $cv = $self->condvar(@_);
260 6 $cv->recv;
261 }
262  
263 =head1 AUTHOR
264  
265 Anthony Johnson, C<< <aj at ohess.org> >>
266  
267 =head1 LICENSE AND COPYRIGHT
268  
269 Copyright 2013 Anthony Johnson.
270  
271 This program is distributed under the MIT (X11) License:
272 L<http://www.opensource.org/licenses/mit-license.php>
273  
274 Permission is hereby granted, free of charge, to any person
275 obtaining a copy of this software and associated documentation
276 files (the "Software"), to deal in the Software without
277 restriction, including without limitation the rights to use,
278 copy, modify, merge, publish, distribute, sublicense, and/or sell
279 copies of the Software, and to permit persons to whom the
280 Software is furnished to do so, subject to the following
281 conditions:
282  
283 The above copyright notice and this permission notice shall be
284 included in all copies or substantial portions of the Software.
285  
286 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
287 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
288 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
289 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
290 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
291 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
292 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
293 OTHER DEALINGS IN THE SOFTWARE.
294  
295 =cut
296  
297 4 1;
↑ Summary