Fix references to window

This commit is contained in:
Tim Mickel 2016-06-21 14:29:40 -04:00
parent 28432b6f01
commit 652cc8a31c
2 changed files with 22 additions and 9 deletions

View file

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

View file

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