From 44f8dccfb6054b08803101ab34dffd9cc8a6245f Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Tue, 12 Mar 2019 11:35:55 +0200 Subject: js: expose mpv_abort_async_command() (match dbe831bd) With minor difference from lua, as documented. --- DOCS/man/javascript.rst | 6 ++++-- player/javascript.c | 8 ++++++++ player/javascript/defaults.js | 15 ++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/DOCS/man/javascript.rst b/DOCS/man/javascript.rst index 77a2ad8817..8399c77d6a 100644 --- a/DOCS/man/javascript.rst +++ b/DOCS/man/javascript.rst @@ -96,8 +96,10 @@ Where the Lua APIs use ``nil`` to indicate error, JS APIs use ``undefined``. ``mp.command_native(table [,def])`` (LE) -``mp.command_native_async(table [,fn])`` (LE) Note: ``error`` is empty string on -success. +``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. + +``mp.abort_async_command(id)`` ``mp.get_property(name [,def])`` (LE) diff --git a/player/javascript.c b/player/javascript.c index 95d2b2f317..acdc30bad0 100644 --- a/player/javascript.c +++ b/player/javascript.c @@ -718,6 +718,13 @@ static void script__command_native_async(js_State *J, void *af) push_status(J, mpv_command_node_async(jclient(J), id, &node)); } +// args: async-command-id +static void script__abort_async_command(js_State *J) +{ + mpv_abort_async_command(jclient(J), jsL_checkuint64(J, 1)); + push_success(J); +} + // args: none, result in millisec static void script_get_time_ms(js_State *J) { @@ -1232,6 +1239,7 @@ static const struct fn_entry main_fns[] = { FN_ENTRY(commandv, 0), AF_ENTRY(command_native, 2), AF_ENTRY(_command_native_async, 2), + FN_ENTRY(_abort_async_command, 1), FN_ENTRY(get_property_bool, 2), FN_ENTRY(get_property_number, 2), AF_ENTRY(get_property_native, 2), diff --git a/player/javascript/defaults.js b/player/javascript/defaults.js index cc7fe04497..c91228087d 100644 --- a/player/javascript/defaults.js +++ b/player/javascript/defaults.js @@ -144,8 +144,15 @@ var async_next_id = 1; mp.command_native_async = function command_native_async(node, cb) { var id = async_next_id++; + cb = cb || function dummy() {}; + if (!mp._command_native_async(id, node)) { + var le = mp.last_error(); + setTimeout(cb, 0, false, undefined, le); /* callback async */ + mp._set_last_error(le); + return undefined; + } async_callbacks[id] = cb; - return mp._command_native_async(id, node); + return id; } function async_command_handler(ev) { @@ -157,6 +164,12 @@ function async_command_handler(ev) { cb(true, ev.result, ""); } +mp.abort_async_command = function abort_async_command(id) { + // cb will be invoked regardless, possibly with the abort result + if (async_callbacks[id]) + mp._abort_async_command(id); +} + /********************************************************************** * key bindings *********************************************************************/ -- cgit v1.2.3