| 1 | package Async::Hooks; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 3 | $Async::Hooks::VERSION = '0.16'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Hook system with asynchronous capabilities | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Mo qw(is default); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Carp 'confess'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Async::Hooks::Ctl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | has registry => ( | ||||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||
| 16 | 1 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 3 | ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 23 | ▶ | sub hook { | ||||||||||||||||||||||||||
|
Subroutine
hook: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 23 | my ($self, $hook, $cb) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 23 | ▶ | confess("Missing first parameter, the hook name, ") unless $hook; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 21 | ▶ | confess("Missing second parameter, the coderef callback, ") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | unless ref($cb) eq 'CODE'; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 19 | ▶ | my $cbs = $self->{registry}{$hook} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"registry"}{$hook} ||= []
MC/DC100%1/1
$self->{"registry"}{$hook} ||= []
$self->{"registry"}{$hook}
Truth table100%2/2
A
A= $self->{"registry"}{$hook}
| |||||||||||||||||||||||||||||
| 28 | 19 | push @$cbs, $cb; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 19 | return; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 6 | ▶ | sub has_hooks_for { | ||||||||||||||||||||||||||
|
Subroutine
has_hooks_for: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 6 | my ($self, $hook) = @_; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 6 | ▶ | confess("Missing first parameter, the hook name, ") unless $hook; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 6 | my $reg = $self->{registry}; | |||||||||||||||||||||||||||
| 40 | 6 | ▶ | return 0 unless exists $reg->{$hook}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 4 | return scalar(@{$reg->{$hook}}); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 22 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 22 | my ($self, $hook, $args, $cleanup) = @_; | |||||||||||||||||||||||||||
| 47 | 22 | ▶ | ($args, $cleanup) = (undef, $args) if ref($args) eq 'CODE' && !$cleanup; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $args eq "CODE" and not $cleanup
MC/DC0%0/2
ref $args eq "CODE" and not $cleanup
ref $args eq "CODE"
not $cleanup
Truth table33%1/3
A and not(B)
A= ref $args eq "CODE"B= not $cleanup
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 22 | ▶ | confess("Missing first parameter, the hook name, ") unless $hook; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 21 | ▶ | confess("Second parameter, the arguments list, must be a arrayref, ") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args and ref $args ne "ARRAY"
MC/DC100%2/2
$args and ref $args ne "ARRAY"
$args
ref $args ne "ARRAY"
Truth table100%3/3
A and B
A= $argsB= ref $args ne "ARRAY"
| |||||||||||||||||||||||||||||
| 51 | if $args && ref($args) ne 'ARRAY'; | ||||||||||||||||||||||||||||
| 52 | 19 | ▶ | confess("Third parameter, the cleanup callback, must be a coderef, ") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cleanup and ref $cleanup ne "CODE"
MC/DC100%2/2
$cleanup and ref $cleanup ne "CODE"
$cleanup
ref $cleanup ne "CODE"
Truth table100%3/3
A and B
A= $cleanupB= ref $cleanup ne "CODE"
| |||||||||||||||||||||||||||||
| 53 | if $cleanup && ref($cleanup) ne 'CODE'; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 17 | my $r = $self->{registry}; | |||||||||||||||||||||||||||
| 56 | 17 | ▶ | my $cbs = exists $r->{$hook} ? $r->{$hook} : []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 17 | return Async::Hooks::Ctl->new([@$cbs], $args, $cleanup)->next; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | 1; # End of Async::Hooks | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | __END__ | ||||||||||||||||||||||||||||