File Coverage

blib/lib/Mojo/Webqq/Message/Queue.pm
Criterion Covered Total %
statement 0 39 0.0
branch 0 18 0.0
condition 0 3 0.0
subroutine 0 5 0.0
pod 0 3 0.0
total 0 68 0.0


line stmt bran cond sub pod time code
1             package Mojo::Webqq::Message::Queue;
2             $Mojo::Webqq::Message::Queue::LAST_GET_TIME = undef;
3             $Mojo::Webqq::Message::Queue::GET_INTERVAL = 3;
4             sub new{
5 0     0 0   my $class = shift;
6 0           my $callback_for_get ;
7             my $callback_for_put ;
8 0           my $callback_for_delay ;
9 0           my $ioloop ;
10 0 0         if(@_ == 1){
11 0           $callback_for_get = shift;
12             }
13             else{
14 0           my %opt = @_;
15 0           $callback_for_get = $opt{get};
16 0           $callback_for_put = $opt{put};
17 0           $callback_for_delay = $opt{delay};
18 0           $ioloop = $opt{ioloop};
19             }
20 0           my $self = {
21             ioloop => $ioloop,
22             queue => [],
23             callback_for_get => undef,
24             callback_for_delay => undef,
25             callback_for_put => undef,
26             callback_for_get_bak => undef,
27             };
28 0 0         $self->{callback_for_get} = $callback_for_get if ref $callback_for_get eq "CODE";
29 0 0         $self->{callback_for_put} = $callback_for_put if ref $callback_for_put eq "CODE";
30 0 0         $self->{callback_for_delay} = $callback_for_delay if ref $callback_for_delay eq "CODE";
31 0           return bless $self,$class;
32             }
33             sub put{
34 0     0 0   my $self = shift;
35             die "Mojo::Webqq::Message::Queue->put()失败,请检查是否已经设置了队列get()回调\n"
36 0 0         unless ref $self->{callback_for_get} eq 'CODE';
37 0           push @{ $self->{queue} } ,$_[0];
  0            
38 0 0 0       if(defined $self->{ioloop} and ref $self->{callback_for_delay} eq "CODE"){
39 0           my $delay = 0;
40 0           my $now = time;
41 0 0         if(defined $Mojo::Webqq::Message::Queue::LAST_GET_TIME){
42 0 0         $delay = $now<$Mojo::Webqq::Message::Queue::LAST_GET_TIME+$Mojo::Webqq::Message::Queue::GET_INTERVAL?
43             $Mojo::Webqq::Message::Queue::LAST_GET_TIME+$Mojo::Webqq::Message::Queue::GET_INTERVAL-$now
44             : 0;
45             }
46             $self->{ioloop}->timer($delay,sub{
47 0     0     $self->{callback_for_delay}->($self->{queue});
48 0           $self->_notify_to_get();
49 0           });
50 0           $Mojo::Webqq::Message::Queue::LAST_GET_TIME = $now+$delay;
51             }
52             else{
53 0           $self->_notify_to_get();
54             }
55             }
56             sub get{
57 0     0 0   my $self = shift;
58 0           my $cb = shift;
59 0 0         die "Mojo::Webqq::Message::Queue->get()仅接受一个函数引用\n" unless ref $cb eq 'CODE';
60 0           $self->{callback_for_get} = $cb;
61 0           $self->{callback_for_get_bak} = $cb;
62             }
63             sub _notify_to_get{
64 0     0     my $self = shift;
65 0           my $msg = shift @{$self->{queue}};
  0            
66 0           $self->{callback_for_get}->($msg);
67             }
68              
69             1;