Util shouldn't modify window object
This commits prevents Util from modifying the window object. - `window.requestAnimFrame` was removed (no polyfill is needed anymore) - the potential redefinition of `console.log` and friends was removed (all supported browsers have `console.xyz` defined anyway)
This commit is contained in:
parent
9eca6889be
commit
e4fef7be2d
|
@ -778,7 +778,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._renderQ.length > 0) {
|
if (this._renderQ.length > 0) {
|
||||||
requestAnimFrame(this._scan_renderQ.bind(this));
|
requestAnimationFrame(this._scan_renderQ.bind(this));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
63
core/util.js
63
core/util.js
|
@ -11,22 +11,6 @@
|
||||||
|
|
||||||
var Util = {};
|
var Util = {};
|
||||||
|
|
||||||
//
|
|
||||||
// requestAnimationFrame shim with setTimeout fallback
|
|
||||||
//
|
|
||||||
|
|
||||||
window.requestAnimFrame = (function () {
|
|
||||||
"use strict";
|
|
||||||
return window.requestAnimationFrame ||
|
|
||||||
window.webkitRequestAnimationFrame ||
|
|
||||||
window.mozRequestAnimationFrame ||
|
|
||||||
window.oRequestAnimationFrame ||
|
|
||||||
window.msRequestAnimationFrame ||
|
|
||||||
function (callback) {
|
|
||||||
window.setTimeout(callback, 1000 / 60);
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ------------------------------------------------------
|
* ------------------------------------------------------
|
||||||
* Namespaced in Util
|
* Namespaced in Util
|
||||||
|
@ -45,39 +29,26 @@ Util.init_logging = function (level) {
|
||||||
} else {
|
} else {
|
||||||
Util._log_level = level;
|
Util._log_level = level;
|
||||||
}
|
}
|
||||||
if (typeof window.console === "undefined") {
|
|
||||||
if (typeof window.opera !== "undefined") {
|
|
||||||
window.console = {
|
|
||||||
'log' : window.opera.postError,
|
|
||||||
'warn' : window.opera.postError,
|
|
||||||
'error': window.opera.postError
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
window.console = {
|
|
||||||
'log' : function (m) {},
|
|
||||||
'warn' : function (m) {},
|
|
||||||
'error': function (m) {}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Util.Debug = Util.Info = Util.Warn = Util.Error = function (msg) {};
|
Util.Debug = Util.Info = Util.Warn = Util.Error = function (msg) {};
|
||||||
/* jshint -W086 */
|
if (typeof window.console !== "undefined") {
|
||||||
switch (level) {
|
/* jshint -W086 */
|
||||||
case 'debug':
|
switch (level) {
|
||||||
Util.Debug = function (msg) { console.log(msg); };
|
case 'debug':
|
||||||
case 'info':
|
Util.Debug = function (msg) { console.log(msg); };
|
||||||
Util.Info = function (msg) { console.log(msg); };
|
case 'info':
|
||||||
case 'warn':
|
Util.Info = function (msg) { console.info(msg); };
|
||||||
Util.Warn = function (msg) { console.warn(msg); };
|
case 'warn':
|
||||||
case 'error':
|
Util.Warn = function (msg) { console.warn(msg); };
|
||||||
Util.Error = function (msg) { console.error(msg); };
|
case 'error':
|
||||||
case 'none':
|
Util.Error = function (msg) { console.error(msg); };
|
||||||
break;
|
case 'none':
|
||||||
default:
|
break;
|
||||||
throw new Error("invalid logging type '" + level + "'");
|
default:
|
||||||
|
throw new Error("invalid logging type '" + level + "'");
|
||||||
|
}
|
||||||
|
/* jshint +W086 */
|
||||||
}
|
}
|
||||||
/* jshint +W086 */
|
|
||||||
};
|
};
|
||||||
Util.get_logging = function () {
|
Util.get_logging = function () {
|
||||||
return Util._log_level;
|
return Util._log_level;
|
||||||
|
|
|
@ -384,15 +384,15 @@ describe('Display/Canvas Helper', function () {
|
||||||
display = new Display({ target: document.createElement('canvas'), prefer_js: false });
|
display = new Display({ target: document.createElement('canvas'), prefer_js: false });
|
||||||
display.resize(4, 4);
|
display.resize(4, 4);
|
||||||
sinon.spy(display, '_scan_renderQ');
|
sinon.spy(display, '_scan_renderQ');
|
||||||
this.old_requestAnimFrame = window.requestAnimFrame;
|
this.old_requestAnimationFrame = window.requestAnimationFrame;
|
||||||
window.requestAnimFrame = function (cb) {
|
window.requestAnimationFrame = function (cb) {
|
||||||
this.next_frame_cb = cb;
|
this.next_frame_cb = cb;
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
this.next_frame = function () { this.next_frame_cb(); };
|
this.next_frame = function () { this.next_frame_cb(); };
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
window.requestAnimFrame = this.old_requestAnimFrame;
|
window.requestAnimationFrame = this.old_requestAnimationFrame;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should try to process an item when it is pushed on, if nothing else is on the queue', function () {
|
it('should try to process an item when it is pushed on, if nothing else is on the queue', function () {
|
||||||
|
|
|
@ -12,12 +12,14 @@ describe('Utils', function() {
|
||||||
sinon.spy(console, 'log');
|
sinon.spy(console, 'log');
|
||||||
sinon.spy(console, 'warn');
|
sinon.spy(console, 'warn');
|
||||||
sinon.spy(console, 'error');
|
sinon.spy(console, 'error');
|
||||||
|
sinon.spy(console, 'info');
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
console.log.restore();
|
console.log.restore();
|
||||||
console.warn.restore();
|
console.warn.restore();
|
||||||
console.error.restore();
|
console.error.restore();
|
||||||
|
console.info.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use noop for levels lower than the min level', function () {
|
it('should use noop for levels lower than the min level', function () {
|
||||||
|
@ -27,12 +29,16 @@ describe('Utils', function() {
|
||||||
expect(console.log).to.not.have.been.called;
|
expect(console.log).to.not.have.been.called;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use console.log for Debug and Info', function () {
|
it('should use console.log for Debug', function () {
|
||||||
Util.init_logging('debug');
|
Util.init_logging('debug');
|
||||||
Util.Debug('dbg');
|
Util.Debug('dbg');
|
||||||
Util.Info('inf');
|
|
||||||
expect(console.log).to.have.been.calledWith('dbg');
|
expect(console.log).to.have.been.calledWith('dbg');
|
||||||
expect(console.log).to.have.been.calledWith('inf');
|
});
|
||||||
|
|
||||||
|
it('should use console.info for Info', function () {
|
||||||
|
Util.init_logging('debug');
|
||||||
|
Util.Info('inf');
|
||||||
|
expect(console.info).to.have.been.calledWith('inf');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use console.warn for Warn', function () {
|
it('should use console.warn for Warn', function () {
|
||||||
|
|
Loading…
Reference in New Issue