| 1 | package Promises; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 13 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 13 | $Promises::VERSION = '1.05'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: An implementation of Promises in Perl | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use Scalar::Util qw[ blessed ]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use Promises::Deferred; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 13 | our $Backend = 'Promises::Deferred'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 13 | our $WARN_ON_UNHANDLED_REJECT = 0; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 13 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | collectors => [ | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 'backend' => \'_set_backend', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 'warn_on_unhandled_reject' => \'_set_warn_on_unhandled_reject', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 21 | exports => [qw[ | ||||||||||||||||||||||||||||||||||||||||||
| 22 | deferred resolved rejected | ||||||||||||||||||||||||||||||||||||||||||
| 23 | collect collect_hash | ||||||||||||||||||||||||||||||||||||||||||
| 24 | ]] | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 13 | }; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | ▶ | sub _set_warn_on_unhandled_reject { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_warn_on_unhandled_reject: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | my( undef, $arg ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | if( $WARN_ON_UNHANDLED_REJECT = $arg->[0] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | # only brings the big guns if asked for | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 12 | ▶ | *Promises::Deferred::DESTROY = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 12 | ▶ | return unless $WARN_ON_UNHANDLED_REJECT; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | return unless | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 12 | ▶ | $self->is_rejected and not $self->{_reject_was_handled}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->is_rejected and not $self->{"_reject_was_handled"}
MC/DC100%2/2
$self->is_rejected and not $self->{"_reject_was_handled"}
$self->is_rejected
not $self->{"_reject_was_handled"}
Truth table100%3/3
A and not(B)
A= $self->is_rejectedB= not $self->{"_reject_was_handled"}
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | require Data::Dumper; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | my $dump = | |||||||||||||||||||||||||||||||||||||||||
| 45 | Data::Dumper->new([$self->result])->Terse(1)->Dump; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 4 | chomp $dump; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 4 | $dump =~ s/\n\s*/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | warn "Promise's rejection ", $dump, | ||||||||||||||||||||||||||||||||||||||||||
| 51 | " was not handled", | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | ▶ | ($self->{_caller} ? ( ' at ', join ' line ', @{$self->{_caller}} ) : ()) , "\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub _set_backend { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_backend: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my ( undef, $arg ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my $backend = $arg->[0] or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | unless ( $backend =~ s/^\+// ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $backend = 'Promises::Deferred::' . $backend; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | require Module::Runtime; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $Backend = Module::Runtime::use_module($backend) || return; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
Module::Runtime::use_module($backend) || (return)
MC/DC0%0/1
Module::Runtime::use_module($backend) || (return)
Module::Runtime::use_module($backend)
Truth table0%0/2
A
A= Module::Runtime::use_module($backend)
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 29 | ▶ | sub deferred(;&) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
deferred: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 29 | my $promise = $Backend->new; | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 29 | ▶ | if ( my $code = shift ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $promise->resolve; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return $promise->then(sub{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $code->($promise); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 29 | return $promise; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 14 | ▶ | sub resolved { deferred->resolve(@_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolved: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | sub rejected { deferred->reject(@_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rejected: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | ▶ | sub collect_hash { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
collect_hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | ▶ | collect(@_)->then( sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | my @values = @$_; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 14 | ▶ | die "'collect_hash' promise returned more than one value: [@{[ join ', ', @values ]} ]\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | if @values > 1; | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 13 | ▶ | @values == 1 ? $values[0] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | } @_ }) | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | } | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 8 | ▶ | sub collect { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
collect: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 8 | my @promises = @_; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 8 | my $all_done = resolved(); | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 8 | my @results; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 8 | for my $p ( @promises ) { | |||||||||||||||||||||||||||||||||||||||||
| 104 | 26 | ▶ | if ( $p && blessed $p && $p->can('then') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$p and builtin::blessed($p) and $p->can("then")
$p and builtin::blessed($p)
MC/DC33%1/3
$p and builtin::blessed($p) and $p->can("then")
$p
builtin::blessed($p)
$p->can("then")
Truth table50%2/4
A and B and C
A= $pB= builtin::blessed($p)C= $p->can("then")
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 11 | ▶ | $all_done = $all_done->then( sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 11 | ▶ | $p->then( sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 11 | push @results, [ @_ ]; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 11 | return; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } ) | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 11 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # not actually a promise; collect directly | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 15 | ▶ | $all_done = $all_done->then( sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 15 | push @results, [ $p ]; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 15 | return; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 15 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 8 | ▶ | return $all_done->then( sub { @results } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||||||||||||||||||||||||