NAME
Mojo::EventEmitter - Event emitter base class
SYNOPSIS
package Cat;
use Mojo::Base 'Mojo::EventEmitter';
# Emit events
sub poke {
my $self = shift;
$self->emit(roar => 3);
}
package main;
# Subscribe to events
my $tiger = Cat->new;
$tiger->on(roar => sub {
my ($tiger, $times) = @_;
say 'RAWR!' for 1 .. $times;
});
$tiger->poke;
DESCRIPTION
Mojo::EventEmitter is a simple base class for event emitting objects. Note that this module is EXPERIMENTAL and might change without warning!
METHODS
Mojo::EventEmitter inherits all methods from Mojo::Base and implements the following new ones.
emit
$e = $e->emit('foo');
$e = $e->emit('foo', 123);
Emit event.
emit_safe
$e = $e->emit_safe('foo');
$e = $e->emit_safe('foo', 123);
Emit event safely and emit error
event on failure.
has_subscribers
my $success = $e->has_subscribers('foo');
Check if event has subscribers.
on
my $cb = $e->on(foo => sub {...});
Subscribe to event.
once
my $cb = $e->once(foo => sub {...});
Subscribe to event and unsubscribe again after it has been emitted once.
subscribers
my $subscribers = $e->subscribers('foo');
All subscribers for event.
unsubscribe
$e->unsubscribe(foo => $cb);
Unsubscribe from event.
unsubscribe_all
$e->unsubscribe_all('foo');
Remove all subscribers from event.
DEBUGGING
You can set the MOJO_EVENTEMITTER_DEBUG
environment variable to get some advanced diagnostics information printed to STDERR
.
MOJO_EVENTEMITTER_DEBUG=1