Showing the changes to player/javascript (1 of 3 files). Show all
Unified · Split
| @@ -17,7 +17,7 @@ function new_cache() { · 16 unmodified lines | ||
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | /********************************************************************** |
| 20 | − * event handlers, property observers, idle, client messages, hooks | |
| 20 | + * event handlers, property observers, idle, client messages, hooks, async | |
| 21 | 21 | *********************************************************************/ |
| 22 | 22 | var ehandlers = new_cache() // items of event-name: array of {maybe cb: fn} |
| 23 | 23 | |
| @@ -138,6 +138,25 @@ mp.add_hook = function add_hook(name, pri, fn) { · 114 unmodified lines | ||
| 138 | 138 | return mp._hook_add(name, pri - 50, hooks.length); |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | +// ----- async commands ----- | |
| 142 | +var async_callbacks = new_cache(); // items of id: fn | |
| 143 | +var async_next_id = 1; | |
| 144 | + | |
| 145 | +mp.command_native_async = function command_native_async(node, cb) { | |
| 146 | + var id = async_next_id++; | |
| 147 | + async_callbacks[id] = cb; | |
| 148 | + return mp._command_native_async(id, node); | |
| 149 | +} | |
| 150 | + | |
| 151 | +function async_command_handler(ev) { | |
| 152 | + var cb = async_callbacks[ev.id]; | |
| 153 | + delete async_callbacks[ev.id]; | |
| 154 | + if (ev.error) | |
| 155 | + cb(false, undefined, ev.error); | |
| 156 | + else | |
| 157 | + cb(true, ev.result, ""); | |
| 158 | +} | |
| 159 | + | |
| 141 | 160 | /********************************************************************** |
| 142 | 161 | * key bindings |
| 143 | 162 | *********************************************************************/ |
| @@ -557,6 +576,7 @@ g.exit = function() { mp.keep_running = false }; // user-facing too · 413 unmodified lines | ||
| 557 | 576 | mp.register_event("shutdown", g.exit); |
| 558 | 577 | mp.register_event("property-change", notify_observer); |
| 559 | 578 | mp.register_event("hook", run_hook); |
| 579 | +mp.register_event("command-reply", async_command_handler); | |
| 560 | 580 | mp.register_event("client-message", dispatch_message); |
| 561 | 581 | mp.register_script_message("key-binding", dispatch_key_binding); |
| 562 | 582 | |