[32205] | 1 | package Mojo::EventEmitter;
|
---|
| 2 | use Mojo::Base -base;
|
---|
| 3 |
|
---|
| 4 | use Scalar::Util qw(blessed weaken);
|
---|
| 5 |
|
---|
| 6 | use constant DEBUG => $ENV{MOJO_EVENTEMITTER_DEBUG} || 0;
|
---|
| 7 |
|
---|
| 8 | sub catch { $_[0]->on(error => $_[1]) and return $_[0] }
|
---|
| 9 |
|
---|
| 10 | sub emit {
|
---|
| 11 | my ($self, $name) = (shift, shift);
|
---|
| 12 |
|
---|
| 13 | if (my $s = $self->{events}{$name}) {
|
---|
| 14 | warn "-- Emit $name in @{[blessed $self]} (@{[scalar @$s]})\n" if DEBUG;
|
---|
| 15 | for my $cb (@$s) { $self->$cb(@_) }
|
---|
| 16 | }
|
---|
| 17 | else {
|
---|
| 18 | warn "-- Emit $name in @{[blessed $self]} (0)\n" if DEBUG;
|
---|
| 19 | die "@{[blessed $self]}: $_[0]" if $name eq 'error';
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | return $self;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | sub has_subscribers { !!shift->{events}{shift()} }
|
---|
| 26 |
|
---|
| 27 | sub on { push @{$_[0]{events}{$_[1]}}, $_[2] and return $_[2] }
|
---|
| 28 |
|
---|
| 29 | sub once {
|
---|
| 30 | my ($self, $name, $cb) = @_;
|
---|
| 31 |
|
---|
| 32 | weaken $self;
|
---|
| 33 | my $wrapper;
|
---|
| 34 | $wrapper = sub {
|
---|
| 35 | $self->unsubscribe($name => $wrapper);
|
---|
| 36 | $cb->(@_);
|
---|
| 37 | };
|
---|
| 38 | $self->on($name => $wrapper);
|
---|
| 39 | weaken $wrapper;
|
---|
| 40 |
|
---|
| 41 | return $wrapper;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | sub subscribers { shift->{events}{shift()} ||= [] }
|
---|
| 45 |
|
---|
| 46 | sub unsubscribe {
|
---|
| 47 | my ($self, $name, $cb) = @_;
|
---|
| 48 |
|
---|
| 49 | # One
|
---|
| 50 | if ($cb) {
|
---|
| 51 | $self->{events}{$name} = [grep { $cb ne $_ } @{$self->{events}{$name}}];
|
---|
| 52 | delete $self->{events}{$name} unless @{$self->{events}{$name}};
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | # All
|
---|
| 56 | else { delete $self->{events}{$name} }
|
---|
| 57 |
|
---|
| 58 | return $self;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | 1;
|
---|
| 62 |
|
---|
| 63 | =encoding utf8
|
---|
| 64 |
|
---|
| 65 | =head1 NAME
|
---|
| 66 |
|
---|
| 67 | Mojo::EventEmitter - Event emitter base class
|
---|
| 68 |
|
---|
| 69 | =head1 SYNOPSIS
|
---|
| 70 |
|
---|
| 71 | package Cat;
|
---|
| 72 | use Mojo::Base 'Mojo::EventEmitter';
|
---|
| 73 |
|
---|
| 74 | # Emit events
|
---|
| 75 | sub poke {
|
---|
| 76 | my $self = shift;
|
---|
| 77 | $self->emit(roar => 3);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | package main;
|
---|
| 81 |
|
---|
| 82 | # Subscribe to events
|
---|
| 83 | my $tiger = Cat->new;
|
---|
| 84 | $tiger->on(roar => sub {
|
---|
| 85 | my ($tiger, $times) = @_;
|
---|
| 86 | say 'RAWR!' for 1 .. $times;
|
---|
| 87 | });
|
---|
| 88 | $tiger->poke;
|
---|
| 89 |
|
---|
| 90 | =head1 DESCRIPTION
|
---|
| 91 |
|
---|
| 92 | L<Mojo::EventEmitter> is a simple base class for event emitting objects.
|
---|
| 93 |
|
---|
| 94 | =head1 EVENTS
|
---|
| 95 |
|
---|
| 96 | L<Mojo::EventEmitter> can emit the following events.
|
---|
| 97 |
|
---|
| 98 | =head2 error
|
---|
| 99 |
|
---|
| 100 | $e->on(error => sub {
|
---|
| 101 | my ($e, $err) = @_;
|
---|
| 102 | ...
|
---|
| 103 | });
|
---|
| 104 |
|
---|
| 105 | This is a special event for errors, it will not be emitted directly by this
|
---|
| 106 | class, but is fatal if unhandled. Subclasses may choose to emit it, but are not
|
---|
| 107 | required to do so.
|
---|
| 108 |
|
---|
| 109 | $e->on(error => sub {
|
---|
| 110 | my ($e, $err) = @_;
|
---|
| 111 | say "This looks bad: $err";
|
---|
| 112 | });
|
---|
| 113 |
|
---|
| 114 | =head1 METHODS
|
---|
| 115 |
|
---|
| 116 | L<Mojo::EventEmitter> inherits all methods from L<Mojo::Base> and implements
|
---|
| 117 | the following new ones.
|
---|
| 118 |
|
---|
| 119 | =head2 catch
|
---|
| 120 |
|
---|
| 121 | $e = $e->catch(sub {...});
|
---|
| 122 |
|
---|
| 123 | Subscribe to L</"error"> event.
|
---|
| 124 |
|
---|
| 125 | # Longer version
|
---|
| 126 | $e->on(error => sub {...});
|
---|
| 127 |
|
---|
| 128 | =head2 emit
|
---|
| 129 |
|
---|
| 130 | $e = $e->emit('foo');
|
---|
| 131 | $e = $e->emit('foo', 123);
|
---|
| 132 |
|
---|
| 133 | Emit event.
|
---|
| 134 |
|
---|
| 135 | =head2 has_subscribers
|
---|
| 136 |
|
---|
| 137 | my $bool = $e->has_subscribers('foo');
|
---|
| 138 |
|
---|
| 139 | Check if event has subscribers.
|
---|
| 140 |
|
---|
| 141 | =head2 on
|
---|
| 142 |
|
---|
| 143 | my $cb = $e->on(foo => sub {...});
|
---|
| 144 |
|
---|
| 145 | Subscribe to event.
|
---|
| 146 |
|
---|
| 147 | $e->on(foo => sub {
|
---|
| 148 | my ($e, @args) = @_;
|
---|
| 149 | ...
|
---|
| 150 | });
|
---|
| 151 |
|
---|
| 152 | =head2 once
|
---|
| 153 |
|
---|
| 154 | my $cb = $e->once(foo => sub {...});
|
---|
| 155 |
|
---|
| 156 | Subscribe to event and unsubscribe again after it has been emitted once.
|
---|
| 157 |
|
---|
| 158 | $e->once(foo => sub {
|
---|
| 159 | my ($e, @args) = @_;
|
---|
| 160 | ...
|
---|
| 161 | });
|
---|
| 162 |
|
---|
| 163 | =head2 subscribers
|
---|
| 164 |
|
---|
| 165 | my $subscribers = $e->subscribers('foo');
|
---|
| 166 |
|
---|
| 167 | All subscribers for event.
|
---|
| 168 |
|
---|
| 169 | # Unsubscribe last subscriber
|
---|
| 170 | $e->unsubscribe(foo => $e->subscribers('foo')->[-1]);
|
---|
| 171 |
|
---|
| 172 | # Change order of subscribers
|
---|
| 173 | @{$e->subscribers('foo')} = reverse @{$e->subscribers('foo')};
|
---|
| 174 |
|
---|
| 175 | =head2 unsubscribe
|
---|
| 176 |
|
---|
| 177 | $e = $e->unsubscribe('foo');
|
---|
| 178 | $e = $e->unsubscribe(foo => $cb);
|
---|
| 179 |
|
---|
| 180 | Unsubscribe from event.
|
---|
| 181 |
|
---|
| 182 | =head1 DEBUGGING
|
---|
| 183 |
|
---|
| 184 | You can set the C<MOJO_EVENTEMITTER_DEBUG> environment variable to get some
|
---|
| 185 | advanced diagnostics information printed to C<STDERR>.
|
---|
| 186 |
|
---|
| 187 | MOJO_EVENTEMITTER_DEBUG=1
|
---|
| 188 |
|
---|
| 189 | =head1 SEE ALSO
|
---|
| 190 |
|
---|
| 191 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
| 192 |
|
---|
| 193 | =cut
|
---|