ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-XMPP2/lib/Net/XMPP2/Event.pm
Revision: 1.2
Committed: Wed Apr 25 13:18:36 2007 UTC (19 years, 5 months ago) by elmex
Branch: MAIN
Changes since 1.1: +30 -4 lines
Log Message:
added Disco support!

File Contents

# Content
1 package Net::XMPP2::Event;
2
3 =head1 NAME
4
5 Net::XMPP2::Event - A event handler class
6
7 =head1 SYNOPSIS
8
9 package foo;
10 use Net::XMPP2::Event;
11
12 our @ISA = qw/Net::XMPP2::Event/;
13
14 package main;
15 my $o = foo->new;
16 $o->reg_cb (foo => sub { ...; 1 });
17 $o->event (foo => 1, 2, 3);
18
19 =head1 DESCRIPTION
20
21 This module is just a small helper module for the connection
22 and client classes.
23
24 You may only derive from this package.
25
26 =head2 reg_cb ($eventname1, $cb1, [$eventname2, $cb2, ...])
27
28 This method registers a callback C<$cb1> for the event with the
29 name C<$eventname1>. You can also pass multiple of these eventname => callback
30 pairs.
31
32 The return value will be an ID that represents the set of callbacks you have installed.
33 Call C<unreg_cb> with that ID to remove those callbacks again.
34
35 To see a documentation of emitted events please take a look at the EVENTS section
36 in the classes that inherit from this one.
37
38 =cut
39
40 sub reg_cb {
41 my ($self, %regs) = @_;
42
43 $self->{id}++;
44
45 for my $cmd (keys %regs) {
46 push @{$self->{events}->{lc $cmd}}, [$self->{id}, $regs{$cmd}]
47 }
48
49 $self->{id}
50 }
51
52 =head2 unreg_cb ($id)
53
54 Removes the set C<$id> of registered callbacks. C<$id> is the
55 return value of a C<reg_cb> call.
56
57 =cut
58
59 sub unreg_cb {
60 my ($self, $id) = @_;
61
62 my $set = delete $self->{ids}->{$id};
63
64 for my $key (keys %{$self->{events}}) {
65 @{$self->{events}->{$key}} =
66 grep {
67 $_->[0] ne $id
68 } @{$self->{events}->{$key}};
69 }
70 }
71
72
73 =head2 event ($eventname, @args)
74
75 Emits the event C<$eventname> and passes the arguments C<@args>.
76
77 =cut
78
79 sub event {
80 my ($self, $ev, @arg) = @_;
81
82 my $nxt = [];
83
84 my $handled;
85 for (@{$self->{events}->{lc $ev}}) {
86 $_->[1]->($self, @arg) and push @$nxt, $_;
87 }
88
89 $self->{events}->{lc $ev} = $nxt;
90 }
91
92 =head1 AUTHOR
93
94 Robin Redeker, C<< <elmex at ta-sa.org> >>
95
96 =head1 COPYRIGHT & LICENSE
97
98 Copyright 2007 Robin Redeker, all rights reserved.
99
100 This program is free software; you can redistribute it and/or modify it
101 under the same terms as Perl itself.
102
103 =cut
104
105 1;