diff --git a/lib/cron.js b/lib/cron.js index 8ef1f109..201191ec 100644 --- a/lib/cron.js +++ b/lib/cron.js @@ -22,7 +22,9 @@ function CronTime(time) { this.dayOfMonth = {}; this.month = {}; - this._parse(); + if (!(this.source instanceof Date)) { + this._parse(); + } }; @@ -40,9 +42,9 @@ CronTime.prototype = { * calculates the next send time */ - sendAt: function(start) { + sendAt: function() { - var date = start ? start : new Date(); + var date = (this.source instanceof Date) ? this.source : new Date(); //add 1 second so next time isn't now (can cause timeout to be 0) date.setSeconds(date.getSeconds() + 1); @@ -240,12 +242,13 @@ CronTime.prototype = { -function CronJob(cronTime, onComplete) { +function CronJob(cronTime, onTick, onComplete) { this._callbacks = []; + this.onComplete = onComplete; this.cronTime = new CronTime(cronTime); - this.addCallback(onComplete); + this.addCallback(onTick); this.start(); } @@ -270,7 +273,7 @@ CronJob.prototype = { for (var i = this._callbacks.length; i--;) { //send this so the callback can call this.stop(); - this._callbacks[i].call(this); + this._callbacks[i].call(this, this.onComplete); } }, @@ -311,9 +314,9 @@ CronJob.prototype = { }; -exports.job = function(cronTime, onComplete) { +exports.job = function(cronTime, onTick, onComplete) { - return new CronJob(cronTime, onComplete); + return new CronJob(cronTime, onTick, onComplete); } exports.time = function(cronTime) { diff --git a/tests/test-cron.js b/tests/test-cron.js index ffe3601d..6eab690f 100644 --- a/tests/test-cron.js +++ b/tests/test-cron.js @@ -9,7 +9,7 @@ module.exports = testCase({ }); setTimeout(function() { assert.done(); - }, 1000); + }, 2000); }, 'test second with oncomplete (* * * * * *)': function(assert) { assert.expect(1); @@ -20,7 +20,7 @@ module.exports = testCase({ }); setTimeout(function() { assert.done(); - }, 1000); + }, 2000); }, 'test every second for 5 seconds (* * * * * *)': function(assert) { assert.expect(5); @@ -29,7 +29,7 @@ module.exports = testCase({ }); setTimeout(function() { assert.done(); - }, 5000); + }, 6000); }, 'test every second for 5 seconds with oncomplete (* * * * * *)': function(assert) { assert.expect(5); @@ -40,7 +40,7 @@ module.exports = testCase({ }); setTimeout(function() { assert.done(); - }, 5000); + }, 6000); }, 'test every 1 second for 5 seconds (*/1 * * * * *)': function(assert) { assert.expect(5); @@ -49,7 +49,7 @@ module.exports = testCase({ }); setTimeout(function() { assert.done(); - }, 5000); + }, 6000); }, 'test every 1 second for 5 seconds with oncomplete (*/1 * * * * *)': function(assert) { assert.expect(5); @@ -60,7 +60,7 @@ module.exports = testCase({ }); setTimeout(function() { assert.done(); - }, 5000); + }, 6000); }, 'test every second for a range ([start]-[end] * * * * *)': function(assert) { assert.expect(5); diff --git a/tests/test-crontime.js b/tests/test-crontime.js index e0ccd977..9e0e0b7f 100644 --- a/tests/test-crontime.js +++ b/tests/test-crontime.js @@ -113,7 +113,12 @@ module.exports = testCase({ new cron.CronTime('* * * * j *'); }); assert.done(); + }, + 'test Date': function(assert) { + assert.expect(1); + var d = new Date(); + var ct = new cron.CronTime(d); + assert.equals(ct.source.getTime(), d.getTime()); + assert.done(); } - - });