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.
This commit is contained in:
Ian Flanigan 2020-10-11 17:50:20 +02:00 committed by GitHub
parent 94d146203a
commit a200d6de83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);
}