| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package YADA; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: "Yet Another Download Accelerator": alias for AnyEvent::Net::Curl::Queued | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 1693 | use feature qw(switch); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 189 |  | 
| 6 | 2 |  |  | 2 |  | 11 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 67 |  | 
| 7 | 2 |  |  | 2 |  | 11 | use utf8; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 15 |  | 
| 8 | 2 |  |  | 2 |  | 55 | use warnings qw(all); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 81 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 2 |  |  | 2 |  | 2027 | use Digest::SHA qw(sha256_base64); | 
|  | 2 |  |  |  |  | 8406 |  | 
|  | 2 |  |  |  |  | 217 |  | 
| 11 | 2 |  |  | 2 |  | 860 | use Moo; | 
|  | 2 |  |  |  |  | 15452 |  | 
|  | 2 |  |  |  |  | 17 |  | 
| 12 | 2 |  |  |  |  | 177 | use MooX::Types::MooseLike::Base qw( | 
| 13 |  |  |  |  |  |  | ArrayRef | 
| 14 |  |  |  |  |  |  | HashRef | 
| 15 |  |  |  |  |  |  | Object | 
| 16 |  |  |  |  |  |  | Str | 
| 17 | 2 |  |  | 2 |  | 2764 | ); | 
|  | 2 |  |  |  |  | 6922 |  | 
| 18 | 2 |  |  | 2 |  | 993 | use URI; | 
|  | 2 |  |  |  |  | 25880 |  | 
|  | 2 |  |  |  |  | 88 |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | extends 'AnyEvent::Net::Curl::Queued'; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 2 |  |  | 2 |  | 1534 | use YADA::Worker; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | no if ($] >= 5.017010), warnings => q(experimental); | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | our $VERSION = '0.047'; # VERSION | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | has _queue      => ( | 
| 29 |  |  |  |  |  |  | is          => 'ro', | 
| 30 |  |  |  |  |  |  | isa         => ArrayRef[Object], | 
| 31 |  |  |  |  |  |  | default     => sub { [] }, | 
| 32 |  |  |  |  |  |  | ); | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | has _unique_url => ( | 
| 35 |  |  |  |  |  |  | is          => 'ro', | 
| 36 |  |  |  |  |  |  | isa         => HashRef[Str], | 
| 37 |  |  |  |  |  |  | default     => sub { {} }, | 
| 38 |  |  |  |  |  |  | ); | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | # serious DWIMmery ahead! | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | ## no critic (RequireArgUnpacking) | 
| 43 |  |  |  |  |  |  | around append   => sub { _dwim(append => @_) }; | 
| 44 |  |  |  |  |  |  | around prepend  => sub { _dwim(prepend => @_) }; | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub _dwim { | 
| 47 |  |  |  |  |  |  | my $type = shift; | 
| 48 |  |  |  |  |  |  | my $orig = shift; | 
| 49 |  |  |  |  |  |  | my $self = shift; | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | if (1 < scalar @_) { | 
| 52 |  |  |  |  |  |  | my (%init, @url); | 
| 53 |  |  |  |  |  |  | for my $arg (@_) { | 
| 54 |  |  |  |  |  |  | for (ref $arg) { | 
| 55 |  |  |  |  |  |  | when ($_ eq '' or m{^URI::}x) { | 
| 56 |  |  |  |  |  |  | push @url, $arg; | 
| 57 |  |  |  |  |  |  | } when ('ARRAY') { | 
| 58 |  |  |  |  |  |  | push @url, @{$arg}; | 
| 59 |  |  |  |  |  |  | } when ('CODE') { | 
| 60 |  |  |  |  |  |  | unless (exists $init{on_finish}) { | 
| 61 |  |  |  |  |  |  | $init{on_finish} = $arg; | 
| 62 |  |  |  |  |  |  | } else { | 
| 63 |  |  |  |  |  |  | @init{qw{on_init on_finish}} = ($init{on_finish}, $arg); | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | } when ('HASH') { | 
| 66 |  |  |  |  |  |  | $init{$_} = $arg->{$_} | 
| 67 |  |  |  |  |  |  | for keys %{$arg}; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | for my $url (@url) { | 
| 73 |  |  |  |  |  |  | $url = URI->new($url); | 
| 74 |  |  |  |  |  |  | next | 
| 75 |  |  |  |  |  |  | if not $self->allow_dups | 
| 76 |  |  |  |  |  |  | and ++$self->_unique_url->{sha256_base64($url->canonical->as_string)} > 1; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | my %copy = %init; | 
| 79 |  |  |  |  |  |  | $copy{initial_url} = $url; | 
| 80 |  |  |  |  |  |  | if ($type eq q(append)) { | 
| 81 |  |  |  |  |  |  | push    @{$self->_queue} => [ $type => \%copy ]; | 
| 82 |  |  |  |  |  |  | } elsif ($type eq q(prepend)) { | 
| 83 |  |  |  |  |  |  | unshift @{$self->_queue} => [ $type => \%copy ]; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  | } else { | 
| 87 |  |  |  |  |  |  | $orig->($self => @_); | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | return $self; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | sub _shift_worker { | 
| 94 |  |  |  |  |  |  | my ($self) = @_; | 
| 95 |  |  |  |  |  |  | my $queue = $self->_queue; | 
| 96 |  |  |  |  |  |  | my $max = $self->max << 2; | 
| 97 |  |  |  |  |  |  | while (@{$queue} and ($self->count < $max)) { | 
| 98 |  |  |  |  |  |  | my ($type, $params) = @{shift @{$queue}}; | 
| 99 |  |  |  |  |  |  | $self->$type(sub { YADA::Worker->new($params) }); | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  | return; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | before wait => sub { shift->_shift_worker }; | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | 1; | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | __END__ |