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   3517 use Moo;
  2         8  
  2         25  
3 2     2   768 use MooX::Types::MooseLike::Base qw( :all );
  2         4  
  2         1195  
4 2     2   1653 use AnyEvent;
  2         6490  
  2         68  
5 2     2   15 use Scalar::Util qw/ weaken /;
  2         5  
  2         165  
6 2     2   11 use namespace::clean -except => 'meta';
  2         4  
  2         20  
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 329 my ($self, $message) = @_;
21 1         5 weaken($self);
22 1         2 my $t; $t = AnyEvent->timer(
23             after => $self->delay_for,
24             cb => sub {
25 1     1   97955 undef $t;
26 1         60 $self->output_to->consume($message);
27             },
28 1         55 );
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             who sponsored its development for its
63             VoIP division called SureVoIP for use with
64             the SureVoIP API -
65            
66              
67             =head1 AUTHOR, COPYRIGHT AND LICENSE
68              
69             See L.
70              
71             =cut