Showing the changes to player/javascript/defaults.js (1 of 2 files). Show all
Unified · Split
| @@ -123,22 +123,19 @@ function dispatch_message(ev) { · 122 unmodified lines | |||
| 123 | } | 123 | } |
| 124 | 124 | ||
| 125 | // ----- hooks ----- | 125 | // ----- hooks ----- |
| 126 | var next_hid = 1, | 126 | var hooks = []; // array of callbacks, id is index+1 |
| 127 | hooks = new_cache(); // items of id: fn | ||
| 128 | 127 | ||
| 129 | function hook_run(id, cont) { | 128 | function run_hook(ev) { |
| 130 | var cb = hooks[id]; | 129 | var cb = ev.id > 0 && hooks[ev.id - 1]; |
| 131 | if (cb) | 130 | if (cb) |
| 132 | cb(); | 131 | cb(); |
| 133 | mp.commandv("hook-ack", cont); | 132 | mp._hook_continue(ev.hook_id); |
| 134 | } | 133 | } |
| 135 | 134 | ||
| 136 | mp.add_hook = function add_hook(name, pri, fn) { | 135 | mp.add_hook = function add_hook(name, pri, fn) { |
| 137 | if (next_hid == 1) // doesn't really matter if we do it once or always | 136 | hooks.push(fn); |
| 138 | mp.register_script_message("hook_run", hook_run); | 137 | // 50 (scripting docs default priority) maps to 0 (default in C API docs) |
| 139 | var id = next_hid++; | 138 | return mp._hook_add(name, pri - 50, hooks.length); |
| 140 | hooks[id] = fn; | ||
| 141 | return mp.commandv("hook-add", name, id, pri); | ||
| 142 | } | 139 | } |
| 143 | 140 | ||
| 144 | /********************************************************************** | 141 | /********************************************************************** |
| @@ -559,6 +556,7 @@ mp.keep_running = true; · 414 unmodified lines | |||
| 559 | g.exit = function() { mp.keep_running = false }; // user-facing too | 556 | g.exit = function() { mp.keep_running = false }; // user-facing too |
| 560 | mp.register_event("shutdown", g.exit); | 557 | mp.register_event("shutdown", g.exit); |
| 561 | mp.register_event("property-change", notify_observer); | 558 | mp.register_event("property-change", notify_observer); |
| 559 | mp.register_event("hook", run_hook); | ||
| 562 | mp.register_event("client-message", dispatch_message); | 560 | mp.register_event("client-message", dispatch_message); |
| 563 | mp.register_script_message("key-binding", dispatch_key_binding); | 561 | mp.register_script_message("key-binding", dispatch_key_binding); |
| 564 | 562 | ||