blib/lib/AnyEvent/Debounce.pm

filter: Statementstmt 100% 33 / 33 Branchbran 100% 8 / 8 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 96.4% 54 / 56 CC 6 SCAR 9.9
CC
6
Cov
100%
send
5.0
send_events_now
2.0
BEGIN
1.0
CRAP 2.7
↑ Summary
1 package AnyEvent::Debounce;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $AnyEvent::Debounce::VERSION = '0.01';
4 }
5 # ABSTRACT: condense multiple temporally-nearby events into one
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 has 'front_triggered' => (
10 is => 'ro',
11 isa => 'Bool',
12 default => 0,
13 documentation => 'if true, trigger immediately after the first event is received',
14 );
15  
16 1 has 'always_reset_timer' => (
17 is => 'ro',
18 isa => 'Bool',
19 default => 0,
20 documentation => 'if true, reset the timer after each event',
21 );
22  
23 1 has 'delay' => (
24 is => 'ro',
25 isa => 'Num',
26 default => 1,
27 documentation => 'number of seconds to wait before considering events separate',
28 );
29  
30 1 has 'cb' => (
31 is => 'ro',
32 isa => 'CodeRef',
33 required => 1,
34 documentation => 'coderef to run when debounced events are ready',
35 );
36  
37 has '_queued_events' => (
38 traits => ['Array'],
39 reader => 'queued_events',
40 isa => 'ArrayRef',
41 4 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 1 lazy => 1,
43 clearer => 'clear_queued_events',
44 handles => { 'queue_event' => 'push', 'event_count' => 'count' },
45 );
46  
47 1 has 'timer' => (
48 reader => 'timer',
49 lazy_build => 1,
50 );
51  
52 22 ▶sub _build_timer {
Subroutine _build_timer: called CC=1 SCAR=0.0
53 22 my $self = shift;
54 return AnyEvent->timer(
55 after => $self->delay,
56 interval => 0,
57 4 ▶ cb => sub { $self->send_events_now },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 22 );
59 }
60  
61 4 ▶sub send_events_now {
Subroutine send_events_now: called CC=2 SCAR=6.9
Pod undocumented
62 4 my $self = shift;
63 4 my $events = $self->queued_events;
64 4 my $count = $self->event_count;
65 4 $self->clear_timer;
66 4 $self->clear_queued_events;
67 4 ▶ $self->cb->(@$events) if $count > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count > 0 2 2 4
68 4 return;
69 }
70  
71 40 ▶sub send {
Subroutine send: called CC=5 SCAR=16.1
Pod undocumented
72 40 my ($self, @args) = @_;
73  
74 40 my $timer_running = $self->has_timer;
75 40 ▶ $self->clear_timer if $self->always_reset_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $self->always_reset_timer 20 20 40
76 40 $self->timer; # resets the timer if we don't have one
77  
78 40 ▶ if($self->front_triggered && !$timer_running){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->front_triggered and not $timer_running) { } 2 38 40
elsif (not $self->front_triggered) { } 20 18 38
Condition100%3/3
$self->front_triggered and not $timer_running
!ll&&!rl&&r
20182
MC/DC100%2/2
$self->front_triggered and not $timer_running
$self->front_triggered not $timer_running
Truth table100%3/3
A and not(B)
A= $self->front_triggeredB= not $timer_running
ABresult
0X0
100
111
79 2 $self->cb->([@args]);
80 }
81 elsif(!$self->front_triggered){
82 20 $self->queue_event([@args]);
83 }
84 else {
85 # warn "discarding event"
86 }
87  
88 40 return;
89 }
90  
91 1 1;
92  
93  
94  
95 =pod
96  
97 =head1 NAME
98  
99 AnyEvent::Debounce - condense multiple temporally-nearby events into one
100  
101 =head1 VERSION
102  
103 version 0.01
104  
105 =head1 SYNOPSIS
106  
107 Create a debouncer:
108  
109 my $damper = AnyEvent::Debounce->new( cb => sub {
110 my (@events) = @_;
111 say "Got ", scalar @events, " event(s) in the batch";
112 say "Got event with args: ", join ',', @$_ for @events;
113 });
114  
115 Send it events in rapid succession:
116  
117 $damper->send(1,2,3);
118 $damper->send(2,3,4);
119  
120 Watch the output:
121  
122 Got 2 events in the batch
123 Got event with args: 1,2,3
124 Got event with args: 2,3,4
125  
126 Send it more evnts:
127  
128 $damper->send(1);
129 sleep 5;
130 $damper->send(2);
131  
132 And notice that there was no need to "debounce" this time:
133  
134 Got 1 event in the batch
135 Got event with args: 1
136  
137 Got 1 event in the batch
138 Got event with args: 2
139  
140 =head1 INITARGS
141  
142 =head1 cb
143  
144 The callback to be called when some events are ready to be handled.
145 Each "event" is an arrayref of the args passed to C<send>.
146  
147 =head1 delay
148  
149 The time to wait after receiving an event before sending it, in case
150 more events happen in the interim.
151  
152 =head1 always_reset_timer
153  
154 Normally, when an event is received and it's the first of a series, a
155 timer is started, and when that timer expires, all events are sent.
156 If you set this initarg to a true value, then the timer is reset after
157 each event is received.
158  
159 For example, if you set the delay to 1, and ten events arrive at 0.5
160 second intervals, then with this flag set to true, you will get one
161 event after 5 seconds. With this flag set to false, you will get an
162 event once a second for 5 seconds.
163  
164 By default, this is false, because setting it to true can lead to
165 events never being sent. (Imagine you set delay to 10 seconds, and
166 someone sends an event ever 9.9 seconds. You'll never get any
167 events.)
168  
169 =head1 front_triggered
170  
171 This flag, when set to true, causes an event to be sent immediately
172 upon receiving the first event. Then, you won't get any events for
173 C<delay> seconds, even if they occur. These events are lost, you will
174 never see them.
175  
176 By default, this is false.
177  
178 If you also set C<always_reset_timer> to true, the same timer-reset
179 logic as described above occurs.
180  
181 =head1 METHODS
182  
183 =head1 send
184  
185 Send an event; the handler callback will get everything you pass in.
186  
187 =head1 REPOSITORY
188  
189 L<http://github.com/jrockway/anyevent-debounce>
190  
191 =head1 AUTHOR
192  
193 Jonathan Rockway <jrockway@cpan.org>
194  
195 =head1 COPYRIGHT AND LICENSE
196  
197 This software is copyright (c) 2011 by Jonathan Rockway.
198  
199 This is free software; you can redistribute it and/or modify it under
200 the same terms as the Perl 5 programming language system itself.
201  
202 =cut
203  
204  
205 __END__
↑ Summary