BeamX-Peer-Emitter

 view release on metacpan or  search on metacpan

lib/BeamX/Peer/Emitter.pm.orig  view on Meta::CPAN

package BeamX::Peer::Emitter;

# ABSTRACT: Beam::Emitter with peer-to-peer messaging

use Types::Standard ':all';
use Safe::Isa;
use Scalar::Util qw[ refaddr ];
use Try::Tiny;

use Moo::Role;

our %LoopDetect;

use namespace::clean;

with 'Beam::Emitter';

our $VERSION = '0.003';

sub _find_listener {

    my ( $self, $peer, $name ) = @_;

    return if !defined $peer;

    return ( grep { $_->has_peer && $_->peer == $peer }
          $self->listeners( $name ) )[0];
}

=method subscribe

  # subscribe as Beam::Emitter does
  $emitter->subscribe( $event_name, $subref, [, %args] );

Subscribe to the named event from C<$emitter>.  C<$subref>
will be called when the event is emitted.

By default, the emitter tracks the listener with an object of class
L<BeamX::Peer::Listener>.  C<%args> is used to pass arguments
to its constructor.

To enable C<$emitter> to send the event directly to a C<$peer> via
the L<send> method, specify the peer with the C<peer> key in C<%args>.

  $emitter->subscribe( $event_name, $subref, peer => $peer, %args );

To use a different listener class, subclass B<BeamX::Peer::Emitter>
and pass its name via the C<class> key in C<%args>.

  $emitter->subscribe( $event_name, $subref, class => MyListener, %args );

=cut

around subscribe => sub {

    my $orig = shift;
    my ( $self, $event_name, $orig_cb ) = @_;

    my $sub;

    $sub = sub {

	my $addr = refaddr $sub;

        my @args = @_;

        try {
	    confess( __PACKAGE__



( run in 2.198 seconds using v1.01-cache-2.11-cpan-364913b4093 )