mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 08:01:30 -07:00
js: Substantially simplify timer code
The C code in the Emscripten front-end already keeps a timer_active variable to ensure that the timer can be activated only when it's inactive, and deactivated only when it's active. Adjusting the JavaScript side to rely on this makes the code much simpler. The only oddity is that it now requests a new animation frame before calling the callback so that it's ready to be cancelled if the callback decides to deactivate the timer.
This commit is contained in:
20
emcclib.js
20
emcclib.js
@ -188,22 +188,18 @@ mergeInto(LibraryManager.library, {
|
|||||||
* void js_activate_timer();
|
* void js_activate_timer();
|
||||||
*
|
*
|
||||||
* Start calling the C timer_callback() function every frame.
|
* Start calling the C timer_callback() function every frame.
|
||||||
|
* The C code ensures that the activate and deactivate functions
|
||||||
|
* are called in a sensible order.
|
||||||
*/
|
*/
|
||||||
js_activate_timer: function() {
|
js_activate_timer: function() {
|
||||||
if (!timer_active) {
|
|
||||||
timer_reference = performance.now();
|
timer_reference = performance.now();
|
||||||
var frame = function(now) {
|
var frame = function(now) {
|
||||||
timer = null;
|
timer = window.requestAnimationFrame(frame);
|
||||||
|
// The callback might call js_deactivate_timer() below.
|
||||||
timer_callback((now - timer_reference) / 1000.0);
|
timer_callback((now - timer_reference) / 1000.0);
|
||||||
/* The callback may have deactivated the timer. */
|
|
||||||
if (timer_active) {
|
|
||||||
timer_reference = now;
|
timer_reference = now;
|
||||||
|
};
|
||||||
timer = window.requestAnimationFrame(frame);
|
timer = window.requestAnimationFrame(frame);
|
||||||
}
|
|
||||||
}
|
|
||||||
timer_active = true;
|
|
||||||
timer = window.requestAnimationFrame(frame);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -212,13 +208,7 @@ mergeInto(LibraryManager.library, {
|
|||||||
* Stop calling the C timer_callback() function every frame.
|
* Stop calling the C timer_callback() function every frame.
|
||||||
*/
|
*/
|
||||||
js_deactivate_timer: function() {
|
js_deactivate_timer: function() {
|
||||||
if (timer_active) {
|
|
||||||
timer_active = false;
|
|
||||||
if (timer !== null) {
|
|
||||||
window.cancelAnimationFrame(timer);
|
window.cancelAnimationFrame(timer);
|
||||||
timer = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -90,8 +90,7 @@ var midpoint_test_str = "ABCDEFGHIKLMNOPRSTUVWXYZ0123456789";
|
|||||||
var midpoint_cache = [];
|
var midpoint_cache = [];
|
||||||
|
|
||||||
// Variables used by js_activate_timer() and js_deactivate_timer().
|
// Variables used by js_activate_timer() and js_deactivate_timer().
|
||||||
var timer = null;
|
var timer;
|
||||||
var timer_active = false;
|
|
||||||
var timer_reference;
|
var timer_reference;
|
||||||
|
|
||||||
// void timer_callback(double tplus);
|
// void timer_callback(double tplus);
|
||||||
|
Reference in New Issue
Block a user