From d5a02dd9342f5c61f3e3a9caf2082dd46b5099e3 Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Fri, 7 Aug 2020 13:42:27 +0300 Subject: 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). --- player/javascript/defaults.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'player/javascript') 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) { -- cgit v1.2.3