Skip to content

Always update connection state using _setState #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/client/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ var ShareDBError = require('../error');
var types = require('../types');
var util = require('../util');

function connectionState(socket) {
if (socket.readyState === 0 || socket.readyState === 1) return 'connecting';
return 'disconnected';
}

/**
* Handles communication with the sharejs server and provides queries and
* documents.
Expand Down Expand Up @@ -48,6 +53,8 @@ function Connection(socket) {

this.debug = false;

this.state = connectionState(socket);

this.bindToSocket(socket);
}
emitter.mixin(Connection);
Expand Down Expand Up @@ -90,7 +97,8 @@ Connection.prototype.bindToSocket = function(socket) {
// - 'disconnected' Connection is closed, but it will reconnect automatically
// - 'closed' The connection was closed by the client, and will not reconnect
// - 'stopped' The connection was closed by the server, and will not reconnect
this.state = (socket.readyState === 0 || socket.readyState === 1) ? 'connecting' : 'disconnected';
var newState = connectionState(socket);
this._setState(newState);

// This is a helper variable the document uses to see whether we're
// currently in a 'live' state. It is true if and only if we're connected
Expand Down
68 changes: 68 additions & 0 deletions test/client/connection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var expect = require('expect.js');
var Backend = require('../../lib/backend');
var Connection = require('../../lib/client/connection');

describe('client connection', function() {

Expand Down Expand Up @@ -90,4 +91,71 @@ describe('client connection', function() {
});
});

describe('state management using setSocket', function() {
it('initial connection.state is connecting, if socket.readyState is CONNECTING', function () {
// https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket-connecting
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the links to the spec! 🥇

var socket = { readyState: 0 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style-wise, we tend not to put spaces inside of brackets, and use semicolons. I'll just go ahead and clean up after merging.

var connection = new Connection(socket)
expect(connection.state).equal('connecting');
});

it('initial connection.state is connecting, if socket.readyState is OPEN', function () {
// https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket-open
var socket = { readyState: 1 }
var connection = new Connection(socket)
expect(connection.state).equal('connecting');
});

it('initial connection.state is disconnected, if socket.readyState is CLOSING', function () {
// https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket-closing
var socket = { readyState: 2 }
var connection = new Connection(socket)
expect(connection.state).equal('disconnected');
});

it('initial connection.state is disconnected, if socket.readyState is CLOSED', function () {
// https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket-closed
var socket = { readyState: 3 }
var connection = new Connection(socket)
expect(connection.state).equal('disconnected');
});

it('initial state is connecting', function() {
var connection = this.backend.connect();
expect(connection.state).equal('connecting');
});

it('after connected event is emitted, state is connected', function(done) {
var connection = this.backend.connect();
connection.on('connected', function() {
expect(connection.state).equal('connected');
done();
});
});

it('when connection is manually closed, state is closed', function(done) {
var connection = this.backend.connect();
connection.on('connected', function() {
connection.close();
});
connection.on('closed', function() {
expect(connection.state).equal('closed');
done();
});
});

it('when connection is disconnected, state is disconnected', function(done) {
var connection = this.backend.connect();
connection.on('connected', function() {
// Mock a disconnection by providing a reason
connection.socket.close('foo');
});
connection.on('disconnected', function() {
expect(connection.state).equal('disconnected');
done();
});
});

});

});