line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Weixin::Message::Queue; |
2
|
|
|
|
|
|
|
sub new{ |
3
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
4
|
0
|
|
|
|
|
|
my $self = { |
5
|
|
|
|
|
|
|
queue => [], |
6
|
|
|
|
|
|
|
callback_for_get => undef, |
7
|
|
|
|
|
|
|
callback_for_get_bak => undef, |
8
|
|
|
|
|
|
|
}; |
9
|
0
|
|
|
|
|
|
return bless $self,$class; |
10
|
|
|
|
|
|
|
} |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub put{ |
13
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
14
|
0
|
0
|
|
|
|
|
die "Weixin::Message::Queue->put()失败,请检查是否已经设置了队列get()回调\n" |
15
|
|
|
|
|
|
|
unless ref $self->{callback_for_get} eq 'CODE'; |
16
|
0
|
|
|
|
|
|
push @{ $self->{queue} } ,$_[0]; |
|
0
|
|
|
|
|
|
|
17
|
0
|
|
|
|
|
|
$self->_notify_to_get(); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
sub get{ |
20
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
21
|
0
|
|
|
|
|
|
my $cb = shift; |
22
|
0
|
0
|
|
|
|
|
die "Weixin::Message::Queue->get()仅接受一个函数引用\n" unless ref $cb eq 'CODE'; |
23
|
0
|
|
|
|
|
|
$self->{callback_for_get} = $cb; |
24
|
0
|
|
|
|
|
|
$self->{callback_for_get_bak} = $cb; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
sub _notify_to_get{ |
27
|
0
|
|
|
0
|
|
|
my $self = shift; |
28
|
0
|
|
|
|
|
|
my $msg = shift @{$self->{queue}}; |
|
0
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
$self->{callback_for_get}->($msg); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
1; |