| 1 | |||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Context::Singleton::Frame::Promise; | ||||||||||||||||||||||||||||
| 7 | 10 | $Context::Singleton::Frame::Promise::VERSION = '1.0.9'; | |||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use Scalar::Util qw[ weaken ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 10 | has q (depth) | |||||||||||||||||||||||||||
| 15 | => is => q (ro) | ||||||||||||||||||||||||||||
| 16 | ; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 10 | has q (value) | |||||||||||||||||||||||||||
| 19 | => is => q (rw) | ||||||||||||||||||||||||||||
| 20 | => writer => q (_value) | ||||||||||||||||||||||||||||
| 21 | => predicate => q (is_deduced) | ||||||||||||||||||||||||||||
| 22 | ; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | has q (is_deducible) | ||||||||||||||||||||||||||||
| 25 | => is => q (rw) | ||||||||||||||||||||||||||||
| 26 | => init_arg => +undef | ||||||||||||||||||||||||||||
| 27 | => writer => q (_is_deducible) | ||||||||||||||||||||||||||||
| 28 | 1526 | ▶ | => default => sub { 0 } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 10 | ; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 10 | has q (deduced_in_depth) | |||||||||||||||||||||||||||
| 32 | => is => q (rw) | ||||||||||||||||||||||||||||
| 33 | => init_arg => +undef | ||||||||||||||||||||||||||||
| 34 | => writer => q (_deduced_in_depth) | ||||||||||||||||||||||||||||
| 35 | ; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | has q (_dependencies) | ||||||||||||||||||||||||||||
| 38 | => is => q (ro) | ||||||||||||||||||||||||||||
| 39 | => init_arg => +undef | ||||||||||||||||||||||||||||
| 40 | 1526 | ▶ | => default => sub { +[] } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 10 | ; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | has q (_listeners) | ||||||||||||||||||||||||||||
| 44 | => is => q (ro) | ||||||||||||||||||||||||||||
| 45 | => init_arg => +undef | ||||||||||||||||||||||||||||
| 46 | 1526 | ▶ | => default => sub { +{} } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 10 | ; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 154 | ▶ | sub set_value { | ||||||||||||||||||||||||||
|
Subroutine
set_value: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 154 | my ($self, $value, $in_depth) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 154 | ▶ | $in_depth //= $self->depth; | ||||||||||||||||||||||||||
|
Condition66%2/3
$in_depth //= $self->depth
MC/DC0%0/2
$in_depth //= $self->depth
$in_depth
$self->depth
Truth table66%2/3
A //= B
A= $in_depthB= $self->depth
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 154 | ▶ | unless ($self->is_deduced) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 113 | $self->_value ($value); | |||||||||||||||||||||||||||
| 56 | 113 | $self->set_deducible ($in_depth); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 154 | $self; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2756 | ▶ | sub set_deducible { | ||||||||||||||||||||||||||
|
Subroutine
set_deducible: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 2756 | my ($self, $in_depth) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2756 | ▶ | unless ($self->is_deducible and $self->deduced_in_depth >= $in_depth) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->is_deducible and $self->deduced_in_depth >= $in_depth
MC/DC100%2/2
$self->is_deducible and $self->deduced_in_depth >= $in_depth
$self->is_deducible
$self->deduced_in_depth >= $in_depth
Truth table100%3/3
A and B
A= $self->is_deducibleB= $self->deduced_in_depth >= $in_depth
| |||||||||||||||||||||||||||||
| 66 | 1377 | $self->_is_deducible (1); | |||||||||||||||||||||||||||
| 67 | 1377 | $self->_deduced_in_depth ($in_depth); | |||||||||||||||||||||||||||
| 68 | 1377 | $self->_broadcast_deducible; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2756 | $self; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 2655 | ▶ | sub add_listeners { | ||||||||||||||||||||||||||
|
Subroutine
add_listeners: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 2655 | my ($self, @new_listeners) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # - Listener life time is a frame it is created for | ||||||||||||||||||||||||||||
| 78 | # weaken helps tracking them (children do listen parents here) | ||||||||||||||||||||||||||||
| 79 | # - Listener is another promise | ||||||||||||||||||||||||||||
| 80 | # - Listeners are stored in linked list | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2655 | my $head = $self->_listeners; | |||||||||||||||||||||||||||
| 83 | 2655 | for my $listener (@new_listeners) { | |||||||||||||||||||||||||||
| 84 | my $entry = $head->{next} = { | ||||||||||||||||||||||||||||
| 85 | prev => $head, | ||||||||||||||||||||||||||||
| 86 | next => $head->{next}, | ||||||||||||||||||||||||||||
| 87 | 2655 | listener => $listener, | |||||||||||||||||||||||||||
| 88 | }; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | $entry->{next}{prev} = $head->{next} | ||||||||||||||||||||||||||||
| 91 | if $entry->{next} | ||||||||||||||||||||||||||||
| 92 | 2655 | ▶ | ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2655 | Scalar::Util::weaken $entry->{listener}; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | $self->_notify_listener ($entry->{listener}) | ||||||||||||||||||||||||||||
| 97 | 2655 | ▶ | if $self->is_deducible | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | ; | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 2655 | $self; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 271 | ▶ | sub listen { | ||||||||||||||||||||||||||
|
Subroutine
listen: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 271 | my ($self, @promises) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 271 | for my $promise (grep defined, @promises) { | |||||||||||||||||||||||||||
| 108 | 272 | $promise->add_listeners ($self); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 271 | $self; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 2777 | ▶ | sub add_dependencies { | ||||||||||||||||||||||||||
|
Subroutine
add_dependencies: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | 2777 | my ($self, @new_dependencies) = @_; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2777 | @new_dependencies = grep defined, @new_dependencies; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 2777 | for my $dependency (@new_dependencies) { | |||||||||||||||||||||||||||
| 120 | 2383 | push @{ $self->_dependencies }, $dependency; | |||||||||||||||||||||||||||
| 121 | #Scalar::Util::weaken ($self->_dependencies->[-1]); | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | $_->add_listeners ($self) | ||||||||||||||||||||||||||||
| 125 | 2777 | for @new_dependencies | |||||||||||||||||||||||||||
| 126 | ; | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 2777 | $self; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 3024 | ▶ | sub dependencies { | ||||||||||||||||||||||||||
|
Subroutine
dependencies: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 132 | 3024 | @{ $_[0]->_dependencies }; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2160 | ▶ | sub deducible_dependencies { | ||||||||||||||||||||||||||
|
Subroutine
deducible_dependencies: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 136 | 2160 | my ($self) = @_; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 2160 | grep { $_->is_deducible } $self->dependencies; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1377 | ▶ | sub _broadcast_deducible { | ||||||||||||||||||||||||||
|
Subroutine
_broadcast_deducible: called CC=5 SCAR=18.7
| |||||||||||||||||||||||||||||
| 142 | 1377 | my ($self) = @_; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | return | ||||||||||||||||||||||||||||
| 145 | 1377 | ▶ | unless $self->is_deducible | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | ; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 1377 | my $head = $self->_listeners; | |||||||||||||||||||||||||||
| 149 | 1377 | ▶ | while ($head = $head->{next}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 8 | ▶ | unless ($head->{listener}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | # obsoleted weak listener | ||||||||||||||||||||||||||||
| 152 | 0 | $head->{prev}{next} = $head->{next}; | |||||||||||||||||||||||||||
| 153 | $head->{next}{prev} = $head->{prev} | ||||||||||||||||||||||||||||
| 154 | if $head->{next} | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | next; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 8 | $self->_notify_listener ($head->{listener}); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1377 | $self; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub notify_deducible { | ||||||||||||||||||||||||||
|
Subroutine
notify_deducible: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 2121 | ▶ | sub _notify_listener { | ||||||||||||||||||||||||||
|
Subroutine
_notify_listener: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 169 | 2121 | my ($self, $listener) = @_; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 2121 | $listener->notify_deducible ($self->deduced_in_depth); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 10 | 1; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | __END__ | ||||||||||||||||||||||||||||