Skip to content

Commit

Permalink
Delegate check for end of reply sequence to unpacker
Browse files Browse the repository at this point in the history
  • Loading branch information
real-alexei committed Apr 29, 2015
1 parent 0df0cea commit 303cda6
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lib/xcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ XClient.prototype.importRequestsFromTemplates = function(target, reqs)
var requestArguments = reqPack[1];

if (callback)
this.replies[this.seq_num] = [reqReplTemplate[1], callback];
this.replies[this.seq_num] = [reqReplTemplate[1], callback, reqReplTemplate[2]];

client.pack_stream.pack(format, requestArguments);
var b = client.pack_stream.write_queue[0];
Expand Down Expand Up @@ -221,7 +221,7 @@ XClient.prototype.importRequestsFromTemplates = function(target, reqs)
requestArguments.push(args[a]);

if (callback)
this.replies[this.seq_num] = [reqReplTemplate[1], callback];
this.replies[this.seq_num] = [reqReplTemplate[1], callback, reqReplTemplate[2]];

client.pack_stream.pack(format, requestArguments);
client.pack_stream.flush();
Expand Down Expand Up @@ -423,6 +423,10 @@ XClient.prototype.expectReplyHeader = function()
var seq_num = res[2];
var bad_value = res[3];

function unsubscribe() {
delete client.replies[seq_num];
}

if (type == 0)
{
var error_code = res[1];
Expand Down Expand Up @@ -455,9 +459,9 @@ XClient.prototype.expectReplyHeader = function()
if (!handled)
client.emit('error', error);
// TODO: should we delete seq2stack and reply even if there is no handler?
if (client.options.debug)
delete client.seq2stack[seq_num];
delete client.replies[seq_num];
if (client.options.debug)
delete client.seq2stack[seq_num];
unsubscribe();
} else
client.emit('error', error);
client.expectReplyHeader();
Expand Down Expand Up @@ -500,17 +504,19 @@ XClient.prototype.expectReplyHeader = function()
var handler = client.replies[seq_num];
if (handler) {
var unpack = handler[0];
var callback = handler[1];
var multiReply = handler[2];
if (client.pending_atoms[seq_num]) {
opt_data = seq_num;
}

var result = unpack.call(client, data, opt_data);
var callback = handler[1];
var multiReply = handler[2];
callback(null, result);
if (!multiReply || result === undefined) {
delete client.replies[seq_num];
var args = [data, opt_data];
if (multiReply) {
args.push(unsubscribe);
} else {
unsubscribe();
}
var result = unpack.apply(client, args);
callback(null, result);
}
// wait for new packet from server
client.expectReplyHeader();
Expand Down

0 comments on commit 303cda6

Please sign in to comment.