js: Make connect() public, and document autoConnect/connect()

This commit is contained in:
Stein Magnus Jodal 2012-12-04 13:23:43 +01:00
parent 74cbb91cfb
commit b14c89dbf1
5 changed files with 30 additions and 11 deletions

View File

@ -12,7 +12,7 @@ function Mopidy(settings) {
this._delegateEvents();
if (this._settings.autoConnect) {
this._connect();
this.connect();
}
}
@ -57,7 +57,7 @@ Mopidy.prototype._delegateEvents = function () {
this.on("state:offline", this._reconnect);
};
Mopidy.prototype._connect = function () {
Mopidy.prototype.connect = function () {
if (this._webSocket) {
if (this._webSocket.readyState === WebSocket.OPEN) {
return;
@ -106,7 +106,7 @@ Mopidy.prototype._reconnect = function () {
setTimeout(function () {
this.emit("reconnecting");
this._connect();
this.connect();
}.bind(this), this._backoffDelay);
this._backoffDelay = this._backoffDelay * 2;

View File

@ -51,12 +51,22 @@ buster.testCase("Mopidy", {
}
},
"._connect": {
".connect": {
"connects when autoConnect is false": function () {
var mopidy = new Mopidy({autoConnect: false});
refute.called(this.webSocketConstructorStub);
mopidy.connect();
assert.calledOnceWith(this.webSocketConstructorStub,
"ws://" + document.location.host + "/mopidy/ws/");
},
"does nothing when the WebSocket is open": function () {
this.webSocket.readyState = WebSocket.OPEN;
var mopidy = new Mopidy({webSocket: this.webSocket});
mopidy._connect();
mopidy.connect();
refute.called(this.webSocket.close);
refute.called(this.webSocketConstructorStub);
@ -164,7 +174,7 @@ buster.testCase("Mopidy", {
"tries to connect after an increasing backoff delay": function () {
var clock = this.useFakeTimers();
var connectStub = this.stub(this.mopidy, "_connect");
var connectStub = this.stub(this.mopidy, "connect");
var pendingSpy = this.spy();
this.mopidy.on("reconnectionPending", pendingSpy);
var reconnectingSpy = this.spy();
@ -209,7 +219,7 @@ buster.testCase("Mopidy", {
"tries to connect at least about once per minute": function () {
var clock = this.useFakeTimers();
var connectStub = this.stub(this.mopidy, "_connect");
var connectStub = this.stub(this.mopidy, "connect");
var pendingSpy = this.spy();
this.mopidy.on("reconnectionPending", pendingSpy);
this.mopidy._backoffDelay = this.mopidy._settings.backoffDelayMax;

View File

@ -181,6 +181,15 @@ WebSocket end point:
webSocketUrl: "ws://localhost:6680/mopidy/ws/"
});
It is also possible to create an instance first and connect to the WebSocket
later:
.. code-block:: js
var mopidy = new Mopidy({autoConnect: false});
// ... do other stuff, like hooking up events ...
mopidy.connect();
Hooking up to events
--------------------

View File

@ -921,7 +921,7 @@ function Mopidy(settings) {
this._delegateEvents();
if (this._settings.autoConnect) {
this._connect();
this.connect();
}
}
@ -966,7 +966,7 @@ Mopidy.prototype._delegateEvents = function () {
this.on("state:offline", this._reconnect);
};
Mopidy.prototype._connect = function () {
Mopidy.prototype.connect = function () {
if (this._webSocket) {
if (this._webSocket.readyState === WebSocket.OPEN) {
return;
@ -1015,7 +1015,7 @@ Mopidy.prototype._reconnect = function () {
setTimeout(function () {
this.emit("reconnecting");
this._connect();
this.connect();
}.bind(this), this._backoffDelay);
this._backoffDelay = this._backoffDelay * 2;

File diff suppressed because one or more lines are too long