dispatch error to request callback if callback set

This commit is contained in:
sidorares 2011-07-20 12:23:46 +10:00
parent 0ca8b090be
commit c792439cc8
2 changed files with 42 additions and 8 deletions

View file

@ -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();
});

25
test/xlsatoms.js Normal file
View file

@ -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();
});