blib/lib/Venus/Future.pm

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