Tiny.pm

filter: Statementstmt 98.1% 103 / 105 Branchbran 85.7% 24 / 28 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 96.8% 31 / 32 Podpod 0.0% 0 / 8 totaltotal 86.7% 163 / 188 CC 20 SCAR 8.4
CC
20
Cov
92%
__ANON__
5.0
_resolve
4.2
_reject
4.2
CRAP 2.3
↑ Summary Tiny/AnyEvent.pm »
1 package Promise::Tiny;
2 8 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 our $VERSION = "0.02";
7  
8 8 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 our @EXPORT_OK = qw(promise);
12  
13 4 ▶sub promise {
Subroutine promise: called CC=1 SCAR=0.0
Pod undocumented
14 4 return __PACKAGE__->new(@_);
15 }
16  
17 #### constructor ####
18  
19 111 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
20 111 my ($class, $code) = @_;
21 111 my $self = bless {}, $class;
22 111 eval {
23 $code->(
24 86 ▶ sub { $self->_resolve(@_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 11 ▶ sub { $self->_reject(@_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 111 );
27 };
28 111 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 6 105 111
29 6 $self->_reject($@);
30 }
31 111 return $self;
32 }
33  
34 #### instance methods ####
35  
36 86 ▶sub _resolve {
Subroutine _resolve: called CC=4 SCAR=14.3
37 86 my ($self, $value) = @_;
38 86 ▶ unless ($self->{_fulfilled} || $self->{_rejected}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_fulfilled"} or $self->{"_rejected"}) 86 0 86
Condition33%1/3
$self->{"_fulfilled"} or $self->{"_rejected"}
l!l&&r!l&&!r
0086
MC/DC0%0/2
$self->{"_fulfilled"} or $self->{"_rejected"}
$self->{"_fulfilled"} $self->{"_rejected"}
Truth table33%1/3
A or B
A= $self->{"_fulfilled"}B= $self->{"_rejected"}
ABresult
1X1
011
000
39 86 $self->{_fulfilled} = 1;
40 86 $self->{_fulfilled_value} = $value;
41 86 ▶ if ($self->{on_fulfilled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"on_fulfilled"}) 16 70 86
42 16 $self->{on_fulfilled}->($value);
43 }
44 }
45 }
46  
47 17 ▶sub _reject {
Subroutine _reject: called CC=4 SCAR=14.3
48 17 my ($self, $reason) = @_;
49 17 ▶ unless ($self->{_fulfilled} || $self->{_rejected}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_fulfilled"} or $self->{"_rejected"}) 17 0 17
Condition33%1/3
$self->{"_fulfilled"} or $self->{"_rejected"}
l!l&&r!l&&!r
0017
MC/DC0%0/2
$self->{"_fulfilled"} or $self->{"_rejected"}
$self->{"_fulfilled"} $self->{"_rejected"}
Truth table33%1/3
A or B
A= $self->{"_fulfilled"}B= $self->{"_rejected"}
ABresult
1X1
011
000
50 17 $self->{_rejected} = 1;
51 17 $self->{_rejected_reason} = $reason;
52 17 ▶ if ($self->{on_rejected}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"on_rejected"}) 3 14 17
53 3 $self->{on_rejected}->($reason);
54 }
55 }
56 }
57  
58 61 ▶sub then {
Subroutine then: called CC=1 SCAR=0.0
Pod undocumented
59 61 my ($self, $on_fulfilled, $on_rejected) = @_;
60 61 my $class = ref $self;
61 61 ▶ return $class->new(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
62 61 my ($resolve, $reject) = @_;
63  
64 113 ▶ my $handler_wrapper = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 113 my ($handler) = @_;
66 57 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
67 57 my ($value) = @_;
68 57 my $resolved_value = eval { $handler->($value); };
69 57 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 1 56 57
elsif (_is_promise($resolved_value)) { } 3 53 56
70 1 $reject->($@);
71 } elsif (_is_promise($resolved_value)) {
72 2 ▶ $resolved_value->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 2 my ($value) = @_;
74 2 $resolve->($value);
75 1 ▶ }, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 1 my ($reason) = @_;
77 1 $reject->($reason);
78 3 });
79 } else {
80 53 $resolve->($resolved_value);
81 }
82 113 };
83 61 };
84  
85 61 ▶ if ($on_fulfilled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_fulfilled) 61 0 61
86 61 $self->{on_fulfilled} = $handler_wrapper->($on_fulfilled);
87 61 ▶ if ($self->{_fulfilled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_fulfilled"}) 24 37 61
88 24 $self->{on_fulfilled}->($self->{_fulfilled_value});
89 }
90 }
91 61 ▶ if ($on_rejected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_rejected) 52 9 61
92 52 $self->{on_rejected} = $handler_wrapper->($on_rejected);
93 52 ▶ if ($self->{_rejected}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_rejected"}) 14 38 52
94 14 $self->{on_rejected}->($self->{_rejected_reason});
95 }
96 }
97 61 });
98 }
99  
100 4 ▶sub catch {
Subroutine catch: called CC=1 SCAR=1.2
Pod undocumented
101 4 my ($self, $on_rejected) = @_;
102 return $self->then(
103 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 my ($value) = @_;
105 0 return $value;
106 },
107 4 $on_rejected
108 );
109 }
110  
111 #### static methods ####
112  
113 5 ▶sub resolve {
Subroutine resolve: called CC=1 SCAR=0.0
Pod undocumented
114 5 my ($class, $value) = @_;
115  
116 5 ▶ return $class->new(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 5 my ($resolve, undef) = @_;
118 5 $resolve->($value);
119 5 });
120 }
121  
122 1 ▶sub reject {
Subroutine reject: called CC=1 SCAR=0.0
Pod undocumented
123 1 my ($class, $reason) = @_;
124  
125 1 ▶ return $class->new(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
126 1 my (undef, $reject) = @_;
127 1 $reject->($reason);
128 1 });
129 }
130  
131 5 ▶sub all {
Subroutine all: called CC=2 SCAR=6.9
Pod undocumented
132 5 my ($class, $iterable) = @_;
133 5 ▶ my @promises = map { _is_promise($_) ? $_ : $class->resolve($_) } @$iterable;
Branch: 1 branch
BranchTrueFalseTotal
_is_promise($_) ? : 8 4 12
134  
135 5 ▶ return $class->new(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
136 5 my ($resolve, $reject) = @_;
137 5 my $unresolved_size = scalar(@promises);
138 5 for my $promise (@promises) {
139 10 ▶ $promise->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
140 10 my ($value) = @_;
141 10 $unresolved_size--;
142 10 ▶ if ($unresolved_size <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unresolved_size <= 0) 3 7 10
143 3 $resolve->([ map { $_->{_fulfilled_value} } @promises ]);
144 }
145 2 ▶ }, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
146 2 my ($reason) = @_;
147 2 $reject->($reason);
148 12 });
149 }
150 5 });
151 }
152  
153 4 ▶sub race {
Subroutine race: called CC=2 SCAR=6.9
Pod undocumented
154 4 my ($class, $iterable) = @_;
155 4 ▶ my @promises = map { _is_promise($_) ? $_ : $class->resolve($_) } @$iterable;
Branch: 1 branch
BranchTrueFalseTotal
_is_promise($_) ? : 8 0 8
156  
157 4 ▶ return $class->new(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
158 4 my ($resolve, $reject) = @_;
159 4 for my $promise (@promises) {
160 3 ▶ $promise->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
161 3 my ($value) = @_;
162 3 $resolve->($value);
163 1 ▶ }, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
164 1 my ($reason) = @_;
165 1 $reject->($reason);
166 8 });
167 }
168 4 });
169 }
170  
171 #### utility ####
172  
173 76 ▶sub _is_promise {
Subroutine _is_promise: called CC=2 SCAR=7.1
174 76 my ($value) = @_;
175 76 ▶ return blessed $value && $value->isa(__PACKAGE__);
Condition66%2/3
builtin::blessed($value) && $value->isa(__PACKAGE__)
!ll&&!rl&&r
57019
MC/DC50%1/2
builtin::blessed($value) && $value->isa(__PACKAGE__)
builtin::blessed($value) $value->isa(__PACKAGE__)
Truth table66%2/3
A && B
A= builtin::blessed($value)B= $value->isa(__PACKAGE__)
ABresult
0X0
100
111
176 }
177  
178 8 1;
179  
180 __END__
↑ Summary Tiny/AnyEvent.pm »