ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-XMPP2/lib/Net/XMPP2/Event.pm
Revision: 1.8
Committed: Tue Jul 24 13:05:55 2007 UTC (19 years, 3 months ago) by elmex
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +9 -4 lines
Log Message:
changed some events to reflect the new meaning of event callback return values.

File Contents

# Content
1 package Net::XMPP2::Event;
2 use strict;
3
4 =head1 NAME
5
6 Net::XMPP2::Event - Event handler class
7
8 =head1 SYNOPSIS
9
10 package foo;
11 use Net::XMPP2::Event;
12
13 our @ISA = qw/Net::XMPP2::Event/;
14
15 package main;
16 my $o = foo->new;
17 $o->reg_cb (foo => sub { ...; 1 });
18 $o->event (foo => 1, 2, 3);
19
20 =head1 DESCRIPTION
21
22 This module is just a small helper module for the connection
23 and client classes.
24
25 You may only derive from this package.
26
27 =head1 METHODS
28
29 =over 4
30
31 =item B<reg_cb ($eventname1, $cb1, [$eventname2, $cb2, ...])>
32
33 This method registers a callback C<$cb1> for the event with the
34 name C<$eventname1>. You can also pass multiple of these eventname => callback
35 pairs.
36
37 The return value will be an ID that represents the set of callbacks you have installed.
38 Call C<unreg_cb> with that ID to remove those callbacks again.
39
40 To see a documentation of emitted events please take a look at the EVENTS section
41 in the classes that inherit from this one.
42
43 The callbacks will be called in an array context. If a callback doesn't want
44 to return any value it should return an empty list.
45 All elements of the returned list will be accumulated and the semantic of the
46 accumulated return values depends on the events.
47
48 =cut
49
50 sub reg_cb {
51 my ($self, %regs) = @_;
52
53 $self->{id}++;
54
55 for my $cmd (keys %regs) {
56 push @{$self->{events}->{lc $cmd}}, [$self->{id}, $regs{$cmd}]
57 }
58
59 $self->{id}
60 }
61
62 =item B<unreg_cb ($id)>
63
64 Removes the set C<$id> of registered callbacks. C<$id> is the
65 return value of a C<reg_cb> call.
66
67 =cut
68
69 sub unreg_cb {
70 my ($self, $id) = @_;
71
72 my $set = delete $self->{ids}->{$id};
73
74 for my $key (keys %{$self->{events}}) {
75 @{$self->{events}->{$key}} =
76 grep {
77 $_->[0] ne $id
78 } @{$self->{events}->{$key}};
79 }
80 }
81
82
83 =item B<event ($eventname, @args)>
84
85 Emits the event C<$eventname> and passes the arguments C<@args>.
86 The return value is a list of defined return values from the event callbacks.
87
88 =cut
89
90 sub event {
91 my ($self, $ev, @arg) = @_;
92
93 my $old_cb_state = $self->{cb_state};
94 my @res;
95 eval {
96 my $nxt = [];
97
98 for my $rev (@{$self->{events}->{lc $ev}}) {
99 my $state = $self->{cb_state} = {};
100
101 my (@r) = $rev->[1]->($self, @arg);
102
103 push @$nxt, $rev unless $state->{remove};
104 push @res, @r if @r;
105 }
106 $self->{events}->{lc $ev} = $nxt;
107
108 for my $ev_frwd (keys %{$self->{event_forwards}}) {
109 my $rev = $self->{event_forwards}->{$ev_frwd};
110 my $state = $self->{cb_state} = {};
111
112 my (@r) = $rev->[1]->($self, $rev->[0], $ev, @arg);
113
114 if ($state->{remove}) {
115 delete $self->{event_forwards}->{$ev_frwd};
116 }
117
118 push @res, @r if @r;
119 }
120 };
121 if ($@) {
122 warn "Uncaught exception: $@";
123 }
124 $self->{cb_state} = $old_cb_state;
125
126 @res
127 }
128
129 =item B<unreg_me>
130
131 If this method is called from a callback on the first argument to the
132 callback (thats C<$self>) the callback will be deleted after it is finished.
133
134 =cut
135
136 sub unreg_me {
137 my ($self) = @_;
138 $self->{cb_state}->{remove} = 1;
139 }
140
141 =item B<add_forward ($obj, $forward_cb)>
142
143 This method allows to forward or copy all events to a object.
144 C<$forward_cb> will be called everytime an event is generated in C<$self>.
145 The first argument to the callback C<$forward_cb> will be <$self>, the second
146 will be C<$obj>, the third will be the event name and the rest will be
147 the event arguments. (For third and rest of argument also see description
148 of C<event>).
149
150 =cut
151
152 sub add_forward {
153 my ($self, $obj, $forward_cb) = @_;
154 $self->{event_forwards}->{$obj} = [$obj, $forward_cb];
155 }
156
157 =item B<remove_forward ($obj)>
158
159 This method removes a forward. C<$obj> must be the same
160 object that was given C<add_forward> as the C<$obj> argument.
161
162 =cut
163
164 sub remove_forward {
165 my ($self, $obj) = @_;
166 delete $self->{event_forwards}->{$obj};
167 }
168
169 =back
170
171 =head1 AUTHOR
172
173 Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
174
175 =head1 COPYRIGHT & LICENSE
176
177 Copyright 2007 Robin Redeker, all rights reserved.
178
179 This program is free software; you can redistribute it and/or modify it
180 under the same terms as Perl itself.
181
182 =cut
183
184 1;