Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

polling-to-event

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

polling-to-event - npm Package Compare versions

Comparing version 1.0.2 to 1.2.0

example/long-polling.js

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 @@

6

package.json
{
"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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc