blib/lib/AnyEvent/Delay.pm

filter: Statementstmt 23.2% 10 / 43 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 20.0% 2 / 10 Podpod 100% 2 / 2 totaltotal 17.2% 14 / 81 CC 11 SCAR 37.4
CC
11
Cov
15%
_step
90.0
__ANON__
6.0
__ANON__
6.0
CRAP 42.3
↑ Summary
1 package AnyEvent::Delay;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use EV;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = 0.01;
6  
7 has on_error => (
8 is => 'rw',
9 0 ▶ isa => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
10 0 ▶ return 2 if ref $_[0] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "CODE" 0 0 0
11 },
12 0 ▶ default => sub { sub { 1 } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 1 );
14  
15 has on_finish => (
16 is => 'rw',
17 0 ▶ isa => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
18 0 ▶ return 2 if ref $_[0] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "CODE" 0 0 0
19 }
20 1 );
21  
22 0 ▶sub begin {
Subroutine begin: not called CC=1 SCAR=6.9
Pod documented
23 0 my $self = shift;
24 0 my @callar = caller;
25 0 my @mark = @_;
26 0 $self->{pending}++;
27 0 my $id = $self->{_ae_counter}++;
28 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 $self->_step($id, @_, @mark)
30 0 };
31 }
32  
33 0 ▶sub steps {
Subroutine steps: not called CC=1 SCAR=6.9
Pod documented
34 0 my $self = shift;
35 0 $self->{steps} = [@_];
36  
37 0 $self->begin->();
38 #$self->{timers}{-1} = EV::timer 0, 0, $self->begin;
39 0 return $self;
40 }
41  
42 0 ▶sub _step {
Subroutine _step: not called CC=9 SCAR=45.0
43 0 my ($self, $id) = (shift, shift);
44
45 0 push @{ $self->{args}[$id] }, [@_];
46 0 ▶ return if $self->{fail} || --$self->{pending} || $self->{lock};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fail"} or --$self->{"pending"} or $self->{"lock"} 0 0 0
Condition0%0/6
$self->{"fail"} or --$self->{"pending"} or $self->{"lock"}
l!l&&r!l&&!r
000
$self->{"fail"} or --$self->{"pending"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"fail"} or --$self->{"pending"} or $self->{"lock"}
$self->{"fail"} --$self->{"pending"} $self->{"lock"}
Truth table0%0/4
A or B or C
A= $self->{"fail"}B= --$self->{"pending"}C= $self->{"lock"}
ABCresult
1XX1
01X1
0011
0000
47  
48 0 local $self->{lock} = 1;
49 0 my @args = map {@$_} @{delete $self->{args}};
50 0 $self->{_ae_counter} = 0;
51 0 ▶ if (my $cb = shift @{$self->{steps} ||= []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = shift @{$self->{"steps"} ||= [];}) 0 0 0
Condition0%0/2
$self->{"steps"} ||= []
l!l
00
MC/DC0%0/1
$self->{"steps"} ||= []
$self->{"steps"}
Truth table0%0/2
A
A= $self->{"steps"}
Aresult
11
00
52 0 ▶ eval { $self->$cb(@args); 1 } or return $self->on_error->($@) and $self->{fail}++;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->$cb(@args); 1 } } or return $self->on_error->($@) 0 0 0
53 }
54  
55 0 ▶ if (! $self->{_ae_counter}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_ae_counter"}) 0 0 0
56 0 return $self->on_finish->( @args );
57 }
58  
59 0 ▶ if (! $self->{pending}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"pending"}) 0 0 0
60 0 $self->{timers}{$id} = EV::timer 0, 0, $self->begin
61 }
62 }
63  
64 1 1;
65  
66 =encoding utf8
67  
68 =head1 NAME
69  
70 AnyEvent::Delay - Manage AnyEvent callbacks and control the flow of events
71  
72 =head1 SYNOPSIS
73  
74 # Synchronize multiple events
75 my $cv = AE::cv;
76 my $delay = AnyEvent::Delay->new();
77 $delay->on_finish(sub { say 'BOOM!'; $cv->send });
78 for my $i (1 .. 10) {
79 my $end = $delay->begin;
80 Mojo::IOLoop->timer($i => sub {
81 say 10 - $i;
82 $end->();
83 });
84 }
85 $cv->recv;
86  
87 # Sequentialize multiple events
88 my $cv = AE::cv;
89 my $delay = AnyEvent::Delay->new();
90 $delay->steps(
91
92 # First step (parallel events)
93 sub {
94 my $delay = shift;
95 Mojo::IOLoop->timer(2 => $delay->begin);
96 http_get( 'http://www.yinyuetai.com' => $delay->begin );
97 say 'Second step in 2 seconds.';
98 },
99
100 # Second step (parallel timers)
101 sub {
102 my ($delay, @args) = @_;
103 say "This http response is $args[1]->[1]{Status}";
104 Mojo::IOLoop->timer(1 => $delay->begin);
105 Mojo::IOLoop->timer(3 => $delay->begin);
106 say 'Third step in 3 seconds.';
107 },
108
109 # Third step (the end)
110 sub {
111 my ($delay, @args) = @_;
112 say 'And done after 5 seconds total.';
113 $cv->send;
114 }
115 );
116 $cv->recv;
117  
118 =head1 DESCRIPTION
119  
120 L<AnyEvent::Delay> manages callbacks and controls the flow of events for L<AnyEvent>. This module is L<Mojo::IOLoop::Delay> version of AnyEvent.
121  
122 =head1 EVENTS
123  
124 L<AnyEvent::Delay> have method the following.
125  
126 =head2 on_error
127  
128 $delay->on_error(sub {
129 my ($delay, $err) = @_;
130 ...
131 });
132  
133 if an error occurs in one of the steps, breaking the chain.
134  
135 =head2 on_finish
136  
137 $delay->on_finish(sub {
138 my ($delay, @args) = @_;
139 ...
140 });
141  
142 the active event _ae_counter reaches zero and there are no more steps.
143  
144 =head1 METHODS
145  
146 =head2 begin
147  
148 my $without_first_arg_arrayref = $delay->begin;
149 my $with_first_arg_arrayref = $delay->begin(0);
150  
151 Increment active event _ae_counter, the returned callback can be used to decrement
152 the active event _ae_counter again. Arguments passed to the callback are queued in
153 the right order for the next step or C<on_finish> event method, the argument will be
154 array references for each begin callback.
155  
156  
157 =head2 steps
158  
159 $delay = $delay->steps(sub {...}, sub {...});
160  
161 Sequentialize multiple events, the first callback will run right away, and the
162 next one once the active event _ae_counter reaches zero. This chain will continue
163 until there are no more callbacks, a callback does not increment the active
164 event _ae_counter or an error occurs in a callback.
165  
166 =head1 SEE ALSO
167  
168 L<Mojo::IOLoop::Delay>.
169  
170 =cut
↑ Summary