Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow method handlers to return promises. #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 46 additions & 22 deletions lib/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var EventEmitter = require('events').EventEmitter;
var introspect = require('./introspect');
var constants = require('./constants');
var stdDbusIfaces= require('./stdifaces');
var Q= require("q"),
promise= Q.defer().promise

module.exports = function bus(conn) {
if (!(this instanceof bus)) {
Expand Down Expand Up @@ -116,7 +118,19 @@ module.exports = function bus(conn) {
} else if (msg.type == constants.messageType.signal) {
self.signals.emit(self.mangle(msg), msg.body, msg.signature);
} else { // methodCall


function _messageResult(res){
var conn= this.conn;
if(res)
this.body = res;
else{
delete this.signature;
delete this.body;
}
delete this.conn;
conn.message(this);
}

// exported interfaces handlers
var obj, iface, impl;
if (obj = self.exportedObjects[msg.path]) {
Expand Down Expand Up @@ -150,7 +164,12 @@ module.exports = function bus(conn) {
reply.signature = resultSignature;
reply.body = result;
}
self.connection.message(reply);
if (result instanceof promise) {
reply.conn = self.conn;
result.then(_messageResult.bind(reply));
} else {
self.connection.message(reply);
}
return;
} else {
console.error('Interface ' + msg['interface'] + ' is not supported');
Expand All @@ -160,29 +179,34 @@ module.exports = function bus(conn) {
// setMethodCall handlers
handler = self.methodCallHandlers[self.mangle(msg)];
if (handler) {
var result;
try {
result = handler[0].apply(null, msg.body);
} catch (e) {
console.error("Caught exception while trying to execute handler: ", e);
self.sendError(e.message, e.description);
return;
}
var reply = {
type: constants.messageType.methodReturn,
destination: msg.sender,
replySerial: msg.serial
//, sender: self.name
};
if (result) {
reply.signature = handler[1];
reply.body = result;
}
self.connection.message(reply);
var result;
try {
result = handler[0].apply(null, msg.body);
} catch (e) {
console.error("Caught exception while trying to execute handler: ", e);
self.sendError(e.message, e.description);
return;
}
var reply = {
type: constants.messageType.methodReturn,
destination: msg.sender,
replySerial: msg.serial
//, sender: self.name
};
if (result instanceof Q)
if (result) {
reply.signature = handler[1];
reply.body = result;
}
if (result instanceof promise) {
reply.conn= self.connection
result.then(_messageResult.bind(reply))
} else {
self.connection.message(reply);
}
} else {
self.sendError(msg, 'org.freedesktop.DBus.Error.UnknownService', 'Uh oh oh');
}

}
});

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"byline": "2.0.3",
"put": "0.0.6",
"abstractsocket": "0.0.2",
"xml2js": "0.1.14"
"xml2js": "0.1.14",
"q": "0.8.12"
},
"devDependencies": {
"mocha": "*",
Expand Down