NAME
Evo::Ee
VERSION
version 0.0199
DESCRIPTION
EventEmitter role for component
SYNOPSYS
package main;
use Evo;
{
package My::Comp;
use Evo '-Comp *';
with '-Ee';
# define available events
sub ee_events {qw( connection close )}
}
my $comp = My::Comp::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 component 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 component and return a list of available names
emit
Emit an event. The component will be passed to the event as the first argument, you can provide additional argument to the subscriber
$comp->emit(connection => 'arg1', 'arg2');
ee_remove
Remove listener from the event by the name and subroutine.
my $sub;
$comp->on(connection => $sub = sub {"here"});
$comp->ee_remove(connection => $sub);
The name of the event will be checked using ee_events
, which should be implemented by component and return a list of available names
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.