| 1 | package Mojolicious::Plugin::Parallol; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Scalar::Util 'weaken'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=6.2
Pod
documented
| |||||||||||||||||||||||||||||
| 8 | 1 | my ($plugin, $app) = @_; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 0 | ▶ | $app->helper(on_parallol => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=28.4
| |||||||||||||||||||||||||||||
| 11 | 0 | my $p = shift->stash('parallol'); | |||||||||||||||||||||||||||
| 12 | 0 | ▶ | carp "Can't call on_parallol outside of Parallol" unless $p; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # Setter. | ||||||||||||||||||||||||||||
| 15 | 0 | ▶ | return $p->{done} = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Getter w/ default. | ||||||||||||||||||||||||||||
| 18 | 0 | ▶ | return $p->{done} ||= sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 19 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 20 | 0 | ▶ | $self->render unless $self->stash('mojo.finished'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | 0 | ▶ | }); | ||||||||||||||||||||||||||
|
Condition0%0/3
$p->{"done"} ||= sub {
my $self = shift();
$self->render unless $self->stash("mojo.finished");
}
MC/DC0%0/2
$p->{"done"} ||= sub {
my $self = shift();
$self->render unless $self->stash("mojo.finished");
}
$p->{"done"}
sub {
my $self = shift();
$self->render unless $self->stash("mojo.finished");
}
Truth table0%0/3
A ||= B
A= $p->{"done"}B= sub {
my $self = shift();
$self->render unless $self->stash("mojo.finished");
}
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | $app->hook(around_dispatch => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 25 | 0 | my ($next, $self) = @_; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | my $p = {}; | |||||||||||||||||||||||||||
| 28 | 0 | $self->stash(parallol => $p); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # This number represents the number of pending calls (that is, calls | ||||||||||||||||||||||||||||
| 31 | # that have been done, but the callback hasn't been called yet). | ||||||||||||||||||||||||||||
| 32 | # Every time this number gets decremented to zero, you must also | ||||||||||||||||||||||||||||
| 33 | # call $self->on_parallol->($self) to signal that everything is | ||||||||||||||||||||||||||||
| 34 | # done. | ||||||||||||||||||||||||||||
| 35 | # | ||||||||||||||||||||||||||||
| 36 | # This number starts at 1 because we consider the action itself a | ||||||||||||||||||||||||||||
| 37 | # pending call. | ||||||||||||||||||||||||||||
| 38 | 0 | $p->{paralloling} = 1; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Call action. | ||||||||||||||||||||||||||||
| 41 | 0 | $next->(); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # If the action didn't call $self->parallol, don't do anything more. | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | return unless $p->{paralloled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # The action is now done. As mentioned above, we must call | ||||||||||||||||||||||||||||
| 47 | # on_parallol if there's nothing left to do. | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | return $self->on_parallol->($self) if --$p->{paralloling} == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # If the IO loop is not running ... | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | return if Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # ... we want to run the IO loop and stop it again when it's done. | ||||||||||||||||||||||||||||
| 54 | 0 | my $cb = $self->on_parallol; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | $self->on_parallol(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 56 | # Run the callback; capture any errors. | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | eval { $cb->(@_); 1 } or my $e = $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | Mojo::IOLoop->stop; | |||||||||||||||||||||||||||
| 59 | # Re-throw the error. | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | die $e if $e; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | }); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | Mojo::IOLoop->start; | |||||||||||||||||||||||||||
| 64 | 1 | }); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | $app->helper( | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | parallol => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 68 | 0 | my $callback = pop; | |||||||||||||||||||||||||||
| 69 | 0 | my ($self, %opts) = @_; | |||||||||||||||||||||||||||
| 70 | 0 | my $p = $self->stash('parallol'); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if (ref $callback && ref $callback eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $callback and ref $callback eq "CODE"
MC/DC0%0/2
ref $callback and ref $callback eq "CODE"
ref $callback
ref $callback eq "CODE"
Truth table0%0/3
A and B
A= ref $callbackB= ref $callback eq "CODE"
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | weaken($self) if $opts{weaken} // 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$opts{"weaken"} // 1
MC/DC0%0/1
$opts{"weaken"} // 1
$opts{"weaken"}
Truth table0%0/2
A
A= $opts{"weaken"}
| |||||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||
| 75 | 0 | my $name = $callback; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | $callback = sub { $self->stash($name => pop) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | 0 | } | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Mark the request as async and paralloled. | ||||||||||||||||||||||||||||
| 80 | 0 | $self->render_later; | |||||||||||||||||||||||||||
| 81 | 0 | $p->{paralloled} = 1; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Increment pending calls. | ||||||||||||||||||||||||||||
| 84 | 0 | $p->{paralloling}++; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 88 | # Run the callback. | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | eval { $callback->(@_); 1 } or $self->render_exception($@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Run on_parallol if it's finished. | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if (--$p->{paralloling} == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | eval { $self->on_parallol->($self); 1 } or $self->render_exception($@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 0 | } | |||||||||||||||||||||||||||
| 97 | 1 | ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1 | "Parallolololololololololol"; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||