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
205 cb(state, key_name);205 cb(state, key_name);
206}206}
207 207
208function update_input_sections() {208var binds_tid = 0; // flush timer id. actual id's are always true-thy
209mp.flush_key_bindings = function flush_key_bindings() {
209 var def = [], forced = [];210 var def = [], forced = [];
210 for (var n in binds) // Array.join() will later skip undefined .input211 for (var n in binds) // Array.join() will later skip undefined .input
211 (binds[n].forced ? forced : def).push(binds[n].input);212 (binds[n].forced ? forced : def).push(binds[n].input);
@@ -217,6 +218,14 @@ function update_input_sections() { · 5 unmodified lines
217 sect = "input_forced_" + mp.script_name;218 sect = "input_forced_" + mp.script_name;
218 mp.commandv("define-section", sect, forced.join("\n"), "force");219 mp.commandv("define-section", sect, forced.join("\n"), "force");
219 mp.commandv("enable-section", sect, "allow-hide-cursor+allow-vo-dragging");220 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
226function sched_bindings_flush() {
227 if (!binds_tid)
228 binds_tid = setTimeout(mp.flush_key_bindings, 0); // fires on idle
220}229}
221 230
222// name/opts maybe omitted. opts: object with optional bool members: repeatable,231// 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
263 if (key)272 if (key)
264 key_data.input = key + " script-binding " + mp.script_name + "/" + name;273 key_data.input = key + " script-binding " + mp.script_name + "/" + name;
265 binds[name] = key_data; // used by user and/or our (key) script-binding274 binds[name] = key_data; // used by user and/or our (key) script-binding
266 update_input_sections();275 sched_bindings_flush();
267}276}
268 277
269mp.add_key_binding = add_binding.bind(null, false);278mp.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
272mp.remove_key_binding = function(name) {281mp.remove_key_binding = function(name) {
273 mp.unregister_script_message(name);282 mp.unregister_script_message(name);
274 delete binds[name];283 delete binds[name];
275 update_input_sections();284 sched_bindings_flush();
276}285}
277 286
278/**********************************************************************287/**********************************************************************

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