File Coverage

blib/lib/Message/Passing/Filter/Delay.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 29 29 100.0


line stmt bran cond sub pod time code
1             package Message::Passing::Filter::Delay;
2 2     2   1510 use Moo;
  2         5  
  2         11  
3 2     2   682 use MooX::Types::MooseLike::Base qw( :all );
  2         146  
  2         979  
4 2     2   851 use AnyEvent;
  2         4754  
  2         79  
5 2     2   15 use Scalar::Util qw/ weaken /;
  2         4  
  2         153  
6 2     2   13 use namespace::clean -except => 'meta';
  2         4  
  2         17  
7              
8             with qw/
9             Message::Passing::Role::Input
10             Message::Passing::Role::Output
11             /;
12              
13             has delay_for => (
14             isa => Num,
15             is => 'ro',
16             required => 1,
17             );
18              
19             sub consume {
20 1     1 1 307 my ($self, $message) = @_;
21 1         5 weaken($self);
22 1         25 my $t; $t = AnyEvent->timer(
23             after => $self->delay_for,
24             cb => sub {
25 1     1   97786 undef $t;
26 1         92 $self->output_to->consume($message);
27             },
28 1         13 );
29             }
30              
31              
32             1;
33              
34             =head1 NAME
35              
36             Message::Passing::Filter::Delay - Delay messages for some time.
37              
38             =head1 DESCRIPTION
39              
40             This filter passes all incoming messages through with no changes, however
41             not immediately - they are delayed .
42              
43             You would normally never want to use this, but it can be useful for
44             testing occasionally, or avoiding race conditions.
45              
46             =head1 ATTRIBUTES
47              
48             =head2 delay_for
49              
50             Floating point number, indicating how many seconds to delay messages for.
51              
52             =head1 METHODS
53              
54             =head2 consume ($msg)
55              
56             Sets up a timed callback in the event loop, which passes the message
57             to the output (and deletes itself) once the timeout has expired
58              
59             =head1 SPONSORSHIP
60              
61             This module exists due to the wonderful people at Suretec Systems Ltd.
62             <http://www.suretecsystems.com/> who sponsored its development for its
63             VoIP division called SureVoIP <http://www.surevoip.co.uk/> for use with
64             the SureVoIP API -
65             <http://www.surevoip.co.uk/support/wiki/api_documentation>
66              
67             =head1 AUTHOR, COPYRIGHT AND LICENSE
68              
69             See L<Message::Passing>.
70              
71             =cut