ma3route-sdk
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -25,19 +25,40 @@ /** | ||
/** | ||
* Poller Class. Inherits from events.EventEmitter. | ||
* Poller Class. Inherits from events.EventEmitter. This poller | ||
* is designed in that you it polls for new updates automagically | ||
* rather than have you implement `lastreadId` logic. | ||
* | ||
* @name Poller | ||
* @global | ||
* @constructor | ||
* | ||
* @example | ||
* // create a new poller | ||
* var client = new sdk.Poller(sdk.trafficUpdates.get, params); | ||
* // create a new poller that keeps retrieiving traffic updates | ||
* var poller = new sdk.Poller(sdk.trafficUpdates.get, { | ||
* interval: 5000, // 5 seconds | ||
* }); | ||
* | ||
* // keeps retrieiving traffic updates | ||
* client.trafficUpdates.getOne(1, function(err, items) { | ||
* console.log(err, items); | ||
* // listen for new updates | ||
* poller.on("message", function(updates, meta, responseObject) { | ||
* console.log("received these updates: %j", updates); | ||
* }); | ||
* | ||
* // listen for errors e.g. network failures etc. | ||
* // if an error occurs an you not listening on the "error" | ||
* // event, the error will bubble up to the domain/process. | ||
* poller.on("error", function(err) { | ||
* console.log("error: %s", err); | ||
* }); | ||
* | ||
* // you have to explicitly start it | ||
* poller.start(); | ||
* | ||
* // lets say we close it after a minute or so | ||
* setTimeout(function() { | ||
* poller.stop(); | ||
* }, 1000 * 60); | ||
* | ||
* @constructor | ||
* @param {itemsGetRequest} getRequest | ||
* @param {Object} [options] | ||
* @param {Object|Function} [options.params] - parameters passed to get request. | ||
* If `options.params` is a function, it is called and it return value is | ||
* assumed an object as the request params. | ||
* If `options.params` requires to do an asynchronous action, it is passed a | ||
* `done` function as its only argument to call with the value when done. | ||
* @param {Integer} [options.interval=5000] - configures the poller's timer | ||
@@ -48,17 +69,9 @@ * @param {Object|Function} [options.params] | ||
options = options || { }; | ||
var me = this; | ||
events.EventEmitter.call(me); | ||
var pollerOptions = utils.getPollerOptions([utils.setup(), me, options]); | ||
me._get = getRequest; | ||
me._params = options.params || { }; | ||
me._timer = setInterval(function() { | ||
var args = me._params; | ||
if (_.isFunction(me._params)) { | ||
args = me._params(); | ||
} | ||
me._get(args, function(err, items, meta, res) { | ||
me.emit("message", err, items, meta, res); | ||
}); | ||
}, pollerOptions.interval); | ||
return me; | ||
events.EventEmitter.call(this); | ||
this._pollerOptions = utils.getPollerOptions([utils.setup().poller, options]); | ||
this._get = getRequest; | ||
this._params = options.params || { }; | ||
this._lastreadId = this._params.lastreadId || null; | ||
this._timer = null; | ||
return this; | ||
} | ||
@@ -73,2 +86,40 @@ | ||
/** | ||
* Starts the poller | ||
*/ | ||
Poller.prototype.start = function start() { | ||
var me = this; | ||
function sendRequest(args) { | ||
me._get(args, function(err, items, meta, res) { | ||
if (err) { | ||
return me.emit("error", err); | ||
} | ||
// save the last read id | ||
if (items.length) { | ||
me._params.lastreadId = items[items.length - 1].id; | ||
} | ||
return me.emit("message", items, meta, res); | ||
}); | ||
} | ||
me._timer = setInterval(function() { | ||
var args = { }; | ||
if (me._lastreadId) { | ||
args.lastreadId = me._lastreadId; | ||
} | ||
if (_.isFunction(me._params)) { | ||
if (me._params.length) { | ||
return me._params(function(params) { | ||
_.merge(args, params); | ||
return sendRequest(args); | ||
}); | ||
} | ||
_.merge(args, me._params()); | ||
} else { | ||
_.merge(args, me._params); | ||
} | ||
return sendRequest(args); | ||
}, me._pollerOptions.interval); | ||
}; | ||
/** | ||
* Stops the poller | ||
@@ -75,0 +126,0 @@ */ |
@@ -87,6 +87,6 @@ /** | ||
if (!settings) { | ||
return SETTINGS; | ||
return _.cloneDeep(SETTINGS); | ||
} | ||
_.merge(SETTINGS, settings); | ||
return SETTINGS; | ||
return _.cloneDeep(SETTINGS); | ||
} | ||
@@ -93,0 +93,0 @@ |
{ | ||
"name": "ma3route-sdk", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Node.js SDK for developing with the Ma3Route REST API", | ||
@@ -5,0 +5,0 @@ "repository": { |
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
41826
1291