| 1 | package Sprocket::Util::Observable; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 8 | ▶ | use Sprocket::Event; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 8 | ▶ | use Sprocket::Common; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 8 | ▶ | use Scalar::Util qw( reftype ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 8 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 8 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 8 | our $observable = {}; | |||||||||||||||||||
| 12 | 8 | our $hooks = {}; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | sub CALLBACK() { 0 } | ||||||||||||||||||||
| 15 | sub UUID() { 1 } | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 8 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||
| 18 | 8 | my $class = shift; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 8 | ▶ | my $self = bless( { @_ }, ref $class || $class ); | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | # auto add a cleanup | ||||||||||||||||||||
| 23 | ( $self->isa( 'Sprocket' ) ? $self : Sprocket->new() )->attach_hook( | ||||||||||||||||||||
| 24 | 'sprocket.shutdown', | ||||||||||||||||||||
| 25 | 5 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | 5 | $self->clear_hooks(); | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | 8 | ▶ | ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 8 | return $self; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 22 | ▶ | sub register_hook { | ||||||||||||||||||
|
Subroutine
register_hook: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 34 | 22 | my $self = shift; | |||||||||||||||||||
| 35 | 22 | my $hook_names =_array_ref( shift ); | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | $observable->{ $_ }++ | ||||||||||||||||||||
| 38 | 22 | foreach ( @$hook_names ); | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 22 | return; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 9 | ▶ | sub attach_hook { | ||||||||||||||||||
|
Subroutine
attach_hook: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||
| 44 | 9 | my ( $self, $callback ) = @_[ 0, 2 ]; | |||||||||||||||||||
| 45 | 9 | my $hook_names =_array_ref( $_[ 1 ] ); | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 9 | ▶ | unless ( reftype( $callback ) eq 'CODE' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | # XXX log | ||||||||||||||||||||
| 49 | 0 | warn "invalid callback: $callback in Sprocket attach_hook, IGNORED!"; | |||||||||||||||||||
| 50 | 0 | return; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 9 | $hook_names = _array_ref( $hook_names ); | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 9 | my $uuid = new_uuid(); | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 9 | ▶ | $hooks->{ "$self" } = {} unless ( exists( $hooks->{ $self } ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 9 | foreach ( @$hook_names ) { | |||||||||||||||||||
| 60 | 15 | ▶ | if ( my $list = $hooks->{ $self }->{ $_ } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 1 | push( @$list, [ $callback, $uuid ] ); | |||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||
| 63 | 14 | $hooks->{$self}->{ $_ } = [ [ $callback, $uuid ] ]; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 9 | return $uuid; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | ▶ | sub remove_hook { | ||||||||||||||||||
|
Subroutine
remove_hook: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 71 | 0 | my ( $self, $uuid ) = @_; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | ▶ | return undef unless ( $hooks->{ $self } ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | foreach ( keys %{$hooks->{ $self }} ) { | |||||||||||||||||||
| 76 | 0 | ▶ | next unless ( $hooks->{ $self }->{ $_ }->[ UUID ] eq $uuid ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 0 | my $h = delete $hooks->{ $self }->{ $_ }; | |||||||||||||||||||
| 78 | 0 | return $h->[ CALLBACK ]; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | return undef; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 92 | ▶ | sub broadcast { | ||||||||||||||||||
|
Subroutine
broadcast: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||
| 85 | 92 | my ( $self, $hook_name, $data ) = @_; | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | warn "unscheduled broadcast on event hook: $hook_name" | ||||||||||||||||||||
| 88 | 92 | ▶ | unless( $observable->{ $hook_name } ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 92 | ▶ | $data = Sprocket::Event->new( $data ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | unless ( UNIVERSAL::isa( $data, 'Sprocket::Event' ) ); | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 92 | $data->hook( $hook_name ); | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 92 | ▶ | if ( $hooks->{ $self }->{ $hook_name } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | $_->[ CALLBACK ]->( $data ) | ||||||||||||||||||||
| 97 | 12 | foreach ( @{ $hooks->{ $self }->{ $hook_name } } ); | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 92 | return $data; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 10 | ▶ | sub clear_hooks { | ||||||||||||||||||
|
Subroutine
clear_hooks: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 104 | 10 | my $self = shift; | |||||||||||||||||||
| 105 | 10 | delete $hooks->{ $self }; | |||||||||||||||||||
| 106 | 10 | return; | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 40 | ▶ | sub _array_ref { | ||||||||||||||||||
|
Subroutine
_array_ref: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 110 | 40 | ▶ | return ( ref( $_[0] ) eq 'ARRAY' ) ? $_[0] : [ $_[0] ]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 8 | 1; | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||