From 303cda6a234095364f6ba03e22f85d70567b1f8d Mon Sep 17 00:00:00 2001 From: Aleks Selivanov Date: Wed, 29 Apr 2015 13:32:48 +0500 Subject: [PATCH] Delegate check for end of reply sequence to unpacker --- lib/xcore.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/xcore.js b/lib/xcore.js index 16878c7..e572f01 100644 --- a/lib/xcore.js +++ b/lib/xcore.js @@ -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]; @@ -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(); @@ -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]; @@ -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(); @@ -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();