1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
NAME
Role::EventEmitter - Event emitter role
SYNOPSIS
package Channel;
use Moo;
with 'Role::EventEmitter';
# Emit events
sub send_message {
my $self = shift;
$self->emit(message => @_);
}
package main;
# Subscribe to events
my $channel_a = Channel->new;
$channel_a->on(message => sub {
my ($channel, $text) = @_;
say "Received message: $text";
});
$channel_a->send_message('All is well');
DESCRIPTION
Role::EventEmitter is a simple Role::Tiny role for event emitting
objects based on Mojo::EventEmitter. This role can be applied to any
hash-based object class such as those created with Class::Tiny, Moo, or
Moose.
EVENTS
Role::EventEmitter can emit the following events.
error
$e->on(error => sub {
my ($e, $err) = @_;
...
});
This is a special event for errors, it will not be emitted directly by
this role but is fatal if unhandled.
$e->on(error => sub {
my ($e, $err) = @_;
say "This looks bad: $err";
});
METHODS
Role::EventEmitter composes the following methods.
catch
$e = $e->catch(sub {...});
Subscribe to "error" event.
# Longer version
$e->on(error => sub {...});
emit
$e = $e->emit('foo');
$e = $e->emit('foo', 123);
Emit event.
has_subscribers
my $bool = $e->has_subscribers('foo');
Check if event has subscribers.
on
my $cb = $e->on(foo => sub {...});
Subscribe to event.
$e->on(foo => sub {
my ($e, @args) = @_;
...
});
once
my $cb = $e->once(foo => sub {...});
Subscribe to event and unsubscribe again after it has been emitted
once.
$e->once(foo => sub {
my ($e, @args) = @_;
...
});
once_f
my $f = $e->once_f('foo');
Subscribe to event as in "once", returning a Future that will be marked
complete after it has been emitted once. Requires Future to be
installed.
my $f = $e->once_f('foo')->on_done(sub {
my ($e, @args) = @_;
...
});
To unsubscribe the returned Future early, cancel it.
$f->cancel;
subscribers
my $subscribers = $e->subscribers('foo');
All subscribers for event.
# Unsubscribe last subscriber
$e->unsubscribe(foo => $e->subscribers('foo')->[-1]);
# Change order of subscribers
@{$e->subscribers('foo')} = reverse @{$e->subscribers('foo')};
unsubscribe
$e = $e->unsubscribe('foo');
$e = $e->unsubscribe(foo => $cb);
Unsubscribe from event. Related Futures will also be cancelled.
DEBUGGING
You can set the ROLE_EVENTEMITTER_DEBUG environment variable to get
some advanced diagnostics information printed to STDERR.
ROLE_EVENTEMITTER_DEBUG=1
BUGS
Report any issues on the public bugtracker.
AUTHOR
Dan Book <dbook@cpan.org>
Code and tests adapted from Mojo::EventEmitter, an event emitter base
class by the Mojolicious team.
COPYRIGHT AND LICENSE
Copyright (c) 2008-2015 Sebastian Riedel.
Copyright (c) 2015 Dan Book for adaptation to a role and further
changes.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
SEE ALSO
Mojo::EventEmitter, Mixin::Event::Dispatch, Beam::Emitter,
Event::Distributor
|