diff --git a/src/blocks/wedo2.js b/src/blocks/wedo2.js index d4a2a9308..d7516bc18 100644 --- a/src/blocks/wedo2.js +++ b/src/blocks/wedo2.js @@ -64,7 +64,7 @@ WeDo2Blocks.prototype._motorOnFor = function(direction, durationSeconds, util) { YieldTimers.resolve(this._motorTimeout); this._motorTimeout = null; } - if (window.native) { + if (typeof window !== 'undefined' && window.native) { window.native.motorRun(direction, this._motorSpeed); } @@ -73,7 +73,7 @@ WeDo2Blocks.prototype._motorOnFor = function(direction, durationSeconds, util) { if (instance._motorTimeout == myTimeout) { instance._motorTimeout = null; } - if (window.native) { + if (typeof window !== 'undefined' && window.native) { window.native.motorStop(); } util.done(); @@ -132,7 +132,7 @@ WeDo2Blocks.prototype._getColor = function(colorName) { }; WeDo2Blocks.prototype.setColor = function(argValues, util) { - if (window.native) { + if (typeof window !== 'undefined' && window.native) { var colorIndex = this._getColor(argValues[0]); window.native.setLedColor(colorIndex); } diff --git a/src/engine/runtime.js b/src/engine/runtime.js index aff2a3aec..f6ad79f0b 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -210,10 +210,6 @@ Runtime.prototype.stopAll = function () { // Actually remove the thread. this._removeThread(poppedThread); } - // @todo call stop function in all extensions/packages/WeDo stub - if (window.native) { - window.native.motorStop(); - } }; /** @@ -243,12 +239,29 @@ Runtime.prototype.glowBlock = function (blockId, isGlowing) { } }; +/** + * setInterval implementation that works in a WebWorker or not. + * @param {?Function} fcn Function to call. + * @param {number} interval Interval at which to call it. + * @return {number} Value returned by setInterval. + */ +Runtime.prototype._setInterval = function(fcn, interval) { + var setInterval = null; + if (typeof window !== 'undefined' && window.setInterval) { + setInterval = window.setInterval; + } else if (typeof self !== 'undefined' && self.setInterval) { + setInterval = self.setInterval; + } else { + return; + } + return setInterval(fcn, interval); +}; + /** * Set up timers to repeatedly step in a browser */ Runtime.prototype.start = function () { - if (!window.setInterval) return; - window.setInterval(function() { + this._setInterval(function() { this._step(); }.bind(this), Runtime.THREAD_STEP_INTERVAL); };