NAME
Evo::Ee
VERSION
version 0.0227
DESCRIPTION
EventEmitter role for Evo classes
SYNOPSYS
package main;
use Evo;
{
package My::Class;
use Evo '-Class *';
with '-Ee';
# define available events
sub ee_events {qw( connection close )}
}
my $comp = My::Class->new();
# subscribe on the event
$comp->on(connection => sub($self, $id) { say "got $id" });
# emit event
$comp->emit(connection => 'MyID');
REQUIREMENTS
This role requires method ee_events
to be implemented in a derived class. It should return a list of available event names. Each invocation of "on" and "ee_remove" will be compared with this list and in case it doesn't exist an exception will be thrown
# throws Not recognized event "coNNection"
$comp->on(coNNection => sub($self, $id) { say "got $id" });
This will prevent people who use your class from the most common mistake in EventEmitter pattern.
METHODS
on
Subscbibe
$comp->on(connection => sub($self, @args) { say "$self got: " . join ';', @args });
The name of the event will be checked using ee_events
, which should be implemented by Evo class and return a list of available names
emit
Emit an event. The object will be passed to the event as the first argument, you can provide additional argument to the subscriber
$comp->emit(connection => 'arg1', 'arg2');
ee_add
ee_remove
Add and remove listener from the event by the name and subroutine.
my $ref = $comp->ee_add(connection => sub {"here"});
$comp->ee_remove($ref);
The name of the event will be checked using ee_events
, which should be implemented by class and return a list of available names
Don't use in the event (or weaken ref if you need to use it)
ee_remove_current
$comp->ee_add(
connection => sub($self) {
$self->ee_remove_current;
}
);
When called in the event, remove current event. Die outside an event
ee_listeners
my @listeners = $comp->ee_listeners('connection');
A list of listeners of the event. Right now a name wouldn't be checked, but this can be changed in the future
ee_check
$comp = $comp->ee_check('connection');
Check the event. If it wasn't in the derivered list returned by ee_events
, an exception will be thrown.
AUTHOR
alexbyk.com
COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by alexbyk.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.