From a200d6de83ab07df53bb72d44f420d844d9825a6 Mon Sep 17 00:00:00 2001 From: Ian Flanigan Date: Sun, 11 Oct 2020 17:50:20 +0200 Subject: [PATCH] Fix a problem where sometimes the emulator would run too fast (#34) Before, when using `requestAnimationFrame`, the emulator did not save the id returned by the browser. This broke the invariant of `run`, namely that on exit either `runAnimationFrame` or `runTimer` would be set. This meant that sometimes when the emulator restarted, there would be two callbacks to `requetsAnimationFrame` run on every frame. Now the id is saved correctly and the invariant of `run` is maintained. --- js/apple2.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/js/apple2.js b/js/apple2.js index dc7956e..cb6f523 100644 --- a/js/apple2.js +++ b/js/apple2.js @@ -67,6 +67,11 @@ export function Apple2(options) { window.webkitCancelAnimationFrame || window.msCancelAnimationFrame; + /** + * Runs the emulator. If the emulator is already running, this does + * nothing. When this function exits either `runTimer` or + * `runAnimationFrame` will be non-null. + */ function run() { if (runTimer || runAnimationFrame) { return; // already running @@ -125,7 +130,7 @@ export function Apple2(options) { } }; if (_requestAnimationFrame) { - _requestAnimationFrame(runFn); + runAnimationFrame = _requestAnimationFrame(runFn); } else { runTimer = setInterval(runFn, interval); }