Skip to content

Commit 63dc20a

Browse files
committed
send_queue is concern of dop, not the transport
1 parent 251e4c8 commit 63dc20a

File tree

12 files changed

+171
-143
lines changed

12 files changed

+171
-143
lines changed

dist/browser.js

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,16 @@ function websocket(dop, node, options) {
162162
send_queue = [];
163163

164164
socket.send = function(message) {
165-
(socket.readyState !== 1) ?
166-
send_queue.push(message)
167-
:
165+
// (socket.readyState !== 1) ?
166+
// send_queue.push(message)
167+
// :
168168
send.call(socket, message);
169169
};
170170

171171
socket.addEventListener('open', function() {
172172
dop.core.onopen(node, socket);
173-
while (send_queue.length>0)
174-
send.call(socket, send_queue.shift());
173+
// while (send_queue.length>0)
174+
// send.call(socket, send_queue.shift());
175175
});
176176

177177
socket.addEventListener('message', function(message) {
@@ -231,6 +231,37 @@ dop.util.get = function(object, path) {
231231

232232

233233

234+
// dop.util.set = function(object, path, value) {
235+
236+
// if (path.length == 0)
237+
// return object;
238+
239+
// path = path.slice(0);
240+
// var obj = object, objdeep, index=0, total=path.length-1;
241+
242+
// for (;index<total; ++index) {
243+
// objdeep = obj[path[index]];
244+
// obj = (objdeep && typeof objdeep == 'object') ?
245+
// objdeep
246+
// :
247+
// obj[path[index]] = {};
248+
// }
249+
250+
// obj[path[index]] = value;
251+
252+
// return object;
253+
// };
254+
255+
// /*
256+
// ori = {test:{hs:124}}
257+
// console.log( dop.util.set(ori, ['test','more'], undefined))
258+
// */
259+
260+
261+
262+
263+
264+
234265

235266
////////// src/util/invariant.js
236267

@@ -357,40 +388,6 @@ dop.util.pathRecursive = function (source, callback, destiny, mutator, circular,
357388

358389

359390

360-
////////// src/util/set.js
361-
362-
// dop.util.set = function(object, path, value) {
363-
364-
// if (path.length == 0)
365-
// return object;
366-
367-
// path = path.slice(0);
368-
// var obj = object, objdeep, index=0, total=path.length-1;
369-
370-
// for (;index<total; ++index) {
371-
// objdeep = obj[path[index]];
372-
// obj = (objdeep && typeof objdeep == 'object') ?
373-
// objdeep
374-
// :
375-
// obj[path[index]] = {};
376-
// }
377-
378-
// obj[path[index]] = value;
379-
380-
// return object;
381-
// };
382-
383-
// /*
384-
// ori = {test:{hs:124}}
385-
// console.log( dop.util.set(ori, ['test','more'], undefined))
386-
// */
387-
388-
389-
390-
391-
392-
393-
394391
////////// src/util/sprintf.js
395392

396393
dop.util.sprintf = function() {
@@ -1062,19 +1059,21 @@ dop.core.node = function() {
10621059
this.request_inc = 1;
10631060
this.requests = {};
10641061
this.requests_queue = [];
1062+
this.send_queue = [];
1063+
this.readyState = 0; //0:close, 1:open, 2:connected
10651064
};
10661065
// Inherit emitter
10671066
dop.util.merge(dop.core.node.prototype, dop.util.emitter.prototype);
10681067

10691068

10701069

10711070
dop.core.node.prototype.send = function(message) {
1072-
return this.socket.send(message);
1071+
(this.readyState>0) ? this.socket.send(message) : this.send_queue.push(message);
10731072
};
10741073

10751074

10761075
dop.core.node.prototype.subscribe = function() {
1077-
return dop.protocol.subscribe(node, arguments);
1076+
return dop.protocol.subscribe(this, arguments);
10781077
};
10791078

10801079

@@ -1083,16 +1082,6 @@ dop.core.node.prototype.close = function() {
10831082
};
10841083

10851084

1086-
dop.protocol.subscribe = function(node, args) {
1087-
args = Array.prototype.slice.call(args, 0);
1088-
args.unshift(node, dop.protocol.instructions.subscribe);
1089-
var request = dop.core.createRequest.apply(node, args);
1090-
dop.core.storeRequest(node, request);
1091-
dop.core.emitRequests(node);
1092-
return request.promise;
1093-
};
1094-
1095-
10961085

10971086

10981087
////////// src/core/error.js
@@ -2123,6 +2112,7 @@ dop.core.onclose = function(listener_or_node, socket) {
21232112
listener_or_node.emit('close', socket);
21242113

21252114
if (dop.util.isObject(node)) {
2115+
node.readyState = 0;
21262116
listener_or_node.emit('disconnect', node);
21272117
dop.core.unregisterNode(node);
21282118
}
@@ -2256,10 +2246,13 @@ dop.core.onopen = function(listener_or_node, socket, transport) {
22562246
// if listener_or_node is listener we send token
22572247
if (listener_or_node.socket !== socket) {
22582248
var node = new dop.core.node();
2249+
node.readyState = 1;
22592250
node.transport = transport;
22602251
node.socket = socket;
22612252
node.try_connects = listener_or_node.options.try_connects;
22622253
node.listener = listener_or_node;
2254+
while (node.send_queue.length>0)
2255+
node.send(node.send_queue.shift());
22632256
dop.protocol.connect(node);
22642257
}
22652258
};
@@ -2357,6 +2350,7 @@ dop.protocol._onconnect = function(node, request_id, request, response) {
23572350

23582351
// Node is connected correctly
23592352
if (response[0]===0) {
2353+
node.readyState = 2;
23602354
node.listener.emit('connect', node, token);
23612355
node.emit('connect', token);
23622356
}
@@ -2521,6 +2515,7 @@ dop.protocol.onconnect = function(node, request_id, request) {
25212515
if (dop.data.node[token] === undefined) {
25222516
dop.core.registerNode(node, token);
25232517
response = dop.core.createResponse(request_id, 0);
2518+
node.readyState = 2;
25242519
node.emit('connect', token);
25252520
}
25262521
else
@@ -2575,6 +2570,20 @@ dop.protocol.onsubscribe = function(node, request_id, request) {
25752570

25762571

25772572

2573+
////////// src/protocol/subscribe.js
2574+
2575+
dop.protocol.subscribe = function(node, args) {
2576+
args = Array.prototype.slice.call(args, 0);
2577+
args.unshift(node, dop.protocol.instructions.subscribe);
2578+
var request = dop.core.createRequest.apply(node, args);
2579+
dop.core.storeRequest(node, request);
2580+
dop.core.emitRequests(node);
2581+
return request.promise;
2582+
};
2583+
2584+
2585+
2586+
25782587
////////// src/umd.js
25792588
// Factory
25802589
if (root === undefined)

dist/browser.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/nodejs.js

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,37 @@ dop.util.get = function(object, path) {
109109

110110

111111

112+
// dop.util.set = function(object, path, value) {
113+
114+
// if (path.length == 0)
115+
// return object;
116+
117+
// path = path.slice(0);
118+
// var obj = object, objdeep, index=0, total=path.length-1;
119+
120+
// for (;index<total; ++index) {
121+
// objdeep = obj[path[index]];
122+
// obj = (objdeep && typeof objdeep == 'object') ?
123+
// objdeep
124+
// :
125+
// obj[path[index]] = {};
126+
// }
127+
128+
// obj[path[index]] = value;
129+
130+
// return object;
131+
// };
132+
133+
// /*
134+
// ori = {test:{hs:124}}
135+
// console.log( dop.util.set(ori, ['test','more'], undefined))
136+
// */
137+
138+
139+
140+
141+
142+
112143

113144
////////// src/util/invariant.js
114145

@@ -235,40 +266,6 @@ dop.util.pathRecursive = function (source, callback, destiny, mutator, circular,
235266

236267

237268

238-
////////// src/util/set.js
239-
240-
// dop.util.set = function(object, path, value) {
241-
242-
// if (path.length == 0)
243-
// return object;
244-
245-
// path = path.slice(0);
246-
// var obj = object, objdeep, index=0, total=path.length-1;
247-
248-
// for (;index<total; ++index) {
249-
// objdeep = obj[path[index]];
250-
// obj = (objdeep && typeof objdeep == 'object') ?
251-
// objdeep
252-
// :
253-
// obj[path[index]] = {};
254-
// }
255-
256-
// obj[path[index]] = value;
257-
258-
// return object;
259-
// };
260-
261-
// /*
262-
// ori = {test:{hs:124}}
263-
// console.log( dop.util.set(ori, ['test','more'], undefined))
264-
// */
265-
266-
267-
268-
269-
270-
271-
272269
////////// src/util/sprintf.js
273270

274271
dop.util.sprintf = function() {
@@ -940,19 +937,21 @@ dop.core.node = function() {
940937
this.request_inc = 1;
941938
this.requests = {};
942939
this.requests_queue = [];
940+
this.send_queue = [];
941+
this.readyState = 0; //0:close, 1:open, 2:connected
943942
};
944943
// Inherit emitter
945944
dop.util.merge(dop.core.node.prototype, dop.util.emitter.prototype);
946945

947946

948947

949948
dop.core.node.prototype.send = function(message) {
950-
return this.socket.send(message);
949+
(this.readyState>0) ? this.socket.send(message) : this.send_queue.push(message);
951950
};
952951

953952

954953
dop.core.node.prototype.subscribe = function() {
955-
return dop.protocol.subscribe(node, arguments);
954+
return dop.protocol.subscribe(this, arguments);
956955
};
957956

958957

@@ -961,16 +960,6 @@ dop.core.node.prototype.close = function() {
961960
};
962961

963962

964-
dop.protocol.subscribe = function(node, args) {
965-
args = Array.prototype.slice.call(args, 0);
966-
args.unshift(node, dop.protocol.instructions.subscribe);
967-
var request = dop.core.createRequest.apply(node, args);
968-
dop.core.storeRequest(node, request);
969-
dop.core.emitRequests(node);
970-
return request.promise;
971-
};
972-
973-
974963

975964

976965
////////// src/core/error.js
@@ -2001,6 +1990,7 @@ dop.core.onclose = function(listener_or_node, socket) {
20011990
listener_or_node.emit('close', socket);
20021991

20031992
if (dop.util.isObject(node)) {
1993+
node.readyState = 0;
20041994
listener_or_node.emit('disconnect', node);
20051995
dop.core.unregisterNode(node);
20061996
}
@@ -2134,10 +2124,13 @@ dop.core.onopen = function(listener_or_node, socket, transport) {
21342124
// if listener_or_node is listener we send token
21352125
if (listener_or_node.socket !== socket) {
21362126
var node = new dop.core.node();
2127+
node.readyState = 1;
21372128
node.transport = transport;
21382129
node.socket = socket;
21392130
node.try_connects = listener_or_node.options.try_connects;
21402131
node.listener = listener_or_node;
2132+
while (node.send_queue.length>0)
2133+
node.send(node.send_queue.shift());
21412134
dop.protocol.connect(node);
21422135
}
21432136
};
@@ -2235,6 +2228,7 @@ dop.protocol._onconnect = function(node, request_id, request, response) {
22352228

22362229
// Node is connected correctly
22372230
if (response[0]===0) {
2231+
node.readyState = 2;
22382232
node.listener.emit('connect', node, token);
22392233
node.emit('connect', token);
22402234
}
@@ -2399,6 +2393,7 @@ dop.protocol.onconnect = function(node, request_id, request) {
23992393
if (dop.data.node[token] === undefined) {
24002394
dop.core.registerNode(node, token);
24012395
response = dop.core.createResponse(request_id, 0);
2396+
node.readyState = 2;
24022397
node.emit('connect', token);
24032398
}
24042399
else
@@ -2453,6 +2448,20 @@ dop.protocol.onsubscribe = function(node, request_id, request) {
24532448

24542449

24552450

2451+
////////// src/protocol/subscribe.js
2452+
2453+
dop.protocol.subscribe = function(node, args) {
2454+
args = Array.prototype.slice.call(args, 0);
2455+
args.unshift(node, dop.protocol.instructions.subscribe);
2456+
var request = dop.core.createRequest.apply(node, args);
2457+
dop.core.storeRequest(node, request);
2458+
dop.core.emitRequests(node);
2459+
return request.promise;
2460+
};
2461+
2462+
2463+
2464+
24562465
////////// src/umd.js
24572466
// Factory
24582467
if (root === undefined)

0 commit comments

Comments
 (0)