History

js: expose async commands (match 159379980e)

Avi Halachmi (:avih) committed 7 years ago

Commit 5b5f7761 parent fc567983 files · +42 −2Browse files

Showing the changes to player/javascript (1 of 3 files). Show all

Unified · Split

player/javascript/defaults.js+21−1
@@ -17,7 +17,7 @@ function new_cache() { · 16 unmodified lines
1717 }
1818
1919 /**********************************************************************
20− * event handlers, property observers, idle, client messages, hooks
20+ * event handlers, property observers, idle, client messages, hooks, async
2121 *********************************************************************/
2222 var ehandlers = new_cache() // items of event-name: array of {maybe cb: fn}
2323
@@ -138,6 +138,25 @@ mp.add_hook = function add_hook(name, pri, fn) { · 114 unmodified lines
138138 return mp._hook_add(name, pri - 50, hooks.length);
139139 }
140140
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+
141160 /**********************************************************************
142161 * key bindings
143162 *********************************************************************/
@@ -557,6 +576,7 @@ g.exit = function() { mp.keep_running = false }; // user-facing too · 413 unmodified lines
557576 mp.register_event("shutdown", g.exit);
558577 mp.register_event("property-change", notify_observer);
559578 mp.register_event("hook", run_hook);
579+mp.register_event("command-reply", async_command_handler);
560580 mp.register_event("client-message", dispatch_message);
561581 mp.register_script_message("key-binding", dispatch_key_binding);
562582

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t