polling-to-event
Advanced tools
Comparing version 1.2.0 to 1.3.0
31
index.js
@@ -5,2 +5,3 @@ var extend = require("extend"), | ||
EventEmitter = require("events").EventEmitter, | ||
pauseable = require("pauseable"), | ||
equal = require("deep-equal"); | ||
@@ -34,2 +35,8 @@ | ||
} | ||
// Do nothing if the user 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()) { | ||
return | ||
} | ||
debug("Emitting '%s'.", options.eventName); | ||
@@ -55,3 +62,3 @@ // Save the event name as first item in the parameters array | ||
} else { | ||
debug("Last polled data and previous poll data are equal."); | ||
debug("Last polled data and previous poll data are equal. Not emitting '%s' event", options.updateEventName); | ||
@@ -62,7 +69,13 @@ } | ||
// Emit the interval event after every polling | ||
return _this.emit.apply(_this, params); | ||
} | ||
setInterval(function() { | ||
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); | ||
@@ -72,2 +85,12 @@ } | ||
// Inherit from EventEmitter | ||
util.inherits(pollingtoevent, EventEmitter); | ||
util.inherits(pollingtoevent, EventEmitter); | ||
pollingtoevent.prototype.pause = function() { | ||
debug("Pausing interval"); | ||
this.interval.pause(); | ||
} | ||
pollingtoevent.prototype.resume = function() { | ||
debug("Resuming interval"); | ||
this.interval.resume(); | ||
} |
{ | ||
"name": "polling-to-event", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Receive events with EventEmitter from a polling function ran on an interval", | ||
@@ -15,2 +15,3 @@ "main": "index.js", | ||
"polling", | ||
"longpolling", | ||
"async", | ||
@@ -28,4 +29,5 @@ "eventemitter", | ||
"debug": "~2.1.1", | ||
"deep-equal": "~0.2.1" | ||
"deep-equal": "~0.2.1", | ||
"pauseable": "~0.1.5" | ||
} | ||
} |
@@ -5,2 +5,5 @@ # node-polling-to-event | ||
Technically speaking [polling](http://en.wikipedia.org/wiki/Polling_%28computer_science%29) is defined as status check from an external device, | ||
but with **node-polling-to-event** you can expect events from your own function that polls whatever resource you want that implies either a synchronous or an asynchronous operation. | ||
## Installation | ||
@@ -71,3 +74,3 @@ | ||
It returns a NodeJS [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter) that emits the polled data on an interval. | ||
It creates an event emitter that emits the polled data on an interval. | ||
@@ -78,17 +81,30 @@ **Arguments** | ||
* `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. | ||
* `arg1, arg2, ... argN` - The data fetched by your polling function. Your `pollingfunction` passes this arguments to `done()` in order to be emitted by the emitter. Any number of arguments will do. | ||
* `options` - **Optional**. An `Object` having any of the following keys: | ||
* `interval` - Interval in milliseconds. **Default**: 1000. | ||
* `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`. | ||
* `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"`. | ||
**Returns** - Returns an `events.EventEmitter` instance. | ||
**Returns** | ||
#### Events | ||
Returns an [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter) instance with some extra methods. See [Methods](#emitter-methods). | ||
* `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. | ||
#### Emitter events | ||
* `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()` with exception of 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`* | ||
* `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()` with exception of the error parameter which is the first parameter `done()` uses. *You can also customize this event's name using the option `updateEventName`* | ||
#### Emitter methods | ||
##### stop() | ||
Stops the interval. | ||
##### resume() | ||
Resumes the interval if it has been stopped | ||
## TODO | ||
@@ -95,0 +111,0 @@ |
13276
9
128
133
4
+ Addedpauseable@~0.1.5
+ Addedpauseable@0.1.7(transitive)