From 0a239005a9585e203fb9ad17485b5a1fe979b0cd Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Tue, 6 Feb 2018 11:06:48 -0500 Subject: [PATCH] Rename and initialize vars --- src/blocks/scratch3_sensing.js | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/blocks/scratch3_sensing.js b/src/blocks/scratch3_sensing.js index acecc3733..fd60c4caa 100644 --- a/src/blocks/scratch3_sensing.js +++ b/src/blocks/scratch3_sensing.js @@ -15,6 +15,24 @@ class Scratch3SensingBlocks { */ this._answer = ''; + /** + * The timer utility. + * @type {Timer} + */ + this._timer = new Timer(); + + /** + * The stored microphone loudness measurement. + * @type {number} + */ + this._cachedLoudness = -1; + + /** + * The time of the most recent microphone loudness measurement. + * @type {number} + */ + this._cachedLoudnessTimestamp = 0; + /** * The list of queued questions and respective `resolve` callbacks. * @type {!Array} @@ -24,8 +42,6 @@ class Scratch3SensingBlocks { this.runtime.on('ANSWER', this._onAnswer.bind(this)); this.runtime.on('PROJECT_START', this._resetAnswer.bind(this)); this.runtime.on('PROJECT_STOP_ALL', this._clearAllQuestions.bind(this)); - - this.timer = new Timer(); } /** @@ -223,16 +239,14 @@ class Scratch3SensingBlocks { if (typeof this.runtime.currentStepTime === 'undefined') return -1; // Only measure loudness once per step - if (this.loudnessTime) { - const timeSinceLoudness = this.timer.time() - this.loudnessTime; - if (timeSinceLoudness < this.runtime.currentStepTime) { - return this.loudness; - } + const timeSinceLoudness = this._timer.time() - this._cachedLoudnessTimestamp; + if (timeSinceLoudness < this.runtime.currentStepTime) { + return this._cachedLoudness; } - this.loudnessTime = this.timer.time(); - this.loudness = this.runtime.audioEngine.getLoudness(); - return this.loudness; + this._cachedLoudnessTimestamp = this._timer.time(); + this._cachedLoudness = this.runtime.audioEngine.getLoudness(); + return this._cachedLoudness; } getAttributeOf (args) {