NAME

AnyEvent::Debounce - condense multiple temporally-nearby events into one

VERSION

version 0.01

SYNOPSIS

Create a debouncer:

   my $damper = AnyEvent::Debounce->new( cb => sub {
       my (@events) = @_;
       say "Got ", scalar @events, " event(s) in the batch";
       say "Got event with args: ", join ',', @$_ for @events;
   });

Send it events in rapid succession:

   $damper->send(1,2,3);
   $damper->send(2,3,4);

Watch the output:

   Got 2 events in the batch
   Got event with args: 1,2,3
   Got event with args: 2,3,4

Send it more evnts:

   $damper->send(1);
   sleep 5;
   $damper->send(2);

And notice that there was no need to "debounce" this time:

   Got 1 event in the batch
   Got event with args: 1

   Got 1 event in the batch
   Got event with args: 2

INITARGS

cb

The callback to be called when some events are ready to be handled. Each "event" is an arrayref of the args passed to send.

delay

The time to wait after receiving an event before sending it, in case more events happen in the interim.

always_reset_timer

Normally, when an event is received and it's the first of a series, a timer is started, and when that timer expires, all events are sent. If you set this initarg to a true value, then the timer is reset after each event is received.

For example, if you set the delay to 1, and ten events arrive at 0.5 second intervals, then with this flag set to true, you will get one event after 5 seconds. With this flag set to false, you will get an event once a second for 5 seconds.

By default, this is false, because setting it to true can lead to events never being sent. (Imagine you set delay to 10 seconds, and someone sends an event ever 9.9 seconds. You'll never get any events.)

front_triggered

This flag, when set to true, causes an event to be sent immediately upon receiving the first event. Then, you won't get any events for delay seconds, even if they occur. These events are lost, you will never see them.

By default, this is false.

If you also set always_reset_timer to true, the same timer-reset logic as described above occurs.

METHODS

send

Send an event; the handler callback will get everything you pass in.

REPOSITORY

http://github.com/jrockway/anyevent-debounce

AUTHOR

Jonathan Rockway <jrockway@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Jonathan Rockway.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.