With minor difference from lua, as documented.
Unified · Split
| @@ -96,8 +96,10 @@ Where the Lua APIs use ``nil`` to indicate error, JS APIs use ``undefined``. · 95 unmodified lines | ||
| 96 | 96 | |
| 97 | 97 | ``mp.command_native(table [,def])`` (LE) |
| 98 | 98 | |
| 99 | −``mp.command_native_async(table [,fn])`` (LE) Note: ``error`` is empty string on | |
| 100 | −success. | |
| 99 | +``id = mp.command_native_async(table [,fn])`` (LE) Notes: ``id`` is true-thy on | |
| 100 | +success, ``fn`` is called always a-sync, ``error`` is empty string on success. | |
| 101 | + | |
| 102 | +``mp.abort_async_command(id)`` | |
| 101 | 103 | |
| 102 | 104 | ``mp.get_property(name [,def])`` (LE) |
| 103 | 105 | |
| @@ -718,6 +718,13 @@ static void script__command_native_async(js_State *J, void *af) · 717 unmodified lines | ||
| 718 | 718 | push_status(J, mpv_command_node_async(jclient(J), id, &node)); |
| 719 | 719 | } |
| 720 | 720 | |
| 721 | +// args: async-command-id | |
| 722 | +static void script__abort_async_command(js_State *J) | |
| 723 | +{ | |
| 724 | + mpv_abort_async_command(jclient(J), jsL_checkuint64(J, 1)); | |
| 725 | + push_success(J); | |
| 726 | +} | |
| 727 | + | |
| 721 | 728 | // args: none, result in millisec |
| 722 | 729 | static void script_get_time_ms(js_State *J) |
| 723 | 730 | { |
| @@ -1232,6 +1239,7 @@ static const struct fn_entry main_fns[] = { · 508 unmodified lines | ||
| 1232 | 1239 | FN_ENTRY(commandv, 0), |
| 1233 | 1240 | AF_ENTRY(command_native, 2), |
| 1234 | 1241 | AF_ENTRY(_command_native_async, 2), |
| 1242 | + FN_ENTRY(_abort_async_command, 1), | |
| 1235 | 1243 | FN_ENTRY(get_property_bool, 2), |
| 1236 | 1244 | FN_ENTRY(get_property_number, 2), |
| 1237 | 1245 | AF_ENTRY(get_property_native, 2), |
| @@ -144,8 +144,15 @@ var async_next_id = 1; · 143 unmodified lines | ||
| 144 | 144 | |
| 145 | 145 | mp.command_native_async = function command_native_async(node, cb) { |
| 146 | 146 | var id = async_next_id++; |
| 147 | + cb = cb || function dummy() {}; | |
| 148 | + if (!mp._command_native_async(id, node)) { | |
| 149 | + var le = mp.last_error(); | |
| 150 | + setTimeout(cb, 0, false, undefined, le); /* callback async */ | |
| 151 | + mp._set_last_error(le); | |
| 152 | + return undefined; | |
| 153 | + } | |
| 147 | 154 | async_callbacks[id] = cb; |
| 148 | − return mp._command_native_async(id, node); | |
| 155 | + return id; | |
| 149 | 156 | } |
| 150 | 157 | |
| 151 | 158 | function async_command_handler(ev) { |
| @@ -157,6 +164,12 @@ function async_command_handler(ev) { · 5 unmodified lines | ||
| 157 | 164 | cb(true, ev.result, ""); |
| 158 | 165 | } |
| 159 | 166 | |
| 167 | +mp.abort_async_command = function abort_async_command(id) { | |
| 168 | + // cb will be invoked regardless, possibly with the abort result | |
| 169 | + if (async_callbacks[id]) | |
| 170 | + mp._abort_async_command(id); | |
| 171 | +} | |
| 172 | + | |
| 160 | 173 | /********************************************************************** |
| 161 | 174 | * key bindings |
| 162 | 175 | *********************************************************************/ |