Skip to content

Commit c2a5e13

Browse files
committed
Avoid synchronous event when creating Connection
1 parent 9548214 commit c2a5e13

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/client/connection.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ var ShareDBError = require('../error');
55
var types = require('../types');
66
var util = require('../util');
77

8+
function connectionState(socket) {
9+
if (socket.readyState === 0 || socket.readyState === 1) return 'connecting';
10+
return 'disconnected';
11+
}
12+
813
/**
914
* Handles communication with the sharejs server and provides queries and
1015
* documents.
@@ -48,7 +53,7 @@ function Connection(socket) {
4853

4954
this.debug = false;
5055

51-
this.state = 'disconnected'
56+
this.state = connectionState(socket);
5257

5358
this.bindToSocket(socket);
5459
}
@@ -92,7 +97,8 @@ Connection.prototype.bindToSocket = function(socket) {
9297
// - 'disconnected' Connection is closed, but it will reconnect automatically
9398
// - 'closed' The connection was closed by the client, and will not reconnect
9499
// - 'stopped' The connection was closed by the server, and will not reconnect
95-
this._setState((socket.readyState === 0 || socket.readyState === 1) ? 'connecting' : 'disconnected');
100+
var newState = connectionState(socket);
101+
this._setState(newState);
96102

97103
// This is a helper variable the document uses to see whether we're
98104
// currently in a 'live' state. It is true if and only if we're connected

0 commit comments

Comments
 (0)