line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Util::Callback; |
2
|
3
|
|
|
3
|
|
2040
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
19
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
my %callback; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# Register the plugin |
9
|
|
|
|
|
|
|
sub register { |
10
|
3
|
|
|
3
|
1
|
86
|
my ($plugin, $mojo) = @_; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Add 'callback' helper |
13
|
|
|
|
|
|
|
$mojo->helper( |
14
|
|
|
|
|
|
|
callback => sub { |
15
|
32
|
|
|
32
|
|
29062
|
my $c = shift; |
16
|
32
|
|
|
|
|
36
|
my $name = shift; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Establish callbacks by array reference |
19
|
32
|
100
|
66
|
|
|
153
|
if (ref $name && ref $name eq 'ARRAY') { |
|
|
100
|
66
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Param hash reference |
22
|
3
|
|
|
|
|
4
|
my $param = shift; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# -once flag |
25
|
3
|
|
|
|
|
3
|
my $flag = shift; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# For each given callback name |
28
|
3
|
|
|
|
|
4
|
foreach (@$name) { |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Get callback |
31
|
4
|
|
|
|
|
5
|
my $cb = delete $param->{$_}; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Get callback |
34
|
4
|
100
|
33
|
|
|
10
|
if ($cb) { |
|
|
50
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Establish callback |
37
|
3
|
50
|
33
|
|
|
16
|
if (ref $cb && ref $cb eq 'CODE') { |
38
|
3
|
|
|
|
|
36
|
$mojo->callback($_, $cb, $flag); |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
elsif ($flag && $flag eq '-once') { |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Only allow the next establishement |
45
|
1
|
|
|
|
|
2
|
$callback{$_} = [undef, 1]; |
46
|
|
|
|
|
|
|
}; |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Everything went fine |
50
|
3
|
|
|
|
|
6
|
return 1; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Establish callback |
54
|
|
|
|
|
|
|
elsif (ref $_[0] && ref $_[0] eq 'CODE') { |
55
|
11
|
|
|
|
|
10
|
my $cb = shift; |
56
|
11
|
100
|
66
|
|
|
35
|
my $once = $_[0] && $_[0] eq '-once' ? 1 : 0; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Callback exists |
59
|
11
|
100
|
66
|
|
|
47
|
if (exists $callback{$name} && # Callback exists |
|
|
|
66
|
|
|
|
|
60
|
|
|
|
|
|
|
$callback{$name}->[0] && # Callback is defined |
61
|
|
|
|
|
|
|
$callback{$name}->[1]) { # Callback is -once |
62
|
2
|
|
|
|
|
15
|
$mojo->log->debug( |
63
|
|
|
|
|
|
|
qq{No allowance to redefine callback "$name"} |
64
|
|
|
|
|
|
|
); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Return nothing |
67
|
2
|
|
|
|
|
395
|
return; |
68
|
|
|
|
|
|
|
}; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Establish callback |
71
|
9
|
|
100
|
|
|
34
|
for ($callback{$name} //= []) { |
72
|
9
|
|
|
|
|
12
|
$_->[0] = $cb; |
73
|
9
|
|
100
|
|
|
42
|
$_->[1] ||= $once; |
74
|
|
|
|
|
|
|
}; |
75
|
|
|
|
|
|
|
|
76
|
9
|
|
|
|
|
36
|
return 1; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Release callback |
80
|
|
|
|
|
|
|
else { |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Release existing callback |
83
|
18
|
|
|
|
|
14
|
my $cb; |
84
|
18
|
100
|
66
|
|
|
81
|
if (exists $callback{$name} && ($cb = $callback{$name}->[0])) { |
85
|
17
|
|
|
|
|
35
|
return $cb->($c, @_) ; |
86
|
|
|
|
|
|
|
}; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Return nothing |
89
|
1
|
|
|
|
|
6
|
return; |
90
|
|
|
|
|
|
|
}; |
91
|
|
|
|
|
|
|
} |
92
|
3
|
|
|
|
|
27
|
); |
93
|
|
|
|
|
|
|
}; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
1; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
__END__ |