Skip to content

Commit 8ca7501

Browse files
committed
readyState of is now stored only inside the transport
1 parent 52c2e9d commit 8ca7501

File tree

5 files changed

+36
-30
lines changed

5 files changed

+36
-30
lines changed

src/core/constructors/node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ dop.core.node = function() {
1515

1616

1717
dop.core.node.prototype.send = function(message) {
18-
this.emit(dop.CONS.SEND, message);
18+
this.emit(dop.cons.SEND, message);
1919
};
2020

2121
dop.core.node.prototype.disconnect = function() {
22-
this.emit(dop.CONS.DISCONNECT);
22+
this.emit(dop.cons.DISCONNECT);
2323
};
2424

2525
dop.core.node.prototype.subscribe = function() {

src/dop.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@ var dop = {
2121
protocol: {},
2222
transports: {listen:{}, connect:{}},
2323

24-
CONS: {
25-
CLOSE: '~CLOSE',
26-
OPEN: '~OPEN',
27-
CONNECTING: '~CONNECTING',
24+
// Const
25+
cons: {
26+
socket_token: '~TOKEN_DOP',
27+
dop: '~DOP',
2828
CONNECT: '~CONNECT',
29-
RECONNECT: '~RECONNECT',
30-
SEND: '~SEND'
29+
SEND: '~SEND',
30+
DISCONNECT: '~DISCONNECT'
3131
}
3232

3333
};
3434

35-
36-
// Special properties assigned to user objects
3735
var CONS = {
3836
socket_token: '~TOKEN_DOP',
39-
dop: '~dop'
40-
};
37+
dop: '~DOP'
38+
};

src/env/browser/websocket.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ function websocket(dop, node, options) {
1414
// Variables
1515
var api = options.transport.getApi(),
1616
socket = new api(url),
17-
send_queue = [];
17+
send_queue = [],
18+
readyState;
1819

1920
// Helpers
2021
function send(message) {
21-
(socket.readyState===socket.constructor.OPEN && node.readyState===dop.CONS.CONNECT) ?
22+
(socket.readyState===socket.constructor.OPEN && readyState===CONNECT) ?
2223
socket.send(message)
2324
:
2425
send_queue.push(message);
@@ -31,20 +32,20 @@ function websocket(dop, node, options) {
3132
// Socket events
3233
function onopen() {
3334
// Reconnect
34-
if (node.readyState === dop.CONS.CONNECTING)
35+
if (readyState === CONNECTING)
3536
socket.send(node.tokenServer);
3637
// Connect
3738
else {
3839
socket.send(''); // Empty means we want to get connected
39-
node.readyState = dop.CONS.OPEN;
40+
readyState = OPEN;
4041
}
4142
dop.core.emitOpen(node, socket, options.transport);
4243
}
4344
function onmessage(message) {
4445
// console.log( 'C<<: `'+message.data+'`' );
4546
// Reconnecting
46-
if (node.readyState===dop.CONS.CONNECTING && message.data===node.tokenServer) {
47-
node.readyState = dop.CONS.CONNECT;
47+
if (readyState===CONNECTING && message.data===node.tokenServer) {
48+
readyState = CONNECT;
4849
dop.core.setSocketToNode(node, socket);
4950
dop.core.emitReconnect(node, oldSocket);
5051
sendQueue();
@@ -53,43 +54,43 @@ function websocket(dop, node, options) {
5354
dop.core.emitMessage(node, message.data, message);
5455
}
5556
function onclose() {
56-
node.readyState = dop.CONS.CLOSE;
57+
readyState = CLOSE;
5758
dop.core.emitClose(node, socket);
5859
}
5960

6061
// dop events
6162
function onconnect(message_response) {
62-
if (node.readyState === dop.CONS.CONNECTING) {
63+
if (readyState === CONNECTING) {
6364
dop.core.emitDisconnect(node);
6465
dop.core.setSocketToNode(node, socket);
6566
}
6667
socket.send(message_response);
67-
node.readyState = dop.CONS.CONNECT;
68+
readyState = CONNECT;
6869
dop.core.emitConnect(node);
6970
sendQueue();
7071
}
7172
function ondisconnect() {
72-
node.readyState = dop.CONS.CLOSE;
73+
readyState = CLOSE;
7374
socket.close();
7475
}
7576

7677
function reconnect() {
77-
if (node.readyState !== dop.CONS.CONNECT) {
78+
if (readyState !== CONNECT) {
7879
oldSocket = socket;
7980
socket = new api(url);
80-
node.readyState = dop.CONS.CONNECTING;
81+
readyState = CONNECTING;
8182
addListeners(socket, onopen, onmessage, onclose);
8283
removeListeners(oldSocket, onopen, onmessage, onclose);
8384
}
8485
}
8586

8687
// Setting up
8788
dop.core.setSocketToNode(node, socket);
88-
node.readyState = dop.CONS.CLOSE;
89+
readyState = CLOSE;
8990
node.reconnect = reconnect;
90-
node.on(dop.CONS.CONNECT, onconnect);
91-
node.on(dop.CONS.SEND, send);
92-
node.on(dop.CONS.DISCONNECT, ondisconnect);
91+
node.on(dop.cons.CONNECT, onconnect);
92+
node.on(dop.cons.SEND, send);
93+
node.on(dop.cons.DISCONNECT, ondisconnect);
9394
addListeners(socket, onopen, onmessage, onclose);
9495

9596
return socket;
@@ -118,4 +119,11 @@ else {
118119
root.dopTransportsConnectWebsocket = websocket;
119120
}
120121

122+
// Cons
123+
var CLOSE = 0,
124+
OPEN = 1,
125+
CONNECTING = 2,
126+
CONNECT = 3;
127+
128+
121129
})(this);

src/protocol/_onconnect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ dop.protocol._onconnect = function(node, request_id, request, response) {
55

66
// Node is connected correctly
77
if (response[0]===0)
8-
node.emit(dop.CONS.CONNECT, request, response);
8+
node.emit(dop.cons.CONNECT, request, response);
99

1010

1111
// We must manage the rejection

src/protocol/onconnect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ dop.protocol.onconnect = function(node, request_id, request) {
33
var tokenServer=request[1],
44
response = dop.core.createResponse(request_id, 0);
55
node.tokenServer = tokenServer;
6-
node.emit(dop.CONS.CONNECT, JSON.stringify(response));
6+
node.emit(dop.cons.CONNECT, JSON.stringify(response));
77
};

0 commit comments

Comments
 (0)