]> git.vpit.fr Git - perl/scripts/xchat.git/blob - Xchat/XPI/Events.pm
b0211c2bf8927f8d4ede911a6dcfc82df298c5d3
[perl/scripts/xchat.git] / Xchat / XPI / Events.pm
1 package Xchat::XPI::Events;
2
3 use strict;
4 use warnings;
5
6 use Xchat qw/:all/;
7
8 use lib get_info 'xchatdir';
9 use Xchat::XPI;
10
11 use constant {
12  DEFAULT_DELAY => 1000
13 };
14
15 our $VERSION = '0.04';
16
17 sub delay {
18  my $d = shift;
19  $d = (defined $d) ? int $d : DEFAULT_DELAY;
20  return hook_timer $d, sub {
21   my ($cb, $data) = @{$_[0]};
22   &$cb($data);
23   return REMOVE;
24  }, { data => \@_ };
25 }
26
27 my %reorder;
28
29 sub filter {
30  return unless @_ == 3;
31  not defined and return for @_;
32  my ($cb, $to) = @_[1, 2];
33  return hook_print $_[0], sub {
34   my @args = @{$_[0]};
35   if ($cb->(@args)) {
36    $reorder{$to}->(@args) if exists $reorder{$to};
37    emit_print $to, @args;
38    return EAT_ALL;
39   } else {
40    return EAT_NONE;
41   }
42  }
43 }
44
45 use base qw/Exporter/;
46
47 our @EXPORT         = ();
48 our %EXPORT_TAGS    = (
49  'funcs'  => [ qw/delay filter/ ],
50  'consts' => [ qw/DEFAULT_DELAY/ ]
51 );
52 our @EXPORT_OK      = map { @$_ } values %EXPORT_TAGS;
53 $EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
54
55 1;