Ryu/Node.pm

filter: Statementstmt 98.7% 76 / 77 Branchbran 79.6% 43 / 54 Conditioncond 57.4% 27 / 47 MC/DCmcdc 30.7% 8 / 26 Subroutinesub 100% 19 / 19 Podpod 63.6% 7 / 11 totaltotal 76.9% 180 / 234 CC 46 SCAR 21.8
CC
46
Cov
82%
resume
15.5
pause
13.6
is_paused
12.4
CRAP 8.9
« Ryu/Sink.pm ↑ Summary Ryu/Source.pm »
1 package Ryu::Node;
2  
3 46 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 46 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 46 our $VERSION = '4.001'; # VERSION
7 46 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
8  
9 =head1 NAME
10  
11 Ryu::Node - generic node
12  
13 =head1 DESCRIPTION
14  
15 This is a common base class for all sources, sinks and other related things.
16 It does very little.
17  
18 =cut
19  
20 46 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 46 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 METHODS
24  
25 Not really. There's a constructor, but that's not particularly exciting.
26  
27 =cut
28  
29 237 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
30 237 bless {
31 pause_propagation => 1,
32 @_[1..$#_]
33 }, $_[0]
34 }
35  
36 =head2 describe
37  
38 Returns a string describing this node and any parents - typically this will result in a chain
39 like C<< from->combine_latest->count >>.
40  
41 =cut
42  
43 # It'd be nice if L<Future> already provided a method for this, maybe I should suggest it
44 1184 ▶sub describe {
Subroutine describe: called CC=4 SCAR=13.9
Pod documented
45 1184 my ($self) = @_;
46 1184 my $completed = $self->_completed;
47 1184 ▶ ($self->parent ? $self->parent->describe . '=>' : '') . '[' . ($self->label // 'unknown') . '](' . ($completed ? $completed->state : 'inactive') . ')';
Branch: 2 branches
BranchTrueFalseTotal
$self->parent ? : 420 764 1184
$completed ? : 1184 0 1184
Condition100%2/2
$self->label // "unknown"
l!l
117212
MC/DC100%1/1
$self->label // "unknown"
$self->label
Truth table100%2/2
A
A= $self->label
Aresult
11
00
48 }
49  
50 =head2 completed
51  
52 Returns a L<Future> indicating completion (or failure) of this stream.
53  
54 =cut
55  
56 42 ▶sub completed {
Subroutine completed: called CC=2 SCAR=7.2
Pod documented
57 42 my ($self) = @_;
58 42 ▶ my $completion = $self->_completed
Branch: 1 branch
BranchTrueFalseTotal
my $completion = $self->_completed or return undef 42 0 42
59 or return undef;
60 39 ▶ return $completion->without_cancel->on_ready(sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
61 39 my $f = shift;
62 39 my ($expected) = $f->state =~ /^(\S+)/;
63 39 my ($actual) = $completion->state =~ /^(\S+)/;
64 39 ▶ if($expected ne $actual) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected ne $actual) 0 39 39
65 0 warn "Completed state $actual does not match internal state $expected - if you are calling ->completed->$expected, this will not work: use ->finish or ->fail instead";
66 }
67 42 });
68 }
69  
70 # Internal use only, since it's cancellable
71 3285 ▶sub _completed {
Subroutine _completed: called CC=3 SCAR=11.0
72 3285 my ($self) = @_;
73 3285 ▶ return $self->{completed} if $self->{completed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"completed"} 3062 223 3285
74 223 $self->{completed} = my $f = $self->new_future(
75 'completion'
76 );
77 223 ▶ $f->on_ready(
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("cleanup") 223 0 223
78 $self->curry::weak::cleanup
79 ) if $self->can('cleanup');
80 223 $f
81 }
82  
83 =head2 pause
84  
85 Does nothing useful.
86  
87 =cut
88  
89 55 ▶sub pause {
Subroutine pause: called CC=13 SCAR=26.1
Pod documented
90 55 my ($self, $src) = @_;
91 55 ▶ my $k = (defined $src and ref $src)
Branch: 1 branch
BranchTrueFalseTotal
defined $src && ref $src ? : 19 36 55
Condition70%7/10
defined $src and ref $src
!ll&&!rl&&r
261019
builtin::refaddr($src // $self) // 0
l!l
190
$src // $self
l!l&&r!l&&!r
1900
$src // 0
l!l
1026
MC/DC100%2/2
defined $src and ref $src
defined $src ref $src
MC/DC0%0/2
builtin::refaddr($src // $self) // 0
$src $self
MC/DC100%1/1
$src // 0
$src
Truth table100%3/3
A and B
A= defined $srcB= ref $src
ABresult
0X0
100
111
Truth table33%1/3
A // B
A= $srcB= $self
ABresult
1X1
011
000
Truth table100%2/2
A
A= $src
Aresult
11
00
92 ? refaddr($src // $self) // 0
93 : $src // 0;
94  
95 55 ▶ my $was_paused = $self->{is_paused} && keys %{$self->{is_paused}};
Condition66%2/3
$self->{'is_paused'} && keys %{$$self{'is_paused'};}
!ll&&!rl&&r
25300
MC/DC0%0/2
$self->{'is_paused'} && keys %{$$self{'is_paused'};}
$self->{'is_paused'} keys %{$$self{'is_paused'};}
Truth table66%2/3
A && B
A= $self->{'is_paused'}B= keys %{$$self{'is_paused'};}
ABresult
0X0
100
111
96 55 ▶ if($self->{unblocked} and $self->{unblocked}->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"unblocked"} and $self->{"unblocked"}->is_ready) 6 49 55
Condition66%2/3
$self->{"unblocked"} and $self->{"unblocked"}->is_ready
!ll&&!rl&&r
4906
MC/DC50%1/2
$self->{"unblocked"} and $self->{"unblocked"}->is_ready
$self->{"unblocked"} $self->{"unblocked"}->is_ready
Truth table66%2/3
A and B
A= $self->{"unblocked"}B= $self->{"unblocked"}->is_ready
ABresult
0X0
100
111
97 6 delete $self->{unblocked};
98 }
99 55 ++$self->{is_paused}{$k};
100 55 ▶ if(my $parent = $self->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->parent) 37 18 55
101 37 ▶ $parent->pause($self) if $self->{pause_propagation};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pause_propagation"} 17 20 37
102 }
103 55 ▶ if(my $flow_control = $self->{flow_control}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $flow_control = $self->{"flow_control"}) 21 34 55
104 21 ▶ $flow_control->emit(0) unless $was_paused;
Branch: 1 branch
BranchTrueFalseTotal
unless $was_paused 21 0 21
105 }
106 $self
107 55 }
108  
109 =head2 resume
110  
111 Is about as much use as L</pause>.
112  
113 =cut
114  
115 59 ▶sub resume {
Subroutine resume: called CC=14 SCAR=27.4
Pod documented
116 59 my ($self, $src) = @_;
117 59 ▶ my $k = (defined $src and ref $src)
Branch: 1 branch
BranchTrueFalseTotal
defined $src && ref $src ? : 21 38 59
Condition70%7/10
defined $src and ref $src
!ll&&!rl&&r
281021
builtin::refaddr($src // $self) // 0
l!l
210
$src // $self
l!l&&r!l&&!r
2100
$src // 0
l!l
1028
MC/DC100%2/2
defined $src and ref $src
defined $src ref $src
MC/DC0%0/2
builtin::refaddr($src // $self) // 0
$src $self
MC/DC100%1/1
$src // 0
$src
Truth table100%3/3
A and B
A= defined $srcB= ref $src
ABresult
0X0
100
111
Truth table33%1/3
A // B
A= $srcB= $self
ABresult
1X1
011
000
Truth table100%2/2
A
A= $src
Aresult
11
00
118 ? refaddr($src // $self) // 0
119 : $src // 0;
120 59 ▶ delete $self->{is_paused}{$k} unless --$self->{is_paused}{$k} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless --$self->{"is_paused"}{$k} > 0 59 0 59
121 59 ▶ return $self if $self->{is_paused} and keys %{$self->{is_paused}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_paused"} and keys %{$$self{"is_paused"};} 0 59 59
Condition33%1/3
$self->{"is_paused"} and keys %{$$self{"is_paused"};}
!ll&&!rl&&r
0590
MC/DC0%0/2
$self->{"is_paused"} and keys %{$$self{"is_paused"};}
$self->{"is_paused"} keys %{$$self{"is_paused"};}
Truth table33%1/3
A and B
A= $self->{"is_paused"}B= keys %{$$self{"is_paused"};}
ABresult
0X0
100
111
122  
123 59 ▶ if(my $f = $self->{unblocked}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $self->{"unblocked"}) 2 57 59
124 2 ▶ $f->done unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 2 0 2
125 }
126 59 ▶ if(my $parent = $self->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->parent) 39 20 59
127 39 ▶ $parent->resume($self) if $self->{pause_propagation};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pause_propagation"} 19 20 39
128 }
129 59 ▶ if(my $flow_control = $self->{flow_control}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $flow_control = $self->{"flow_control"}) 21 38 59
130 21 $flow_control->emit(1);
131 }
132 $self
133 59 }
134  
135 =head2 unblocked
136  
137 Returns a L<Future> representing the current flow control state of this node.
138  
139 It will be L<pending|Future/is_pending> if this node is currently paused,
140 otherwise L<ready|Future/is_ready>.
141  
142 =cut
143  
144 6 ▶sub unblocked {
Subroutine unblocked: called CC=1 SCAR=0.0
Pod documented
145 # Since we don't want stray callers to affect our internal state, we always return
146 # a non-cancellable version of our internal Future.
147 6 my $self = shift;
148 6 return $self->_unblocked->without_cancel
149 }
150  
151 6 ▶sub _unblocked {
Subroutine _unblocked: called CC=3 SCAR=11.4
152 6 my ($self) = @_;
153 # Since we don't want stray callers to affect our internal state, we always return
154 # a non-cancellable version of our internal Future.
155 6 ▶ $self->{unblocked} //= do {
Condition33%1/3
$self->{"unblocked"} //= do { $self->is_paused ? $self->new_future : "Future"->done }
l!l&&r!l&&!r
060
MC/DC0%0/2
$self->{"unblocked"} //= do { $self->is_paused ? $self->new_future : "Future"->done }
$self->{"unblocked"} do { $self->is_paused ? $self->new_future : "Future"->done }
Truth table33%1/3
A //= B
A= $self->{"unblocked"}B= do { $self->is_paused ? $self->new_future : "Future"->done }
ABresult
1X1
011
000
156 6 ▶ $self->is_paused
Branch: 1 branch
BranchTrueFalseTotal
$self->is_paused ? : 2 4 6
157 ? $self->new_future
158 : Future->done
159 };
160 }
161  
162 =head2 is_paused
163  
164 Might return 1 or 0, but is generally meaningless.
165  
166 =cut
167  
168 274 ▶sub is_paused {
Subroutine is_paused: called CC=9 SCAR=25.2
Pod documented
169 274 my ($self, $src) = @_;
170 274 ▶ return keys(%{ $self->{is_paused} }) ? 1 : 0 unless defined $src;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $src 136 138 274
keys %{$$self{'is_paused'};} ? : 42 94 136
171 138 ▶ my $k = (defined $src and ref $src)
Branch: 1 branch
BranchTrueFalseTotal
defined $src && ref $src ? : 138 0 138
Condition30%3/10
defined $src and ref $src
!ll&&!rl&&r
00138
builtin::refaddr($src // $self) // 0
l!l
1380
$src // $self
l!l&&r!l&&!r
13800
$src // 0
l!l
00
MC/DC0%0/2
defined $src and ref $src
defined $src ref $src
MC/DC0%0/2
builtin::refaddr($src // $self) // 0
$src $self
MC/DC0%0/1
$src // 0
$src
Truth table33%1/3
A and B
A= defined $srcB= ref $src
ABresult
0X0
100
111
Truth table33%1/3
A // B
A= $srcB= $self
ABresult
1X1
011
000
Truth table0%0/2
A
A= $src
Aresult
11
00
172 ? refaddr($src // $self) // 0
173 : $src // 0;
174 return exists $self->{is_paused}{$k}
175 138 ▶ ? 0 + $self->{is_paused}{$k}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'is_paused'}{$k} ? : 6 132 138
176 : 0;
177 }
178  
179 31 ▶sub flow_control {
Subroutine flow_control: called CC=2 SCAR=7.1
Pod undocumented
180 31 my ($self) = @_;
181 31 ▶ return $self->{flow_control} if $self->{flow_control};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"flow_control"} 0 31 31
182 $self->{flow_control} = my $fc = Ryu::Source->new(
183 new_future => $self->{new_future}
184 31 );
185 29 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
186 my $fc = delete $self->{flow_control}
187 29 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fc = delete $self->{"flow_control"} or return 29 0 29
188 29 $fc->finish;
189 31 });
190 31 $fc
191 }
192  
193 1378 ▶sub label { shift->{label} }
Subroutine label: called CC=1 SCAR=0.0
Pod undocumented
194  
195 2040 ▶sub parent { shift->{parent} }
Subroutine parent: called CC=1 SCAR=0.0
Pod undocumented
196  
197 =head2 new_future
198  
199 Used internally to get a L<Future>.
200  
201 =cut
202  
203 228 ▶sub new_future {
Subroutine new_future: called CC=2 SCAR=7.1
Pod documented
204 228 my $self = shift;
205 (
206 228 ▶ $self->{new_future} //= $Ryu::Source::FUTURE_FACTORY
Condition66%2/3
$self->{"new_future"} //= $Ryu::Source::FUTURE_FACTORY
l!l&&r!l&&!r
651630
MC/DC0%0/2
$self->{"new_future"} //= $Ryu::Source::FUTURE_FACTORY
$self->{"new_future"} $Ryu::Source::FUTURE_FACTORY
Truth table66%2/3
A //= B
A= $self->{"new_future"}B= $Ryu::Source::FUTURE_FACTORY
ABresult
1X1
011
000
207 )->($self, @_)
208 }
209  
210  
211 46 1;
212  
213 __END__
« Ryu/Sink.pm ↑ Summary Ryu/Source.pm »