polling-to-event
Advanced tools
Comparing version 1.0.2 to 1.2.0
32
index.js
var extend = require("extend"), | ||
debug = require("debug")("polling-to-event"), | ||
util = require("util"), | ||
EventEmitter = require("events").EventEmitter; | ||
EventEmitter = require("events").EventEmitter, | ||
equal = require("deep-equal"); | ||
@@ -13,6 +14,9 @@ module.exports = pollingtoevent; | ||
var lastParams = undefined; | ||
var _this = this, | ||
defaults = { | ||
interval: 1000, | ||
eventName: "interval" | ||
eventName: "interval", | ||
updateEventName: "update", | ||
longpolling: false, | ||
}; | ||
@@ -31,2 +35,4 @@ | ||
debug("Emitting '%s'.", options.eventName); | ||
// Save the event name as first item in the parameters array | ||
// that will be used wit _this.emit.apply() | ||
var params = [options.eventName]; | ||
@@ -36,4 +42,22 @@ for (var i = 1; i < arguments.length; i++) { | ||
} | ||
_this.emit.apply(_this, params); | ||
//return _this.emit(options.eventName, data); | ||
// If long polling is set, compare | ||
// the last value polled with the last one | ||
// emit | ||
if (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; | ||
// Emit the update event after longpolling | ||
_this.emit.apply(_this, params.slice(1)) | ||
} else { | ||
debug("Last polled data and previous poll data are equal."); | ||
} | ||
lastParams = params.slice(0); | ||
} | ||
// Emit the interval event after every polling | ||
return _this.emit.apply(_this, params); | ||
} | ||
@@ -40,0 +64,0 @@ |
{ | ||
"name": "polling-to-event", | ||
"version": "1.0.2", | ||
"version": "1.2.0", | ||
"description": "Receive events with EventEmitter from a polling function ran on an interval", | ||
@@ -25,6 +25,6 @@ "main": "index.js", | ||
"dependencies": { | ||
"superagent": "~0.21.0", | ||
"extend": "~2.0.0", | ||
"debug": "~2.1.1" | ||
"debug": "~2.1.1", | ||
"deep-equal": "~0.2.1" | ||
} | ||
} |
@@ -49,2 +49,19 @@ # node-polling-to-event | ||
**Long polling** | ||
If you set the option `longpolling:true` the emitter will emit an *update* event when | ||
the polled data differs. | ||
emitter = pollingtoevent(function(done) { | ||
request.get(url, function(err, req, data) { | ||
done(err, data); | ||
}); | ||
}, { | ||
longpolling:true | ||
}); | ||
emitter.on("update", function(data) { | ||
console.log("Update emitted at %s, with data %j", Date.now(), data); | ||
}); | ||
## API | ||
@@ -57,9 +74,11 @@ | ||
**Arguments** | ||
* `pollingfunction(done)` - The function you want to be called at an interval. When called, this function will receive a `done` parameter as its last argument. | ||
* `pollingfunction(done)` - **Required**. The function you want to be called at an interval. When called, this function will receive a `done` parameter as its last argument. | ||
* `done(error, arg1, arg2, ... argN) ` - You must call **done()** inside your function when your function finish its work. | ||
* `error` - Call `done()` with `null` as its first argument if there was no error. Call it with a standard nodejs `Error()` instance as first argument if you wish the emitter to emit an `error` event.. | ||
* `error` - **Required**. Call `done()` with `null` as its first argument if there was no error. Call it with an [error object](https://www.joyent.com/developers/node/design/errors) instance as first argument if you wish the emitter to emit an `error` event.. | ||
* `arg1, arg2, ... argN` - The data fetched by your polling function. You pass it to `done()` in order to be emitted by the emitter. Any number of arguments will do. | ||
* `options` - `{Object}` | ||
* `options` - **Optional**. An `Object` having any of the following keys: | ||
* `interval` - Interval in milliseconds. **Default**: 1000. | ||
* `eventName` - The event name to emit on each successful call to `done()` as second argument. **Default**: `"interval"`. | ||
* `eventName` - The event name to emit on each successful call to `done()`. **Default**: `"interval"`. | ||
* `longpolling` - Set to true if you want to be notified when data from the last poll differ from previous polled data. The data taken for comparison is every argument your `pollingfunction` passes to `done()`. The comparison is made with [deep-equal](https://www.npmjs.com/package/deep-equal). **Default:** `false`. | ||
* `eventUpdateName` - The event name to emit when last polled data differs from previous polling data. **Default**: `"update"`. | ||
@@ -70,6 +89,6 @@ **Returns** - Returns an `events.EventEmitter` instance. | ||
* `interval` - Emitted when an interval has completed and the `done()` function was called with no errors. *You can also customize this event's name using the option `eventName`*. **Arguments**: By listening to this event, you get on the listener the arguments passed by you to `done()` after the first argument. | ||
* `interval` - Emitted when an interval has completed and the `done()` function was called with no errors. *You can also customize this event's name using the option `eventName`*. **Parameters**: Your listener gets the parameter passed to `done()` excepting the error parameter which is the first parameter `done()` uses. | ||
* `error` - Emitted when `done()` was called with an error object. It emits the data polled by your polling function. **Parameters**. An error object. | ||
* `update` - Emitted when option `longpolling` is true and the last polled data differs from the previous polling data. **Parameters**: Your listener gets the parameter received by `done()` excepting the error parameter which is the first parameter `done()` uses. *You can also customize this event's name using the option `updateEventName`* | ||
* `error` - Emitted when `done()` was called with an error. It emits the data polled by your polling function. **Arguments**. A NodeJS error object. | ||
## TODO | ||
@@ -76,0 +95,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11154
8
86
117
+ Addeddeep-equal@~0.2.1
+ Addeddeep-equal@0.2.2(transitive)
- Removedsuperagent@~0.21.0
- Removedasync@0.9.2(transitive)
- Removedcombined-stream@0.0.7(transitive)
- Removedcomponent-emitter@1.1.2(transitive)
- Removedcookiejar@2.0.1(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddelayed-stream@0.0.5(transitive)
- Removedextend@1.2.1(transitive)
- Removedform-data@0.1.3(transitive)
- Removedformidable@1.0.14(transitive)
- Removedinherits@2.0.4(transitive)
- Removedisarray@0.0.1(transitive)
- Removedmethods@1.0.1(transitive)
- Removedmime@1.2.11(transitive)
- Removedqs@1.2.0(transitive)
- Removedreadable-stream@1.0.27-1(transitive)
- Removedreduce-component@1.0.1(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedsuperagent@0.21.0(transitive)