blib/lib/Venus/Future.pm

filter: Statementstmt 85.9% 226 / 263 Branchbran 63.5% 103 / 162 Conditioncond 47.6% 41 / 86 MC/DCmcdc 23.2% 13 / 56 Subroutinesub 97.4% 38 / 39 Podpod 54.8% 17 / 31 totaltotal 68.7% 438 / 637 SCAR 59.4
CC
113
Cov
72%
reject
55.9
resolve
21.7
finally
16.0
CRAP 381.3
« blib/lib/Venus/Cli.pm ↑ Summary blib/lib/Venus/Container.pm »
1 package Venus::Future;
2  
3 1 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Venus::Class 'attr', 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 base 'Venus::Kind::Utility';
11  
12 1 with 'Venus::Role::Buildable';
13  
14 # STATE
15  
16 1 state $FULFILLED = 'fulfilled';
17 1 state $PENDING = 'pending';
18 1 state $REJECTED = 'rejected';
19  
20 # HOOKS
21  
22 sub _time {
23 1 ▶ CORE::time();
Subroutine _time: called CC=1 SCAR=0.0
24 }
25  
26 # BUILDERS
27  
28 sub build_arg {
29 1 ▶ my ($self, $data) = @_;
Subroutine build_arg: called CC=1 SCAR=0.0
Pod undocumented
30  
31 return {
32 1 promise => $data,
33 };
34 }
35  
36 sub build_self {
37 83 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=3 SCAR=11.0
Pod undocumented
38  
39 83 $self->{status} = $PENDING;
40  
41 83 ▶ return $self if !exists $data->{promise};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{"promise"} 81 2 83
42  
43 2 $self->promise($data->{promise});
44  
45 2 my $tryable = $self->try('fulfill')->error;
46  
47 2 my $result = $tryable->result;
48  
49 2 ▶ $self->reject($result) if UNIVERSAL::isa($result, 'Venus::Error');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($result, "Venus::Error") 0 2 2
50  
51 2 return $self;
52 }
53  
54 # METHODS
55  
56 sub catch {
57 2 ▶ my ($self, $on_reject) = @_;
Subroutine catch: called CC=1 SCAR=0.0
Pod documented
58  
59 2 return $self->then(undef, $on_reject);
60 }
61  
62 sub finally {
63 3 ▶ my ($self, $on_finally) = @_;
Subroutine finally: called CC=8 SCAR=27.7
Pod documented
64  
65 3 my $future = $self->class->new;
66  
67 3 ▶ my $is_rejectable = my $is_resolvable = ref $on_finally eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $on_finally eq 'CODE' ? : 3 0 3
68 ? true : false;
69  
70 3 ▶ if ($is_resolvable && $self->is_fulfilled) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_resolvable and $self->is_fulfilled) { } 0 3 3
elsif ($is_rejectable and $self->is_rejected) { } 0 3 3
Condition33%1/3
$is_resolvable and $self->is_fulfilled
!ll&&!rl&&r
030
MC/DC0%0/2
$is_resolvable and $self->is_fulfilled
$is_resolvable $self->is_fulfilled
Truth table33%1/3
A and B
A= $is_resolvableB= $self->is_fulfilled
ABresult
0X0
100
111
71 0 local $_ = $self->{value};
72 0 $on_finally->($self->{value});
73 }
74 ▶ elsif ($is_rejectable && $self->is_rejected) {
Condition33%1/3
$is_rejectable and $self->is_rejected
!ll&&!rl&&r
030
MC/DC0%0/2
$is_rejectable and $self->is_rejected
$is_rejectable $self->is_rejected
Truth table33%1/3
A and B
A= $is_rejectableB= $self->is_rejected
ABresult
0X0
100
111
75 0 local $_ = $self->{issue};
76 0 $on_finally->($self->{issue});
77 }
78 else {
79 3 ▶ if ($is_resolvable && $is_rejectable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_resolvable and $is_rejectable) 3 0 3
Condition33%1/3
$is_resolvable and $is_rejectable
!ll&&!rl&&r
003
MC/DC0%0/2
$is_resolvable and $is_rejectable
$is_resolvable $is_rejectable
Truth table33%1/3
A and B
A= $is_resolvableB= $is_rejectable
ABresult
0X0
100
111
80 3 $self->on_finally($self->then_finally($future, $on_finally));
81 }
82 }
83  
84 3 return $future;
85 }
86  
87 sub fulfill {
88 14 ▶ my ($self) = @_;
Subroutine fulfill: called CC=7 SCAR=19.6
Pod documented
89  
90 14 ▶ return true if !$self->is_pending;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_pending 0 14 14
91  
92 14 my $is_suspended = $self->is_suspended;
93  
94 14 ▶ $self->resume if $is_suspended;
Branch: 1 branch
BranchTrueFalseTotal
if $is_suspended 1 13 14
95  
96 14 ▶ return $self->is_fulfilled ? true : false if $is_suspended;
Branch: 2 branches
BranchTrueFalseTotal
if $is_suspended 1 13 14
$self->is_fulfilled ? : 1 0 1
97  
98 13 ▶ return false if !$self->{promise};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"promise"} 0 13 13
99  
100 13 my $rejectable = $self->try('reject');
101  
102 13 my $resolvable = $self->try('resolve');
103  
104 13 $self->{promise}->($resolvable, $rejectable);
105  
106 13 ▶ return $self->is_pending ? false : true;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_pending ? : 1 12 13
107 }
108  
109 sub is {
110 3 ▶ my ($self, $name) = @_;
Subroutine is: called CC=3 SCAR=11.7
Pod documented
111  
112 3 my $method = "is_$name";
113  
114 3 ▶ return $self->can($method) ? ($self->$method ? true : false) : false;
Branch: 2 branches
BranchTrueFalseTotal
$self->can($method) ? : 3 0 3
$self->$method ? : 3 0 3
115 }
116  
117 sub is_fulfilled {
118 42 ▶ my ($self) = @_;
Subroutine is_fulfilled: called CC=3 SCAR=11.0
Pod documented
119  
120 42 my $status = $self->{status};
121  
122 42 ▶ return ($status && $status eq $FULFILLED) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$status && $status eq $FULFILLED ? : 15 27 42
Condition66%2/3
$status and $status eq $FULFILLED
!ll&&!rl&&r
02715
MC/DC50%1/2
$status and $status eq $FULFILLED
$status $status eq $FULFILLED
Truth table66%2/3
A and B
A= $statusB= $status eq $FULFILLED
ABresult
0X0
100
111
123 }
124  
125 sub is_pending {
126 171 ▶ my ($self) = @_;
Subroutine is_pending: called CC=3 SCAR=11.0
Pod documented
127  
128 171 my $status = $self->{status};
129  
130 171 ▶ return ($status && $status eq $PENDING) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$status && $status eq $PENDING ? : 123 48 171
Condition66%2/3
$status and $status eq $PENDING
!ll&&!rl&&r
048123
MC/DC50%1/2
$status and $status eq $PENDING
$status $status eq $PENDING
Truth table66%2/3
A and B
A= $statusB= $status eq $PENDING
ABresult
0X0
100
111
131 }
132  
133 sub is_promised {
134 3 ▶ my ($self) = @_;
Subroutine is_promised: called CC=2 SCAR=6.9
Pod documented
135  
136 3 ▶ return exists $self->{promise} ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'promise'} ? : 1 2 3
137 }
138  
139 sub is_rejected {
140 20 ▶ my ($self) = @_;
Subroutine is_rejected: called CC=3 SCAR=11.0
Pod documented
141  
142 20 my $status = $self->{status};
143  
144 20 ▶ return ($status && $status eq $REJECTED) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$status && $status eq $REJECTED ? : 7 13 20
Condition66%2/3
$status and $status eq $REJECTED
!ll&&!rl&&r
0137
MC/DC50%1/2
$status and $status eq $REJECTED
$status $status eq $REJECTED
Truth table66%2/3
A and B
A= $statusB= $status eq $REJECTED
ABresult
0X0
100
111
145 }
146  
147 sub is_resuming {
148 5 ▶ my ($self) = @_;
Subroutine is_resuming: called CC=2 SCAR=6.9
Pod undocumented
149  
150 5 ▶ return exists $self->{resuming} ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'resuming'} ? : 2 3 5
151 }
152  
153 sub is_suspended {
154 124 ▶ my ($self) = @_;
Subroutine is_suspended: called CC=9 SCAR=23.0
Pod undocumented
155  
156 124 ▶ my $has_resume = exists $self->{resume} ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'resume'} ? : 6 118 124
157  
158 124 ▶ return false if !$has_resume;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_resume 118 6 124
159  
160 6 ▶ my $has_method = defined $self->{resume}->{method} ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'resume'}{'method'} ? : 6 0 6
161  
162 6 ▶ return false if !$has_method;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_method 0 6 6
163  
164 6 ▶ my $has_position = defined $self->{resume}->{position} ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'resume'}{'position'} ? : 6 0 6
165  
166 6 ▶ return false if !$has_position;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_position 0 6 6
167  
168 6 ▶ my $has_value = exists $self->{resume}->{value} ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'resume'}{'value'} ? : 6 0 6
169  
170 6 ▶ return false if !$has_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_value 0 6 6
171  
172 6 return true;
173 }
174  
175 sub is_thenable {
176 41 ▶ my ($self, $value) = @_;
Subroutine is_thenable: called CC=6 SCAR=18.0
Pod undocumented
177  
178 41 ▶ $value = $self if @_ < 2 ;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 41 41
179  
180 41 require Scalar::Util;
181  
182 41 my $blessed = Scalar::Util::blessed($value);
183  
184 41 ▶ return false if !$blessed;
Branch: 1 branch
BranchTrueFalseTotal
unless $blessed 15 26 41
185  
186 26 ▶ my $is_future = $value->isa('Venus::Future') ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$value->isa('Venus::Future') ? : 25 1 26
187  
188 26 ▶ return true if $is_future;
Branch: 1 branch
BranchTrueFalseTotal
if $is_future 25 1 26
189  
190 1 ▶ return $value->can('then') ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$value->can('then') ? : 1 0 1
191 }
192  
193 sub issue {
194 11 ▶ my ($self) = @_;
Subroutine issue: called CC=1 SCAR=0.0
Pod documented
195  
196 11 return $self->{issue};
197 }
198  
199 sub on {
200 0 ▶ my ($self, @args) = @_;
Subroutine on: not called CC=3 SCAR=24.8
Pod undocumented
201  
202 0 require Venus;
203 0 for my $pair (Venus::pairs(Venus::hashref(@args))) {
204 0 my ($name, $callback) = Venus::list($pair);
205 0 my $method = "on_$name";
206 0 ▶ $self->$method($callback) if $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method) 0 0 0
207 }
208  
209 0 return $self;
210 }
211  
212 sub on_finally {
213 3 ▶ my ($self, $code) = @_;
Subroutine on_finally: called CC=2 SCAR=7.0
Pod undocumented
214  
215 3 ▶ push @{$self->{on_finally}}, $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 3 0 3
216  
217 3 return $self;
218 }
219  
220 sub on_fulfilled {
221 19 ▶ my ($self, $code) = @_;
Subroutine on_fulfilled: called CC=2 SCAR=7.0
Pod undocumented
222  
223 19 ▶ push @{$self->{on_fulfilled}}, $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 19 0 19
224  
225 19 return $self;
226 }
227  
228 sub on_rejected {
229 7 ▶ my ($self, $code) = @_;
Subroutine on_rejected: called CC=2 SCAR=7.0
Pod undocumented
230  
231 7 ▶ push @{$self->{on_rejected}}, $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 7 0 7
232  
233 7 return $self;
234 }
235  
236 sub promise {
237 16 ▶ my ($self, $code) = @_;
Subroutine promise: called CC=3 SCAR=11.0
Pod documented
238  
239 16 ▶ $self->{promise} ||= $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 15 1 16
Condition66%2/3
$self->{"promise"} ||= $code
l!l&&r!l&&!r
2130
MC/DC0%0/2
$self->{"promise"} ||= $code
$self->{"promise"} $code
Truth table66%2/3
A ||= B
A= $self->{"promise"}B= $code
ABresult
1X1
011
000
240  
241 16 return $self;
242 }
243  
244 sub reject {
245 16 ▶ my ($self, $issue) = @_;
Subroutine reject: called CC=19 SCAR=40.2
Pod documented
246  
247 16 ▶ return $self if !$self->is_pending;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_pending 0 16 16
248  
249 16 my $position = 0;
250  
251 16 ▶ if ($self->is_suspended) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_suspended) 0 16 16
252 0 my $future = $self->{resume}->{future};
253 0 my $is_thenable = $self->is_thenable($future);
254 0 my $is_future = $future->isa('Venus::Future');
255  
256 0 ▶ return $self if $is_future && $future->is_pending;
Branch: 1 branch
BranchTrueFalseTotal
if $is_future and $future->is_pending 0 0 0
Condition0%0/3
$is_future and $future->is_pending
!ll&&!rl&&r
000
MC/DC0%0/2
$is_future and $future->is_pending
$is_future $future->is_pending
Truth table0%0/3
A and B
A= $is_futureB= $future->is_pending
ABresult
0X0
100
111
257 0 ▶ return $self if $is_future && $future->is_fulfilled;
Branch: 1 branch
BranchTrueFalseTotal
if $is_future and $future->is_fulfilled 0 0 0
Condition0%0/3
$is_future and $future->is_fulfilled
!ll&&!rl&&r
000
MC/DC0%0/2
$is_future and $future->is_fulfilled
$is_future $future->is_fulfilled
Truth table0%0/3
A and B
A= $is_futureB= $future->is_fulfilled
ABresult
0X0
100
111
258  
259 $position
260 = ($is_future && $future->is_fulfilled)
261 ? ($self->{resume}->{position} + 1)
262 0 ▶ : $self->{resume}->{position};
Branch: 1 branch
BranchTrueFalseTotal
$is_future && $future->is_fulfilled ? : 0 0 0
Condition0%0/3
$is_future and $future->is_fulfilled
!ll&&!rl&&r
000
MC/DC0%0/2
$is_future and $future->is_fulfilled
$is_future $future->is_fulfilled
Truth table0%0/3
A and B
A= $is_futureB= $future->is_fulfilled
ABresult
0X0
100
111
263 }
264  
265 16 ▶ my $on_finally = $self->{on_finally} || [];
Condition100%2/2
$self->{'on_finally'} || []
l!l
115
MC/DC100%1/1
$self->{'on_finally'} || []
$self->{'on_finally'}
Truth table100%2/2
A
A= $self->{'on_finally'}
Aresult
11
00
266  
267 16 ▶ my $on_rejected = $self->{on_rejected} || [];
Condition100%2/2
$self->{'on_rejected'} || []
l!l
115
MC/DC100%1/1
$self->{'on_rejected'} || []
$self->{'on_rejected'}
Truth table100%2/2
A
A= $self->{'on_rejected'}
Aresult
11
00
268  
269 16 my @callbacks = (@{$on_rejected}, @{$on_finally});
270  
271 16 ▶ for (my $i = $position; $i < @callbacks; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @callbacks) 2 16 18
272 2 my $callback = $callbacks[$i];
273 2 local $_ = $issue;
274 2 my $future = $callback->($issue);
275 2 ▶ my $is_future = $self->is_thenable($future)
Branch: 1 branch
BranchTrueFalseTotal
$self->is_thenable($future) && $future->isa('Venus::Future') ? : 2 0 2
Condition33%1/3
$self->is_thenable($future) and $future->isa("Venus::Future")
!ll&&!rl&&r
002
MC/DC0%0/2
$self->is_thenable($future) and $future->isa("Venus::Future")
$self->is_thenable($future) $future->isa("Venus::Future")
Truth table33%1/3
A and B
A= $self->is_thenable($future)B= $future->isa("Venus::Future")
ABresult
0X0
100
111
276 && $future->isa('Venus::Future') ? true : false;
277 2 ▶ if ($is_future && $future->is_pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_future and $future->is_pending) 0 2 2
Condition33%1/3
$is_future and $future->is_pending
!ll&&!rl&&r
020
MC/DC0%0/2
$is_future and $future->is_pending
$is_future $future->is_pending
Truth table33%1/3
A and B
A= $is_futureB= $future->is_pending
ABresult
0X0
100
111
278 0 $future->resume;
279 }
280 2 ▶ if ($is_future && $future->is_pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_future and $future->is_pending) 0 2 2
Condition33%1/3
$is_future and $future->is_pending
!ll&&!rl&&r
020
MC/DC0%0/2
$is_future and $future->is_pending
$is_future $future->is_pending
Truth table33%1/3
A and B
A= $is_futureB= $future->is_pending
ABresult
0X0
100
111
281 0 $self->suspend('reject', $i, $issue, $future);
282 0 last;
283 }
284 }
285  
286 16 ▶ return $self if $self->is_suspended;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_suspended 0 16 16
287  
288 16 delete $self->{on_finally};
289 16 delete $self->{on_fulfilled};
290 16 delete $self->{on_rejected};
291 16 delete $self->{promise};
292 16 delete $self->{resume};
293 16 delete $self->{resuming};
294  
295 16 $self->{status} = $REJECTED;
296  
297 16 $self->{issue} = $issue;
298  
299 16 return $self;
300 }
301  
302 sub resolve {
303 39 ▶ my ($self, $value) = @_;
Subroutine resolve: called CC=19 SCAR=30.8
Pod documented
304  
305 39 ▶ return $self if !$self->is_pending;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_pending 0 39 39
306  
307 39 my $position = 0;
308  
309 39 ▶ if ($self->is_suspended) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_suspended) 1 38 39
310 1 my $future = $self->{resume}->{future};
311 1 my $is_thenable = $self->is_thenable($future);
312 1 my $is_future = $future->isa('Venus::Future');
313  
314 1 ▶ return $self if $is_future && $future->is_pending;
Branch: 1 branch
BranchTrueFalseTotal
if $is_future and $future->is_pending 0 1 1
Condition33%1/3
$is_future and $future->is_pending
!ll&&!rl&&r
010
MC/DC0%0/2
$is_future and $future->is_pending
$is_future $future->is_pending
Truth table33%1/3
A and B
A= $is_futureB= $future->is_pending
ABresult
0X0
100
111
315 1 ▶ return $self if $is_future && $future->is_rejected;
Branch: 1 branch
BranchTrueFalseTotal
if $is_future and $future->is_rejected 0 1 1
Condition33%1/3
$is_future and $future->is_rejected
!ll&&!rl&&r
010
MC/DC0%0/2
$is_future and $future->is_rejected
$is_future $future->is_rejected
Truth table33%1/3
A and B
A= $is_futureB= $future->is_rejected
ABresult
0X0
100
111
316  
317 $position
318 = ($is_future && $future->is_fulfilled)
319 ? ($self->{resume}->{position} + 1)
320 1 ▶ : $self->{resume}->{position};
Branch: 1 branch
BranchTrueFalseTotal
$is_future && $future->is_fulfilled ? : 1 0 1
Condition33%1/3
$is_future and $future->is_fulfilled
!ll&&!rl&&r
001
MC/DC0%0/2
$is_future and $future->is_fulfilled
$is_future $future->is_fulfilled
Truth table33%1/3
A and B
A= $is_futureB= $future->is_fulfilled
ABresult
0X0
100
111
321 }
322  
323 39 ▶ my $on_finally = $self->{on_finally} || [];
Condition100%2/2
$self->{'on_finally'} || []
l!l
138
MC/DC100%1/1
$self->{'on_finally'} || []
$self->{'on_finally'}
Truth table100%2/2
A
A= $self->{'on_finally'}
Aresult
11
00
324  
325 39 ▶ my $on_fulfilled = $self->{on_fulfilled} || [];
Condition100%2/2
$self->{'on_fulfilled'} || []
l!l
1623
MC/DC100%1/1
$self->{'on_fulfilled'} || []
$self->{'on_fulfilled'}
Truth table100%2/2
A
A= $self->{'on_fulfilled'}
Aresult
11
00
326  
327 39 my @callbacks = (@{$on_fulfilled}, @{$on_finally});
328  
329 39 ▶ for (my $i = $position; $i < @callbacks; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @callbacks) 18 36 54
330 18 my $callback = $callbacks[$i];
331 18 local $_ = $value;
332 18 my $future = $callback->($value);
333 18 ▶ my $is_future = $self->is_thenable($future)
Branch: 1 branch
BranchTrueFalseTotal
$self->is_thenable($future) && $future->isa('Venus::Future') ? : 18 0 18
Condition33%1/3
$self->is_thenable($future) and $future->isa("Venus::Future")
!ll&&!rl&&r
0018
MC/DC0%0/2
$self->is_thenable($future) and $future->isa("Venus::Future")
$self->is_thenable($future) $future->isa("Venus::Future")
Truth table33%1/3
A and B
A= $self->is_thenable($future)B= $future->isa("Venus::Future")
ABresult
0X0
100
111
334 && $future->isa('Venus::Future') ? true : false;
335 18 ▶ if ($is_future && $future->is_pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_future and $future->is_pending) 4 14 18
Condition66%2/3
$is_future and $future->is_pending
!ll&&!rl&&r
0144
MC/DC50%1/2
$is_future and $future->is_pending
$is_future $future->is_pending
Truth table66%2/3
A and B
A= $is_futureB= $future->is_pending
ABresult
0X0
100
111
336 4 $future->resume;
337 }
338 18 ▶ if ($is_future && $future->is_pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_future and $future->is_pending) 3 15 18
Condition66%2/3
$is_future and $future->is_pending
!ll&&!rl&&r
0153
MC/DC50%1/2
$is_future and $future->is_pending
$is_future $future->is_pending
Truth table66%2/3
A and B
A= $is_futureB= $future->is_pending
ABresult
0X0
100
111
339 3 $self->suspend('resolve', $i, $value, $future);
340 3 last;
341 }
342 }
343  
344 39 ▶ return $self if $self->is_suspended;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_suspended 4 35 39
345  
346 35 delete $self->{on_finally};
347 35 delete $self->{on_fulfilled};
348 35 delete $self->{on_rejected};
349 35 delete $self->{promise};
350 35 delete $self->{resume};
351 35 delete $self->{resuming};
352  
353 35 $self->{status} = $FULFILLED;
354  
355 35 $self->{value} = $value;
356  
357 35 return $self;
358 }
359  
360 sub resume {
361 5 ▶ my ($self) = @_;
Subroutine resume: called CC=2 SCAR=6.9
Pod undocumented
362  
363 5 ▶ my $resume = delete $self->{resume} or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $resume = delete $self->{"resume"} or return $self 2 3 5
364  
365 3 $self->{resuming} = true;
366  
367 3 my $method = $resume->{method};
368  
369 3 my $value = $resume->{value};
370  
371 3 return $self->$method($value);
372 }
373  
374 sub status {
375 7 ▶ my ($self) = @_;
Subroutine status: called CC=1 SCAR=0.0
Pod documented
376  
377 7 return $self->{status};
378 }
379  
380 sub suspend {
381 3 ▶ my ($self, $method, $position, $value, $future) = @_;
Subroutine suspend: called CC=1 SCAR=0.0
Pod undocumented
382  
383 3 delete $self->{resuming};
384  
385 $self->{resume} = {
386 3 method => $method,
387 position => $position,
388 value => $value,
389 future => $future,
390 };
391  
392 3 return $self;
393 }
394  
395 sub then {
396 23 ▶ my ($self, $on_fulfill, $on_reject) = @_;
Subroutine then: called CC=11 SCAR=25.8
Pod documented
397  
398 23 my $future = $self->class->new;
399  
400 23 ▶ my $is_rejectable = ref $on_reject eq 'CODE' ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
ref $on_reject eq 'CODE' ? : 7 16 23
401 23 ▶ my $is_resolvable = ref $on_fulfill eq 'CODE' ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
ref $on_fulfill eq 'CODE' ? : 19 4 23
402  
403 23 ▶ if ($is_resolvable && $self->is_fulfilled) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_resolvable and $self->is_fulfilled) { } 0 23 23
elsif ($is_rejectable and $self->is_rejected) { } 0 23 23
Condition66%2/3
$is_resolvable and $self->is_fulfilled
!ll&&!rl&&r
4190
MC/DC0%0/2
$is_resolvable and $self->is_fulfilled
$is_resolvable $self->is_fulfilled
Truth table66%2/3
A and B
A= $is_resolvableB= $self->is_fulfilled
ABresult
0X0
100
111
404 0 local $_ = $self->{value};
405 0 $on_fulfill->($self->{value});
406 }
407 ▶ elsif ($is_rejectable && $self->is_rejected) {
Condition66%2/3
$is_rejectable and $self->is_rejected
!ll&&!rl&&r
1670
MC/DC0%0/2
$is_rejectable and $self->is_rejected
$is_rejectable $self->is_rejected
Truth table66%2/3
A and B
A= $is_rejectableB= $self->is_rejected
ABresult
0X0
100
111
408 0 local $_ = $self->{issue};
409 0 $on_reject->($self->{issue});
410 }
411 else {
412 23 ▶ if ($is_resolvable && $self->is_pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_resolvable and $self->is_pending) 19 4 23
Condition66%2/3
$is_resolvable and $self->is_pending
!ll&&!rl&&r
4019
MC/DC50%1/2
$is_resolvable and $self->is_pending
$is_resolvable $self->is_pending
Truth table66%2/3
A and B
A= $is_resolvableB= $self->is_pending
ABresult
0X0
100
111
413 19 $self->on_fulfilled($self->then_fulfill($future, $on_fulfill));
414 }
415 23 ▶ if ($is_rejectable && $self->is_pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_rejectable and $self->is_pending) 7 16 23
Condition66%2/3
$is_rejectable and $self->is_pending
!ll&&!rl&&r
1607
MC/DC50%1/2
$is_rejectable and $self->is_pending
$is_rejectable $self->is_pending
Truth table66%2/3
A and B
A= $is_rejectableB= $self->is_pending
ABresult
0X0
100
111
416 7 $self->on_rejected($self->then_reject($future, $on_reject));
417 }
418 }
419  
420 23 return $future;
421 }
422  
423 sub then_finally {
424 3 ▶ my ($self, $future, $on_finally) = @_;
Subroutine then_finally: called CC=1 SCAR=1.8
Pod undocumented
425  
426 return sub {
427 2 ▶ my ($value) = @_;
Subroutine __ANON__: called CC=6 SCAR=27.3
428  
429 2 ▶ if (ref $on_finally eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $on_finally eq 'CODE') { } 2 0 2
430 2 local $_ = $value;
431 2 my $result = $on_finally->($value);
432 2 ▶ if ($self->is_thenable($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_thenable($result)) { } 0 2 2
433 0 ▶ if ($self->is_resuming) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_resuming) { } 0 0 0
elsif ($result->isa('Venus::Future') and $result->is_fulfilled) { } 0 0 0
434 0 return $future;
435 }
436 ▶ elsif ($result->isa('Venus::Future') && $result->is_fulfilled) {
Condition0%0/3
$result->isa("Venus::Future") and $result->is_fulfilled
!ll&&!rl&&r
000
MC/DC0%0/2
$result->isa("Venus::Future") and $result->is_fulfilled
$result->isa("Venus::Future") $result->is_fulfilled
Truth table0%0/3
A and B
A= $result->isa("Venus::Future")B= $result->is_fulfilled
ABresult
0X0
100
111
437 0 return $result;
438 }
439 else {
440 0 return $result->then($future->defer('resolve'), $future->defer('reject'));
441 }
442 }
443 else {
444 2 return $future->resolve($result);
445 }
446 }
447 else {
448 0 return $future->resolve($value);
449 }
450 3 };
451 }
452  
453 sub then_fulfill {
454 19 ▶ my ($self, $future, $on_fulfill) = @_;
Subroutine then_fulfill: called CC=1 SCAR=0.0
Pod undocumented
455  
456 return sub {
457 17 ▶ my ($value) = @_;
Subroutine __ANON__: called CC=6 SCAR=18.0
458  
459 17 ▶ if (ref $on_fulfill eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $on_fulfill eq 'CODE') { } 17 0 17
460 17 local $_ = $value;
461 17 my $result = $on_fulfill->($value);
462 17 ▶ if ($self->is_thenable($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_thenable($result)) { } 5 12 17
463 5 ▶ if ($self->is_resuming) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_resuming) { } 2 3 5
elsif ($result->isa('Venus::Future') and $result->is_fulfilled) { } 1 2 3
464 2 return $future;
465 }
466 ▶ elsif ($result->isa('Venus::Future') && $result->is_fulfilled) {
Condition100%3/3
$result->isa("Venus::Future") and $result->is_fulfilled
!ll&&!rl&&r
111
MC/DC100%2/2
$result->isa("Venus::Future") and $result->is_fulfilled
$result->isa("Venus::Future") $result->is_fulfilled
Truth table100%3/3
A and B
A= $result->isa("Venus::Future")B= $result->is_fulfilled
ABresult
0X0
100
111
467 1 return $result;
468 }
469 else {
470 2 return $result->then($future->defer('resolve'), $future->defer('reject'));
471 }
472 }
473 else {
474 12 return $future->resolve($result);
475 }
476 }
477 else {
478 0 return $future->resolve($value);
479 }
480 19 };
481 }
482  
483 sub then_reject {
484 7 ▶ my ($self, $future, $on_reject) = @_;
Subroutine then_reject: called CC=1 SCAR=1.8
Pod undocumented
485  
486 return sub {
487 1 ▶ my ($issue) = @_;
Subroutine __ANON__: called CC=6 SCAR=27.3
488  
489 1 ▶ if (ref $on_reject eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $on_reject eq 'CODE') { } 1 0 1
490 1 local $_ = $issue;
491 1 my $result = $on_reject->($issue);
492 1 ▶ if ($self->is_thenable($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_thenable($result)) { } 0 1 1
493 0 ▶ if ($self->is_resuming) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_resuming) { } 0 0 0
elsif ($result->isa('Venus::Future') and $result->is_fulfilled) { } 0 0 0
494 0 return $future;
495 }
496 ▶ elsif ($result->isa('Venus::Future') && $result->is_fulfilled) {
Condition0%0/3
$result->isa("Venus::Future") and $result->is_fulfilled
!ll&&!rl&&r
000
MC/DC0%0/2
$result->isa("Venus::Future") and $result->is_fulfilled
$result->isa("Venus::Future") $result->is_fulfilled
Truth table0%0/3
A and B
A= $result->isa("Venus::Future")B= $result->is_fulfilled
ABresult
0X0
100
111
497 0 return $result;
498 }
499 else {
500 0 return $result->then($future->defer('resolve'), $future->defer('reject'));
501 }
502 }
503 else {
504 1 return $future->reject($result);
505 }
506 }
507 else {
508 0 return $future->reject($issue);
509 }
510 7 };
511 }
512  
513 sub value {
514 14 ▶ my ($self) = @_;
Subroutine value: called CC=1 SCAR=0.0
Pod documented
515  
516 14 return $self->{value};
517 }
518  
519 sub wait {
520 2 ▶ my ($self, $timeout) = @_;
Subroutine wait: called CC=6 SCAR=19.1
Pod documented
521  
522 2 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) { } 2 0 2
523 2 my $seen = 0;
524 2 my $time = _time();
525 2 my $then = $time + $timeout;
526 2 ▶ while (time <= $then) {
Branch: 1 branch
BranchTrueFalseTotal
if (time <= $then) 1 1 2
527 1 ▶ last if $seen = $self->fulfill;
Branch: 1 branch
BranchTrueFalseTotal
if $seen = $self->fulfill 1 0 1
528 }
529 2 ▶ if (!$seen) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen) 1 1 2
530 1 $self->error({throw => 'error_on_timeout', timeout => $timeout});
531 }
532 }
533 else {
534 0 while (1) {
535 0 ▶ last if $self->fulfill;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fulfill 0 0 0
536 }
537 }
538  
539 1 return $self;
540 }
541  
542 # ERRORS
543  
544 sub error_on_timeout {
545 2 ▶ my ($self, $data) = @_;
Subroutine error_on_timeout: called CC=1 SCAR=0.0
Pod documented
546  
547 2 my $message = 'Future timed-out after {{timeout}} seconds';
548  
549 my $stash = {
550 timeout => $data->{timeout},
551 2 };
552  
553 2 my $result = {
554 name => 'on.timeout',
555 raise => true,
556 stash => $stash,
557 message => $message,
558 };
559  
560 2 return $result;
561 }
562  
563 1 1;
564  
565  
566  
567 =head1 NAME
568  
569 Venus::Future - Future Class
570  
571 =cut
572  
573 =head1 ABSTRACT
574  
575 Future Class for Perl 5
576  
577 =cut
578  
579 =head1 SYNOPSIS
580  
581 package main;
582  
583 use Venus::Future;
584  
585 my $future = Venus::Future->new;
586  
587 # bless({...}, 'Venus::Future')
588  
589 # $future->promise(sub{
590 # my ($resolve, $reject) = @_;
591 # $resolve->result(1);
592 # });
593  
594 # bless({...}, 'Venus::Future')
595  
596 # $future->fulfill;
597  
598 # true
599  
600 =cut
601  
602 =head1 DESCRIPTION
603  
604 This package provides a framework-agnostic "Future" and implementation of the
605 "Promise/A+" pattern for asynchronous programming. The futures are non-blocking
606 and support "suspend" and "resume" allowing them to be used in any asynchronous
607 operating environment.
608  
609 =cut
610  
611 =head1 INHERITS
612  
613 This package inherits behaviors from:
614  
615 L<Venus::Kind::Utility>
616  
617 =cut
618  
619 =head1 INTEGRATES
620  
621 This package integrates behaviors from:
622  
623 L<Venus::Role::Buildable>
624  
625 =cut
626  
627 =head1 METHODS
628  
629 This package provides the following methods:
630  
631 =cut
632  
633 =head2 catch
634  
635 catch(coderef $on_reject) (Venus::Future)
636  
637 The catch method registers a rejection handler and returns the future that
638 invokes the handlers.
639  
640 I<Since C<3.55>>
641  
642 =over 4
643  
644 =item catch example 1
645  
646 # given: synopsis
647  
648 package main;
649  
650 my $catch = $future->catch(sub{
651 my ($issue) = @_;
652  
653 return $issue;
654 });
655  
656 # bless(..., "Venus::Future")
657  
658 # $catch->then(sub{...});
659  
660 # bless(..., "Venus::Future")
661  
662 =back
663  
664 =over 4
665  
666 =item catch example 2
667  
668 # given: synopsis
669  
670 package main;
671  
672 my $catch = $future->catch(sub{
673 my ($issue) = @_;
674  
675 return $issue;
676 });
677  
678 # bless(..., "Venus::Future")
679  
680 $future = $future;
681  
682 # bless(..., "Venus::Future")
683  
684 # $future->reject('Oops!');
685  
686 # bless(..., "Venus::Future")
687  
688 =back
689  
690 =cut
691  
692 =head2 finally
693  
694 finally(coderef $on_finally) (Venus::Future)
695  
696 The finally method registers a finally handler and returns the future that
697 invokes the handlers.
698  
699 I<Since C<3.55>>
700  
701 =over 4
702  
703 =item finally example 1
704  
705 # given: synopsis
706  
707 package main;
708  
709 my $finally = $future->finally(sub{
710 my ($data) = @_;
711  
712 return $data;
713 });
714  
715 # bless(..., "Venus::Future")
716  
717 # $finally->then(sub{...});
718  
719 # bless(..., "Venus::Future")
720  
721 =back
722  
723 =over 4
724  
725 =item finally example 2
726  
727 # given: synopsis
728  
729 package main;
730  
731 $future->then(sub{
732 $_
733 });
734  
735 my $finally = $future->finally(sub{
736 my ($data) = @_;
737  
738 $future->{stash} = $data;
739  
740 return $data;
741 });
742  
743 # bless(..., "Venus::Future")
744  
745 $future = $future;
746  
747 # bless(..., "Venus::Future")
748  
749 # $future->resolve('Hello.');
750  
751 # bless(..., "Venus::Future")
752  
753 =back
754  
755 =over 4
756  
757 =item finally example 3
758  
759 # given: synopsis
760  
761 package main;
762  
763 $future->then(sub{
764 $_
765 });
766  
767 my $finally = $future->finally(sub{
768 my ($data) = @_;
769  
770 $future->{stash} = $data;
771  
772 return $data;
773 });
774  
775 # bless(..., "Venus::Future")
776  
777 $future = $future;
778  
779 # bless(..., "Venus::Future")
780  
781 # $future->reject('Oops!');
782  
783 # bless(..., "Venus::Future")
784  
785 =back
786  
787 =cut
788  
789 =head2 fulfill
790  
791 fulfill() (Venus::Future)
792  
793 The fulfill method attempts to fulfill the L<promise|/promise> by actuating it,
794 or resuming a previously actuated promise, and returns true if the future has
795 been resolved, i.e. the future is either L<is_fulfilled> or L<is_rejected>, and
796 otherwise returns false.
797  
798 I<Since C<3.55>>
799  
800 =over 4
801  
802 =item fulfill example 1
803  
804 # given: synopsis
805  
806 package main;
807  
808 $future->promise(sub{
809 # resolve
810 $_[0]->result;
811 });
812  
813 my $fulfilled = $future->fulfill;
814  
815 # true
816  
817 =back
818  
819 =over 4
820  
821 =item fulfill example 2
822  
823 # given: synopsis
824  
825 package main;
826  
827 $future->promise(sub{
828 # resolve
829 $_[0]->result;
830 });
831  
832 $future->fulfill;
833  
834 my $result = $future;
835  
836 # bless(..., "Venus::Future")
837  
838 # $result->is_fulfilled;
839  
840 # true
841  
842 # $result->value;
843  
844 # undef
845  
846 =back
847  
848 =over 4
849  
850 =item fulfill example 3
851  
852 # given: synopsis
853  
854 package main;
855  
856 $future->promise(sub{
857 # resolve
858 $_[1]->result;
859 });
860  
861 my $fulfilled = $future->fulfill;
862  
863 # true
864  
865 =back
866  
867 =over 4
868  
869 =item fulfill example 4
870  
871 # given: synopsis
872  
873 package main;
874  
875 $future->promise(sub{
876 # resolve
877 $_[1]->result;
878 });
879  
880 $future->fulfill;
881  
882 my $result = $future;
883  
884 # bless(..., "Venus::Future")
885  
886 # $result->is_rejected;
887  
888 # true
889  
890 # $result->issue;
891  
892 # undef
893  
894 =back
895  
896 =over 4
897  
898 =item fulfill example 5
899  
900 # given: synopsis
901  
902 package main;
903  
904 $future->promise(sub{
905 # resolve
906 $_[0]->result(1);
907 })->then(sub{
908 return $future->{stash} = $_ * 2; # 2
909 })->then(sub{
910 return $future->{stash} = $_ * 2; # 4
911 })->then(sub{
912 return $future->{stash} = $_ * 2; # 8
913 });
914  
915 $future->fulfill;
916  
917 my $result = $future;
918  
919 # bless(..., "Venus::Future")
920  
921 # $result->is_fulfilled;
922  
923 # true
924  
925 # $result->value;
926  
927 # 1
928  
929 =back
930  
931 =over 4
932  
933 =item fulfill example 6
934  
935 # given: synopsis
936  
937 package main;
938  
939 $future->promise(sub{
940 # resolve
941 $_[0]->result(1);
942 });
943  
944 $future->then(sub{
945 return $future->{stash} = $_ * 2; # 2
946 });
947  
948 $future->then(sub{
949 return $future->{stash} = $_ * 2; # 2
950 });
951  
952 $future->then(sub{
953 return $future->{stash} = $_ * 2; # 2
954 });
955  
956 $future->fulfill;
957  
958 my $result = $future;
959  
960 # bless(..., "Venus::Future")
961  
962 # $result->is_fulfilled;
963  
964 # true
965  
966 # $result->value;
967  
968 # 1
969  
970 =back
971  
972 =over 4
973  
974 =item fulfill example 7
975  
976 # given: synopsis
977  
978 package main;
979  
980 my $pending_future = Venus::Future->new;
981  
982 $future->promise(sub{
983 # resolve
984 $_[0]->result(1);
985 })->then(sub{
986 return $_
987 })->then(sub{
988 return $pending_future;
989 })->then(sub{
990 return $_
991 });
992  
993 $future->fulfill;
994  
995 my @results = ($future, $pending_future);
996  
997 # my $result = $future;
998  
999 # bless(..., "Venus::Future")
1000  
1001 # $result->is_fulfilled;
1002  
1003 # false
1004  
1005 # $result->is_pending;
1006  
1007 # true
1008  
1009 # $result->value;
1010  
1011 # undef
1012  
1013 # $pending_future->resolve(0);
1014  
1015 # bless(..., "Venus::Future")
1016  
1017 # $pending_future->is_fulfilled;
1018  
1019 # true
1020  
1021 # $result->fulfill;
1022  
1023 # true
1024  
1025 # $result->is_fulfilled;
1026  
1027 # true
1028  
1029 =back
1030  
1031 =over 4
1032  
1033 =item fulfill example 8
1034  
1035 # given: synopsis
1036  
1037 package Thenable;
1038  
1039 use Venus::Class;
1040  
1041 sub then {
1042 my ($self, $resolve, $reject) = @_;
1043  
1044 $resolve->(100);
1045 }
1046  
1047 package main;
1048  
1049 my $thenable_object = Thenable->new;
1050  
1051 $future->promise(sub{
1052 # resolve
1053 $_[0]->result(1);
1054 })->then(sub{
1055 return $_
1056 })->then(sub{
1057 return $thenable_object;
1058 })->then(sub{
1059 return $future->{stash} = $_
1060 });
1061  
1062 $future->fulfill;
1063  
1064 my @results = ($future, $thenable_object);
1065  
1066 # my $result = $future;
1067  
1068 # bless(..., "Venus::Future")
1069  
1070 # $result->is_fulfilled;
1071  
1072 # true
1073  
1074 # $result->value;
1075  
1076 # 1
1077  
1078 =back
1079  
1080 =cut
1081  
1082 =head2 is
1083  
1084 is(string $name) (boolean)
1085  
1086 The is method take a name and dispatches to the corresponding C<is_$name>
1087 method and returns the result.
1088  
1089 I<Since C<3.55>>
1090  
1091 =over 4
1092  
1093 =item is example 1
1094  
1095 # given: synopsis
1096  
1097 package main;
1098  
1099 $future->resolve;
1100  
1101 my $is_fulfilled = $future->is('fulfilled');
1102  
1103 # true
1104  
1105 =back
1106  
1107 =over 4
1108  
1109 =item is example 2
1110  
1111 # given: synopsis
1112  
1113 package main;
1114  
1115 my $is_pending = $future->is('pending');
1116  
1117 # true
1118  
1119 =back
1120  
1121 =over 4
1122  
1123 =item is example 3
1124  
1125 # given: synopsis
1126  
1127 package main;
1128  
1129 $future->reject;
1130  
1131 my $is_rejected = $future->is('rejected');
1132  
1133 # true
1134  
1135 =back
1136  
1137 =cut
1138  
1139 =head2 is_fulfilled
1140  
1141 is_fulfilled() (boolean)
1142  
1143 The is_fulfilled method returns true if the future has been fulfilled,
1144 otherwise returns false.
1145  
1146 I<Since C<3.55>>
1147  
1148 =over 4
1149  
1150 =item is_fulfilled example 1
1151  
1152 # given: synopsis
1153  
1154 package main;
1155  
1156 my $is_fulfilled = $future->is_fulfilled;
1157  
1158 # false
1159  
1160 =back
1161  
1162 =over 4
1163  
1164 =item is_fulfilled example 2
1165  
1166 # given: synopsis
1167  
1168 package main;
1169  
1170 $future->resolve;
1171  
1172 my $is_fulfilled = $future->is_fulfilled;
1173  
1174 # true
1175  
1176 =back
1177  
1178 =over 4
1179  
1180 =item is_fulfilled example 3
1181  
1182 # given: synopsis
1183  
1184 package main;
1185  
1186 $future->reject;
1187  
1188 my $is_fulfilled = $future->is_fulfilled;
1189  
1190 # false
1191  
1192 =back
1193  
1194 =cut
1195  
1196 =head2 is_pending
1197  
1198 is_pending() (boolean)
1199  
1200 The is_pending method returns true if the future has remained pending,
1201 otherwise returns false.
1202  
1203 I<Since C<3.55>>
1204  
1205 =over 4
1206  
1207 =item is_pending example 1
1208  
1209 # given: synopsis
1210  
1211 package main;
1212  
1213 my $is_pending = $future->is_pending;
1214  
1215 # true
1216  
1217 =back
1218  
1219 =over 4
1220  
1221 =item is_pending example 2
1222  
1223 # given: synopsis
1224  
1225 package main;
1226  
1227 $future->resolve;
1228  
1229 my $is_pending = $future->is_pending;
1230  
1231 # false
1232  
1233 =back
1234  
1235 =over 4
1236  
1237 =item is_pending example 3
1238  
1239 # given: synopsis
1240  
1241 package main;
1242  
1243 $future->reject;
1244  
1245 my $is_pending = $future->is_pending;
1246  
1247 # false
1248  
1249 =back
1250  
1251 =cut
1252  
1253 =head2 is_promised
1254  
1255 is_promised() (boolean)
1256  
1257 The is_promised method returns true if the future a registered promise,
1258 otherwise returns false.
1259  
1260 I<Since C<3.55>>
1261  
1262 =over 4
1263  
1264 =item is_promised example 1
1265  
1266 # given: synopsis
1267  
1268 package main;
1269  
1270 my $is_promised = $future->is_promised;
1271  
1272 # false
1273  
1274 =back
1275  
1276 =over 4
1277  
1278 =item is_promised example 2
1279  
1280 # given: synopsis
1281  
1282 package main;
1283  
1284 $future->promise;
1285  
1286 my $is_promised = $future->is_promised;
1287  
1288 # false
1289  
1290 =back
1291  
1292 =over 4
1293  
1294 =item is_promised example 3
1295  
1296 # given: synopsis
1297  
1298 package main;
1299  
1300 $future->promise(sub{$_[0]->result});
1301  
1302 my $is_promised = $future->is_promised;
1303  
1304 # true
1305  
1306 =back
1307  
1308 =cut
1309  
1310 =head2 is_rejected
1311  
1312 is_rejected() (boolean)
1313  
1314 The is_rejected method returns true if the future has been rejected, otherwise
1315 returns false.
1316  
1317 I<Since C<3.55>>
1318  
1319 =over 4
1320  
1321 =item is_rejected example 1
1322  
1323 # given: synopsis
1324  
1325 package main;
1326  
1327 my $is_rejected = $future->is_rejected;
1328  
1329 # false
1330  
1331 =back
1332  
1333 =over 4
1334  
1335 =item is_rejected example 2
1336  
1337 # given: synopsis
1338  
1339 package main;
1340  
1341 $future->resolve;
1342  
1343 my $is_rejected = $future->is_rejected;
1344  
1345 # false
1346  
1347 =back
1348  
1349 =over 4
1350  
1351 =item is_rejected example 3
1352  
1353 # given: synopsis
1354  
1355 package main;
1356  
1357 $future->reject;
1358  
1359 my $is_rejected = $future->is_rejected;
1360  
1361 # true
1362  
1363 =back
1364  
1365 =cut
1366  
1367 =head2 issue
1368  
1369 issue() (any)
1370  
1371 The issue method returns the result of the L</reject> operation once the future
1372 has been rejected.
1373  
1374 I<Since C<3.55>>
1375  
1376 =over 4
1377  
1378 =item issue example 1
1379  
1380 # given: synopsis
1381  
1382 package main;
1383  
1384 my $issue = $future->issue;
1385  
1386 # undef
1387  
1388 # $future->is_pending
1389  
1390 # true
1391  
1392 =back
1393  
1394 =over 4
1395  
1396 =item issue example 2
1397  
1398 # given: synopsis
1399  
1400 package main;
1401  
1402 $future->reject(0);
1403  
1404 my $issue = $future->issue;
1405  
1406 # 0
1407  
1408 # $future->is_rejected
1409  
1410 # true
1411  
1412 =back
1413  
1414 =over 4
1415  
1416 =item issue example 3
1417  
1418 # given: synopsis
1419  
1420 package main;
1421  
1422 $future->reject({fail => 1});
1423  
1424 my $issue = $future->issue;
1425  
1426 # {fail => 1}
1427  
1428 # $future->is_rejected
1429  
1430 # true
1431  
1432 =back
1433  
1434 =cut
1435  
1436 =head2 new
1437  
1438 new(any @args) (Venus::Future)
1439  
1440 The new method instantiates this package and returns a new instance.
1441  
1442 I<Since C<3.55>>
1443  
1444 =over 4
1445  
1446 =item new example 1
1447  
1448 package main;
1449  
1450 my $future = Venus::Future->new;
1451  
1452 # bless(..., "Venus::Future")
1453  
1454 =back
1455  
1456 =over 4
1457  
1458 =item new example 2
1459  
1460 package main;
1461  
1462 my $future = Venus::Future->new(sub{
1463 my ($resolve, $reject) = @_;
1464 $resolve->result('okay');
1465 });
1466  
1467 # bless(..., "Venus::Future")
1468  
1469 # $future->is('fulfilled');
1470  
1471 # true
1472  
1473 # $future->value;
1474  
1475 # 'okay'
1476  
1477 =back
1478  
1479 =over 4
1480  
1481 =item new example 3
1482  
1483 package main;
1484  
1485 my $future = Venus::Future->new(promise => sub{
1486 my ($resolve, $reject) = @_;
1487 $reject->result('boom');
1488 });
1489  
1490 # bless(..., "Venus::Future")
1491  
1492 # $future->is('rejected');
1493  
1494 # true
1495  
1496 # $future->issue;
1497  
1498 # 'boom'
1499  
1500 =back
1501  
1502 =cut
1503  
1504 =head2 promise
1505  
1506 promise(coderef $code) (Venus::Future)
1507  
1508 The promise method registers a callback executed by the L</fulfill> method,
1509 which is provided two arguments; the first argument being a L<Venus::Try>
1510 instance representing a C<resolve> operaiton; the second argument being a
1511 L<Venus::Try> instance representing a C<reject> operaiton; and returns the
1512 invocant.
1513  
1514 I<Since C<3.55>>
1515  
1516 =over 4
1517  
1518 =item promise example 1
1519  
1520 # given: synopsis
1521  
1522 package main;
1523  
1524 $future = $future->promise(sub{
1525 my ($resolve, $reject) = @_;
1526  
1527 $resolve->result('pass');
1528 });
1529  
1530 # bless(..., "Venus::Future")
1531  
1532 # $future->fulfill;
1533  
1534 # true
1535  
1536 =back
1537  
1538 =over 4
1539  
1540 =item promise example 2
1541  
1542 # given: synopsis
1543  
1544 package main;
1545  
1546 $future = $future->promise(sub{
1547 my ($resolve, $reject) = @_;
1548  
1549 $reject->result('fail');
1550 });
1551  
1552 # bless(..., "Venus::Future")
1553  
1554 # $future->fulfill;
1555  
1556 # true
1557  
1558 =back
1559  
1560 =cut
1561  
1562 =head2 reject
1563  
1564 reject(any $issue) (Venus::Future)
1565  
1566 The reject method cascades a rejection operation causes the future to be
1567 rejected, and returns the invocant.
1568  
1569 I<Since C<3.55>>
1570  
1571 =over 4
1572  
1573 =item reject example 1
1574  
1575 # given: synopsis
1576  
1577 package main;
1578  
1579 my $rejected = $future->reject;
1580  
1581 # bless(..., "Venus::Future")
1582  
1583 # $rejected->status
1584  
1585 # "rejected"
1586  
1587 # $rejected->issue
1588  
1589 # undef
1590  
1591 =back
1592  
1593 =over 4
1594  
1595 =item reject example 2
1596  
1597 # given: synopsis
1598  
1599 package main;
1600  
1601 my $rejected = $future->reject('Oops!');
1602  
1603 # bless(..., "Venus::Future")
1604  
1605 # $rejected->status
1606  
1607 # "rejected"
1608  
1609 # $rejected->issue
1610  
1611 # "Oops!"
1612  
1613 =back
1614  
1615 =cut
1616  
1617 =head2 resolve
1618  
1619 resolve(any $value) (Venus::Future)
1620  
1621 The resolve method cascades a rejection operation causes the future to be
1622 rejected, and returns the invocant.
1623  
1624 I<Since C<3.55>>
1625  
1626 =over 4
1627  
1628 =item resolve example 1
1629  
1630 # given: synopsis
1631  
1632 package main;
1633  
1634 my $fulfilled = $future->resolve;
1635  
1636 # bless(..., "Venus::Future")
1637  
1638 # $fulfilled->status
1639  
1640 # "fulfilled"
1641  
1642 # $fulfilled->value
1643  
1644 # undef
1645  
1646 =back
1647  
1648 =over 4
1649  
1650 =item resolve example 2
1651  
1652 # given: synopsis
1653  
1654 package main;
1655  
1656 my $fulfilled = $future->resolve('Great!');
1657  
1658 # bless(..., "Venus::Future")
1659  
1660 # $fulfilled->status
1661  
1662 # "fulfilled"
1663  
1664 # $fulfilled->value
1665  
1666 # "Great!"
1667  
1668 =back
1669  
1670 =cut
1671  
1672 =head2 status
1673  
1674 status() (any)
1675  
1676 The status method returns the status of the future. Valid statuses are
1677 C<fulfilled>, C<pending>, and C<rejected>.
1678  
1679 I<Since C<3.55>>
1680  
1681 =over 4
1682  
1683 =item status example 1
1684  
1685 # given: synopsis
1686  
1687 package main;
1688  
1689 my $status = $future->status;
1690  
1691 # "pending"
1692  
1693 =back
1694  
1695 =over 4
1696  
1697 =item status example 2
1698  
1699 # given: synopsis
1700  
1701 package main;
1702  
1703 $future->resolve(0);
1704  
1705 my $status = $future->status;
1706  
1707 # "fulfilled"
1708  
1709 =back
1710  
1711 =over 4
1712  
1713 =item status example 3
1714  
1715 # given: synopsis
1716  
1717 package main;
1718  
1719 $future->reject(0);
1720  
1721 my $status = $future->status;
1722  
1723 # "rejected"
1724  
1725 =back
1726  
1727 =cut
1728  
1729 =head2 then
1730  
1731 then(coderef $fulfill, coderef $reject) (Venus::Future)
1732  
1733 The then method registers fulfillment and rejection handlers and returns the
1734 future that invokes the handlers.
1735  
1736 I<Since C<3.55>>
1737  
1738 =over 4
1739  
1740 =item then example 1
1741  
1742 # given: synopsis
1743  
1744 package main;
1745  
1746 my $new_future = $future->then(sub{
1747 # fulfillment handler
1748 $_
1749 });
1750  
1751 # "Venus::Future"
1752  
1753 # $new_future->is_pending;
1754  
1755 # true
1756  
1757 =back
1758  
1759 =over 4
1760  
1761 =item then example 2
1762  
1763 # given: synopsis
1764  
1765 package main;
1766  
1767 my $new_future = $future->then(sub{
1768 # fulfillment handler
1769 $_
1770 },
1771 sub{
1772 # rejection handler
1773 $_
1774 });
1775  
1776 # "Venus::Future"
1777  
1778 # $new_future->is_pending;
1779  
1780 # true
1781  
1782 =back
1783  
1784 =over 4
1785  
1786 =item then example 3
1787  
1788 # given: synopsis
1789  
1790 package main;
1791  
1792 my $new_future = $future->then(undef, sub{
1793 # rejection handler
1794 $_
1795 });
1796  
1797 # "Venus::Future"
1798  
1799 # $new_future->is_pending;
1800  
1801 # true
1802  
1803 =back
1804  
1805 =over 4
1806  
1807 =item then example 4
1808  
1809 # given: synopsis
1810  
1811 package main;
1812  
1813 my $new_future = $future->then(sub{
1814 # fulfillment handler
1815 $_
1816 });
1817  
1818 # "Venus::Future"
1819  
1820 # $new_future->is_pending;
1821  
1822 # true
1823  
1824 $future = $future;
1825  
1826 # "Venus::Future"
1827  
1828 # $new_future->is_pending;
1829  
1830 # true
1831  
1832 =back
1833  
1834 =over 4
1835  
1836 =item then example 5
1837  
1838 # given: synopsis
1839  
1840 package main;
1841  
1842 my $new_future = $future->then(sub{
1843 # fulfillment handler
1844 $_
1845 },
1846 sub{
1847 # rejection handler
1848 $_
1849 });
1850  
1851 # "Venus::Future"
1852  
1853 # $new_future->is_pending;
1854  
1855 # true
1856  
1857 $future = $future;
1858  
1859 # "Venus::Future"
1860  
1861 # $new_future->is_pending;
1862  
1863 # true
1864  
1865 =back
1866  
1867 =over 4
1868  
1869 =item then example 6
1870  
1871 # given: synopsis
1872  
1873 package main;
1874  
1875 my $new_future = $future->then(undef, sub{
1876 # rejection handler
1877 $_
1878 });
1879  
1880 # "Venus::Future"
1881  
1882 # $new_future->is_pending;
1883  
1884 # true
1885  
1886 $future = $future;
1887  
1888 # "Venus::Future"
1889  
1890 # $new_future->is_pending;
1891  
1892 # true
1893  
1894 =back
1895  
1896 =cut
1897  
1898 =head2 value
1899  
1900 value() (any)
1901  
1902 The value method returns the result of the L</resolve> operation once the
1903 future has been fulfilled.
1904  
1905 I<Since C<3.55>>
1906  
1907 =over 4
1908  
1909 =item value example 1
1910  
1911 # given: synopsis
1912  
1913 package main;
1914  
1915 my $value = $future->value;
1916  
1917 # undef
1918  
1919 # $future->is_pending
1920  
1921 # true
1922  
1923 =back
1924  
1925 =over 4
1926  
1927 =item value example 2
1928  
1929 # given: synopsis
1930  
1931 package main;
1932  
1933 $future->resolve(1);
1934  
1935 my $value = $future->value;
1936  
1937 # 1
1938  
1939 # $future->is_fulfilled
1940  
1941 # true
1942  
1943 =back
1944  
1945 =over 4
1946  
1947 =item value example 3
1948  
1949 # given: synopsis
1950  
1951 package main;
1952  
1953 $future->resolve({pass => 1});
1954  
1955 my $value = $future->value;
1956  
1957 # {pass => 1}
1958  
1959 # $future->is_fulfilled
1960  
1961 # true
1962  
1963 =back
1964  
1965 =cut
1966  
1967 =head2 wait
1968  
1969 wait(number $timeout) (Venus::Future)
1970  
1971 The wait method blocks the execution of the current process until a value is
1972 received. If a timeout is provided, execution will be blocked until a value is
1973 received or the wait time expires. If a timeout of C<0> is provided, execution
1974 will not be blocked. If no timeout is provided at all, execution will block
1975 indefinitely.
1976  
1977 I<Since C<3.55>>
1978  
1979 =over 4
1980  
1981 =item wait example 1
1982  
1983 # given: synopsis
1984  
1985 package main;
1986  
1987 $future->promise(sub{
1988 # resolve
1989 $_[0]->result;
1990 });
1991  
1992 $future = $future->wait(0);
1993  
1994 # bless(..., "Venus::Future")
1995  
1996 # $future->is_fulfilled;
1997  
1998 # true
1999  
2000 =back
2001  
2002 =over 4
2003  
2004 =item wait example 2
2005  
2006 # given: synopsis
2007  
2008 package main;
2009  
2010 $future->promise(sub{
2011 # never fulfilled
2012 });
2013  
2014 $future->wait(1);
2015  
2016 # Exception! (isa Venus::Future::Error) (see error_on_timeout)
2017  
2018 =back
2019  
2020 =cut
2021  
2022 =head1 ERRORS
2023  
2024 This package may raise the following errors:
2025  
2026 =cut
2027  
2028 =over 4
2029  
2030 =item error: C<error_on_timeout>
2031  
2032 This package may raise an error_on_timeout exception.
2033  
2034 B<example 1>
2035  
2036 # given: synopsis;
2037  
2038 my $input = {
2039 throw => 'error_on_timeout',
2040 timeout => 10,
2041 };
2042  
2043 my $error = $future->try('error', $input)->error->result;
2044  
2045 # my $name = $error->name;
2046  
2047 # "on_timeout"
2048  
2049 # my $message = $error->render;
2050  
2051 # "Future timed-out after 10 seconds"
2052  
2053 # my $timeout = $error->stash('timeout');
2054  
2055 # 10
2056  
2057 =back
2058  
2059 =head1 AUTHORS
2060  
2061 Awncorp, C<awncorp@cpan.org>
2062  
2063 =cut
2064  
2065 =head1 LICENSE
2066  
2067 Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>.
2068  
2069 This program is free software, you can redistribute it and/or modify it under
2070 the terms of the Apache license version 2.0.
2071  
2072 =cut
« blib/lib/Venus/Cli.pm ↑ Summary blib/lib/Venus/Container.pm »