--- AnyEvent-MPV/MPV.pm 2023/03/20 13:32:52 1.11 +++ AnyEvent-MPV/MPV.pm 2023/03/20 22:10:37 1.14 @@ -220,7 +220,8 @@ our $JSON = eval { require JSON::XS; JSON::XS:: } || do { require JSON::PP; JSON::PP:: }; -our $JSON_CODER = +our $JSON_ENCODER = $JSON->new->utf8; +our $JSON_DECODER = $JSON->new->latin1; our $mpv_path; # last mpv path used our $mpv_optionlist; # output of mpv --list-options @@ -402,7 +403,7 @@ if ("{" eq substr $1, 0, 1) { eval { - my $reply = $JSON->new->latin1->decode ($1); + my $reply = $JSON_DECODER->decode ($1); if (defined (my $event = delete $reply->{event})) { if ( @@ -470,7 +471,7 @@ $self->{cmdcv}{++$reqid} = $cv; - my $cmd = $JSON->new->utf8->encode ({ command => ref $_[0] ? $_[0] : \@_, request_id => $reqid*1 }); + my $cmd = $JSON_ENCODER->encode ({ command => ref $_[0] ? $_[0] : \@_, request_id => $reqid*1 }); # (un-)apply escape_binary hack $cmd =~ s/\xf4\x8e\x97\x9f(..)/sprintf sprintf "\\x%02x", hex $1/ges; # f48e979f == 10e5df in utf-8 @@ -696,11 +697,17 @@ completely ignore this method and handle events in a C handler, or mix both approaches as you see fit. +Note that unlike commands, event handlers are registered immediately, that +is, you can issue a command, then register an event handler and then get +an event for this handler I the command is even sent to F. If +this kind of race is an issue, you can issue a dummy command such as +C and register the handler when the reply is received. + =cut sub AnyEvent::MPV::Unevent::DESTROY { - my ($evtcb, $evtid) = @{$_[0]}; - delete $evtcb->{$evtid}; + my ($evtcb, $event, $evtid) = @{$_[0]}; + delete $evtcb->{$event}{$evtid}; } sub register_event { @@ -712,7 +719,7 @@ $self->{evtcb}{$event}{$evtid} = $cb; defined wantarray - and bless [$self->{evtcb}, $evtid], AnyEvent::MPV::Unevent:: + and bless [$self->{evtcb}, $event, $evtid], AnyEvent::MPV::Unevent:: } =item [$guard] = $mpv->observe_property ($name => $coderef->($mpv, $name, $value))