NAME
Net::XMPP2::Event - Event handler class
SYNOPSIS
package foo;
use Net::XMPP2::Event;
our @ISA = qw/Net::XMPP2::Event/;
package main;
my $o = foo->new;
$o->reg_cb (foo => sub { ...; 1 });
$o->event (foo => 1, 2, 3);
DESCRIPTION
This module is just a small helper module for the connection and client classes.
You may only derive from this package.
METHODS
- set_exception_cb ($cb)
-
If some event callback threw an exception then
$cbis called with the exception as first argument. - reg_cb ($eventname1, $cb1, [$eventname2, $cb2, ...])
-
This method registers a callback
$cb1for the event with the name$eventname1. You can also pass multiple of these eventname => callback pairs.The return value will be an ID that represents the set of callbacks you have installed. Call
unreg_cbwith that ID to remove those callbacks again.To see a documentation of emitted events please take a look at the EVENTS section in the classes that inherit from this one.
The callbacks will be called in an array context. If a callback doesn't want to return any value it should return an empty list. All elements of the returned list will be accumulated and the semantic of the accumulated return values depends on the events.
For every event there are two other events emitted:
Before the callbacks for
$eventnameis being exectued the event"before_$eventname"is being emitted. And after the callbacks for$eventnamehave been run, the event"after_$eventname"is being emitted.The
"before_$eventname"callbacks allow you to stop the execution of all callbacks for the event$eventnameand"after_$eventname". This can be used to intercept events and stop them.(Please note that for Net::XMPP2::Ext::* there are special events for the runs before and after the events. They are named
"ext_before_$eventname"and"ext_after_$eventname". If you are writing an extension you should use these events to ensure that users of Net::XMPP2 can still intercept everything.)If you give reg_cb a special argument called
_while_referencedyou can prevent callbacks from being executed once the reference in the second argument becomes undef. This works by converting the internal reference of the argument to_while_referencedto a weak reference and looking whether that reference becomes undef.It works like this:
Scalar::Util::weaken $window; $event_source->reg_cb ( _while_referenced => $window, disconnect => sub { $window->destroy } );Whenever the
disconnectevent is emitted now and$windowdoesn't exist anymore the callback will be removed; - unreg_cb ($id)
-
Removes the set
$idof registered callbacks.$idis the return value of areg_cbcall. - event ($eventname, @args)
-
Emits the event
$eventnameand passes the arguments@args. The return value is a list of defined return values from the event callbacks.See also the specification of the before and after events in
reg_cbabove. - _event ($eventname, @args)
-
This directly executes the event
$eventnamewithout executing callbacks of the before and after events (as specified inreg_cbabove). - unreg_me
-
If this method is called from a callback on the first argument to the callback (thats
$self) the callback will be deleted after it is finished. - stop_event
-
When called in a 'before_' event callback then the execution of the event is stopped after all 'before_' callbacks have been run.
- add_forward ($obj, $forward_cb)
-
This method allows to forward or copy all events to an object.
$forward_cbwill be called everytime an event is generated in$self. The first argument to the callback$forward_cbwill be <$self>, the second will be$obj, the third will be the event name and the rest will be the event arguments. (For third and rest of argument also see description ofevent).(Please note that it might be most useful to call
_eventin the callback to allow objects that receive the forwarded events to react better.) - remove_forward ($obj)
-
This method removes a forward.
$objmust be the same object that was givenadd_forwardas the$objargument. - remove_all_callbacks
-
This method removes all registered event callbacks and forwards from this object.
AUTHOR
Robin Redeker, <elmex at ta-sa.org>, JID: <elmex at jabber.org>
COPYRIGHT & LICENSE
Copyright 2007 Robin Redeker, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.