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