History

js: implement mp.register_idle

Due to earlier misinterpretation of the Lua docs as if mp.register_idle registers a one-shot callback, the JS docs suggested to use setTimeout. But the behavior and Lua docs are such that it's a repeating callback which fires just before the script thread goes to sleep. Implement it for JS too.

Avi Halachmi (:avih) committed 8 years ago

Commit 9a470231 parent b04f0ca2 files · +53 −7Browse files

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

Unified · Split

player/javascript/defaults.js+37−2
@@ -17,7 +17,7 @@ function new_cache() { · 16 unmodified lines
1717 }
1818
1919 /**********************************************************************
20− * event handlers, property observers, client messages, hooks
20+ * event handlers, property observers, idle, client messages, hooks
2121 *********************************************************************/
2222 var ehandlers = new_cache() // items of event-name: array of {maybe cb: fn}
2323
@@ -54,6 +54,31 @@ function dispatch_event(e) { · 30 unmodified lines
5454 }
5555 }
5656
57+// ----- idle observers -----
58+var iobservers = [], // array of callbacks
59+ ideleted = false;
60+
61+mp.register_idle = function(fn) {
62+ iobservers.push(fn);
63+}
64+
65+mp.unregister_idle = function(fn) {
66+ iobservers.forEach(function(f, i) {
67+ if (f == fn)
68+ delete iobservers[i]; // -> same length but [more] sparse
69+ });
70+ ideleted = true;
71+}
72+
73+function notify_idle_observers() {
74+ // forEach and filter skip deleted items and newly added items
75+ iobservers.forEach(function(f) { f() });
76+ if (ideleted) {
77+ iobservers = iobservers.filter(function() { return true });
78+ ideleted = false;
79+ }
80+}
81+
5782 // ----- property observers -----
5883 var next_oid = 1,
5984 observers = new_cache(); // items of id: fn
@@ -254,6 +279,10 @@ function peek_wait(arr) { · 194 unmodified lines
254279 return arr.length ? Math.max(0, arr[arr.length - 1].when - now()) : -1;
255280 }
256281
282+function peek_timers_wait() {
283+ return peek_wait(timers); // must not be called while in process_timers
284+}
285+
257286 // Callback all due non-canceled timers which were inserted before calling us.
258287 // Returns wait in ms till the next timer (possibly 0), or -1 if nothing pends.
259288 function process_timers() {
@@ -481,6 +510,8 @@ mp.get_time = function() { return mp.get_time_ms() / 1000 }; · 221 unmodified lines
481510 mp.utils.getcwd = function() { return mp.get_property("working-directory") };
482511 mp.dispatch_event = dispatch_event;
483512 mp.process_timers = process_timers;
513+mp.notify_idle_observers = notify_idle_observers;
514+mp.peek_timers_wait = peek_timers_wait;
484515
485516 mp.get_opt = function(key, def) {
486517 var v = mp.get_property_native("options/script-opts")[key];
@@ -533,13 +564,17 @@ mp.register_script_message("key-binding", dispatch_key_binding); · 46 unmodified lines
533564
534565 g.mp_event_loop = function mp_event_loop() {
535566 var wait = 0; // seconds
536− do { // distapch events as long as they arrive, then do the timers
567+ do { // distapch events as long as they arrive, then do the timers/idle
537568 var e = mp.wait_event(wait);
538569 if (e.event != "none") {
539570 dispatch_event(e);
540571 wait = 0; // poll the next one
541572 } else {
542573 wait = process_timers() / 1000;
574+ if (wait != 0) {
575+ notify_idle_observers(); // can add timers -> recalculate wait
576+ wait = peek_timers_wait() / 1000;
577+ }
543578 }
544579 } while (mp.keep_running);
545580 };

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