scratch-vm/src/io/clock.js
Katie Broida 2a60391fb4 Make Scratch 3 project timer more compatible with Scratch 2 currentMSecs usage
These compatibility changes:
 - Use runtime.currentMSecs for the Clock timer "now" value
 - Start executing hats before other threads change values
 - Update test and fixtures to work with earlier hat execution
 - Add test for hat execution block order
2019-03-29 11:43:37 -04:00

39 lines
935 B
JavaScript

const Timer = require('../util/timer');
class Clock {
constructor (runtime) {
this._projectTimer = new Timer({now: () => runtime.currentMSecs});
this._projectTimer.start();
this._pausedTime = null;
this._paused = false;
/**
* Reference to the owning Runtime.
* @type{!Runtime}
*/
this.runtime = runtime;
}
projectTimer () {
if (this._paused) {
return this._pausedTime / 1000;
}
return this._projectTimer.timeElapsed() / 1000;
}
pause () {
this._paused = true;
this._pausedTime = this._projectTimer.timeElapsed();
}
resume () {
this._paused = false;
const dt = this._projectTimer.timeElapsed() - this._pausedTime;
this._projectTimer.startTime += dt;
}
resetProjectTimer () {
this._projectTimer.start();
}
}
module.exports = Clock;