Client/LocalQueue.pm

filter: Statementstmt 100% 28 / 28 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 100% 38 / 38 CC 3 SCAR 5.6
CC
3
Cov
100%
_drain_queue
3.0
BEGIN
1.0
__ANON__
1.0
CRAP 1.8
« Client/Method.pm ↑ Summary
1 package Mojo::RabbitMQ::Client::LocalQueue;
2 6 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶has message_queue => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
5 1 ▶has drain_code_queue => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 6 ▶sub push {
Subroutine push: called CC=1 SCAR=0.0
Pod documented
8 6 my $self = shift;
9  
10 6 CORE::push @{$self->message_queue}, @_;
11 6 return $self->_drain_queue();
12 }
13  
14 6 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
15 6 my $self = shift;
16  
17 6 CORE::push @{$self->drain_code_queue}, @_;
18 6 return $self->_drain_queue();
19 }
20  
21 12 ▶sub _drain_queue {
Subroutine _drain_queue: called CC=3 SCAR=11.0
22 12 my $self = shift;
23  
24 12 my $message_count = scalar @{$self->message_queue};
25 12 my $drain_code_count = scalar @{$self->drain_code_queue};
26  
27 12 ▶ my $count
Branch: 1 branch
BranchTrueFalseTotal
$message_count < $drain_code_count ? : 4 8 12
28 = $message_count < $drain_code_count ? $message_count : $drain_code_count;
29  
30 12 for (1 .. $count) {
31 10 &{shift @{$self->drain_code_queue}}(shift @{$self->message_queue});
32 }
33  
34 12 return $self;
35 }
36  
37 6 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<Mojo::RabbitMQ::Client::LocalQueue> is a queue for callbacks expecting some content to be received.
63  
64 =head1 METHODS
65  
66 L<Mojo::RabbitMQ::Client::LocalQueue> 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<Mojo::RabbitMQ::Client>, L<Mojo::RabbitMQ::Client::Channel>
84  
85 =head1 COPYRIGHT AND LICENSE
86  
87 Copyright (C) 2015-2017, Sebastian Podjasek and others
88  
89 Based on L<AnyEvent::RabbitMQ::LocalQueue> - 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
« Client/Method.pm ↑ Summary