File Coverage

blib/lib/Mojo/RabbitMQ/Client/LocalQueue.pm
Criterion Covered Total %
statement 23 23 100.0
branch 2 2 100.0
condition n/a
subroutine 4 4 100.0
pod 2 2 100.0
total 31 31 100.0


line stmt bran cond sub pod time code
1             package Mojo::RabbitMQ::Client::LocalQueue;
2 6     6   167513 use Mojo::Base -base;
  6         165809  
  6         47  
3              
4             has message_queue => sub { [] };
5             has drain_code_queue => sub { [] };
6              
7             sub push {
8 6     6 1 160 my $self = shift;
9              
10 6         9 CORE::push @{$self->message_queue}, @_;
  6         16  
11 6         29 return $self->_drain_queue();
12             }
13              
14             sub get {
15 6     6 1 57 my $self = shift;
16              
17 6         10 CORE::push @{$self->drain_code_queue}, @_;
  6         16  
18 6         34 return $self->_drain_queue();
19             }
20              
21             sub _drain_queue {
22 12     12   21 my $self = shift;
23              
24 12         15 my $message_count = scalar @{$self->message_queue};
  12         26  
25 12         44 my $drain_code_count = scalar @{$self->drain_code_queue};
  12         26  
26              
27 12 100       49 my $count
28             = $message_count < $drain_code_count ? $message_count : $drain_code_count;
29              
30 12         30 for (1 .. $count) {
31 10         2253 &{shift @{$self->drain_code_queue}}(shift @{$self->message_queue});
  10         40  
  10         23  
  10         25  
32             }
33              
34 12         3762 return $self;
35             }
36              
37             1;
38              
39             =encoding utf8
40              
41             =head1 NAME
42              
43             Mojo::RabbitMQ::Client::LocalQueue - Callback queue
44              
45             =head1 SYNOPSIS
46              
47             use Mojo::RabbitMQ::Client::LocalQueue
48              
49             my $queue = Mojo::RabbitMQ::Client::LocalQueue->new();
50              
51             # Register callback when content appears
52             $queue->get(sub { say "got expected content: " . $_[0] });
53              
54             # Push some content to consume
55             $queue->push("It Works!");
56              
57             # This prints:
58             # got expected content: It Works!
59              
60             =head1 DESCRIPTION
61              
62             L is a queue for callbacks expecting some content to be received.
63              
64             =head1 METHODS
65              
66             L implements following methods:
67              
68             =head2 get
69              
70             $queue->get(sub { process_message($_[0]) })
71              
72             Registers a callback which is executed when new message is pushed to queue.
73              
74             =head2 push
75              
76             $queue->push("Some content");
77             $queue->push({objects => 'are also welcome});
78              
79             Pushes content to queue and also drains all declared callbacks.
80              
81             =head1 SEE ALSO
82              
83             L, L
84              
85             =head1 COPYRIGHT AND LICENSE
86              
87             Copyright (C) 2015-2017, Sebastian Podjasek and others
88              
89             Based on L - Copyright (C) 2010 Masahito Ikuta, maintained by C<< bobtfish@bobtfish.net >>
90              
91             This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
92              
93             =cut