/usr/share/perl5/Mojo/EventEmitter.pm is in libmojolicious-perl 2.98+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | package Mojo::EventEmitter;
use Mojo::Base -base;
use Scalar::Util qw(blessed weaken);
use constant DEBUG => $ENV{MOJO_EVENTEMITTER_DEBUG} || 0;
# "Are we there yet?
# No
# Are we there yet?
# No
# Are we there yet?
# No
# ...Where are we going?"
sub emit {
my ($self, $name) = (shift, shift);
if (my $s = $self->{events}{$name}) {
warn "-- Emit @{[blessed($self)]} $name (@{[scalar(@$s)]})\n" if DEBUG;
for my $cb (@$s) { $self->$cb(@_) }
}
elsif (DEBUG) { warn "-- Emit @{[blessed($self)]} $name (0)\n" }
return $self;
}
sub emit_safe {
my ($self, $name) = (shift, shift);
if (my $s = $self->{events}{$name}) {
warn "-- Safe @{[blessed($self)]} $name (@{[scalar(@$s)]})\n" if DEBUG;
for my $cb (@$s) {
if (!eval { $self->$cb(@_); 1 } && $name ne 'error') {
$self->once(error => sub { warn $_[1] })
unless $self->has_subscribers('error');
$self->emit_safe('error', qq{Event "$name" failed: $@});
}
}
}
elsif (DEBUG) { warn "-- Safe @{[blessed($self)]} $name (0)\n" }
return $self;
}
sub has_subscribers { scalar @{shift->subscribers(shift)} }
sub on {
my ($self, $name, $cb) = @_;
push @{$self->{events}{$name} ||= []}, $cb;
return $cb;
}
sub once {
my ($self, $name, $cb) = @_;
weaken $self;
my $wrapper;
$wrapper = sub {
$self->unsubscribe($name => $wrapper);
$cb->(@_);
};
$self->on($name => $wrapper);
weaken $wrapper;
return $wrapper;
}
sub subscribers { shift->{events}{shift()} || [] }
# "Back you robots!
# Nobody ruins my family vacation but me!
# And maybe the boy."
sub unsubscribe {
my ($self, $name, $cb) = @_;
# All
unless ($cb) {
delete $self->{events}{$name};
return $self;
}
# One
$self->{events}{$name} = [grep { $cb ne $_ } @{$self->{events}{$name}}];
return $self;
}
1;
=head1 NAME
Mojo::EventEmitter - Event emitter base class
=head1 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;
=head1 DESCRIPTION
L<Mojo::EventEmitter> is a simple base class for event emitting objects.
=head1 METHODS
L<Mojo::EventEmitter> inherits all methods from L<Mojo::Base> and
implements the following new ones.
=head2 C<emit>
$e = $e->emit('foo');
$e = $e->emit('foo', 123);
Emit event.
=head2 C<emit_safe>
$e = $e->emit_safe('foo');
$e = $e->emit_safe('foo', 123);
Emit event safely and emit C<error> event on failure.
=head2 C<has_subscribers>
my $success = $e->has_subscribers('foo');
Check if event has subscribers.
=head2 C<on>
my $cb = $e->on(foo => sub {...});
Subscribe to event.
$e->on(foo => sub {
my ($e, @args) = @_;
...
});
=head2 C<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) = @_;
...
});
=head2 C<subscribers>
my $subscribers = $e->subscribers('foo');
All subscribers for event.
# Unsubscribe last subscriber
$e->unsubscribe(foo => $e->subscribers('foo')->[-1]);
=head2 C<unsubscribe>
$e = $e->unsubscribe('foo');
$e = $e->unsubscribe(foo => $cb);
Unsubscribe from event.
=head1 DEBUGGING
You can set the C<MOJO_EVENTEMITTER_DEBUG> environment variable to get some
advanced diagnostics information printed to C<STDERR>.
MOJO_EVENTEMITTER_DEBUG=1
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|