lua: command_native_async: always callback a-sync

Previously if the raw command_native_async returned an error then the
callback function was run directly. This meant that script writers
potentially had to account for both synchronous and asynchronous logic
in the callback, which can happen even with seemingly 'safe' commands
if the mpv event queue is full.

This was at odds with the Javascript implementation of
the function, which always runs the callback asynchronously.

Now the mp.add_timeout function is used to run the callback
asynchronously on error, replicating the Javascript implementation.

This provides consistency for developers in how the callback is handled
in Lua, and increases consistency between the Lua and Javascript APIs.
This commit is contained in:
CogentRedTester 2022-06-22 21:06:09 +09:30 committed by Avi Halachmi (:avih)
parent 24f4582b6f
commit 099ae86717
3 changed files with 4 additions and 2 deletions

View File

@ -91,7 +91,7 @@ Where the Lua APIs use ``nil`` to indicate error, JS APIs use ``undefined``.
``mp.command_native(table [,def])`` (LE)
``id = mp.command_native_async(table [,fn])`` (LE) Notes: ``id`` is true-thy on
success, ``fn`` is called always a-sync, ``error`` is empty string on success.
success, ``error`` is empty string on success.
``mp.abort_async_command(id)``

View File

@ -191,6 +191,8 @@ The ``mp`` module is preloaded, although it can be loaded manually with
If starting the command failed for some reason, ``nil, error`` is returned,
and ``fn`` is called indicating failure, using the same error value.
``fn`` is always called asynchronously, even if the command failed to start.
``mp.abort_async_command(t)``
Abort a ``mp.command_native_async`` call. The argument is the return value
of that command (which starts asynchronous execution of the command).

View File

@ -617,7 +617,7 @@ function mp.command_native_async(node, cb)
async_next_id = async_next_id + 1
local res, err = mp.raw_command_native_async(id, node)
if not res then
cb(false, nil, err)
mp.add_timeout(0, function() cb(false, nil, err) end)
return res, err
end
local t = {cb = cb, id = id}