File Coverage

blib/lib/Mojo/UserAgent/Role/Queued/Queue.pm
Criterion Covered Total %
statement 24 24 100.0
branch 2 2 100.0
condition 4 6 66.6
subroutine 6 6 100.0
pod 0 3 0.0
total 36 41 87.8


line stmt bran cond sub pod time code
1             package Mojo::UserAgent::Role::Queued::Queue;
2              
3 3     3   21 use strict;
  3         7  
  3         106  
4 3     3   18 use warnings;
  3         5  
  3         82  
5              
6 3     3   16 use Mojo::Base 'Mojo::EventEmitter';
  3         6  
  3         17  
7              
8             has jobs => sub { [] };
9             has active => 0;
10             has max_active => 4;
11             has callback => undef;
12              
13              
14             sub process {
15 18     18 0 50 my ($self) = @_;
16             # we have jobs and can run them:
17 18   66     49 while ($self->active < $self->max_active
18 27         8349 and my $job = shift @{$self->jobs})
19             {
20 9         68 $self->active($self->active + 1);
21 9         84 $self->callback->( @$job );
22             }
23 18 100 66     150 if (scalar @{$self->jobs} == 0 && $self->active == 0) {
  18         48  
24 5         80 $self->emit('queue_empty');
25             }
26             }
27              
28             sub tx_finish {
29 9     9 0 102 my ($self) = @_;
30 9         50 $self->active($self->active - 1);
31 9         118 $self->process();
32             }
33              
34             sub enqueue {
35 9     9 0 67 my ($self, $job) = @_;
36 9         18 push @{$self->jobs}, $job;
  9         33  
37 9         45 $self->process();
38             }
39              
40             1;