From 4c1c8be8f16f92e5550366b11e632d4284db93d8 Mon Sep 17 00:00:00 2001 From: Michael Morrison Date: Wed, 10 Jul 2013 05:59:10 -0500 Subject: [PATCH] Don't clobber options, and allow callback as a seperate param --- index.js | 14 +++++++++----- protocols/core.js | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 286a5f8..53f6cb1 100644 --- a/index.js +++ b/index.js @@ -20,21 +20,25 @@ udpSocket.on('message', function(buffer, rinfo) { module.exports = { - query: function(options) { - var type = (options.type || '').replace(/\W/g,''); + query: function(options,callback) { + if(callback) options.callback = callback; + var type = (options.type || '').replace(/\W/g,''); var protocol = require('./protocols/'+type); + var query = new protocol(); query.udpSocket = udpSocket; query.type = type; - query.options = options; + + // copy over options + query.options = {}; + for(var i in options) query.options[i] = options[i]; + activeQueries.push(query); query.on('finished',function(state) { var i = activeQueries.indexOf(query); if(i >= 0) activeQueries.splice(i, 1); - - if(options.callback) options.callback(state); }); process.nextTick(function() { diff --git a/protocols/core.js b/protocols/core.js index 25f9043..57175b3 100644 --- a/protocols/core.js +++ b/protocols/core.js @@ -48,6 +48,7 @@ module.exports = Class.extend(EventEmitter,{ this.reset(); this.finished = true; this.emit('finished',result); + if(this.options.callback) this.options.callback(result); }, reset: function() {