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