js: Fix console setup, allow mocking of the console

This commit is contained in:
Stein Magnus Jodal 2014-06-20 11:14:49 +02:00
parent 5c726c3228
commit c5028a8576
2 changed files with 19 additions and 11 deletions

View File

@ -138,6 +138,10 @@ When creating an instance, you can specify the following settings:
.. versionadded:: 0.19 (Mopidy.js 0.4)
``console``
If set, this object will be used to log errors from Mopidy.js. This is
mostly useful for testing Mopidy.js.
``webSocket``
An existing WebSocket object to be used instead of creating a new
WebSocket. Defaults to undefined.

View File

@ -9,8 +9,8 @@ function Mopidy(settings) {
return new Mopidy(settings);
}
this._console = this._getConsole(settings || {});
this._settings = this._configure(settings || {});
this._console = this._getConsole();
this._backoffDelay = this._settings.backoffDelayMin;
this._pendingRequests = {};
@ -40,6 +40,20 @@ Mopidy.ServerError.prototype.constructor = Mopidy.ServerError;
Mopidy.WebSocket = websocket.Client;
Mopidy.prototype._getConsole = function (settings) {
if (typeof settings.console !== "undefined") {
return settings.console;
}
var con = typeof console !== "undefined" && console || {};
con.log = con.log || function () {};
con.warn = con.warn || function () {};
con.error = con.error || function () {};
return con;
};
Mopidy.prototype._configure = function (settings) {
var currentHost = (typeof document !== "undefined" &&
document.location.host) || "localhost";
@ -59,16 +73,6 @@ Mopidy.prototype._configure = function (settings) {
return settings;
};
Mopidy.prototype._getConsole = function () {
var console = typeof console !== "undefined" && console || {};
console.log = console.log || function () {};
console.warn = console.warn || function () {};
console.error = console.error || function () {};
return console;
};
Mopidy.prototype._delegateEvents = function () {
// Remove existing event handlers
this.off("websocket:close");