File Coverage

blib/lib/Mojo/UserAgent/Role/Queued.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package Mojo::UserAgent::Role::Queued;
2 3     3   3330 use Mojo::Base '-role';
  3         387836  
  3         19  
3              
4             our $VERSION = "1.14";
5 3     3   2538 use Mojo::UserAgent::Role::Queued::Queue;
  3         8  
  3         24  
6              
7             has max_active => sub { shift->max_connections };
8              
9             has job_queue => sub {
10             Mojo::UserAgent::Role::Queued::Queue->new(max_active => shift->max_active);
11             };
12              
13             around start => sub {
14             my ($orig, $self, $tx, $cb) = @_;
15             if ($cb) {
16             unless ($self->job_queue->callback) {
17             my $this = $self;
18             weaken $this;
19             $self->job_queue->callback(sub { $this->$orig(@_) });
20             }
21             unless ($self->job_queue->has_subscribers('queue_empty')) {
22             my $this = $self;
23             weaken $this;
24             $self->job_queue->on(queue_empty => sub { $this->emit('queue_empty') });
25             }
26             $self->job_queue->enqueue($tx, $cb);
27             }
28             else {
29             return $orig->($self, $tx); # Blocking calls skip the queue
30             }
31             };
32              
33              
34             1;
35             __END__