Don't clobber options, and allow callback as a seperate param

This commit is contained in:
Michael Morrison 2013-07-10 05:59:10 -05:00
parent 69be6d8017
commit 4c1c8be8f1
2 changed files with 10 additions and 5 deletions

View file

@ -20,21 +20,25 @@ udpSocket.on('message', function(buffer, rinfo) {
module.exports = { module.exports = {
query: function(options) { query: function(options,callback) {
var type = (options.type || '').replace(/\W/g,''); if(callback) options.callback = callback;
var type = (options.type || '').replace(/\W/g,'');
var protocol = require('./protocols/'+type); var protocol = require('./protocols/'+type);
var query = new protocol(); var query = new protocol();
query.udpSocket = udpSocket; query.udpSocket = udpSocket;
query.type = type; query.type = type;
query.options = options;
// copy over options
query.options = {};
for(var i in options) query.options[i] = options[i];
activeQueries.push(query); activeQueries.push(query);
query.on('finished',function(state) { query.on('finished',function(state) {
var i = activeQueries.indexOf(query); var i = activeQueries.indexOf(query);
if(i >= 0) activeQueries.splice(i, 1); if(i >= 0) activeQueries.splice(i, 1);
if(options.callback) options.callback(state);
}); });
process.nextTick(function() { process.nextTick(function() {

View file

@ -48,6 +48,7 @@ module.exports = Class.extend(EventEmitter,{
this.reset(); this.reset();
this.finished = true; this.finished = true;
this.emit('finished',result); this.emit('finished',result);
if(this.options.callback) this.options.callback(result);
}, },
reset: function() { reset: function() {