From c792439cc8678a895c35e67abbcb30ad2b6e3a5c Mon Sep 17 00:00:00 2001 From: sidorares Date: Wed, 20 Jul 2011 12:23:46 +1000 Subject: [PATCH] dispatch error to request callback if callback set --- lib/x11/xcore.js | 25 +++++++++++++++++-------- test/xlsatoms.js | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 test/xlsatoms.js diff --git a/lib/x11/xcore.js b/lib/x11/xcore.js index 4fc29bf..f016453 100644 --- a/lib/x11/xcore.js +++ b/lib/x11/xcore.js @@ -89,7 +89,7 @@ XClient.prototype.importRequestsFromTemplates = function(target, reqs) for (r in reqs) { // r is request name - target[r] = (function(reqName) { + target[r] = (function(reqName) { var reqFunc = function req_proxy() { client.seq_num++; // TODO: handle overflow (seq should be last 15 (?) bits of the number // is it fast? @@ -111,26 +111,29 @@ XClient.prototype.importRequestsFromTemplates = function(target, reqs) { // call template with input arguments (not including callback which is last argument TODO currently with callback. won't hurt) //reqPack = reqTemplate.call(args); - reqPack = reqTemplate.apply(this, req_proxy.arguments); + var reqPack = reqTemplate.apply(this, req_proxy.arguments); var format = reqPack[0]; var requestArguments = reqPack[1]; if (callback) this.replies[this.seq_num] = [reqName, callback]; - //console.error([format, requestArguments]); + //console.log([format, requestArguments]); client.pack_stream.pack(format, requestArguments); client.pack_stream.flush(); } else if (templateType == 'Array'){ var format = reqTemplate[0]; - var requestArguments = reqTemplate[1]; + var requestArguments = []; + + for (a = 0; a < reqTemplate[1].length; ++a) + requestArguments.push(reqTemplate[1][a]); for (a in args) requestArguments.push(args[a]); if (callback) this.replies[this.seq_num] = [reqName, callback]; - //console.error([format, requestArguments]); + //console.log([format, requestArguments]); client.pack_stream.pack(format, requestArguments); client.pack_stream.flush(); } else { @@ -210,8 +213,14 @@ XClient.prototype.expectReplyHeader = function() error.minorOpcode = res[1]; error.majorOpcode = res[2]; } - console.log(error); - //client.emit('error', error); + //console.log(error); + var handler = client.replies[seq_num]; + if (handler) { + var callback = handler[1]; + callback(error); + delete client.replies[seq_num]; + } else + client.emit('error', error); client.expectReplyHeader(); } ); return; @@ -244,8 +253,8 @@ XClient.prototype.expectReplyHeader = function() var result = unpack( data ); var callback = handler[1]; callback(result); + delete client.replies[seq_num]; } - // wait for new packet from server client.expectReplyHeader(); }); diff --git a/test/xlsatoms.js b/test/xlsatoms.js new file mode 100644 index 0000000..b9328f6 --- /dev/null +++ b/test/xlsatoms.js @@ -0,0 +1,25 @@ +var x11 = require('../lib/x11'); + +var xclient = x11.createClient(); +var atomId = 10; +xclient.on('connect', function(display) { + var X = this; + function listAtoms() + { + function getAtom(a) + { + X.GetAtomName(a, function(str) { + if (typeof str != 'string') // 'Bad atom' error + { + X.terminate(); + return; + } + console.log(a + ' ' + str); + listAtoms(); + }); + } + getAtom(atomId); + atomId++; + } + listAtoms(); +});