History

js: batch key bindings updates (match 96932fe7)

Implemented using one-time idle observer (i.e. setTimeout), to avoid additional function call(back) every time the event loop enters idle. The lua mp.flush_key_bindings() is also available, also undocumented.

Avi Halachmi (:avih) committed 6 years ago

Commit 5a74bf51 parent 7ed70f91 file · +12 −3Browse files

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

Unified · Split

player/javascript/defaults.js+12−3
@@ -205,7 +205,8 @@ function dispatch_key_binding(name, state, key_name) { · 204 unmodified lines
205205 cb(state, key_name);
206206 }
207207
208−function update_input_sections() {
208+var binds_tid = 0; // flush timer id. actual id's are always true-thy
209+mp.flush_key_bindings = function flush_key_bindings() {
209210 var def = [], forced = [];
210211 for (var n in binds) // Array.join() will later skip undefined .input
211212 (binds[n].forced ? forced : def).push(binds[n].input);
@@ -217,6 +218,14 @@ function update_input_sections() { · 5 unmodified lines
217218 sect = "input_forced_" + mp.script_name;
218219 mp.commandv("define-section", sect, forced.join("\n"), "force");
219220 mp.commandv("enable-section", sect, "allow-hide-cursor+allow-vo-dragging");
221+
222+ clearTimeout(binds_tid); // cancel future flush if called directly
223+ binds_tid = 0;
224+}
225+
226+function sched_bindings_flush() {
227+ if (!binds_tid)
228+ binds_tid = setTimeout(mp.flush_key_bindings, 0); // fires on idle
220229 }
221230
222231 // name/opts maybe omitted. opts: object with optional bool members: repeatable,
@@ -263,7 +272,7 @@ function add_binding(forced, key, name, fn, opts) { · 40 unmodified lines
263272 if (key)
264273 key_data.input = key + " script-binding " + mp.script_name + "/" + name;
265274 binds[name] = key_data; // used by user and/or our (key) script-binding
266− update_input_sections();
275+ sched_bindings_flush();
267276 }
268277
269278 mp.add_key_binding = add_binding.bind(null, false);
@@ -272,7 +281,7 @@ mp.add_forced_key_binding = add_binding.bind(null, true); · 2 unmodified lines
272281 mp.remove_key_binding = function(name) {
273282 mp.unregister_script_message(name);
274283 delete binds[name];
275− update_input_sections();
284+ sched_bindings_flush();
276285 }
277286
278287 /**********************************************************************

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