AnyEvent-MPV
view release on metacpan or search on metacpan
}
$self->{fh} = $fh;
my $trace = $self->{trace} || sub { };
$trace = sub { warn "$_[0] $_[1]\n" } if $trace && !ref $trace;
my $buf;
Scalar::Util::weaken $self;
$self->{rw} = AE::io $fh, 0, sub {
if (sysread $fh, $buf, 8192, length $buf) {
while ($buf =~ s/^([^\n]+)\n//) {
$trace->("mpv>" => "$1");
if ("{" eq substr $1, 0, 1) {
eval {
my $reply = $JSON_DECODER->decode ($1);
sub _observe_property {
my ($self, $type, $property, $cb) = @_;
my $obsid = OBSID + ++$self->{obsid};
$self->cmd ($type => $obsid+0, $property);
$self->{obscb}{$obsid} = $cb;
defined wantarray and do {
my $unobserve = bless [$self, $self->{obscb}, $obsid], AnyEvent::MPV::Unobserve::;
Scalar::Util::weaken $unobserve->[0];
$unobserve
}
}
sub observe_property {
my ($self, $property, $cb) = @_;
$self->_observe_property (observe_property => $property, $cb)
}
return if $guards != $self->{mpv_guards};
Commands do not have guards since they cnanot be cancelled, so we don't
have to do this for commands. But what prevents us form misinterpreting
an old event? Since F<mpv> (by default) handles commands synchronously,
we can queue a dummy command, whose only purpose is to tell us when all
previous commands are done. We use C<get_version> for this.
The simplified code looks like this:
Scalar::Util::weaken $self;
$mpv->cmd ("get_version")->cb (sub {
$guards->{file_loaded} = $mpv->register_event (file_loaded => sub {
delete $guards->{file_loaded};
return if $guards != $self->{mpv_guards};
$mpv->cmd (get_property => "video-format")->cb (sub {
return if $guards != $self->{mpv_guards};
return if $guards != $self->{mpv_guards};
Commands do not have guards since they cnanot be cancelled, so we don't
have to do this for commands. But what prevents us form misinterpreting
an old event? Since mpv (by default) handles commands synchronously, we
can queue a dummy command, whose only purpose is to tell us when all
previous commands are done. We use "get_version" for this.
The simplified code looks like this:
Scalar::Util::weaken $self;
$mpv->cmd ("get_version")->cb (sub {
$guards->{file_loaded} = $mpv->register_event (file_loaded => sub {
delete $guards->{file_loaded};
return if $guards != $self->{mpv_guards};
$mpv->cmd (get_property => "video-format")->cb (sub {
return if $guards != $self->{mpv_guards};
( run in 0.882 second using v1.01-cache-2.11-cpan-65fba6d93b7 )