polling-to-event
Advanced tools
Comparing version 1.4.0 to 1.4.1
@@ -18,4 +18,4 @@ var pollingtoevent = require(".."), | ||
emitter.on("error", function(err, data) { | ||
console.log("Emitter errored: %s. with data %j", err, data); | ||
emitter.on("error", function(err) { | ||
console.log("Emitter errored: %s. with", err); | ||
}); |
@@ -9,7 +9,9 @@ var pollingtoemitter = require(".."); | ||
i = (i == nums.length - 1) ? 0 : i + 1; | ||
}, { | ||
longpolling: true | ||
}); | ||
poller.on("interval", function(data) { | ||
console.log("Chicho :%s", nums[i]); | ||
poller.on("update", function(data) { | ||
console.log("Current number is :%s", nums[i]); | ||
console.log(data); | ||
}); |
46
index.js
@@ -16,2 +16,3 @@ var extend = require("extend"), | ||
var _this = this, | ||
firstpoll = true, | ||
lastParams = undefined, | ||
@@ -38,2 +39,5 @@ defaults = { | ||
// after the user's wish to pause the event emission. | ||
// | ||
// I check if _this.interval is defined here because on the first poll | ||
// the interval has not been set yet. | ||
if (_this.interval && _this.interval.isPaused()) { | ||
@@ -44,29 +48,27 @@ return; | ||
// that will be used wit _this.emit.apply() | ||
var params = [options.eventName]; | ||
var params = []; | ||
for (var i = 1; i < arguments.length; i++) { | ||
params.push(arguments[i]); | ||
} | ||
debug("Emitting '%s' with params %j.", options.eventName, params); | ||
debug("Emitting '%s'.", options.eventName); | ||
// Emit the interval event after every polling | ||
_this.emit.apply(_this, params); | ||
_this.emit.apply(_this, [options.eventName].concat(params)); | ||
// If long polling is set, compare | ||
// If this is the first call or long polling is set, compare | ||
// the last value polled with the last one | ||
// emit | ||
if (options.longpolling) { | ||
if (firstpoll || options.longpolling) { | ||
debug("Comparing last polled parameters"); | ||
//debug("%j, %j", params, lastParams); | ||
if (!equal(params, lastParams)) { | ||
debug("Last polled data and previous poll data are not equal. Emitting '%s' event", options.updateEventName); | ||
var updateEventParams = params.slice(0); | ||
updateEventParams[0] = options.updateEventName; | ||
debug("Last polled data and previous poll data are not equal."); | ||
debug("Emitting '%s'.", options.updateEventName); | ||
// Emit the update event after longpolling | ||
_this.emit.apply(_this, params.slice(1)) | ||
_this.emit.apply(_this, [options.updateEventName].concat(params)) | ||
} else { | ||
debug("Last polled data and previous poll data are equal. Not emitting '%s' event", options.updateEventName); | ||
debug("Last polled data and previous poll data are equal."); | ||
} | ||
lastParams = params.slice(0); | ||
lastParams = params; | ||
} | ||
// Set this to false when the function ends | ||
firstpoll = false; | ||
} | ||
@@ -79,12 +81,12 @@ | ||
func(done); | ||
_this.interval = pauseable.setInterval(function() { | ||
// Call the user's function only if he has not paused the interval. | ||
// Otherwise the user calls pause() and a callback from previous intervals are called | ||
// after the user's wish to pause the event emission. | ||
if (!_this.interval.isPaused()) { | ||
func(done); | ||
} | ||
}, options.interval); | ||
}, 0); | ||
// Set the interval | ||
_this.interval = pauseable.setInterval(function() { | ||
// Call the user's function only if he has not paused the interval. | ||
// Otherwise the user calls pause() and a callback from previous intervals are called | ||
// after the user's wish to pause the event emission. | ||
if (!_this.interval.isPaused()) { | ||
func(done); | ||
} | ||
}, options.interval); | ||
@@ -91,0 +93,0 @@ |
{ | ||
"name": "polling-to-event", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "Receive events with EventEmitter from a polling function ran on an interval", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
14072
151