blib/lib/AnyEvent/XSPromises.pm

filter: Statementstmt 97.5% 40 / 41 Branchbran 66.6% 4 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 89.2% 58 / 65 CC 6 SCAR 7.4
CC
6
Cov
90%
collect
4.1
__ANON__
2.0
__ANON__
2.0
CRAP 2.1
↑ Summary blib/lib/AnyEvent/XSPromises/Loader.pm »
1 package AnyEvent::XSPromises;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use AnyEvent::XSPromises::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our @EXPORT_OK= qw/collect deferred resolved rejected/;
11  
12 29 ▶sub resolved {
Subroutine resolved: called CC=1 SCAR=0.0
Pod documented
13 29 my $d= deferred;
14 29 $d->resolve(@_);
15 29 return $d->promise;
16 }
17  
18 7 ▶sub rejected {
Subroutine rejected: called CC=1 SCAR=0.0
Pod documented
19 7 my $d= deferred;
20 7 $d->reject(@_);
21 7 return $d->promise;
22 }
23  
24 # XXX This is pure-perl, not XS like we promise our users.
25 6 ▶sub collect {
Subroutine collect: called CC=4 SCAR=14.1
Pod documented
26 6 my $remaining= 0+@_;
27 6 my @values;
28 6 my $failed= 0;
29 6 my $then_what= deferred;
30 6 my $pending= 1;
31 6 my $i= 0;
32 6 for my $p (@_) {
33 54 my $i= $i++;
34 53 ▶ $p->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
35 53 $values[$i]= [@_];
36 53 ▶ if ((--$remaining) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$remaining == 0) 5 48 53
37 5 $pending= 0;
38 5 $then_what->resolve(@values);
39 }
40 1 ▶ }, sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
41 1 ▶ if (!$failed++) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $failed++) 1 0 1
42 1 $pending= 0;
43 1 $then_what->reject(@_);
44 }
45 54 });
46 }
47 6 ▶ if (!$remaining && $pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $remaining and $pending) 0 6 6
Condition33%1/3
not $remaining and $pending
!ll&&!rl&&r
600
MC/DC0%0/2
not $remaining and $pending
not $remaining $pending
Truth table33%1/3
not(A) and B
A= not $remainingB= $pending
ABresult
0X0
100
111
48 0 $then_what->resolve(@values);
49 }
50 6 return $then_what->promise;
51 }
52  
53 2 1;
54  
55 =head1 NAME
56  
57 AnyEvent::XSPromises - Another Promises library, this time implemented in XS for performance
58  
59 =head1 SYNOPSIS
60  
61 use AnyEvent::XSPromises qw/deferred/;
62 use AnyEvent::YACurl;
63  
64 sub do_request {
65 my $request_args= @_;
66  
67 my $deferred= deferred;
68 AnyEvent::YACurl->new({})->request(
69 sub {
70 my ($response, $error)= @_;
71 if ($error) { $deferred->reject($error); return; }
72 $deferred->resolve($response);
73 },
74 $request_args
75 );
76  
77 return $deferred->promise;
78 }
79  
80 =head1 DESCRIPTION
81  
82 This library provides a Promises interface, written in XS for performance, conforming to the Promises/A+ specification.
83  
84 Performance may not immediately seem important, but when promises are used as the building block for sending thousands
85 of database queries per second from a single Perl process, those extra microseconds suddenly start to matter.
86  
87 =head1 API
88  
89 =head2 AnyEvent::XSPromises
90  
91 =over
92  
93 =item deferred()
94  
95 C<deferred> is the main entry point for using promises. This function will return a Deferred Object that must be
96 resolved or rejected after some event completes.
97  
98 sub get_perl {
99 my $d= deferred;
100 http_get("https://perl.org", sub {
101 $d->resolve(@_);
102 });
103 return $d->promise;
104 }
105  
106 =item collect(...)
107  
108 C<collect> makes a promise out of a collection of other promises (thenables). If all inputs get resolved, the promise will
109 be resolved with the outputs of each. If any input gets rejected, the promise will be rejected with its reason.
110  
111 Because of how context (array vs scalar) works in Perl, all outputs are wrapped in an arrayref.
112  
113 collect(
114 resolved(1),
115 resolved(2)
116 )->then(sub {
117 # @_ is now ( [1], [2] )
118 })
119  
120 =item resolved(...)
121  
122 Shortcut for creating a promise that has been resolved with the given inputs
123  
124 resolved(5)->then(sub {
125 my $five= shift;
126 })
127  
128 =item rejected(...)
129  
130 Shortcut for creating a promise that has been rejected with the given inputs. See C<resolved>
131  
132 =back
133  
134 =head2 Deferred objects
135  
136 =over
137  
138 =item $d->promise()
139  
140 Gets a thenable promise associated to the Deferred object.
141  
142 my $d= deferred;
143 ...
144 return $d->promise;
145  
146 =item $d->resolve(...)
147  
148 Resolves the deferred object (assigns a value). All associated promises will have their callback invoked in the next event
149 loop iteration.
150  
151 =item $d->reject(...)
152  
153 Rejects the deferred object (assigns a reason for why it failed). All associated promises will have their callback invoked
154 in the next event loop iteration.
155  
156 =item $d->is_in_progress()
157  
158 Returns true iff the C<reject> or C<resolve> method has not been called yet. Useful for racing multiple code paths to
159 resolve/reject a single deferred object, like one would do to build a timeout.
160  
161 sub get_with_timeout {
162 my $d= deferred;
163 my $timer; $timer= AE::timer 1, 0, sub {
164 undef $timer;
165 $d->reject("Timed out") if $d->is_in_progress;
166 };
167  
168 http_get("https://perl.org", sub {
169 my $result= shift
170 $d->resolve($result) if $d->is_in_progress;
171 });
172  
173 This method is intentionally not available on promise objects.
174  
175 =back
176  
177 =head2 Promise objects
178  
179 =over
180  
181 =item $p->then($on_resolve, $on_reject)
182  
183 Registers the given C<on_resolve> and/or C<on_reject> callback on the promise, and returns a new promise.
184  
185 =item $p->catch($on_reject)
186  
187 Similar to C<then>, but only takes C<on_reject>.
188  
189 =item $p->finally($on_finally)
190  
191 Register a callback on the promise that will be invoked once it completes. The callback is quietly executed but cannot
192 change the output or status of the promise. Returns a promise that will be resolved/rejected based on the original promise.
193  
194 =back
195  
196 =head1 COMPARISON TO OTHER PROMISES LIBRARIES
197  
198 =over
199  
200 =item Promises
201  
202 L<Promises> is a pure-Perl Promises implementation that allows selecting one of multiple event loop backends. However,
203 this backend is defined globally and the documentation suggests that it would be best if only the application developer
204 picks a backend. This means that libraries cannot know up front which event loop backend they have to use, and they need
205 to support all event loops or the library would break if a different event loop is chosen. This has lead library authors
206 to mandate that the selected backend is AnyEvent, defying the purpose of backend selection other than for usage in
207 scripts that do not need compatibility with other code such as libraries from CPAN.
208  
209 The library also trades performance and resilience for a few features that are not needed to implement the Promises/A+
210 specification.
211  
212 Promises from this library are compatible with ours if the backend is set to C<AE> or C<AnyEvent>.
213  
214 =item AnyEvent::Promises
215  
216 L<AnyEvent::Promises> is another pure-Perl Promises implementation. It is a lot simpler than L<Promises>, but comes with
217 performance implications, and has not been very hardened against developer error. Since it is also based on AnyEvent,
218 and comes with an identical C<< then($on_resolve, $on_reject) >> API, its promises are fully compatible with ours and
219 can be freely passed around between the two libraries if necessary.
220  
221 =back
222  
223 =head1 AUTHOR
224  
225 Tom van der Woerdt <tvdw@cpan.org>
↑ Summary blib/lib/AnyEvent/XSPromises/Loader.pm »