| 1 | package Ryu::Sink; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 39 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 39 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 39 | ▶ | use parent qw(Ryu::Node); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 39 | our $VERSION = '4.001'; # VERSION | |||||||||||||||||||
| 9 | 39 | our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | Ryu::Sink - base representation for a thing that receives events | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | This is currently of limited utility. | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | my $src = Ryu::Source->new; | ||||||||||||||||||||
| 20 | my $sink = Ryu::Sink->new; | ||||||||||||||||||||
| 21 | $sink->from($src); | ||||||||||||||||||||
| 22 | $sink->source->say; | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 39 | ▶ | use Future; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 39 | ▶ | use Log::Any qw($log); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =head1 METHODS | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 5 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 34 | 5 | my $class = shift; | |||||||||||||||||||
| 35 | 5 | $class->SUPER::new( | |||||||||||||||||||
| 36 | sources => [], | ||||||||||||||||||||
| 37 | @_ | ||||||||||||||||||||
| 38 | ) | ||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head2 from | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | Given a source, will attach it as the input for this sink. | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | The key difference between L</from> and L</drain_from> is that this method will mark the sink as completed | ||||||||||||||||||||
| 46 | when the source is finished. L</drain_from> allows sequencing of multiple sources, keeping the sink active | ||||||||||||||||||||
| 47 | as each of those completes. | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | ▶ | sub from { | ||||||||||||||||||
|
Subroutine
from: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 52 | 0 | my ($self, $src, %args) = @_; | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | ▶ | die 'expected a subclass of Ryu::Source, received ' . $src . ' instead' unless $src->isa('Ryu::Source'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | ▶ | $self = $self->new unless ref $self; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | $self->drain_from($src); | |||||||||||||||||||
| 58 | 0 | ▶ | $src->completed->on_ready(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 59 | 0 | $self->finish; | |||||||||||||||||||
| 60 | 0 | }); | |||||||||||||||||||
| 61 | 0 | return $self | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 17 | ▶ | sub drain_from { | ||||||||||||||||||
|
Subroutine
drain_from: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 65 | 17 | my ($self, $src) = @_; | |||||||||||||||||||
| 66 | 17 | ▶ | if(ref $src eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 1 | my $data = $src; | |||||||||||||||||||
| 68 | $src = Ryu::Source->new( | ||||||||||||||||||||
| 69 | new_future => $self->{new_future}, | ||||||||||||||||||||
| 70 | 1 | label => 'array', | |||||||||||||||||||
| 71 | )->from($data); | ||||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | 17 | ▶ | die 'expected a subclass of Ryu::Source, received ' . $src . ' instead' unless $src->isa('Ryu::Source'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 17 | $log->tracef('Will drain from %s, with %d sources in queue already', $src->describe, 0 + $self->{sources}->@*); | |||||||||||||||||||
| 76 | 17 | push $self->{sources}->@*, (my $buffered = $src->buffer)->pause; | |||||||||||||||||||
| 77 | 17 | return $self->start_drain; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 34 | ▶ | sub start_drain { | ||||||||||||||||||
|
Subroutine
start_drain: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||
| 81 | 34 | my ($self) = @_; | |||||||||||||||||||
| 82 | 34 | ▶ | if($self->is_draining) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 2 | $log->tracef('Still draining from %s, no need to start new source yet', $self->{active_source}->describe); | |||||||||||||||||||
| 84 | 2 | return $self; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | 32 | ▶ | unless($self->{sources}->@*) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | 15 | $log->tracef('No need to start draining, we have no pending sources in queue'); | |||||||||||||||||||
| 88 | 15 | return $self; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | my $src = shift $self->{sources}->@* | ||||||||||||||||||||
| 92 | 17 | ▶ | or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | $log->warnf('Invalid pending source'); | |||||||||||||||||||
| 94 | 0 | return $self; | |||||||||||||||||||
| 95 | }; | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 17 | $log->tracef('Draining from source %s', $src->describe); | |||||||||||||||||||
| 98 | 17 | $self->{active_source} = $src; | |||||||||||||||||||
| 99 | 17 | my $original_parent = delete $self->{parent}; | |||||||||||||||||||
| 100 | 17 | $self->{parent} = $src; | |||||||||||||||||||
| 101 | 17 | ▶ | $src->_completed->on_ready(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 102 | 17 | undef $self->{active_source}; | |||||||||||||||||||
| 103 | 17 | $self->{parent} = $original_parent; | |||||||||||||||||||
| 104 | 17 | $self->start_drain; | |||||||||||||||||||
| 105 | 17 | }); | |||||||||||||||||||
| 106 | 38 | ▶ | $src->each_while_source(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 107 | 38 | $self->emit($_) | |||||||||||||||||||
| 108 | 17 | }, $self->source, finish_source => 0); | |||||||||||||||||||
| 109 | 17 | ▶ | $src->resume if $src->is_paused; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 17 | $src->prepare_await; | |||||||||||||||||||
| 111 | 17 | return $self; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 34 | ▶ | sub is_draining { !!shift->{active_source} } | ||||||||||||||||||
|
Subroutine
is_draining: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 38 | ▶ | sub emit { | ||||||||||||||||||
|
Subroutine
emit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 117 | 38 | my ($self, $data) = @_; | |||||||||||||||||||
| 118 | 38 | $self->source->emit($data); | |||||||||||||||||||
| 119 | 38 | $self | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 0 | ▶ | sub finish { | ||||||||||||||||||
|
Subroutine
finish: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||
| 123 | 0 | my ($self) = @_; | |||||||||||||||||||
| 124 | 0 | ▶ | return $self if $self->{is_finished}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 0 | $self->{is_finished} = 1; | |||||||||||||||||||
| 126 | 0 | delete $self->{new_source}; | |||||||||||||||||||
| 127 | 0 | my @src = splice $self->{sources}->@*; | |||||||||||||||||||
| 128 | 0 | ▶ | push @src, delete $self->{active_source} if $self->{active_source}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 0 | for my $src (@src) { | |||||||||||||||||||
| 130 | 0 | ▶ | $src->resume if $src->is_paused; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | 0 | ▶ | return $self unless my $src = $self->{source}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 0 | $src->finish; | |||||||||||||||||||
| 134 | 0 | return $self; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 100 | ▶ | sub source { | ||||||||||||||||||
|
Subroutine
source: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 138 | 100 | my ($self) = @_; | |||||||||||||||||||
| 139 | 100 | ▶ | return $self->{source} if $self->{source}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | 10 | $log->tracef('Creating source for sink %s', "$self"); | |||||||||||||||||||
| 141 | 10 | ▶ | my $src = ($self->{new_source} //= sub { Ryu::Source->new(label => 'sink source') })->(); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$self->{"new_source"} //= sub {
"Ryu::Source"->new("label", "sink source");
}
MC/DC0%0/2
$self->{"new_source"} //= sub {
"Ryu::Source"->new("label", "sink source");
}
$self->{"new_source"}
sub {
"Ryu::Source"->new("label", "sink source");
}
Truth table66%2/3
A //= B
A= $self->{"new_source"}B= sub {
"Ryu::Source"->new("label", "sink source");
}
| |||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 10 | $self->{source} = $src; | |||||||||||||||||||
| 144 | 10 | Scalar::Util::weaken($src->{parent} = $self); | |||||||||||||||||||
| 145 | 10 | ▶ | $src->finish if $self->{is_finished}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 10 | return $src; | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 15 | ▶ | sub _completed { | ||||||||||||||||||
|
Subroutine
_completed: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 150 | 15 | my ($self) = @_; | |||||||||||||||||||
| 151 | 15 | return $self->source->_completed; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 5 | ▶ | sub notify_child_completion { } | ||||||||||||||||||
|
Subroutine
notify_child_completion: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 5 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 157 | 5 | my ($self) = @_; | |||||||||||||||||||
| 158 | 5 | ▶ | return unless my $src = delete $self->{source}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | 5 | $src->finish; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 39 | 1; | |||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | __END__ | ||||||||||||||||||||