diff options
author | Avi Halachmi (:avih) <avihpit@yahoo.com> | 2020-08-07 13:42:27 +0300 |
---|---|---|
committer | Avi Halachmi (:avih) <avihpit@yahoo.com> | 2020-08-07 15:12:20 +0300 |
commit | d5a02dd9342f5c61f3e3a9caf2082dd46b5099e3 (patch) | |
tree | a6e6f4da7b23b727ca98b2bbc0a7c9882260db15 | |
parent | 53ee1ae4171a0ddfb5c9a965c404a18d93e82952 (diff) | |
download | mpv-d5a02dd9342f5c61f3e3a9caf2082dd46b5099e3.tar.bz2 mpv-d5a02dd9342f5c61f3e3a9caf2082dd46b5099e3.tar.xz |
js: hooks: allow deferred continuation (match d0ab562b)
The callback now gets an object argument with defer/cont functions.
Like the lua code, the behavior is that each hook event allows at
most one continue, but nothing enforces the order of continuations
if more hook events arrive before prior ones were continued - which
is possible now with the defer option, but wasn't possible before
(continuation was synchronous from the hook event handler).
-rw-r--r-- | DOCS/man/javascript.rst | 2 | ||||
-rw-r--r-- | player/javascript/defaults.js | 11 |
2 files changed, 10 insertions, 3 deletions
diff --git a/DOCS/man/javascript.rst b/DOCS/man/javascript.rst index 53e5771b54..1e5ce99cdc 100644 --- a/DOCS/man/javascript.rst +++ b/DOCS/man/javascript.rst @@ -193,7 +193,7 @@ success, ``fn`` is called always a-sync, ``error`` is empty string on success. ``mp.utils.getpid()`` (LE) -``mp.add_hook(type, priority, fn)`` +``mp.add_hook(type, priority, fn(hook))`` ``mp.options.read_options(obj [, identifier [, on_update]])`` (types: string/boolean/number) diff --git a/player/javascript/defaults.js b/player/javascript/defaults.js index 9d53f2d7a3..2435390b87 100644 --- a/player/javascript/defaults.js +++ b/player/javascript/defaults.js @@ -126,10 +126,17 @@ function dispatch_message(ev) { var hooks = []; // array of callbacks, id is index+1 function run_hook(ev) { + var state = 0; // 0:initial, 1:deferred, 2:continued + function do_cont() { return state = 2, mp._hook_continue(ev.hook_id) } + + function err() { return mp.msg.error("hook already continued"), undefined } + function usr_defer() { return state == 2 ? err() : (state = 1, true) } + function usr_cont() { return state == 2 ? err() : do_cont() } + var cb = ev.id > 0 && hooks[ev.id - 1]; if (cb) - cb(); - mp._hook_continue(ev.hook_id); + cb({ defer: usr_defer, cont: usr_cont }); + return state == 0 ? do_cont() : true; } mp.add_hook = function add_hook(name, pri, fn) { |