package/package.json000664 001750 001750 0000001240 12732020005013005 0ustar00000000 000000 { "name": "reinterval", "version": "1.1.0", "description": "reschedulable setInterval for node.js", "main": "index.js", "scripts": { "test": "mocha tests/" }, "repository": { "type": "git", "url": "https://github.com/4rzael/reInterval.git" }, "keywords": [ "node", "nodejs", "setInterval", "retimer", "schedule" ], "author": "4rzael", "license": "MIT", "bugs": { "url": "https://github.com/4rzael/reInterval/issues" }, "homepage": "https://github.com/4rzael/reInterval", "devDependencies": { "mocha": "^2.3.4", "chai": "^3.4.1", "chai-as-promised": "^5.1.0", "es6-shim": "^0.33.13" } } package/.npmignore000664 001750 001750 0000001016 12732017143012530 0ustar00000000 000000 # Logs logs *.log # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # node-waf configuration .lock-wscript # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules package/README.md000664 001750 001750 0000004113 12732020005012000 0ustar00000000 000000 # reInterval ![TRAVIS](https://travis-ci.org/4rzael/reInterval.svg) [![NPM](https://nodei.co/npm/reinterval.png?downloads=true&downloadRank=true)](https://nodei.co/npm/reinterval/) Reschedulable setInterval for node.js. ###### Note: Work highly inspired by [mcollina](https://github.com/mcollina)'s [retimer](https://github.com/mcollina/retimer). ## Example ```js var reInterval = require('reInterval'); var inter = reInterval(function () { console.log('this should be called after 13s'); }, 10 * 1000); // This will reset/reschedule the interval after 3 seconds, therefore // the interval callback won't be called for at least 13 seconds. setTimeout(function () { inter.reschedule(10 * 1000); }, 3 * 1000); ``` ## API: ###`reInterval(callback, interval[, param1, param2, ...])` This is exactly like setInterval. _Arguments:_ - `callback`: The callback to be executed repeatedly. - `interval`: The number of milliseconds (thousandths of a second) that the `reInterval()` function should wait before each call to `callback`. - `param1, param2, ...`: *(OPTIONAL)* These arguments are passed to the `callback` function. ####returns an `interval` object with the following methods: ###`interval.reschedule([interval])` This function resets the `interval` and restarts it now. _Arguments:_ - `interval`: *(OPTIONAL)* This argument can be used to change the amount of milliseconds to wait before each call to the `callback` passed to the `reInterval()` function. ###`interval.clear()` This function clears the interval. Can be used to temporarily clear the `interval`, which can be rescheduled at a later time. ###`interval.destroy()` This function clears the interval, and will also clear the `callback` and `params` passed to reInterval, so calling this essentially just makes this object ready for overwriting with a new `interval` object. #### Note: Please ensure that either the `interval.clear()` or `interval.destroy()` function is called before overwriting the `interval` object, because the internal `interval` can continue to run in the background unless cleared. ## license **MIT** package/LICENSE000664 001750 001750 0000002067 12732017143011545 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2015 Agor Maxime Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/index.js000664 001750 001750 0000002423 12732020005012170 0ustar00000000 000000 'use strict' function ReInterval (callback, interval, args) { var self = this; this._callback = callback; this._args = args; this._interval = setInterval(callback, interval, this._args); this.reschedule = function (interval) { // if no interval entered, use the interval passed in on creation if (!interval) interval = self._interval; if (self._interval) clearInterval(self._interval); self._interval = setInterval(self._callback, interval, self._args); }; this.clear = function () { if (self._interval) { clearInterval(self._interval); self._interval = undefined; } }; this.destroy = function () { if (self._interval) { clearInterval(self._interval); } self._callback = undefined; self._interval = undefined; self._args = undefined; }; } function reInterval () { if (typeof arguments[0] !== 'function') throw new Error('callback needed'); if (typeof arguments[1] !== 'number') throw new Error('interval needed'); var args; if (arguments.length > 0) { args = new Array(arguments.length - 2); for (var i = 0; i < args.length; i++) { args[i] = arguments[i + 2]; } } return new ReInterval(arguments[0], arguments[1], args); } module.exports = reInterval; package/.travis.yml000664 001750 001750 0000000377 12732017143012653 0ustar00000000 000000 sudo: false language: node_js node_js: - "0.10" - "0.12" - "iojs-v1" - "iojs-v2" - "iojs-v3" - "4.0" - "4.1" script: - npm run test notifications: webhooks: on_success: change on_failure: always on_start: never email: falsepackage/tests/test.js000664 001750 001750 0000002565 12732017143013222 0ustar00000000 000000 'use strict'; require('es6-shim'); var chai = require('chai'); var chaiAsPromised = require('chai-as-promised'); var reInterval = require('../index.js'); describe('reInterval', function() { it('should work as an usual setInterval', function () { return new Promise(function (resolve, reject) { var startTime = new Date().getTime(); reInterval(function () { if (Math.abs(new Date().getTime() - startTime - 1000) <= 10) resolve(); else reject(new Error('Took too much (or not enough) time')); }, 1000); }); }); it('should be able to clear an Interval', function () { return new Promise(function (resolve, reject) { var startTime = new Date().getTime(); var interval = reInterval(function () { reject(new Error('Interval not cleared')); }, 200); setTimeout(interval.clear, 100); setTimeout(resolve, 300); }); }); it('should be able to reschedule an Interval', function () { return new Promise(function (resolve, reject) { var startTime = new Date().getTime(); var interval = reInterval(function () { if (Math.abs(new Date().getTime() - startTime - 800) <= 10) resolve(); else reject(new Error('Took too much (or not enough) time')); }, 500); setTimeout(interval.reschedule, 300, [500]) }); }); });