Acme-Sort-Sleep
view release on metacpan or search on metacpan
local/lib/perl5/IO/Async/Notifier.pm view on Meta::CPAN
# You may distribute under the terms of either the GNU General Public License
# or the Artistic License (the same terms as Perl itself)
#
# (C) Paul Evans, 2006-2015 -- leonerd@leonerd.org.uk
package IO::Async::Notifier;
use strict;
use warnings;
our $VERSION = '0.70';
use Carp;
use Scalar::Util qw( weaken );
use Future 0.26; # ->is_failed
use IO::Async::Debug;
# Perl 5.8.4 cannot do trampolines by modiying @_ then goto &$code
use constant HAS_BROKEN_TRAMPOLINES => ( $] == "5.008004" );
=head1 NAME
C<IO::Async::Notifier> - base class for L<IO::Async> event objects
=head1 SYNOPSIS
Usually not directly used by a program, but one valid use case may be:
use IO::Async::Notifier;
use IO::Async::Stream;
use IO::Async::Signal;
use IO::Async::Loop;
my $loop = IO::Async::Loop->new;
my $notifier = IO::Async::Notifier->new;
$notifier->add_child(
IO::Async::Stream->new_for_stdin(
on_read => sub {
my $self = shift;
my ( $buffref, $eof ) = @_;
while( $$buffref =~ s/^(.*)\n// ) {
print "You said $1\n";
}
return 0;
},
)
);
$notifier->add_child(
IO::Async::Signal->new(
name => 'INT',
on_receipt => sub {
print "Goodbye!\n";
$loop->stop;
},
)
);
$loop->add( $notifier );
$loop->run;
=head1 DESCRIPTION
This object class forms the basis for all the other event objects that an
L<IO::Async> program uses. It provides the lowest level of integration with a
L<IO::Async::Loop> container, and a facility to collect Notifiers together, in
local/lib/perl5/IO/Async/Notifier.pm view on Meta::CPAN
=head2 configure
$notifier->configure( %params )
Adjust the named parameters of the C<Notifier> as given by the C<%params>
hash.
=cut
# for subclasses to override and call down to
sub configure
{
my $self = shift;
my %params = @_;
foreach (qw( notifier_name on_error )) {
$self->{"IO_Async_Notifier__$_"} = delete $params{$_} if exists $params{$_};
}
$self->configure_unknown( %params ) if keys %params;
}
sub configure_unknown
{
my $self = shift;
my %params = @_;
my $class = ref $self;
croak "Unrecognised configuration keys for $class - " . join( " ", keys %params );
}
=head2 loop
$loop = $notifier->loop
Returns the L<IO::Async::Loop> that this Notifier is a member of.
=cut
sub loop
{
my $self = shift;
return $self->{IO_Async_Notifier__loop}
}
*get_loop = \&loop;
# Only called by IO::Async::Loop, not external interface
sub __set_loop
{
my $self = shift;
my ( $loop ) = @_;
# early exit if no change
return if !$loop and !$self->loop or
$loop and $self->loop and $loop == $self->loop;
$self->_remove_from_loop( $self->loop ) if $self->loop;
$self->{IO_Async_Notifier__loop} = $loop;
weaken( $self->{IO_Async_Notifier__loop} ); # To avoid a cycle
$self->_add_to_loop( $self->loop ) if $self->loop;
}
=head2 notifier_name
$name = $notifier->notifier_name
Returns the name to identify this Notifier. If a has not been set, it will
return the empty string. Subclasses may wish to override this behaviour to
return some more useful information, perhaps from configured parameters.
=cut
sub notifier_name
{
my $self = shift;
return $self->{IO_Async_Notifier__notifier_name} || "";
}
=head2 adopt_future
$f = $notifier->adopt_future( $f )
Stores a reference to the L<Future> instance within the notifier itself, so
the reference doesn't get lost. This reference will be dropped when the future
becomes ready (either by success or failure). Additionally, if the future
failed the notifier's C<invoke_error> method will be informed.
This means that if the notifier does not provide an C<on_error> handler, nor
is there one anywhere in the parent chain, this will be fatal to the caller of
C<< $f->fail >>. To avoid this being fatal if the failure is handled
elsewhere, use the C<else_done> method on the future to obtain a sequence one
that never fails.
$notifier->adopt_future( $f->else_done() )
The future itself is returned.
=cut
sub adopt_future
{
my $self = shift;
my ( $f ) = @_;
my $fkey = "$f"; # stable stringification
$self->{IO_Async_Notifier__futures}{$fkey} = $f;
$f->on_ready( $self->_capture_weakself( sub {
my $self = shift;
my ( $f ) = @_;
delete $self->{IO_Async_Notifier__futures}{$fkey};
$self->invoke_error( $f->failure ) if $f->is_failed;
}));
return $f;
local/lib/perl5/IO/Async/Notifier.pm view on Meta::CPAN
from the L<IO::Async::Loop> that manages their parent.
=cut
=head2 parent
$parent = $notifier->parent
Returns the parent of the notifier, or C<undef> if does not have one.
=cut
sub parent
{
my $self = shift;
return $self->{IO_Async_Notifier__parent};
}
=head2 children
@children = $notifier->children
Returns a list of the child notifiers contained within this one.
=cut
sub children
{
my $self = shift;
return unless $self->{IO_Async_Notifier__children};
return @{ $self->{IO_Async_Notifier__children} };
}
=head2 add_child
$notifier->add_child( $child )
Adds a child notifier. This notifier will be added to the containing loop, if
the parent has one. Only a notifier that does not currently have a parent and
is not currently a member of any loop may be added as a child. If the child
itself has grandchildren, these will be recursively added to the containing
loop.
=cut
sub add_child
{
my $self = shift;
my ( $child ) = @_;
croak "Cannot add a child that already has a parent" if defined $child->{IO_Async_Notifier__parent};
croak "Cannot add a child that is already a member of a loop" if defined $child->loop;
if( defined( my $loop = $self->loop ) ) {
$loop->add( $child );
}
push @{ $self->{IO_Async_Notifier__children} }, $child;
$child->{IO_Async_Notifier__parent} = $self;
weaken( $child->{IO_Async_Notifier__parent} );
return;
}
=head2 remove_child
$notifier->remove_child( $child )
Removes a child notifier. The child will be removed from the containing loop,
if the parent has one. If the child itself has grandchildren, these will be
recurively removed from the loop.
=cut
sub remove_child
{
my $self = shift;
my ( $child ) = @_;
LOOP: {
my $childrenref = $self->{IO_Async_Notifier__children};
for my $i ( 0 .. $#$childrenref ) {
next unless $childrenref->[$i] == $child;
splice @$childrenref, $i, 1, ();
last LOOP;
}
croak "Cannot remove child from a parent that doesn't contain it";
}
undef $child->{IO_Async_Notifier__parent};
if( defined( my $loop = $self->loop ) ) {
$loop->remove( $child );
}
}
=head2 remove_from_parent
$notifier->remove_from_parent
Removes this notifier object from its parent (either another notifier object
or the containing loop) if it has one. If the notifier is not a child of
another notifier nor a member of a loop, this method does nothing.
=cut
sub remove_from_parent
{
my $self = shift;
if( my $parent = $self->parent ) {
$parent->remove_child( $self );
}
elsif( my $loop = $self->loop ) {
$loop->remove( $self );
}
}
=head1 SUBCLASS METHODS
local/lib/perl5/IO/Async/Notifier.pm view on Meta::CPAN
=head2 _capture_weakself
$mref = $notifier->_capture_weakself( $code )
Returns a new CODE ref which, when invoked, will invoke the originally-passed
ref, with additionally a reference to the Notifier as its first argument. The
Notifier reference is stored weakly in C<$mref>, so this CODE ref may be
stored in the Notifier itself without creating a cycle.
For example,
my $mref = $notifier->_capture_weakself( sub {
my ( $notifier, $arg ) = @_;
print "Notifier $notifier got argument $arg\n";
} );
$mref->( 123 );
This is provided as a utility for Notifier subclasses to use to build a
callback CODEref to pass to a Loop method, but which may also want to store
the CODE ref internally for efficiency.
The C<$code> argument may also be a plain string, which will be used as a
method name; the returned CODE ref will then invoke that method on the object.
In this case the method name is stored symbolically in the returned CODE
reference, and dynamically dispatched each time the reference is invoked. This
allows it to follow code reloading, dynamic replacement of class methods, or
other similar techniques.
If the C<$mref> CODE reference is being stored in some object other than the
one it refers to, remember that since the Notifier is only weakly captured, it
is possible that it has been destroyed by the time the code runs, and so the
reference will be passed as C<undef>. This should be protected against by the
code body.
$other_object->{on_event} = $notifier->_capture_weakself( sub {
my $notifier = shift or return;
my ( @event_args ) = @_;
...
} );
For stand-alone generic implementation of this behaviour, see also L<curry>
and C<curry::weak>.
=cut
sub _capture_weakself
{
my $self = shift;
my ( $code ) = @_; # actually bare method names work too
if( !ref $code ) {
my $class = ref $self;
# Don't save this coderef, or it will break dynamic method dispatch,
# which means code reloading, dynamic replacement, or other funky
# techniques stop working
$self->can( $code ) or
croak qq(Can't locate object method "$code" via package "$class");
}
weaken $self;
return sub {
my $cv = ref( $code ) ? $code : $self->can( $code );
if( HAS_BROKEN_TRAMPOLINES ) {
return $cv->( $self, @_ );
}
else {
unshift @_, $self;
goto &$cv;
}
};
}
=head2 _replace_weakself
$mref = $notifier->_replace_weakself( $code )
Returns a new CODE ref which, when invoked, will invoke the originally-passed
ref, with a reference to the Notifier replacing its first argument. The
Notifier reference is stored weakly in C<$mref>, so this CODE ref may be
stored in the Notifier itself without creating a cycle.
For example,
my $mref = $notifier->_replace_weakself( sub {
my ( $notifier, $arg ) = @_;
print "Notifier $notifier got argument $arg\n";
} );
$mref->( $object, 123 );
This is provided as a utility for Notifier subclasses to use for event
callbacks on other objects, where the delegated object is passed in the
function's arguments.
The C<$code> argument may also be a plain string, which will be used as a
method name; the returned CODE ref will then invoke that method on the object.
As with C<_capture_weakself> this is stored symbolically.
As with C<_capture_weakself>, care should be taken against Notifier
destruction if the C<$mref> CODE reference is stored in some other object.
=cut
sub _replace_weakself
{
my $self = shift;
my ( $code ) = @_; # actually bare method names work too
if( !ref $code ) {
# Don't save this coderef, see _capture_weakself for why
my $class = ref $self;
$self->can( $code ) or
croak qq(Can't locate object method "$code" via package "$class");
}
weaken $self;
return sub {
my $cv = ref( $code ) ? $code : $self->can( $code );
if( HAS_BROKEN_TRAMPOLINES ) {
return $cv->( $self, @_[1..$#_] );
}
else {
# Don't assign to $_[0] directly or we will change caller's first argument
shift @_;
unshift @_, $self;
goto &$cv;
}
};
}
=head2 can_event
$code = $notifier->can_event( $event_name )
Returns a C<CODE> reference if the object can perform the given event name,
either by a configured C<CODE> reference parameter, or by implementing a
method. If the object is unable to handle this event, C<undef> is returned.
=cut
sub can_event
{
my $self = shift;
my ( $event_name ) = @_;
return $self->{$event_name} || $self->can( $event_name );
}
=head2 make_event_cb
$callback = $notifier->make_event_cb( $event_name )
Returns a C<CODE> reference which, when invoked, will execute the given event
handler. Event handlers may either be subclass methods, or parameters given to
the C<new> or C<configure> method.
The event handler can be passed extra arguments by giving them to the C<CODE>
reference; the first parameter received will be a reference to the notifier
itself. This is stored weakly in the closure, so it is safe to store the
resulting C<CODE> reference in the object itself without causing a reference
cycle.
=cut
sub make_event_cb
{
my $self = shift;
my ( $event_name ) = @_;
my $code = $self->can_event( $event_name )
or croak "$self cannot handle $event_name event";
my $caller = caller;
( run in 1.474 second using v1.01-cache-2.11-cpan-39bf76dae61 )